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

Add insta-based snapshot tests that cover side-by-side format #730

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ flamegraph.svg
.idea

sample_files/compare.result
tests/snapshots/*.snap.new

notes.md
26 changes: 26 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ smallvec = "1.13.2"
# assert_cmd 2.0.10 requires predicates 3.
# TODO: update.
assert_cmd = ">= 2, < 2.0.9"
insta = { version = "1.39.0", features = ["glob"], default-features = false }
predicates = "2.1.0"

pretty_assertions = "1.3.0"
Expand Down
9 changes: 9 additions & 0 deletions manual/src/adding_a_parser.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,15 @@ $ ./sample_files/compare_all.sh
$ cp sample_files/compare.result sample_files/compare.expected
```

Run the CLI test and [update the snapshots][insta] as well.

```
$ cargo insta test
$ cargo insta accept
```

[insta]: https://insta.rs/docs/quickstart/#reviewing-snapshots

## Maintenance

To update a parser that is already imported, use `git subtree pull`.
Expand Down
Empty file modified sample_files/chinese_1.po
100755 → 100644
Empty file.
2 changes: 1 addition & 1 deletion sample_files/compare.expected
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ sample_files/change_outer_1.el sample_files/change_outer_2.el
2b9334a4cc72da63bba28eff958f0038 -

sample_files/chinese_1.po sample_files/chinese_2.po
bc93dffd067b6da1916388557b7ffbaf -
ea3108d593e8ea08eff9af42b041f098 -

sample_files/clojure_1.clj sample_files/clojure_2.clj
453785e11dbee818ea0e8cac78c8be47 -
Expand Down
43 changes: 43 additions & 0 deletions tests/cli.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::process::Command;
use std::str;

use assert_cmd::prelude::*;
use predicates::prelude::*;
Expand Down Expand Up @@ -269,3 +270,45 @@ fn walk_hidden_items() {
.and(predicate::str::contains("after"));
cmd.assert().stdout(predicate_fn);
}

const MAX_SAMPLE_FILE_SIZE: u64 = 10_000;

#[test]
fn samples_side_by_side() {
insta::glob!("../sample_files", "*_1.*", |left_file| {
let base_dir = left_file.parent().unwrap().parent().unwrap();
let file_name = left_file.file_name().unwrap().to_str().unwrap();
let right_file = left_file.with_file_name(file_name.replace("_1.", "_2."));
// Large sample files are excluded because it's slow to diff with debug
// binary, and the snapshot results wouldn't help review changes.
if left_file.metadata().unwrap().len() > MAX_SAMPLE_FILE_SIZE
|| right_file.metadata().unwrap().len() > MAX_SAMPLE_FILE_SIZE
{
eprintln!("Skipping large sample: {file_name}");
return;
}
// TODO: Fix CI instability on the following architectures:
// x86_64-apple-darwin, x86_64-pc-windows-msvc, aarch64-apple-darwin
if file_name == "f_sharp_1.fs" {
eprintln!("Skipping unstable sample: {file_name}");
return;
}

let to_path_arg = |path: &Path| {
let short_path = path.strip_prefix(base_dir).unwrap();
let short_str = short_path.to_str().unwrap();
short_str.replace(std::path::MAIN_SEPARATOR, "/")
};
let mut cmd = get_base_command();
let assert = cmd
.arg("--color=always")
.arg("--display=side-by-side")
.arg("--width=160")
.arg(to_path_arg(left_file))
.arg(to_path_arg(&right_file))
.assert()
.success();
let stdout = str::from_utf8(&assert.get_output().stdout).unwrap();
insta::assert_snapshot!(stdout);
});
}
32 changes: 32 additions & 0 deletions tests/snapshots/cli__samples_side_by_side@Session_1.kt.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
---
source: tests/cli.rs
expression: stdout
input_file: sample_files/Session_1.kt
---
sample_files/Session_2.kt --- 1/2 --- Kotlin
 96  /**  96  /**
 97  * The session's photo URL.  97  * The session's photo URL.
 98  */  98  */
 99  val photoUrl: String?,  99  val photoUrl: String,
100  100 
101  /** 101  /**
102  * IDs of the sessions related to this session. 102  * IDs of the sessions related to this session.

sample_files/Session_2.kt --- 2/2 --- Kotlin
114  return startTime <= now && endTime >= now 114  return startTime <= now && endTime >= now
115  } 115  }
...  116 
...  117  val hasPhoto inline get() = photoUrl.isNotEmpty()
116  118 
117  /** 119  /**
118  * Returns whether the session has a video or not. A session could be l 120  * Returns whether the session has a video or not. A session could be l
... ive streaming or have a ... ive streaming or have a
119  * recorded session. Both live stream and recorded videos are stored in 121  * recorded session. Both live stream and recorded videos are stored in
...  [Session.youTubeUrl]. ...  [Session.youTubeUrl].
120  */ 122  */
121  fun hasVideo() = youTubeUrl.isNotEmpty() 123  val hasVideo inline get() = youTubeUrl.isNotEmpty()
...  124 
...  125  val hasPhotoOrVideo inline get() = hasPhoto || hasVideo
122  126 
123  /** 127  /**
124  * The year the session was held. 128  * The year the session was held.
14 changes: 14 additions & 0 deletions tests/snapshots/cli__samples_side_by_side@ada_1.adb.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
source: tests/cli.rs
expression: stdout
input_file: sample_files/ada_1.adb
---
sample_files/ada_2.adb --- Ada
1  with Ada.Text_IO; use Ada.Text_IO; procedure Hello is begin Put_Line ("Hello 1 with Ada.Text_IO;
.  WORLD!"); end Hello; . 
.  2 
.  3 procedure Hello is
.  4  package TIO renames Ada.Text_IO;
.  5 begin
.  6  TIO.Put_Line ("Hello World!");
.  7 end Hello;
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
source: tests/cli.rs
expression: stdout
input_file: sample_files/added_line_1.txt
---
sample_files/added_line_2.txt --- Text
1 1 potato
2 2 tomato
 3 legato
10 changes: 10 additions & 0 deletions tests/snapshots/cli__samples_side_by_side@align_footer_1.txt.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
source: tests/cli.rs
expression: stdout
input_file: sample_files/align_footer_1.txt
---
sample_files/align_footer_2.txt --- Text
1 before 1 before
2  foo x 2  x
3 y . 
4 after 3 after
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
source: tests/cli.rs
expression: stdout
input_file: sample_files/all_changed_1.js
---
sample_files/all_changed_2.js --- JavaScript
1 a 1 b
7 changes: 7 additions & 0 deletions tests/snapshots/cli__samples_side_by_side@apex_1.cls.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
source: tests/cli.rs
expression: stdout
input_file: sample_files/apex_1.cls
---
sample_files/apex_2.cls --- Apex
No syntactic changes.
33 changes: 33 additions & 0 deletions tests/snapshots/cli__samples_side_by_side@bad_combine_1.rs.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
source: tests/cli.rs
expression: stdout
input_file: sample_files/bad_combine_1.rs
---
sample_files/bad_combine_2.rs --- 1/2 --- Rust
 1  . 
 2 fn column_widths( . 
 3  hunks: &[Hunk], . 
 4  lhs_mps: &[MatchedPos], . 
 5  rhs_mps: &[MatchedPos], . 
 6  max_lhs_src_line: LineNumber, . 
 7  max_rhs_src_line: LineNumber, . 
 8 ) { . 
 9 } . 
10  . 
11 fn display_line_nums( 1 fn display_line_nums(
12 ) -> (String, String) { 2 ) -> (String, String) {
13  let display_rhs_line_num: String = match rhs_line_num { 3  let display_rhs_line_num: String = match rhs_line_num {
14  Some(line_num) => { 4  Some(line_num) => {
15  let s = format_line_num_padded(line_num, rhs_column_width); 5  let s = format_line_num_padded(line_num, widths.rhs_line_nums);
16  if rhs_lines_with_novel.contains(&line_num) { 6  if rhs_lines_with_novel.contains(&line_num) {
17  s.bright_green().to_string() 7  s.bright_green().to_string()
18  } else { 8  } else {

sample_files/bad_combine_2.rs --- 2/2 --- Rust
21  } 11  }
22  None => format_missing_line_num( 12  None => format_missing_line_num(
23  prev_rhs_line_num.unwrap_or_else(|| 10.into()), 13  prev_rhs_line_num.unwrap_or_else(|| 10.into()),
24  rhs_column_width, 14  widths.rhs_line_nums,
25  ), 15  ),
26  }; 16  };
27  17 
Loading
Loading