Skip to content

Commit

Permalink
chore: testing that nargo fmt is idempotent (#4765)
Browse files Browse the repository at this point in the history
# Description

## Problem\*

Resolves #4666

## Summary\*

Ensures that `nargo_fmt(nargo_fmt(source_code)) ==
nargo_fmt(source_code)`.

Because we have expected outputs, we can avoid re-running on the
unformatted inputs:

```rust
nargo_fmt(expected_output) == expected_output
```

### Bugs found

- #4766
- #4767
- #4768

Currently failing on arrays and tuples:

```bash

---- tests::format_idempotent_array stdout ----
thread 'tests::format_idempotent_array' panicked at /Users/michaelklein/Coding/rust/noir/target/debug/build/nargo_fmt-14fb91f269fc38b6/out/execute.rs:3418:9:
assertion failed: `(left == right)`'
  left: `"fn big_array() {\n    [\n        1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000, 10000000000, 100000000000, 1000000000000, 10000000000000, 100000000000000, 1000000000000000, 1..."` (truncated)
 right: `"fn big_array() {\n    [\n        1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000, 10000000000, 100000000000, 1000000000000, 10000000000000, 100000000000000, 1000000000000000, 1..."` (truncated)

Differences (-left|+right):
     [
         // hello!
         1,
         // asd
-        10
+        10 
         // asdasd
     ];
 
     [[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]], [[13, 14, 15], [16, 17, 18]]];


note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

---- tests::format_idempotent_tuple stdout ----
thread 'tests::format_idempotent_tuple' panicked at /Users/michaelklein/Coding/rust/noir/target/debug/build/nargo_fmt-14fb91f269fc38b6/out/execute.rs:776:9:
assertion failed: `(left == right)`'
  left: `"fn main() {\n    (1,);\n    (// hello\n        1,);\n    (/*hello*/ 1,);\n    (1/*hello*/,);\n    (1,);\n    (/*test*/ 1,);\n    (/*a*/ 1/*b*/,);\n    (/*a*/ 1/*b*/, /*c*/ 2/*d*/, /*c*/ 2/*d*/);\n    (/*a*/ 1/*..."` (truncated)
 right: `"fn main() {\n    (1,);\n    (// hello\n        1,);\n    (/*hello*/ 1,);\n    (1/*hello*/,);\n    (1,);\n    (/*test*/ 1,);\n    (/*a*/ 1/*b*/,);\n    (/*a*/ 1/*b*/, /*c*/ 2/*d*/, /*c*/ 2/*d*/);\n    (/*a*/ 1/*..."` (truncated)

Differences (-left|+right):
     (//
         1,);
 
     (// 1
-        1,// 2,
+        1, // 2,
         2);
 
     (/*1*/ 1, /*2*/ 2);
 
     // FIXME:
     (((//2
                 1,),),);
     (/*a*/
-        1/*b*/, /*c*/ 2/*d*/, /*c*/ 2/*d*/, /*e*/ 3/*f*/);
+        1/*b*/,
+/*c*/ 2/*d*/, /*c*/ 2/*d*/, /*e*/ 3/*f*/);
 }
```


## Additional Context



## Documentation\*

Check one:
- [x] No documentation needed.
- [ ] Documentation included in this PR.
- [ ] **[For Experimental Features]** Documentation to be submitted in a
separate PR.

# PR Checklist\*

- [x] I have tested the changes locally.
- [x] I have formatted the changes with [Prettier](https://prettier.io/)
and/or `cargo fmt` on default settings.

---------

Co-authored-by: Tom French <[email protected]>
  • Loading branch information
michaeljklein and TomAFrench authored Apr 12, 2024
1 parent 305bcdc commit 9fd91b7
Showing 1 changed file with 39 additions and 12 deletions.
51 changes: 39 additions & 12 deletions tooling/nargo_fmt/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,28 +49,55 @@ fn generate_formatter_tests(test_file: &mut File, test_data_dir: &Path) {
let output_source_path = outputs_dir.join(file_name).display().to_string();
let output_source = std::fs::read_to_string(output_source_path.clone()).unwrap();

let skip_idempotent_test =
// TODO(https://github.com/noir-lang/noir/issues/4766): spurious trailing space
test_name == "array" ||
// TODO(https://github.com/noir-lang/noir/issues/4767): pre-comment space
// TODO(https://github.com/noir-lang/noir/issues/4768): spurious newline
test_name == "tuple";

write!(
test_file,
r##"
#[test]
fn format_{test_name}() {{
let input = r#"{input_source}"#;
let expected_output = r#"{output_source}"#;
#[test]
fn format_{test_name}() {{
let input = r#"{input_source}"#;
let expected_output = r#"{output_source}"#;
let (parsed_module, _errors) = noirc_frontend::parse_program(input);
let (parsed_module, _errors) = noirc_frontend::parse_program(input);
let config = nargo_fmt::Config::of("{config}").unwrap();
let fmt_text = nargo_fmt::format(input, parsed_module, &config);
let config = nargo_fmt::Config::of("{config}").unwrap();
let fmt_text = nargo_fmt::format(input, parsed_module, &config);
if std::env::var("UPDATE_EXPECT").is_ok() {{
std::fs::write("{output_source_path}", fmt_text.clone()).unwrap();
}}
if std::env::var("UPDATE_EXPECT").is_ok() {{
std::fs::write("{output_source_path}", fmt_text.clone()).unwrap();
}}
similar_asserts::assert_eq!(fmt_text, expected_output);
}}
similar_asserts::assert_eq!(fmt_text, expected_output);
}}
"##
)
.expect("Could not write templated test file.");

if !skip_idempotent_test {
write!(
test_file,
r##"
#[test]
fn format_idempotent_{test_name}() {{
let expected_output = r#"{output_source}"#;
let (parsed_module, _errors) = noirc_frontend::parse_program(expected_output);
let config = nargo_fmt::Config::of("{config}").unwrap();
let fmt_text = nargo_fmt::format(expected_output, parsed_module, &config);
similar_asserts::assert_eq!(fmt_text, expected_output);
}}
"##
)
.expect("Could not write templated test file.");
}
}
}

0 comments on commit 9fd91b7

Please sign in to comment.