Skip to content

Commit

Permalink
tests: restore snapshots of line/word-oriented diff hunks
Browse files Browse the repository at this point in the history
test_diff_basic() is now testing file-level changes such as renames.
  • Loading branch information
yuja committed Aug 16, 2024
1 parent 2f2e5fb commit be35ab1
Showing 1 changed file with 91 additions and 0 deletions.
91 changes: 91 additions & 0 deletions cli/tests/test_diff_command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -649,6 +649,97 @@ fn test_diff_relative_paths() {
"###);
}

#[test]
fn test_diff_hunks() {
let test_env = TestEnvironment::default();
test_env.jj_cmd_ok(test_env.env_root(), &["git", "init", "repo"]);
let repo_path = test_env.env_root().join("repo");

// Test added, removed, inserted, and modified lines. The modified line
// contains unchanged words.
std::fs::write(repo_path.join("file1"), "").unwrap();
std::fs::write(repo_path.join("file2"), "foo\n").unwrap();
std::fs::write(repo_path.join("file3"), "foo\nbaz qux blah blah\n").unwrap();
test_env.jj_cmd_ok(&repo_path, &["new"]);
std::fs::write(repo_path.join("file1"), "foo\n").unwrap();
std::fs::write(repo_path.join("file2"), "").unwrap();
std::fs::write(repo_path.join("file3"), "foo\nbar\nbaz quux blah blah\n").unwrap();

let stdout = test_env.jj_cmd_success(&repo_path, &["diff"]);
insta::assert_snapshot!(stdout, @r###"
Modified regular file file1:
1: foo
Modified regular file file2:
1 : foo
Modified regular file file3:
1 1: foo
2: bar
2 3: baz quxquux blah blah
"###);

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

let stdout = test_env.jj_cmd_success(&repo_path, &["diff", "--git"]);
insta::assert_snapshot!(stdout, @r###"
diff --git a/file1 b/file1
index e69de29bb2..257cc5642c 100644
--- a/file1
+++ b/file1
@@ -1,0 +1,1 @@
+foo
diff --git a/file2 b/file2
index 257cc5642c..e69de29bb2 100644
--- a/file2
+++ b/file2
@@ -1,1 +1,0 @@
-foo
diff --git a/file3 b/file3
index 221a95a095..a543ef3892 100644
--- a/file3
+++ b/file3
@@ -1,2 +1,3 @@
foo
-baz qux blah blah
+bar
+baz quux blah blah
"###);

let stdout = test_env.jj_cmd_success(&repo_path, &["diff", "--git", "--color=debug"]);
insta::assert_snapshot!(stdout, @r###"
<<diff file_header::diff --git a/file1 b/file1>>
<<diff file_header::index e69de29bb2..257cc5642c 100644>>
<<diff file_header::--- a/file1>>
<<diff file_header::+++ b/file1>>
<<diff hunk_header::@@ -1,0 +1,1 @@>>
<<diff added::+>><<diff added token::foo>>
<<diff file_header::diff --git a/file2 b/file2>>
<<diff file_header::index 257cc5642c..e69de29bb2 100644>>
<<diff file_header::--- a/file2>>
<<diff file_header::+++ b/file2>>
<<diff hunk_header::@@ -1,1 +1,0 @@>>
<<diff removed::->><<diff removed token::foo>>
<<diff file_header::diff --git a/file3 b/file3>>
<<diff file_header::index 221a95a095..a543ef3892 100644>>
<<diff file_header::--- a/file3>>
<<diff file_header::+++ b/file3>>
<<diff hunk_header::@@ -1,2 +1,3 @@>>
<<diff context:: foo>>
<<diff removed::-baz >><<diff removed token::qux>><<diff removed:: blah blah>>
<<diff added::+>><<diff added token::bar>>
<<diff added::+baz >><<diff added token::quux>><<diff added:: blah blah>>
"###);
}

#[test]
fn test_diff_missing_newline() {
let test_env = TestEnvironment::default();
Expand Down

0 comments on commit be35ab1

Please sign in to comment.