Skip to content

Commit

Permalink
tests: add word-level hunks and capture some colorized diff outputs
Browse files Browse the repository at this point in the history
  • Loading branch information
yuja committed Jun 27, 2024
1 parent 9b6ee8c commit a7896b8
Showing 1 changed file with 57 additions and 10 deletions.
67 changes: 57 additions & 10 deletions cli/tests/test_diff_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ fn test_diff_basic() {
let repo_path = test_env.env_root().join("repo");

std::fs::write(repo_path.join("file1"), "foo\n").unwrap();
std::fs::write(repo_path.join("file2"), "foo\n").unwrap();
std::fs::write(repo_path.join("file2"), "foo\nbaz qux\n").unwrap();
test_env.jj_cmd_ok(&repo_path, &["new"]);
std::fs::remove_file(repo_path.join("file1")).unwrap();
std::fs::write(repo_path.join("file2"), "foo\nbar\n").unwrap();
std::fs::write(repo_path.join("file2"), "foo\nbar\nbaz quux\n").unwrap();
std::fs::write(repo_path.join("file3"), "foo\n").unwrap();

let stdout = test_env.jj_cmd_success(&repo_path, &["diff"]);
Expand All @@ -36,6 +36,7 @@ fn test_diff_basic() {
Modified regular file file2:
1 1: foo
2: bar
2 3: baz quxquux
Added regular file file3:
1: foo
"###);
Expand All @@ -47,10 +48,23 @@ fn test_diff_basic() {
Modified regular file file2:
1 1: foo
2: bar
2 3: baz quxquux
Added regular file file3:
1: foo
"###);

let stdout = test_env.jj_cmd_success(&repo_path, &["diff", "--color=debug"]);
insta::assert_snapshot!(stdout, @r###"
<<diff header::Removed >><<diff header::regular file>><<diff header:: >><<diff header::file1>><<diff header:::>>
<<diff removed line_number:: >><<diff removed line_number:: >><<diff removed line_number:: >><<diff removed line_number::1>><<diff:: >><<diff:: : >><<diff removed token::foo>>
<<diff header::Modified regular file>><<diff header:: >><<diff header::file2>><<diff header:::>>
<<diff removed line_number:: >><<diff removed line_number:: >><<diff removed line_number:: >><<diff removed line_number::1>><<diff:: >><<diff added line_number:: >><<diff added line_number:: >><<diff added line_number:: >><<diff added line_number::1>><<diff::: >><<diff::foo>>
<<diff:: >><<diff added line_number:: >><<diff added line_number:: >><<diff added line_number:: >><<diff added line_number::2>><<diff::: >><<diff added token::bar>>
<<diff removed line_number:: >><<diff removed line_number:: >><<diff removed line_number:: >><<diff removed line_number::2>><<diff:: >><<diff added line_number:: >><<diff added line_number:: >><<diff added line_number:: >><<diff added line_number::3>><<diff::: >><<diff::baz >><<diff removed token::qux>><<diff added token::quux>><<diff::>>
<<diff header::Added >><<diff header::regular file>><<diff header:: >><<diff header::file3>><<diff header:::>>
<<diff:: >><<diff added line_number:: >><<diff added line_number:: >><<diff added line_number:: >><<diff added line_number::1>><<diff::: >><<diff added token::foo>>
"###);

let stdout = test_env.jj_cmd_success(&repo_path, &["diff", "-s"]);
insta::assert_snapshot!(stdout, @r###"
D file1
Expand All @@ -75,12 +89,14 @@ fn test_diff_basic() {
@@ -1,1 +1,0 @@
-foo
diff --git a/file2 b/file2
index 257cc5642c...3bd1f0e297 100644
index 523a4a9de8...485b56a572 100644
--- a/file2
+++ b/file2
@@ -1,1 +1,2 @@
@@ -1,2 +1,3 @@
foo
-baz qux
+bar
+baz quux
diff --git a/file3 b/file3
new file mode 100644
index 0000000000..257cc5642c
Expand All @@ -100,11 +116,13 @@ fn test_diff_basic() {
@@ -1,1 +1,0 @@
-foo
diff --git a/file2 b/file2
index 257cc5642c...3bd1f0e297 100644
index 523a4a9de8...485b56a572 100644
--- a/file2
+++ b/file2
@@ -2,0 +2,1 @@
@@ -2,1 +2,2 @@
-baz qux
+bar
+baz quux
diff --git a/file3 b/file3
new file mode 100644
index 0000000000..257cc5642c
Expand All @@ -114,6 +132,33 @@ fn test_diff_basic() {
+foo
"###);

let stdout = test_env.jj_cmd_success(&repo_path, &["diff", "--git", "--color=debug"]);
insta::assert_snapshot!(stdout, @r###"
<<diff file_header::diff --git a/>><<diff file_header::file1>><<diff file_header:: b/>><<diff file_header::file1>><<diff file_header::>>
<<diff file_header::deleted file mode >><<diff file_header::100644>><<diff file_header::>>
<<diff file_header::index >><<diff file_header::257cc5642c>><<diff file_header::..0000000000>>
<<diff file_header::--- a/>><<diff file_header::file1>><<diff file_header::>>
<<diff file_header::+++ /dev/null>>
<<diff hunk_header::@@ ->><<diff hunk_header::1>><<diff hunk_header::,>><<diff hunk_header::1>><<diff hunk_header:: +>><<diff hunk_header::1>><<diff hunk_header::,>><<diff hunk_header::0>><<diff hunk_header:: @@>>
<<diff removed::->><<diff removed::foo>>
<<diff file_header::diff --git a/>><<diff file_header::file2>><<diff file_header:: b/>><<diff file_header::file2>><<diff file_header::>>
<<diff file_header::index >><<diff file_header::523a4a9de8>><<diff file_header::...>><<diff file_header::485b56a572>><<diff file_header:: >><<diff file_header::100644>><<diff file_header::>>
<<diff file_header::--- a/>><<diff file_header::file2>><<diff file_header::>>
<<diff file_header::+++ b/>><<diff file_header::file2>><<diff file_header::>>
<<diff hunk_header::@@ ->><<diff hunk_header::1>><<diff hunk_header::,>><<diff hunk_header::2>><<diff hunk_header:: +>><<diff hunk_header::1>><<diff hunk_header::,>><<diff hunk_header::3>><<diff hunk_header:: @@>>
<<diff context:: >><<diff context::foo>>
<<diff removed::->><<diff removed::baz qux>>
<<diff added::+>><<diff added::bar>>
<<diff added::+>><<diff added::baz quux>>
<<diff file_header::diff --git a/>><<diff file_header::file3>><<diff file_header:: b/>><<diff file_header::file3>><<diff file_header::>>
<<diff file_header::new file mode >><<diff file_header::100644>><<diff file_header::>>
<<diff file_header::index 0000000000..>><<diff file_header::257cc5642c>><<diff file_header::>>
<<diff file_header::--- /dev/null>>
<<diff file_header::+++ b/>><<diff file_header::file3>><<diff file_header::>>
<<diff hunk_header::@@ ->><<diff hunk_header::1>><<diff hunk_header::,>><<diff hunk_header::0>><<diff hunk_header:: +>><<diff hunk_header::1>><<diff hunk_header::,>><<diff hunk_header::1>><<diff hunk_header:: @@>>
<<diff added::+>><<diff added::foo>>
"###);

let stdout = test_env.jj_cmd_success(&repo_path, &["diff", "-s", "--git"]);
insta::assert_snapshot!(stdout, @r###"
D file1
Expand All @@ -127,12 +172,14 @@ fn test_diff_basic() {
@@ -1,1 +1,0 @@
-foo
diff --git a/file2 b/file2
index 257cc5642c...3bd1f0e297 100644
index 523a4a9de8...485b56a572 100644
--- a/file2
+++ b/file2
@@ -1,1 +1,2 @@
@@ -1,2 +1,3 @@
foo
-baz qux
+bar
+baz quux
diff --git a/file3 b/file3
new file mode 100644
index 0000000000..257cc5642c
Expand All @@ -145,9 +192,9 @@ fn test_diff_basic() {
let stdout = test_env.jj_cmd_success(&repo_path, &["diff", "--stat"]);
insta::assert_snapshot!(stdout, @r###"
file1 | 1 -
file2 | 1 +
file2 | 3 ++-
file3 | 1 +
3 files changed, 2 insertions(+), 1 deletion(-)
3 files changed, 3 insertions(+), 2 deletions(-)
"###);

// Filter by glob pattern
Expand Down

0 comments on commit a7896b8

Please sign in to comment.