Debian Codegraph @

cli/tests/test_file_annotate_command.rs

raw
1 // Copyright 2024 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 std::fs::OpenOptions;
16 use std::io::Write as _;
17 use std::path::Path;
18
19 use jj_lib::file_util::check_symlink_support;
20 use jj_lib::file_util::symlink_file;
21 use testutils::TestResult;
22
23 use crate::common::TestEnvironment;
24
25 fn append_to_file(file_path: &Path, contents: &str) {
26 let mut options = OpenOptions::new();
27 options.append(true);
28 let mut file = options.open(file_path).unwrap();
29 writeln!(file, "{contents}").unwrap();
30 }
31
32 #[test]
33 fn test_annotate_linear() {
34 let test_env = TestEnvironment::default();
35 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
36 let work_dir = test_env.work_dir("repo");
37
38 work_dir.write_file("file.txt", "line1\n");
39 work_dir
40 .run_jj(["metaedit", "-m=initial", "--author=Foo <[email protected]>"])
41 .success();
42
43 work_dir.run_jj(["new", "-m=next"]).success();
44 append_to_file(
45 &work_dir.root().join("file.txt"),
46 "new text from new commit",
47 );
48
49 let output = work_dir.run_jj(["file", "annotate", "file.txt"]);
50 insta::assert_snapshot!(output, @"
51 qpvuntsm foo 2001-02-03 08:05:08 1: line1
52 kkmpptxz test.use 2001-02-03 08:05:10 2: new text from new commit
53 [EOF]
54 ");
55 }
56
57 #[test]
58 fn test_annotate_non_file() -> TestResult {
59 let test_env = TestEnvironment::default();
60 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
61 let work_dir = test_env.work_dir("repo");
62
63 let output = work_dir.run_jj(["file", "annotate", "non-existent.txt"]);
64 insta::assert_snapshot!(output, @"
65 ------- stderr -------
66 Error: No such path: non-existent.txt
67 [EOF]
68 [exit status: 1]
69 ");
70
71 work_dir.write_file("dir/file.txt", "");
72 let output = work_dir.run_jj(["file", "annotate", "dir"]);
73 insta::assert_snapshot!(output, @"
74 ------- stderr -------
75 Error: Path exists but is not a regular file: dir
76 [EOF]
77 [exit status: 1]
78 ");
79
80 if check_symlink_support()? {
81 symlink_file("target", work_dir.root().join("symlink"))?;
82 let output = work_dir.run_jj(["file", "annotate", "symlink"]);
83 insta::assert_snapshot!(output, @"
84 ------- stderr -------
85 Error: Path exists but is not a regular file: symlink
86 [EOF]
87 [exit status: 1]
88 ");
89 }
90 Ok(())
91 }
92
93 #[test]
94 fn test_annotate_merge() {
95 let test_env = TestEnvironment::default();
96 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
97 let work_dir = test_env.work_dir("repo");
98
99 work_dir.write_file("file.txt", "line1\n");
100 work_dir.run_jj(["describe", "-m=initial"]).success();
101 work_dir
102 .run_jj(["bookmark", "create", "-r@", "initial"])
103 .success();
104
105 work_dir.run_jj(["new", "-m=commit1"]).success();
106 append_to_file(
107 &work_dir.root().join("file.txt"),
108 "new text from new commit 1",
109 );
110 work_dir
111 .run_jj(["bookmark", "create", "-r@", "commit1"])
112 .success();
113
114 work_dir.run_jj(["new", "-m=commit2", "initial"]).success();
115 append_to_file(
116 &work_dir.root().join("file.txt"),
117 "new text from new commit 2",
118 );
119 work_dir
120 .run_jj(["bookmark", "create", "-r@", "commit2"])
121 .success();
122
123 // create a (conflicted) merge
124 work_dir
125 .run_jj(["new", "-m=merged", "commit1", "commit2"])
126 .success();
127 // resolve conflicts
128 work_dir.write_file(
129 "file.txt",
130 "line1\nnew text from new commit 1\nnew text from new commit 2\n",
131 );
132
133 let output = work_dir.run_jj(["file", "annotate", "file.txt"]);
134 insta::assert_snapshot!(output, @"
135 qpvuntsm test.use 2001-02-03 08:05:08 1: line1
136 zsuskuln test.use 2001-02-03 08:05:11 2: new text from new commit 1
137 royxmykx test.use 2001-02-03 08:05:13 3: new text from new commit 2
138 [EOF]
139 ");
140 }
141
142 #[test]
143 fn test_annotate_conflicted() {
144 let test_env = TestEnvironment::default();
145 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
146 let work_dir = test_env.work_dir("repo");
147
148 work_dir.write_file("file.txt", "line1\n");
149 work_dir.run_jj(["describe", "-m=initial"]).success();
150 work_dir
151 .run_jj(["bookmark", "create", "-r@", "initial"])
152 .success();
153
154 work_dir.run_jj(["new", "-m=commit1"]).success();
155 append_to_file(
156 &work_dir.root().join("file.txt"),
157 "new text from new commit 1",
158 );
159 work_dir
160 .run_jj(["bookmark", "create", "-r@", "commit1"])
161 .success();
162
163 work_dir.run_jj(["new", "-m=commit2", "initial"]).success();
164 append_to_file(
165 &work_dir.root().join("file.txt"),
166 "new text from new commit 2",
167 );
168 work_dir
169 .run_jj(["bookmark", "create", "-r@", "commit2"])
170 .success();
171
172 // create a (conflicted) merge
173 work_dir
174 .run_jj(["new", "-m=merged", "commit1", "commit2"])
175 .success();
176 work_dir.run_jj(["new"]).success();
177
178 let output = work_dir.run_jj(["file", "annotate", "file.txt"]);
179 insta::assert_snapshot!(output, @r#"
180 qpvuntsm test.use 2001-02-03 08:05:08 1: line1
181 yostqsxw test.use 2001-02-03 08:05:15 2: <<<<<<< conflict 1 of 1
182 yostqsxw test.use 2001-02-03 08:05:15 3: %%%%%%% diff from: qpvuntsm a5daff01 "initial"
183 yostqsxw test.use 2001-02-03 08:05:15 4: \\\\\\\ to: zsuskuln 30cd4478 "commit1"
184 yostqsxw test.use 2001-02-03 08:05:15 5: +new text from new commit 1
185 yostqsxw test.use 2001-02-03 08:05:15 6: +++++++ royxmykx ad312256 "commit2"
186 royxmykx test.use 2001-02-03 08:05:13 7: new text from new commit 2
187 yostqsxw test.use 2001-02-03 08:05:15 8: >>>>>>> conflict 1 of 1 ends
188 [EOF]
189 "#);
190 }
191
192 #[test]
193 fn test_annotate_merge_one_sided_conflict_resolution() {
194 let test_env = TestEnvironment::default();
195 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
196 let work_dir = test_env.work_dir("repo");
197
198 work_dir.write_file("file.txt", "line1\n");
199 work_dir.run_jj(["describe", "-m=initial"]).success();
200 work_dir
201 .run_jj(["bookmark", "create", "-r@", "initial"])
202 .success();
203
204 work_dir.run_jj(["new", "-m=commit1"]).success();
205 append_to_file(
206 &work_dir.root().join("file.txt"),
207 "new text from new commit 1",
208 );
209 work_dir
210 .run_jj(["bookmark", "create", "-r@", "commit1"])
211 .success();
212
213 work_dir.run_jj(["new", "-m=commit2", "initial"]).success();
214 append_to_file(
215 &work_dir.root().join("file.txt"),
216 "new text from new commit 2",
217 );
218 work_dir
219 .run_jj(["bookmark", "create", "-r@", "commit2"])
220 .success();
221
222 // create a (conflicted) merge
223 work_dir
224 .run_jj(["new", "-m=merged", "commit1", "commit2"])
225 .success();
226 // resolve conflicts
227 work_dir.write_file("file.txt", "line1\nnew text from new commit 1\n");
228
229 let output = work_dir.run_jj(["file", "annotate", "file.txt"]);
230 insta::assert_snapshot!(output, @"
231 qpvuntsm test.use 2001-02-03 08:05:08 1: line1
232 zsuskuln test.use 2001-02-03 08:05:11 2: new text from new commit 1
233 [EOF]
234 ");
235 }
236
237 #[test]
238 fn test_annotate_abandoned() {
239 let test_env = TestEnvironment::default();
240 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
241 let work_dir = test_env.work_dir("repo");
242
243 work_dir.write_file("file.txt", "line1\n");
244 work_dir.run_jj(["new"]).success();
245 work_dir.write_file("file.txt", "line1\nline2\n");
246 work_dir.run_jj(["abandon"]).success();
247
248 let output = work_dir.run_jj(["file", "annotate", "-rat_operation(@-, @)", "file.txt"]);
249 insta::assert_snapshot!(output, @"
250 qpvuntsm test.use 2001-02-03 08:05:08 1: line1
251 rlvkpnrz test.use 2001-02-03 08:05:09 2: line2
252 [EOF]
253 ");
254 }
255
256 #[test]
257 fn test_annotate_with_template() {
258 let test_env = TestEnvironment::default();
259 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
260 let work_dir = test_env.work_dir("repo");
261
262 work_dir.write_file("file.txt", "initial 1\ninitial 2\ninitial 3\n");
263 work_dir.run_jj(["commit", "-m=initial"]).success();
264
265 work_dir.write_file(
266 work_dir.root().join("file.txt"),
267 "initial 2\nnew text from new commit 1\nthat splits into multiple lines\n",
268 );
269 work_dir.run_jj(["commit", "-m=commit1"]).success();
270
271 append_to_file(
272 &work_dir.root().join("file.txt"),
273 "new text from new commit 2\nalso continuing on a second line\nand a third!",
274 );
275 work_dir.run_jj(["describe", "-m=commit2"]).success();
276
277 let template = indoc::indoc! {r#"
278 if(first_line_in_hunk, "\n" ++ separate("\n",
279 commit.change_id().shortest(8)
280 ++ " "
281 ++ commit.description().first_line(),
282 commit_timestamp(commit).local().format('%Y-%m-%d %H:%M:%S')
283 ++ " "
284 ++ commit.author(),
285 ) ++ "\n") ++ pad_start(4, original_line_number) ++ " ->" ++ pad_start(4, line_number) ++ ": " ++ content
286 "#};
287
288 let output = work_dir.run_jj(["file", "annotate", "file.txt", "-T", template]);
289 insta::assert_snapshot!(output, @"
290
291 qpvuntsm initial
292 2001-02-03 08:05:08 Test User <[email protected]>
293 2 -> 1: initial 2
294
295 rlvkpnrz commit1
296 2001-02-03 08:05:09 Test User <[email protected]>
297 2 -> 2: new text from new commit 1
298 3 -> 3: that splits into multiple lines
299
300 kkmpptxz commit2
301 2001-02-03 08:05:10 Test User <[email protected]>
302 4 -> 4: new text from new commit 2
303 5 -> 5: also continuing on a second line
304 6 -> 6: and a third!
305 [EOF]
306 ");
307 }