Skip to content

Commit

Permalink
B Fixes flaky removal of working directory (again)
Browse files Browse the repository at this point in the history
  • Loading branch information
jwloka committed May 7, 2024
1 parent 240de58 commit 4d7da05
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 16 deletions.
2 changes: 1 addition & 1 deletion packages/examples/tests/foobar-replace-processor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ describe("foobar-replace-processor", () => {
testObj
.addProjectFromSource({
"bar.ts": `console.log("Hello, Bar!");`,
"foo.ts": `export class Foobar {}`,
"foo.ts": `export class FooBar {}`,
})
.compile();

Expand Down
6 changes: 1 addition & 5 deletions packages/examples/tests/foobar-replace-transformer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { join } from "path";
describe("foobar-replace-transformer", () => {
let testObj: CompilationEnv;
beforeAll(() => {
testObj = compilationEnv("./__TEST__", { compilerOptions: { project: { outDir: "dist" } }, virtual: false }).addAddon(
testObj = compilationEnv("./__TEST__", { compilerOptions: { project: { outDir: "dist" } } }).addAddon(
"foobar-replace-transformer",
join(__dirname, "../addons")
);
Expand All @@ -14,10 +14,6 @@ describe("foobar-replace-transformer", () => {
testObj.cleanUp("project");
});

afterAll(() => {
testObj.cleanUp();
});

it("should replace 'foo' with 'bar' in the output files", () => {
testObj
.addProjectFromSource({
Expand Down
33 changes: 23 additions & 10 deletions packages/testing/src/compilation/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import ts from "typescript";
import { compileOptions } from "../compile-options";
import { copyDirectory } from "./copy-directory";
import { resolvePath } from "./resolve-path";
import { rmSync } from "fs";

const DEFAULT_ROOT_DIR = "/";
const DEFAULT_BUILD_DIR = "./src";
Expand All @@ -35,7 +36,7 @@ export class CompilationEnv {
private buildDir: string;
private system: ts.System;
private virtual: boolean;
private addons?: AddonRegistry;
private addons: AddonRegistry;

constructor(rootDir?: string, options?: Partial<CompilationOptions>, addonConfig?: Partial<AddonConfig>) {
const { virtual = true, compilerOptions = {}, useCaseSensitiveFileNames } = options ?? {};
Expand Down Expand Up @@ -73,10 +74,10 @@ export class CompilationEnv {
});

const registryConfig = {
...(addonConfig ?? {}),
addonsDir: join(this.rootDir, "./addons"),
reporter: new DefaultReporter(this.system),
system: this.system,
...(addonConfig ?? {}),
};
this.addons = new AddonRegistry(registryConfig);
if (!this.system.directoryExists(this.addons.getAddonsDir())) {
Expand All @@ -93,6 +94,10 @@ export class CompilationEnv {
return this.rootDir;
}

public getOutDir(): string {
return resolvePath(this.system, this.rootDir, this.compilerOptions.project.outDir ?? DEFAULT_OUT_DIR);
}

public getCompilerOptions(): CompilerOptions {
return this.compilerOptions;
}
Expand All @@ -106,14 +111,22 @@ export class CompilationEnv {
}

public cleanUp(options: "project" | "addons" | "all" = "all"): this {
const target = options === "project" ? this.getProjectDir() : options === "addons" && this.addons ? this.addons.getAddonsDir() : this.rootDir;
if (this.system.directoryExists(target)) {
if (this.isVirtual()) {
this.system.readDirectory(target).forEach(it => this.system.deleteFile!(it));
} else {
ts.sys.readDirectory(target).forEach(it => this.system.deleteFile!(it));
}
let directories = [];
switch (options) {
case "all":
directories = [this.rootDir];
break;
case "project":
directories = [this.buildDir, this.getOutDir()];
break;
case "addons":
directories = [this.addons.getAddonsDir()];
break;
}
directories.forEach(dir =>
this.virtual ? this.system.readDirectory(dir).forEach((it: string) => this.system.deleteFile!(it)) : rmSync(dir, { recursive: true })
);

return this;
}

Expand Down Expand Up @@ -290,7 +303,7 @@ export class CompilationEnv {
acc[filePath] = content;
} else {
if (filePath.includes(addonName)) {
acc[join(this.addons!.getAddonsDir(), filePath)] = content;
acc[join(this.addons.getAddonsDir(), filePath)] = content;
} else {
acc[join(addonTargetPath, filePath)] = content;
}
Expand Down

0 comments on commit 4d7da05

Please sign in to comment.