Skip to content

Commit

Permalink
fix(cli): cant run tests with external packages in non-entry files (#…
Browse files Browse the repository at this point in the history
…4545)

Tests output directories are now under the target directory. This closes #4394. Although there is still an issue where `require` doesn't respect the vm context, i think this makes sense since the current OS directories are temporary which increases the chances of lost terraform data and dangling cloud resources 

## Checklist

- [X] Title matches [Winglang's style guide](https://www.winglang.io/contributing/start-here/pull_requests#how-are-pull-request-titles-formatted)
- [X] Description explains motivation and solution
- [ ] Tests added (always)
- [ ] Docs updated (only required for features)
- [ ] Added `pr/e2e-full` label if this feature requires end-to-end testing

*By submitting this pull request, I confirm that my contribution is made under the terms of the [Wing Cloud Contribution License](https://github.com/winglang/wing/blob/main/CONTRIBUTION_LICENSE.md)*.
  • Loading branch information
eladcon authored Oct 22, 2023
1 parent 6c24208 commit 54f03f7
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 6 deletions.
5 changes: 1 addition & 4 deletions apps/wing/src/commands/test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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` }),
})
);

Expand Down
71 changes: 71 additions & 0 deletions libs/wingcompiler/src/compile.test.ts
Original file line number Diff line number Diff line change
@@ -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")
});
});
5 changes: 3 additions & 2 deletions libs/wingcompiler/src/compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 54f03f7

Please sign in to comment.