diff --git a/apps/wing/src/commands/test/test.ts b/apps/wing/src/commands/test/test.ts index 400d6ef34dd..360307aafc2 100644 --- a/apps/wing/src/commands/test/test.ts +++ b/apps/wing/src/commands/test/test.ts @@ -10,7 +10,7 @@ import debug from "debug"; import { glob } from "glob"; import { nanoid } from "nanoid"; import { printResults, validateOutputFilePath, writeResultsToFile } from "./results"; -import { generateTmpDir, withSpinner } from "../../util"; +import { withSpinner } from "../../util"; import { compile, CompileOptions } from "../compile"; const log = debug("wing:test"); @@ -98,9 +98,6 @@ async function testOne(entrypoint: string, options: TestOptions) { ...options, rootId: options.rootId ?? `Test.${nanoid(10)}`, testing: true, - // since the test cleans up after each run, it's essential to create a temporary output directory- - // at least one that is different then the usual compilation output dir, otherwise we might end up cleaning up the user's actual resources. - ...(options.target !== Target.SIM && { targetDir: `${await generateTmpDir()}/target` }), }) ); diff --git a/libs/wingcompiler/src/compile.test.ts b/libs/wingcompiler/src/compile.test.ts new file mode 100644 index 00000000000..7521f91d2cf --- /dev/null +++ b/libs/wingcompiler/src/compile.test.ts @@ -0,0 +1,71 @@ +import { describe, test, expect } from "vitest"; +import { join, resolve, basename } from "path"; +import { stat, mkdtemp } from "fs/promises"; +import { tmpdir } from "os"; +import { Target } from "./constants"; +import { compile } from "./compile"; + +const exampleDir = resolve("../../examples/tests/valid"); +const exampleFilePath = join(exampleDir, "enums.test.w"); + +export async function generateTmpDir() { + return mkdtemp(join(tmpdir(), "-wing-compile-test")); +} + +describe( + "compile tests", + () => { + test("should produce stable artifacts for tf-aws", async () => { + const targetDir = `${await generateTmpDir()}/target`; + const artifactDir = await compile(exampleFilePath, { + target: Target.TF_AWS, + targetDir, + }); + + const stats = await stat(artifactDir); + expect(stats.isDirectory()).toBeTruthy(); + expect(artifactDir).toContain(targetDir); + expect(basename(artifactDir)).toEqual("enums.test.tfaws") + }); + + test("should produce temp artifacts for tf-aws testing", async () => { + const targetDir = `${await generateTmpDir()}/target`; + const artifactDir = await compile(exampleFilePath, { + target: Target.TF_AWS, + targetDir, + testing: true, + }); + + const stats = await stat(artifactDir); + expect(stats.isDirectory()).toBeTruthy(); + expect(artifactDir).toContain(targetDir); + expect(basename(artifactDir).match(/enums\.test\.tfaws\.\d+$/)).toBeTruthy(); + }); + + test("should produce stable artifacts for sim", async () => { + const targetDir = `${await generateTmpDir()}/target`; + const artifactDir = await compile(exampleFilePath, { + target: Target.SIM, + targetDir, + }); + + const stats = await stat(artifactDir); + expect(stats.isDirectory()).toBeTruthy(); + expect(artifactDir).toContain(targetDir); + expect(basename(artifactDir)).toEqual("enums.test.wsim") + }); + + test("should produce stable artifacts for sim testing", async () => { + const targetDir = `${await generateTmpDir()}/target`; + const artifactDir = await compile(exampleFilePath, { + target: Target.SIM, + targetDir, + testing: true, + }); + + const stats = await stat(artifactDir); + expect(stats.isDirectory()).toBeTruthy(); + expect(artifactDir).toContain(targetDir); + expect(basename(artifactDir)).toEqual("enums.test.wsim") + }); +}); diff --git a/libs/wingcompiler/src/compile.ts b/libs/wingcompiler/src/compile.ts index 0a8f02dc277..b1c9b991c89 100644 --- a/libs/wingcompiler/src/compile.ts +++ b/libs/wingcompiler/src/compile.ts @@ -80,8 +80,9 @@ function resolveSynthDir( log?.(err); throw new Error("Source file cannot be found"); } - const tmpSuffix = tmp ? `.${Date.now().toString().slice(-6)}.tmp` : ""; - const lastPart = `${entrypointName}.${targetDirSuffix}${tmpSuffix}`; + const randomPart = tmp || (testing && target !== Target.SIM) ? `.${Date.now().toString().slice(-6)}` : ""; + const tmpSuffix = tmp ? ".tmp" : ""; + const lastPart = `${entrypointName}.${targetDirSuffix}${randomPart}${tmpSuffix}`; if (testing) { return join(outDir, "test", lastPart); } else {