Debian Codegraph @

cli/tests/test_advance_bookmarks.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 test_case::test_case;
16
17 use crate::common::CommandOutput;
18 use crate::common::TestEnvironment;
19 use crate::common::TestWorkDir;
20
21 #[must_use]
22 fn get_log_output_with_bookmarks(work_dir: &TestWorkDir) -> CommandOutput {
23 // Don't include commit IDs since they will be different depending on
24 // whether the test runs with `jj commit` or `jj describe` + `jj new`.
25 let template = r#""bookmarks{" ++ local_bookmarks ++ "} desc: " ++ description"#;
26 work_dir.run_jj(["log", "-T", template])
27 }
28
29 fn set_advance_bookmarks(test_env: &TestEnvironment, enabled: bool) {
30 if enabled {
31 test_env.add_config(
32 r#"[experimental-advance-branches]
33 enabled-branches = ["*"]
34 "#,
35 );
36 } else {
37 test_env.add_config(
38 r#"[experimental-advance-branches]
39 enabled-branches = []
40 "#,
41 );
42 }
43 }
44
45 // Runs a command in the specified test workspace that describes the current
46 // commit with `commit_message` and creates a new commit on top of it.
47 type CommitFn = fn(work_dir: &TestWorkDir, commit_message: &str);
48
49 // Implements CommitFn using the `jj commit` command.
50 fn commit_cmd(work_dir: &TestWorkDir, commit_message: &str) {
51 work_dir.run_jj(["commit", "-m", commit_message]).success();
52 }
53
54 // Implements CommitFn using the `jj describe` and `jj new`.
55 fn describe_new_cmd(work_dir: &TestWorkDir, commit_message: &str) {
56 work_dir
57 .run_jj(["describe", "-m", commit_message])
58 .success();
59 work_dir.run_jj(["new"]).success();
60 }
61
62 // Check that enabling and disabling advance-bookmarks works as expected.
63 #[test_case(commit_cmd ; "commit")]
64 #[test_case(describe_new_cmd; "new")]
65 fn test_advance_bookmarks_enabled(make_commit: CommitFn) {
66 let test_env = TestEnvironment::default();
67 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
68 let work_dir = test_env.work_dir("repo");
69
70 // First, test with advance-bookmarks enabled. Start by creating a bookmark on
71 // the root commit.
72 set_advance_bookmarks(&test_env, true);
73 work_dir
74 .run_jj(["bookmark", "create", "-r", "@-", "test_bookmark"])
75 .success();
76
77 // Check the initial state of the repo.
78 insta::allow_duplicates! {
79 insta::assert_snapshot!(get_log_output_with_bookmarks(&work_dir), @"
80 @ bookmarks{} desc:
81 ◆ bookmarks{test_bookmark} desc:
82 [EOF]
83 ");
84 }
85
86 // Run jj commit, which will advance the bookmark pointing to @-.
87 make_commit(&work_dir, "first");
88 insta::allow_duplicates! {
89 insta::assert_snapshot!(get_log_output_with_bookmarks(&work_dir), @"
90 @ bookmarks{} desc:
91 ○ bookmarks{test_bookmark} desc: first
92 ◆ bookmarks{} desc:
93 [EOF]
94 ");
95 }
96
97 // Now disable advance bookmarks and commit again. The bookmark shouldn't move.
98 set_advance_bookmarks(&test_env, false);
99 make_commit(&work_dir, "second");
100 insta::allow_duplicates! {
101 insta::assert_snapshot!(get_log_output_with_bookmarks(&work_dir), @"
102 @ bookmarks{} desc:
103 ○ bookmarks{} desc: second
104 ○ bookmarks{test_bookmark} desc: first
105 ◆ bookmarks{} desc:
106 [EOF]
107 ");
108 }
109 }
110
111 // Check that only a bookmark pointing to @- advances. Branches pointing to @
112 // are not advanced.
113 #[test_case(commit_cmd ; "commit")]
114 #[test_case(describe_new_cmd; "new")]
115 fn test_advance_bookmarks_at_minus(make_commit: CommitFn) {
116 let test_env = TestEnvironment::default();
117 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
118 let work_dir = test_env.work_dir("repo");
119
120 set_advance_bookmarks(&test_env, true);
121 work_dir
122 .run_jj(["bookmark", "create", "test_bookmark", "-r", "@"])
123 .success();
124
125 insta::allow_duplicates! {
126 insta::assert_snapshot!(get_log_output_with_bookmarks(&work_dir), @"
127 @ bookmarks{test_bookmark} desc:
128 ◆ bookmarks{} desc:
129 [EOF]
130 ");
131 }
132
133 make_commit(&work_dir, "first");
134 insta::allow_duplicates! {
135 insta::assert_snapshot!(get_log_output_with_bookmarks(&work_dir), @"
136 @ bookmarks{} desc:
137 ○ bookmarks{test_bookmark} desc: first
138 ◆ bookmarks{} desc:
139 [EOF]
140 ");
141 }
142
143 // Create a second bookmark pointing to @. On the next commit, only the first
144 // bookmark, which points to @-, will advance.
145 work_dir
146 .run_jj(["bookmark", "create", "test_bookmark2", "-r", "@"])
147 .success();
148 make_commit(&work_dir, "second");
149 insta::allow_duplicates! {
150 insta::assert_snapshot!(get_log_output_with_bookmarks(&work_dir), @"
151 @ bookmarks{} desc:
152 ○ bookmarks{test_bookmark test_bookmark2} desc: second
153 ○ bookmarks{} desc: first
154 ◆ bookmarks{} desc:
155 [EOF]
156 ");
157 }
158 }
159
160 // Test that per-bookmark overrides invert the behavior of
161 // experimental-advance-branches.enabled.
162 #[test_case(commit_cmd ; "commit")]
163 #[test_case(describe_new_cmd; "new")]
164 fn test_advance_bookmarks_overrides(make_commit: CommitFn) {
165 let test_env = TestEnvironment::default();
166 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
167 let work_dir = test_env.work_dir("repo");
168
169 // advance-branches is disabled by default.
170 work_dir
171 .run_jj(["bookmark", "create", "-r", "@-", "test_bookmark"])
172 .success();
173
174 // Check the initial state of the repo.
175 insta::allow_duplicates! {
176 insta::assert_snapshot!(get_log_output_with_bookmarks(&work_dir), @"
177 @ bookmarks{} desc:
178 ◆ bookmarks{test_bookmark} desc:
179 [EOF]
180 ");
181 }
182
183 // Commit will not advance the bookmark since advance-branches is disabled.
184 make_commit(&work_dir, "first");
185 insta::allow_duplicates! {
186 insta::assert_snapshot!(get_log_output_with_bookmarks(&work_dir), @"
187 @ bookmarks{} desc:
188 ○ bookmarks{} desc: first
189 ◆ bookmarks{test_bookmark} desc:
190 [EOF]
191 ");
192 }
193
194 // Now enable advance branches for "test_bookmark", move the bookmark, and
195 // commit again.
196 test_env.add_config(
197 r#"[experimental-advance-branches]
198 enabled-branches = ["test_bookmark"]
199 "#,
200 );
201 work_dir
202 .run_jj(["bookmark", "set", "test_bookmark", "-r", "@-"])
203 .success();
204 insta::allow_duplicates! {
205 insta::assert_snapshot!(get_log_output_with_bookmarks(&work_dir), @"
206 @ bookmarks{} desc:
207 ○ bookmarks{test_bookmark} desc: first
208 ◆ bookmarks{} desc:
209 [EOF]
210 ");
211 }
212 make_commit(&work_dir, "second");
213 insta::allow_duplicates! {
214 insta::assert_snapshot!(get_log_output_with_bookmarks(&work_dir), @"
215 @ bookmarks{} desc:
216 ○ bookmarks{test_bookmark} desc: second
217 ○ bookmarks{} desc: first
218 ◆ bookmarks{} desc:
219 [EOF]
220 ");
221 }
222
223 // Now disable advance branches for "test_bookmark" and enable for
224 // "second_bookmark", which we will use later. Disabling always takes
225 // precedence over enabling.
226 test_env.add_config(
227 r#"[experimental-advance-branches]
228 enabled-branches = ["test_bookmark | second_bookmark"]
229 disabled-branches = ["test_bookmark"]
230 "#,
231 );
232 make_commit(&work_dir, "third");
233 insta::allow_duplicates! {
234 insta::assert_snapshot!(get_log_output_with_bookmarks(&work_dir), @"
235 @ bookmarks{} desc:
236 ○ bookmarks{} desc: third
237 ○ bookmarks{test_bookmark} desc: second
238 ○ bookmarks{} desc: first
239 ◆ bookmarks{} desc:
240 [EOF]
241 ");
242 }
243
244 // If we create a new bookmark at @- and move test_bookmark there as well. When
245 // we commit, only "second_bookmark" will advance since "test_bookmark" is
246 // disabled.
247 work_dir
248 .run_jj(["bookmark", "create", "second_bookmark", "-r", "@-"])
249 .success();
250 work_dir
251 .run_jj(["bookmark", "set", "test_bookmark", "-r", "@-"])
252 .success();
253 insta::allow_duplicates! {
254 insta::assert_snapshot!(get_log_output_with_bookmarks(&work_dir), @"
255 @ bookmarks{} desc:
256 ○ bookmarks{second_bookmark test_bookmark} desc: third
257 ○ bookmarks{} desc: second
258 ○ bookmarks{} desc: first
259 ◆ bookmarks{} desc:
260 [EOF]
261 ");
262 }
263 make_commit(&work_dir, "fourth");
264 insta::allow_duplicates! {
265 insta::assert_snapshot!(get_log_output_with_bookmarks(&work_dir), @"
266 @ bookmarks{} desc:
267 ○ bookmarks{second_bookmark} desc: fourth
268 ○ bookmarks{test_bookmark} desc: third
269 ○ bookmarks{} desc: second
270 ○ bookmarks{} desc: first
271 ◆ bookmarks{} desc:
272 [EOF]
273 ");
274 }
275 }
276
277 // If multiple eligible bookmarks point to @-, all of them will be advanced.
278 #[test_case(commit_cmd ; "commit")]
279 #[test_case(describe_new_cmd; "new")]
280 fn test_advance_bookmarks_multiple_bookmarks(make_commit: CommitFn) {
281 let test_env = TestEnvironment::default();
282 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
283 let work_dir = test_env.work_dir("repo");
284
285 set_advance_bookmarks(&test_env, true);
286 work_dir
287 .run_jj(["bookmark", "create", "-r", "@-", "first_bookmark"])
288 .success();
289 work_dir
290 .run_jj(["bookmark", "create", "-r", "@-", "second_bookmark"])
291 .success();
292
293 insta::allow_duplicates! {
294 // Check the initial state of the repo.
295 insta::assert_snapshot!(get_log_output_with_bookmarks(&work_dir), @"
296 @ bookmarks{} desc:
297 ◆ bookmarks{first_bookmark second_bookmark} desc:
298 [EOF]
299 ");
300 }
301
302 // Both bookmarks are eligible and both will advance.
303 make_commit(&work_dir, "first");
304 insta::allow_duplicates! {
305 insta::assert_snapshot!(get_log_output_with_bookmarks(&work_dir), @"
306 @ bookmarks{} desc:
307 ○ bookmarks{first_bookmark second_bookmark} desc: first
308 ◆ bookmarks{} desc:
309 [EOF]
310 ");
311 }
312 }
313
314 // Call `jj new` on an interior commit and see that the bookmark pointing to its
315 // parent's parent is advanced.
316 #[test]
317 fn test_new_advance_bookmarks_interior() {
318 let test_env = TestEnvironment::default();
319 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
320 let work_dir = test_env.work_dir("repo");
321
322 set_advance_bookmarks(&test_env, true);
323
324 // Check the initial state of the repo.
325 insta::assert_snapshot!(get_log_output_with_bookmarks(&work_dir), @"
326 @ bookmarks{} desc:
327 ◆ bookmarks{} desc:
328 [EOF]
329 ");
330
331 // Create a gap in the commits for us to insert our new commit with --before.
332 work_dir.run_jj(["commit", "-m", "first"]).success();
333 work_dir.run_jj(["commit", "-m", "second"]).success();
334 work_dir.run_jj(["commit", "-m", "third"]).success();
335 work_dir
336 .run_jj(["bookmark", "create", "-r", "@---", "test_bookmark"])
337 .success();
338 insta::assert_snapshot!(get_log_output_with_bookmarks(&work_dir), @"
339 @ bookmarks{} desc:
340 ○ bookmarks{} desc: third
341 ○ bookmarks{} desc: second
342 ○ bookmarks{test_bookmark} desc: first
343 ◆ bookmarks{} desc:
344 [EOF]
345 ");
346
347 work_dir.run_jj(["new", "-r", "@--"]).success();
348 insta::assert_snapshot!(get_log_output_with_bookmarks(&work_dir), @"
349 @ bookmarks{} desc:
350 │ ○ bookmarks{} desc: third
351 ├─╯
352 ○ bookmarks{test_bookmark} desc: second
353 ○ bookmarks{} desc: first
354 ◆ bookmarks{} desc:
355 [EOF]
356 ");
357 }
358
359 // If the `--before` flag is passed to `jj new`, bookmarks are not advanced.
360 #[test]
361 fn test_new_advance_bookmarks_before() {
362 let test_env = TestEnvironment::default();
363 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
364 let work_dir = test_env.work_dir("repo");
365
366 set_advance_bookmarks(&test_env, true);
367
368 // Check the initial state of the repo.
369 insta::assert_snapshot!(get_log_output_with_bookmarks(&work_dir), @"
370 @ bookmarks{} desc:
371 ◆ bookmarks{} desc:
372 [EOF]
373 ");
374
375 // Create a gap in the commits for us to insert our new commit with --before.
376 work_dir.run_jj(["commit", "-m", "first"]).success();
377 work_dir.run_jj(["commit", "-m", "second"]).success();
378 work_dir.run_jj(["commit", "-m", "third"]).success();
379 work_dir
380 .run_jj(["bookmark", "create", "-r", "@---", "test_bookmark"])
381 .success();
382 insta::assert_snapshot!(get_log_output_with_bookmarks(&work_dir), @"
383 @ bookmarks{} desc:
384 ○ bookmarks{} desc: third
385 ○ bookmarks{} desc: second
386 ○ bookmarks{test_bookmark} desc: first
387 ◆ bookmarks{} desc:
388 [EOF]
389 ");
390
391 work_dir.run_jj(["new", "--before", "@-"]).success();
392 insta::assert_snapshot!(get_log_output_with_bookmarks(&work_dir), @"
393 ○ bookmarks{} desc: third
394 @ bookmarks{} desc:
395 ○ bookmarks{} desc: second
396 ○ bookmarks{test_bookmark} desc: first
397 ◆ bookmarks{} desc:
398 [EOF]
399 ");
400 }
401
402 // If the `--after` flag is passed to `jj new`, bookmarks are not advanced.
403 #[test]
404 fn test_new_advance_bookmarks_after() {
405 let test_env = TestEnvironment::default();
406 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
407 let work_dir = test_env.work_dir("repo");
408
409 set_advance_bookmarks(&test_env, true);
410 work_dir
411 .run_jj(["bookmark", "create", "-r", "@-", "test_bookmark"])
412 .success();
413
414 // Check the initial state of the repo.
415 insta::assert_snapshot!(get_log_output_with_bookmarks(&work_dir), @"
416 @ bookmarks{} desc:
417 ◆ bookmarks{test_bookmark} desc:
418 [EOF]
419 ");
420
421 work_dir.run_jj(["describe", "-m", "first"]).success();
422 work_dir.run_jj(["new", "--after", "@"]).success();
423 insta::assert_snapshot!(get_log_output_with_bookmarks(&work_dir), @"
424 @ bookmarks{} desc:
425 ○ bookmarks{} desc: first
426 ◆ bookmarks{test_bookmark} desc:
427 [EOF]
428 ");
429 }
430
431 #[test]
432 fn test_new_advance_bookmarks_merge_children() {
433 let test_env = TestEnvironment::default();
434 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
435 let work_dir = test_env.work_dir("repo");
436
437 set_advance_bookmarks(&test_env, true);
438 work_dir.run_jj(["desc", "-m", "0"]).success();
439 work_dir.run_jj(["new", "-m", "1"]).success();
440 work_dir.run_jj(["new", "subject(0)", "-m", "2"]).success();
441 work_dir
442 .run_jj(["bookmark", "create", "test_bookmark", "-rsubject(0)"])
443 .success();
444
445 // Check the initial state of the repo.
446 insta::assert_snapshot!(get_log_output_with_bookmarks(&work_dir), @"
447 @ bookmarks{} desc: 2
448 │ ○ bookmarks{} desc: 1
449 ├─╯
450 ○ bookmarks{test_bookmark} desc: 0
451 ◆ bookmarks{} desc:
452 [EOF]
453 ");
454
455 // The bookmark won't advance because `jj new` had multiple targets.
456 work_dir
457 .run_jj(["new", "subject(1)", "subject(2)"])
458 .success();
459 insta::assert_snapshot!(get_log_output_with_bookmarks(&work_dir), @"
460 @ bookmarks{} desc:
461 ├─╮
462 │ ○ bookmarks{} desc: 2
463 ○ │ bookmarks{} desc: 1
464 ├─╯
465 ○ bookmarks{test_bookmark} desc: 0
466 ◆ bookmarks{} desc:
467 [EOF]
468 ");
469 }