Debian Codegraph @

cli/tests/test_interdiff_command.rs

raw
1 // Copyright 2022 The Jujutsu Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 use crate::common::TestEnvironment;
16
17 #[test]
18 fn test_interdiff_basic() {
19 let test_env = TestEnvironment::default();
20 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
21 let work_dir = test_env.work_dir("repo");
22
23 work_dir.write_file("file1", "foo\n");
24 work_dir.run_jj(["new", "-madd file2 left"]).success();
25 work_dir.write_file("file2", "foo\n");
26 work_dir
27 .run_jj(["bookmark", "create", "-r@", "left"])
28 .success();
29
30 work_dir.run_jj(["new", "root()"]).success();
31 work_dir.write_file("file3", "foo\n");
32 work_dir.run_jj(["new", "-madd file2 right"]).success();
33 work_dir.write_file("file2", "foo\nbar\n");
34 work_dir
35 .run_jj(["bookmark", "create", "-r@", "right"])
36 .success();
37 let setup_opid = work_dir.current_operation_id();
38
39 // implicit --to
40 let output = work_dir.run_jj(["interdiff", "--from", "left"]);
41 insta::assert_snapshot!(output, @"
42 Modified commit description:
43 1 : add file2 left
44 1: add file2 right
45 Modified regular file file2:
46 1 1: foo
47 2: bar
48 [EOF]
49 ");
50
51 // explicit --to
52 work_dir.run_jj(["new", "@-"]).success();
53 let output = work_dir.run_jj(["interdiff", "--from", "left", "--to", "right"]);
54 insta::assert_snapshot!(output, @"
55 Modified commit description:
56 1 : add file2 left
57 1: add file2 right
58 Modified regular file file2:
59 1 1: foo
60 2: bar
61 [EOF]
62 ");
63 work_dir.run_jj(["op", "restore", &setup_opid]).success();
64
65 // formats specifiers
66 let output = work_dir.run_jj(["interdiff", "--from", "left", "--to", "right", "-s"]);
67 insta::assert_snapshot!(output, @"
68 M file2
69 [EOF]
70 ");
71
72 let output = work_dir.run_jj(["interdiff", "--from", "left", "--to", "right", "--git"]);
73 insta::assert_snapshot!(output, @"
74 diff --git a/JJ-COMMIT-DESCRIPTION b/JJ-COMMIT-DESCRIPTION
75 --- JJ-COMMIT-DESCRIPTION
76 +++ JJ-COMMIT-DESCRIPTION
77 @@ -1,1 +1,1 @@
78 -add file2 left
79 +add file2 right
80 diff --git a/file2 b/file2
81 index 257cc5642c..3bd1f0e297 100644
82 --- a/file2
83 +++ b/file2
84 @@ -1,1 +1,2 @@
85 foo
86 +bar
87 [EOF]
88 ");
89 }
90
91 #[test]
92 fn test_interdiff_paths() {
93 let test_env = TestEnvironment::default();
94 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
95 let work_dir = test_env.work_dir("repo");
96
97 work_dir.write_file("file1", "foo\n");
98 work_dir.write_file("file2", "foo\n");
99 work_dir.run_jj(["new"]).success();
100 work_dir.write_file("file1", "bar\n");
101 work_dir.write_file("file2", "bar\n");
102 work_dir
103 .run_jj(["bookmark", "create", "-r@", "left"])
104 .success();
105
106 work_dir.run_jj(["new", "root()"]).success();
107 work_dir.write_file("file1", "foo\n");
108 work_dir.write_file("file2", "foo\n");
109 work_dir.run_jj(["new"]).success();
110 work_dir.write_file("file1", "baz\n");
111 work_dir.write_file("file2", "baz\n");
112 work_dir
113 .run_jj(["bookmark", "create", "-r@", "right"])
114 .success();
115
116 let output = work_dir.run_jj(["interdiff", "--from", "left", "--to", "right", "file1"]);
117 insta::assert_snapshot!(output, @"
118 Modified regular file file1:
119 1 : bar
120 1: baz
121 [EOF]
122 ");
123
124 let output = work_dir.run_jj([
125 "interdiff",
126 "--from",
127 "left",
128 "--to",
129 "right",
130 "file1",
131 "file2",
132 "nonexistent",
133 ]);
134 insta::assert_snapshot!(output, @"
135 Modified regular file file1:
136 1 : bar
137 1: baz
138 Modified regular file file2:
139 1 : bar
140 1: baz
141 [EOF]
142 ------- stderr -------
143 Warning: No matching entries for paths: nonexistent
144 [EOF]
145 ");
146
147 // Running interdiff on commits with deleted files should not show a warning.
148 work_dir.run_jj(["edit", "right"]).success();
149 work_dir.remove_file("file1");
150 work_dir.run_jj(["new"]).success();
151
152 let output = work_dir.run_jj([
153 "interdiff",
154 "--from",
155 "left",
156 "--to",
157 "right",
158 "file1",
159 "file2",
160 ]);
161 insta::assert_snapshot!(output, @"
162 Removed regular file file1:
163 1 : bar
164 Modified regular file file2:
165 1 : bar
166 1: baz
167 [EOF]
168 ");
169 }
170
171 #[test]
172 fn test_interdiff_conflicting() {
173 let test_env = TestEnvironment::default();
174 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
175 let work_dir = test_env.work_dir("repo");
176
177 work_dir.write_file("file", "foo\n");
178 work_dir.run_jj(["new"]).success();
179 work_dir.write_file("file", "bar\n");
180 work_dir
181 .run_jj(["bookmark", "create", "-r@", "left"])
182 .success();
183
184 work_dir.run_jj(["new", "root()"]).success();
185 work_dir.write_file("file", "abc\n");
186 work_dir.run_jj(["new"]).success();
187 work_dir.write_file("file", "def\n");
188 work_dir
189 .run_jj(["bookmark", "create", "-r@", "right"])
190 .success();
191
192 let output = work_dir.run_jj(["interdiff", "--from", "left", "--to", "right", "--git"]);
193 insta::assert_snapshot!(output, @r"
194 diff --git a/file b/file
195 index 0000000000..24c5735c3e 100644
196 --- a/file
197 +++ b/file
198 @@ -1,8 +1,1 @@
199 -<<<<<<< conflict 1 of 1
200 -%%%%%%% diff from: qpvuntsm d0c049cd (original parents)
201 -\\\\\\\ to: zsuskuln 0b2c304e (new parents)
202 --foo
203 -+abc
204 -+++++++ rlvkpnrz b23f92c3 (original revision)
205 -bar
206 ->>>>>>> conflict 1 of 1 ends
207 +def
208 [EOF]
209 ");
210
211 let output = work_dir.run_jj([
212 "interdiff",
213 "--config=diff.color-words.conflict=pair",
214 "--color=always",
215 "--from=left",
216 "--to=right",
217 ]);
218 insta::assert_snapshot!(output, @"
219 Resolved conflict in file:
220 <<<<<<< Resolved conflict
221 +++++++ left side #1 to right side #1
222  1  1: abcdef
223 ------- left base #1 to right side #1
224  1  1: foodef
225 +++++++ left side #2 to right side #1
226  1  1: bardef
227 >>>>>>> Conflict ends
228 [EOF]
229 ");
230 }