Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: consolidate snapshot tests into hangar #754

Merged
merged 5 commits into from
Nov 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 6 additions & 71 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 0 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,3 @@ members = [
"libs/wingc",
"libs/wingii",
]

# See https://insta.rs/docs/quickstart/#optional-faster-runs
[profile.dev.package.insta]
opt-level = 3
[profile.dev.package.similar]
opt-level = 3
3 changes: 1 addition & 2 deletions docs/06-contributors/handbook.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ npm run build
To run all tests:

```sh
cargo install cargo-insta # one-time setup
npm run test
```

Expand Down Expand Up @@ -205,7 +204,7 @@ Add a `.env` file to `tools/hangar` with the following:
NPM_TOKEN=<GitHub PAT with access to @winglang packages>
```

This allows spun-up registry to pull down @winglang/polycons from the private github registry.
This allows the spun-up registry to pull down @winglang packages from the private github registry.

To run the tests (and update snapshots), run the following commands from the root of the Hangar project:

Expand Down
23 changes: 0 additions & 23 deletions libs/wingc/CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ and submit pull requests to the GitHub repository.
- [Getting Started](#getting-started)
- [Prerequisites](#prerequisites)
- [Setting up GitHub private packages](#setting-up-github-private-packages)
- [Setting up `insta` snapshot tool](#setting-up-insta-snapshot-tool)
- [Orientation](#orientation)
- [`src` folder](#src-folder)
- [Setting up and building the project](#setting-up-and-building-the-project)
Expand Down Expand Up @@ -56,28 +55,6 @@ npm login --scope=@winglang --registry=https://npm.pkg.github.com

See [Working with the npm registry](https://docs.github.com/en/packages/working-with-a-github-packages-registry/working-with-the-npm-registry) for more information.

### Setting up `insta` snapshot tool

Snapshot testing uses [insta](https://insta.rs/) tool to generate and compare snapshots. To install it, run:

```sh
cargo install cargo-insta
```

Recommended way to run tests and review snapshot changes:

```sh
cargo insta test --review
```
See https://insta.rs/docs/quickstart/ for more information.
If you prefer to avoid the `insta` tool, you may set INSTA_UPDATE to `always` to update snapshots without review with a regular cargo test command, or just use nx:

```sh
npx nx test
```

otherwise, `cargo test` fails if snapshots are out of date.

## Orientation


Expand Down
4 changes: 0 additions & 4 deletions libs/wingc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ itertools = "0.10"
inflections = "1.1.1"
phf = { version = "0.11", features = ["macros"] }

[dev-dependencies]
insta = "1.21.0"
walkdir = "2.3.2"

[lib]
path = "src/lib.rs"

Expand Down
2 changes: 1 addition & 1 deletion libs/wingc/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"examples-tests"
],
"options": {
"command": "INSTA_UPDATE=always cargo test",
"command": "cargo test",
"cwd": "libs/wingc"
},
"configurations": {
Expand Down
59 changes: 19 additions & 40 deletions libs/wingc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,66 +156,45 @@ mod sanity {
for entry in fs::read_dir(dir).unwrap() {
let entry = entry.unwrap();
let path = entry.path();
if path.extension().unwrap() == "w" {
files.push(path);
if let Some(ext) = path.extension() {
if ext == "w" {
files.push(path);
}
}
}
files
}

fn snapshot_test(test_dir: &str, expect_failure: bool) {
fn compile_test(test_dir: &str, expect_failure: bool) {
for test_pathbuf in get_wing_files(test_dir) {
let test_file = test_pathbuf.to_str().unwrap();
let test_file_name = test_pathbuf.file_stem().unwrap().to_str().unwrap();
println!("\n=== {} ===\n", test_file);

insta::with_settings!({
omit_expression => true,
prepend_module_to_snapshot => false,
description => test_file,
}, {
let out_dir = format!("{}.out", test_file);

// reset out_dir
let out_dirbuf = PathBuf::from(&out_dir);
if out_dirbuf.exists() {
fs::remove_dir_all(&out_dirbuf).expect("remove out dir");
}

let result = compile(test_file, Some(out_dir.as_str()));
let mut snapshot = String::new();

if let Err(diagnostics) = result {
assert!(expect_failure, "Expected compilation success, but failed");
let out_dir = format!("{}.out", test_file);

insta::assert_debug_snapshot!(format!("invalid_{}", test_file_name), diagnostics);
} else {
assert!(!expect_failure, "Expected compilation failure, but succeeded");
// reset out_dir
let out_dirbuf = PathBuf::from(&out_dir);
if out_dirbuf.exists() {
fs::remove_dir_all(&out_dirbuf).expect("remove out dir");
}

let output_files = walkdir::WalkDir::new(out_dir).sort_by_file_name();
for file in output_files {
if let Ok(file) = file {
let path = file.path();
if path.is_file() {
let file_contents = fs::read_to_string(path).unwrap();
snapshot.push_str(&format!("// {}\n{}\n", path.to_str().unwrap(), file_contents));
}
}
}
let result = compile(test_file, Some(out_dir.as_str()));

insta::assert_snapshot!(format!("valid_{}", test_file_name), snapshot);
}
});
if result.is_err() {
assert!(expect_failure, "Expected compilation success, but failed");
} else {
assert!(!expect_failure, "Expected compilation failure, but succeeded");
}
}
}

#[test]
fn can_compile_valid_files() {
snapshot_test("../../examples/tests/valid", false);
compile_test("../../examples/tests/valid", false);
}

#[test]
fn cannot_compile_invalid_files() {
snapshot_test("../../examples/tests/invalid", true);
compile_test("../../examples/tests/invalid", true);
}
}
44 changes: 0 additions & 44 deletions libs/wingc/src/snapshots/invalid_optional_invoke.snap

This file was deleted.

Loading