Debian Codegraph @

cli/tests/test_global_opts.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 std::ffi::OsString;
16
17 use indoc::indoc;
18 use itertools::Itertools as _;
19 use regex::Regex;
20 use testutils::TestResult;
21
22 use crate::common::TestEnvironment;
23 use crate::common::TestWorkDir;
24
25 #[test]
26 fn test_non_utf8_arg() {
27 let test_env = TestEnvironment::default();
28 #[cfg(unix)]
29 let invalid_utf = {
30 use std::os::unix::ffi::OsStringExt as _;
31 OsString::from_vec(vec![0x66, 0x6f, 0x80, 0x6f])
32 };
33 #[cfg(windows)]
34 let invalid_utf = {
35 use std::os::windows::prelude::*;
36 OsString::from_wide(&[0x0066, 0x006f, 0xD800, 0x006f])
37 };
38 let output = test_env.run_jj_in(".", [&invalid_utf]);
39 insta::assert_snapshot!(output, @"
40 ------- stderr -------
41 Error: Non-UTF-8 argument
42 [EOF]
43 [exit status: 2]
44 ");
45 }
46
47 #[test]
48 fn test_version() {
49 let test_env = TestEnvironment::default();
50
51 let output = test_env.run_jj_in(".", ["--version"]).success();
52 let stdout = output.stdout.into_raw();
53 let sanitized = stdout.replace(|c: char| c.is_ascii_hexdigit(), "?");
54 let expected = [
55 "jj ?.??.?\n",
56 "jj ?.??.?-????????????????????????????????????????\n",
57 // This test could be made to succeed when `jj` is compiled at a merge commit by adding a
58 // few entries like "jj ?.??.?-????????????????????????????????????????-?????????????????
59 // ???????????????????????\n" here. However, it might be best to keep it failing, so that
60 // we avoid releasing `jj` with such `--version` output.
61 ];
62 assert!(
63 expected.contains(&sanitized.as_str()),
64 "`jj version` output: {stdout:?}.\nSanitized: {sanitized:?}\nExpected one of: {expected:?}"
65 );
66 }
67
68 #[test]
69 fn test_no_subcommand() {
70 let test_env = TestEnvironment::default();
71 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
72 let work_dir = test_env.work_dir("repo");
73
74 // Outside of a repo.
75 let output = test_env.run_jj_in(".", [""; 0]);
76 insta::assert_snapshot!(output, @r#"
77 ------- stderr -------
78 Hint: Use `jj -h` for a list of available commands.
79 Run `jj config set --user ui.default-command log` to disable this message.
80 Error: There is no jj repo in "."
81 [EOF]
82 [exit status: 1]
83 "#);
84
85 test_env.add_config(r#"ui.default-command="log""#);
86 let output = test_env.run_jj_in(".", [""; 0]);
87 insta::assert_snapshot!(output, @r#"
88 ------- stderr -------
89 Error: There is no jj repo in "."
90 [EOF]
91 [exit status: 1]
92 "#);
93
94 let output = test_env.run_jj_in(".", ["--help"]).success();
95 insta::assert_snapshot!(
96 output.stdout.normalized().lines().next().unwrap(),
97 @"Jujutsu (An experimental VCS)");
98 insta::assert_snapshot!(output.stderr, @"");
99
100 let output = test_env.run_jj_in(".", ["-R", "repo"]).success();
101 assert_eq!(output, work_dir.run_jj(["log"]));
102
103 // Inside of a repo.
104 let output = work_dir.run_jj([""; 0]).success();
105 assert_eq!(output, work_dir.run_jj(["log"]));
106
107 // Command argument that looks like a command name.
108 work_dir
109 .run_jj(["bookmark", "create", "-r@", "help"])
110 .success();
111 work_dir
112 .run_jj(["bookmark", "create", "-r@", "log"])
113 .success();
114 work_dir
115 .run_jj(["bookmark", "create", "-r@", "show"])
116 .success();
117 // TODO: test_env.run_jj(["-r", "help"]).success()
118 insta::assert_snapshot!(work_dir.run_jj(["-r", "log"]), @"
119 @ qpvuntsm [email protected] 2001-02-03 08:05:07 help log show e8849ae1
120 │ (empty) (no description set)
121 ~
122 [EOF]
123 ");
124 insta::assert_snapshot!(work_dir.run_jj(["-r", "show"]), @"
125 @ qpvuntsm [email protected] 2001-02-03 08:05:07 help log show e8849ae1
126 │ (empty) (no description set)
127 ~
128 [EOF]
129 ");
130
131 // Multiple default command strings work.
132 test_env.add_config(r#"ui.default-command=["commit", "-m", "foo"]"#);
133 work_dir.run_jj(["new"]).success();
134 work_dir.write_file("file.txt", "file");
135 let output = work_dir.run_jj([""; 0]);
136 insta::assert_snapshot!(output, @"
137 ------- stderr -------
138 Working copy (@) now at: kxryzmor 8db1ba9a (empty) (no description set)
139 Parent commit (@-) : lylxulpl 19f3adb2 foo
140 [EOF]
141 ");
142
143 // We get a warning if the default command isn't recognized and looks like
144 // it should have been specified as an array.
145 test_env.add_config(r#"ui.default-command="log -n 10""#);
146 let output = work_dir.run_jj([""; 0]);
147 insta::assert_snapshot!(output, @r#"
148 ------- stderr -------
149 Warning: To include flags/arguments in `ui.default-command`, use an array instead of a string: `ui.default-command = ["log", "-n", "10"]`
150 error: unrecognized subcommand 'log -n 10'
151
152 tip: a similar subcommand exists: 'log'
153
154 Usage: jj [OPTIONS] <COMMAND>
155
156 For more information, try '--help'.
157 [EOF]
158 [exit status: 2]
159 "#);
160
161 // Hint isn't printed for explicit subcommands.
162 let output = work_dir.run_jj(["foobar"]);
163 insta::assert_snapshot!(output, @r"
164 ------- stderr -------
165 error: unrecognized subcommand 'foobar'
166
167 tip: a similar subcommand exists: 'bookmark'
168
169 Usage: jj [OPTIONS] <COMMAND>
170
171 For more information, try '--help'.
172 [EOF]
173 [exit status: 2]
174 ");
175
176 // Hint isn't printed for explicit subcommands with spaces.
177 let output = work_dir.run_jj(["log -n 10"]);
178 insta::assert_snapshot!(output, @r"
179 ------- stderr -------
180 error: unrecognized subcommand 'log -n 10'
181
182 tip: a similar subcommand exists: 'log'
183
184 Usage: jj [OPTIONS] <COMMAND>
185
186 For more information, try '--help'.
187 [EOF]
188 [exit status: 2]
189 ");
190
191 // While the default command is invalid, explicit subcommands still work.
192 let output = work_dir.run_jj(["status"]);
193 insta::assert_snapshot!(output, @r"
194 The working copy has no changes.
195 Working copy (@) : kxryzmor 8db1ba9a (empty) (no description set)
196 Parent commit (@-): lylxulpl 19f3adb2 foo
197 [EOF]
198 ");
199 }
200
201 #[test]
202 fn test_ignore_working_copy() {
203 let test_env = TestEnvironment::default();
204 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
205 let work_dir = test_env.work_dir("repo");
206
207 work_dir.write_file("file", "initial");
208 let output = work_dir.run_jj(["log", "-T", "commit_id"]);
209 insta::assert_snapshot!(output, @"
210 @ 82a10a4d9ef783fd68b661f40ce10dd80d599d9e
211 ◆ 0000000000000000000000000000000000000000
212 [EOF]
213 ");
214
215 // Modify the file. With --ignore-working-copy, we still get the same commit
216 // ID.
217 work_dir.write_file("file", "modified");
218 let output_again = work_dir.run_jj(["log", "-T", "commit_id", "--ignore-working-copy"]);
219 assert_eq!(output_again, output);
220
221 // But without --ignore-working-copy, we get a new commit ID.
222 let output = work_dir.run_jj(["log", "-T", "commit_id"]);
223 insta::assert_snapshot!(output, @"
224 @ 00fc09f48ccf5c8b025a0f93b0ec3b0e4294a598
225 ◆ 0000000000000000000000000000000000000000
226 [EOF]
227 ");
228 }
229
230 #[test]
231 fn test_no_integrate_operation() {
232 let test_env = TestEnvironment::default();
233 test_env
234 .run_jj_in(test_env.env_root(), &["git", "init", "repo"])
235 .success();
236
237 let repo_path = test_env.env_root().join("repo");
238
239 std::fs::write(repo_path.join("file1"), "initial").unwrap();
240 test_env
241 .run_jj_in(&repo_path, &["commit", "-m=initial"])
242 .success();
243 let op_log_output = test_env.run_jj_in(&repo_path, &["op", "log"]);
244 let working_copy_output = test_env.run_jj_in(&repo_path, &["debug", "working-copy"]);
245
246 // Modify the working copy and run a mutating operation. With
247 // --no-integrate-operation, the working copy gets snapshotted and the operation
248 // gets created, but there's no new operation in the operation log, and the
249 // working copy state is not updated.
250 std::fs::write(repo_path.join("file2"), "initial").unwrap();
251 let output = test_env.run_jj_in(&repo_path, &["squash", "--no-integrate-operation"]);
252 insta::assert_snapshot!(output.stdout, @"");
253 insta::assert_snapshot!(output.stderr, @"
254 Snapshot operation left uncommitted because --no-integrate-operation was requested: 1b6e5938fe7e
255 Operation left uncommitted because --no-integrate-operation was requested: 0e885c491646
256 [EOF]
257 ");
258 let stderr = output.stderr.into_raw();
259 let last_line = stderr.lines().last().unwrap();
260 let op_id_hex = last_line[last_line.len() - 12..].to_string();
261 let output = test_env.run_jj_in(&repo_path, &["op", "log", "--ignore-working-copy"]);
262 assert_eq!(output.stdout, op_log_output.stdout);
263 let output = test_env.run_jj_in(&repo_path, &["debug", "working-copy"]);
264 assert_eq!(output.stdout, working_copy_output.stdout);
265
266 // We can see the resulting log and op log with --at-op
267 let stdout = test_env.run_jj_in(&repo_path, &["log", "-s", "--at-op", op_id_hex.as_str()]);
268 insta::assert_snapshot!(stdout, @"
269 @ mzvwutvl [email protected] 2001-02-03 08:05:11 10644da4
270 │ (empty) (no description set)
271 ○ qpvuntsm [email protected] 2001-02-03 08:05:11 599772f7
272 │ initial
273 │ A file1
274 │ A file2
275 ◆ zzzzzzzz root() 00000000
276 [EOF]
277 ");
278 let stdout = test_env.run_jj_in(&repo_path, &["op", "log", "--at-op", op_id_hex.as_str()]);
279 insta::assert_snapshot!(stdout, @"
280 @ 0e885c491646 [email protected] default@ 2001-02-03 04:05:11.000 +07:00 - 2001-02-03 04:05:11.000 +07:00
281 │ squash commits into e6fc2362ee5fdd5eb879befc0ae556a2f57b94a0
282 │ args: jj squash --no-integrate-operation
283 ○ 1b6e5938fe7e [email protected] default@ 2001-02-03 04:05:11.000 +07:00 - 2001-02-03 04:05:11.000 +07:00
284 │ snapshot working copy
285 │ args: jj squash --no-integrate-operation
286 ○ 8d4c74253280 [email protected] default@ 2001-02-03 04:05:08.000 +07:00 - 2001-02-03 04:05:08.000 +07:00
287 │ commit 289d54a4554ed0d7df9c47d566480a6b773ee431
288 │ args: jj commit '-m=initial'
289 ○ 65136c03793e [email protected] default@ 2001-02-03 04:05:08.000 +07:00 - 2001-02-03 04:05:08.000 +07:00
290 │ snapshot working copy
291 │ args: jj commit '-m=initial'
292 ○ f63ee16f9553 [email protected] 2001-02-03 04:05:07.000 +07:00 - 2001-02-03 04:05:07.000 +07:00
293 │ add workspace 'default'
294 ○ 000000000000 root()
295 [EOF]
296 ");
297 }
298
299 #[test]
300 fn test_no_integrate_operation_colocated() {
301 let test_env = TestEnvironment::default();
302 test_env
303 .run_jj_in(test_env.env_root(), &["git", "init", "--colocate", "repo"])
304 .success();
305
306 let repo_path = test_env.env_root().join("repo");
307
308 std::fs::write(repo_path.join("file1"), "initial").unwrap();
309 test_env
310 .run_jj_in(&repo_path, &["commit", "-m=initial"])
311 .success();
312 let op_log_output = test_env.run_jj_in(&repo_path, &["op", "log"]);
313 let working_copy_output = test_env.run_jj_in(&repo_path, &["debug", "working-copy"]);
314
315 // Modify the working copy and run a mutating operation. With
316 // --no-integrate-operation, the working copy gets snapshotted and the operation
317 // gets created, but there's no new operation in the operation log, and the
318 // working copy state is not updated.
319 std::fs::write(repo_path.join("file2"), "initial").unwrap();
320 let output = test_env.run_jj_in(&repo_path, &["squash", "--no-integrate-operation"]);
321 insta::assert_snapshot!(output.stdout, @"");
322 insta::assert_snapshot!(output.stderr, @"
323 Snapshot operation left uncommitted because --no-integrate-operation was requested: 62fa29d079c6
324 Operation left uncommitted because --no-integrate-operation was requested: ba14e2cc9e65
325 [EOF]
326 ");
327 let stderr = output.stderr.into_raw();
328 let last_line = stderr.lines().last().unwrap();
329 let op_id_hex = last_line[last_line.len() - 12..].to_string();
330 let output = test_env.run_jj_in(&repo_path, &["op", "log", "--ignore-working-copy"]);
331 assert_eq!(output.stdout, op_log_output.stdout);
332 let output = test_env.run_jj_in(&repo_path, &["debug", "working-copy"]);
333 assert_eq!(output.stdout, working_copy_output.stdout);
334
335 // We can see the resulting log and op log with --at-op
336 let stdout = test_env.run_jj_in(&repo_path, &["log", "-s", "--at-op", op_id_hex.as_str()]);
337 insta::assert_snapshot!(stdout, @"
338 @ mzvwutvl [email protected] 2001-02-03 08:05:11 10644da4
339 │ (empty) (no description set)
340 ○ qpvuntsm [email protected] 2001-02-03 08:05:11 599772f7
341 │ initial
342 │ A file1
343 │ A file2
344 ◆ zzzzzzzz root() 00000000
345 [EOF]
346 ");
347 let stdout = test_env.run_jj_in(&repo_path, &["op", "log", "--at-op", op_id_hex.as_str()]);
348 insta::assert_snapshot!(stdout, @"
349 @ ba14e2cc9e65 [email protected] default@ 2001-02-03 04:05:11.000 +07:00 - 2001-02-03 04:05:11.000 +07:00
350 │ squash commits into e6fc2362ee5fdd5eb879befc0ae556a2f57b94a0
351 │ args: jj squash --no-integrate-operation
352 ○ 62fa29d079c6 [email protected] default@ 2001-02-03 04:05:11.000 +07:00 - 2001-02-03 04:05:11.000 +07:00
353 │ snapshot working copy
354 │ args: jj squash --no-integrate-operation
355 ○ b1b9a22805e6 [email protected] default@ 2001-02-03 04:05:08.000 +07:00 - 2001-02-03 04:05:08.000 +07:00
356 │ commit 289d54a4554ed0d7df9c47d566480a6b773ee431
357 │ args: jj commit '-m=initial'
358 ○ 65136c03793e [email protected] default@ 2001-02-03 04:05:08.000 +07:00 - 2001-02-03 04:05:08.000 +07:00
359 │ snapshot working copy
360 │ args: jj commit '-m=initial'
361 ○ f63ee16f9553 [email protected] 2001-02-03 04:05:07.000 +07:00 - 2001-02-03 04:05:07.000 +07:00
362 │ add workspace 'default'
363 ○ 000000000000 root()
364 [EOF]
365 ");
366 }
367
368 #[test]
369 fn test_repo_arg_with_git_init() {
370 let test_env = TestEnvironment::default();
371 let output = test_env.run_jj_in(".", ["git", "init", "-R=.", "repo"]);
372 insta::assert_snapshot!(output, @r#"
373 ------- stderr -------
374 Error: There is no jj repo in "."
375 [EOF]
376 [exit status: 1]
377 "#);
378 }
379
380 #[test]
381 fn test_repo_arg_with_git_clone() {
382 let test_env = TestEnvironment::default();
383 let output = test_env.run_jj_in(".", ["git", "clone", "-R=.", "remote"]);
384 insta::assert_snapshot!(output, @r#"
385 ------- stderr -------
386 Error: There is no jj repo in "."
387 [EOF]
388 [exit status: 1]
389 "#);
390 }
391
392 #[test]
393 fn test_resolve_workspace_directory() {
394 let test_env = TestEnvironment::default();
395 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
396 let work_dir = test_env.work_dir("repo");
397 let sub_dir = work_dir.create_dir_all("dir/subdir");
398
399 // Ancestor of cwd
400 let output = sub_dir.run_jj(["status"]);
401 insta::assert_snapshot!(output, @"
402 The working copy has no changes.
403 Working copy (@) : qpvuntsm e8849ae1 (empty) (no description set)
404 Parent commit (@-): zzzzzzzz 00000000 (empty) (no description set)
405 [EOF]
406 ");
407
408 // Explicit subdirectory path
409 let output = sub_dir.run_jj(["status", "-R", "."]);
410 insta::assert_snapshot!(output, @r#"
411 ------- stderr -------
412 Error: There is no jj repo in "."
413 [EOF]
414 [exit status: 1]
415 "#);
416
417 // Valid explicit path
418 let output = sub_dir.run_jj(["status", "-R", "../.."]);
419 insta::assert_snapshot!(output, @"
420 The working copy has no changes.
421 Working copy (@) : qpvuntsm e8849ae1 (empty) (no description set)
422 Parent commit (@-): zzzzzzzz 00000000 (empty) (no description set)
423 [EOF]
424 ");
425
426 // "../../..".ancestors() contains "../..", but it should never be looked up.
427 let output = sub_dir.run_jj(["status", "-R", "../../.."]);
428 insta::assert_snapshot!(output, @r#"
429 ------- stderr -------
430 Error: There is no jj repo in "../../.."
431 [EOF]
432 [exit status: 1]
433 "#);
434 }
435
436 #[test]
437 fn test_no_workspace_directory() {
438 let test_env = TestEnvironment::default();
439 let work_dir = test_env.work_dir("repo");
440 work_dir.create_dir_all("");
441
442 let output = work_dir.run_jj(["status"]);
443 insta::assert_snapshot!(output, @r#"
444 ------- stderr -------
445 Error: There is no jj repo in "."
446 [EOF]
447 [exit status: 1]
448 "#);
449
450 let output = test_env.run_jj_in(".", ["status", "-R", "repo"]);
451 insta::assert_snapshot!(output, @r#"
452 ------- stderr -------
453 Error: There is no jj repo in "repo"
454 [EOF]
455 [exit status: 1]
456 "#);
457
458 work_dir.create_dir_all(".git");
459 let output = work_dir.run_jj(["status"]);
460 insta::assert_snapshot!(output, @r#"
461 ------- stderr -------
462 Error: There is no jj repo in "."
463 Hint: It looks like this is a git repo. You can create a jj repo backed by it by running this:
464 jj git init
465 [EOF]
466 [exit status: 1]
467 "#);
468 }
469
470 #[test]
471 fn test_bad_path() {
472 let test_env = TestEnvironment::default();
473 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
474 let work_dir = test_env.work_dir("repo");
475 let sub_dir = work_dir.create_dir_all("dir");
476
477 // cwd == workspace_root
478 let output = work_dir.run_jj(["file", "show", "../out"]);
479 insta::assert_snapshot!(output.normalize_backslash(), @r#"
480 ------- stderr -------
481 Error: Failed to parse fileset: Invalid file pattern
482 Caused by:
483 1: --> 1:1
484 |
485 1 | ../out
486 | ^----^
487 |
488 = Invalid file pattern
489 2: Path "../out" is not in the repo "."
490 3: Invalid component ".." in repo-relative path "../out"
491 [EOF]
492 [exit status: 1]
493 "#);
494
495 // cwd != workspace_root, can't be parsed as repo-relative path
496 let output = sub_dir.run_jj(["file", "show", "../.."]);
497 insta::assert_snapshot!(output.normalize_backslash(), @r#"
498 ------- stderr -------
499 Error: Failed to parse fileset: Invalid file pattern
500 Caused by:
501 1: --> 1:1
502 |
503 1 | ../..
504 | ^---^
505 |
506 = Invalid file pattern
507 2: Path "../.." is not in the repo ".."
508 3: Invalid component ".." in repo-relative path ".."
509 [EOF]
510 [exit status: 1]
511 "#);
512
513 // cwd != workspace_root, can be parsed as repo-relative path
514 let output = test_env.run_jj_in(".", ["file", "show", "-Rrepo", "out"]);
515 insta::assert_snapshot!(output.normalize_backslash(), @r#"
516 ------- stderr -------
517 Error: Failed to parse fileset: Invalid file pattern
518 Caused by:
519 1: --> 1:1
520 |
521 1 | out
522 | ^-^
523 |
524 = Invalid file pattern
525 2: Path "out" is not in the repo "repo"
526 3: Invalid component ".." in repo-relative path "../out"
527 Hint: Consider using root:"out" to specify repo-relative path
528 [EOF]
529 [exit status: 1]
530 "#);
531 }
532
533 #[test]
534 fn test_invalid_filesets_looking_like_filepaths() {
535 let test_env = TestEnvironment::default();
536 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
537 let work_dir = test_env.work_dir("repo");
538
539 let output = work_dir.run_jj(["file", "show", "abc~"]);
540 insta::assert_snapshot!(output, @"
541 ------- stderr -------
542 Error: Failed to parse fileset: Syntax error
543 Caused by: --> 1:5
544 |
545 1 | abc~
546 | ^---
547 |
548 = expected `~` or <primary>
549 Hint: See https://docs.jj-vcs.dev/latest/filesets/ or use `jj help -k filesets` for filesets syntax and how to match file paths.
550 [EOF]
551 [exit status: 1]
552 ");
553 }
554
555 #[test]
556 fn test_broken_repo_structure() {
557 let test_env = TestEnvironment::default();
558 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
559 let work_dir = test_env.work_dir("repo");
560 let store_path = work_dir.root().join(".jj").join("repo").join("store");
561 let store_type_path = store_path.join("type");
562
563 // Test the error message when the git repository can't be located.
564 work_dir.remove_file(store_path.join("git_target"));
565 let output = work_dir.run_jj(["log"]);
566 insta::assert_snapshot!(output.strip_stderr_last_line(), @"
567 ------- stderr -------
568 Internal error: The repository appears broken or inaccessible
569 Caused by:
570 1: Cannot access $TEST_ENV/repo/.jj/repo/store/git_target
571 [EOF]
572 [exit status: 255]
573 ");
574
575 // Test the error message when the commit backend is of unknown type.
576 work_dir.write_file(&store_type_path, "unknown");
577 let output = work_dir.run_jj(["log"]);
578 insta::assert_snapshot!(output, @"
579 ------- stderr -------
580 Internal error: This version of the jj binary doesn't support this type of repo
581 Caused by: Unsupported commit backend type 'unknown'
582 [EOF]
583 [exit status: 255]
584 ");
585
586 // Test the error message when the file indicating the commit backend type
587 // cannot be read.
588 work_dir.remove_file(&store_type_path);
589 work_dir.create_dir(&store_type_path);
590 let output = work_dir.run_jj(["log"]);
591 insta::assert_snapshot!(output.strip_stderr_last_line(), @"
592 ------- stderr -------
593 Internal error: The repository appears broken or inaccessible
594 Caused by:
595 1: Failed to read commit backend type
596 2: Cannot access $TEST_ENV/repo/.jj/repo/store/type
597 [EOF]
598 [exit status: 255]
599 ");
600
601 // Test when the .jj directory is empty. The error message is identical to
602 // the previous one, but writing the default type file would also fail.
603 work_dir.remove_dir_all(".jj");
604 work_dir.create_dir(".jj");
605 let output = work_dir.run_jj(["log"]);
606 insta::assert_snapshot!(output.strip_stderr_last_line(), @"
607 ------- stderr -------
608 Internal error: The repository appears broken or inaccessible
609 Caused by:
610 1: Failed to read commit backend type
611 2: Cannot access $TEST_ENV/repo/.jj/repo/store/type
612 [EOF]
613 [exit status: 255]
614 ");
615 }
616
617 #[test]
618 fn test_color_config() {
619 let mut test_env = TestEnvironment::default();
620
621 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
622 let work_dir = test_env.work_dir("repo");
623
624 // Test that --color=always is respected.
625 let output = work_dir.run_jj(["--color=always", "log", "-T", "commit_id"]);
626 insta::assert_snapshot!(output, @"
627 @ e8849ae12c709f2321908879bc724fdb2ab8a781
628 ◆ 0000000000000000000000000000000000000000
629 [EOF]
630 ");
631
632 // Test that color is used if it's requested in the config file
633 test_env.add_config(r#"ui.color="always""#);
634 let output = work_dir.run_jj(["log", "-T", "commit_id"]);
635 insta::assert_snapshot!(output, @"
636 @ e8849ae12c709f2321908879bc724fdb2ab8a781
637 ◆ 0000000000000000000000000000000000000000
638 [EOF]
639 ");
640
641 // Test that --color=never overrides the config.
642 let output = work_dir.run_jj(["--color=never", "log", "-T", "commit_id"]);
643 insta::assert_snapshot!(output, @"
644 @ e8849ae12c709f2321908879bc724fdb2ab8a781
645 ◆ 0000000000000000000000000000000000000000
646 [EOF]
647 ");
648
649 // Test that --color=auto overrides the config.
650 let output = work_dir.run_jj(["--color=auto", "log", "-T", "commit_id"]);
651 insta::assert_snapshot!(output, @"
652 @ e8849ae12c709f2321908879bc724fdb2ab8a781
653 ◆ 0000000000000000000000000000000000000000
654 [EOF]
655 ");
656
657 // Test that --config 'ui.color=never' overrides the config.
658 let output = work_dir.run_jj(["--config=ui.color=never", "log", "-T", "commit_id"]);
659 insta::assert_snapshot!(output, @"
660 @ e8849ae12c709f2321908879bc724fdb2ab8a781
661 ◆ 0000000000000000000000000000000000000000
662 [EOF]
663 ");
664
665 // --color overrides --config 'ui.color=...'.
666 let output = work_dir.run_jj([
667 "--color",
668 "never",
669 "--config=ui.color=always",
670 "log",
671 "-T",
672 "commit_id",
673 ]);
674 insta::assert_snapshot!(output, @"
675 @ e8849ae12c709f2321908879bc724fdb2ab8a781
676 ◆ 0000000000000000000000000000000000000000
677 [EOF]
678 ");
679
680 // Test that NO_COLOR does NOT override the request for color in the config file
681 test_env.add_env_var("NO_COLOR", "1");
682 let work_dir = test_env.work_dir("repo");
683 let output = work_dir.run_jj(["log", "-T", "commit_id"]);
684 insta::assert_snapshot!(output, @"
685 @ e8849ae12c709f2321908879bc724fdb2ab8a781
686 ◆ 0000000000000000000000000000000000000000
687 [EOF]
688 ");
689
690 // Test that per-repo config overrides the user config.
691 work_dir
692 .run_jj(["config", "set", "--repo", "ui.color", "never"])
693 .success();
694
695 let output = work_dir.run_jj(["log", "-T", "commit_id"]);
696 insta::assert_snapshot!(output, @"
697 @ e8849ae12c709f2321908879bc724fdb2ab8a781
698 ◆ 0000000000000000000000000000000000000000
699 [EOF]
700 ");
701
702 // Invalid --color
703 let output = work_dir.run_jj(["log", "--color=foo"]);
704 insta::assert_snapshot!(output, @"
705 ------- stderr -------
706 error: invalid value 'foo' for '--color <WHEN>'
707 [possible values: always, never, debug, auto]
708
709 For more information, try '--help'.
710 [EOF]
711 [exit status: 2]
712 ");
713 // Invalid ui.color
714 let stderr = work_dir.run_jj(["log", "--config=ui.color=true"]);
715 insta::assert_snapshot!(stderr, @"
716 ------- stderr -------
717 Config error: Invalid type or value for ui.color
718 Caused by: wanted string or table
719
720 For help, see https://docs.jj-vcs.dev/latest/config/ or use `jj help -k config`.
721 [EOF]
722 [exit status: 1]
723 ");
724 }
725
726 #[test]
727 fn test_pager_env_config() {
728 let mut test_env = TestEnvironment::default();
729 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
730 test_env.add_config(r#"ui.pager = "user""#);
731 test_env.add_env_var("PAGER", "env-base");
732 test_env.add_env_var("JJ_PAGER", "env-override");
733 let work_dir = test_env.work_dir("repo");
734
735 let output = work_dir.run_jj(["config", "list", "ui.pager", "--include-overridden"]);
736 insta::assert_snapshot!(output, @r#"
737 # ui.pager = "user"
738 ui.pager = "env-override"
739 [EOF]
740 "#);
741
742 let output = work_dir.run_jj([
743 "config",
744 "list",
745 "ui.pager",
746 "--include-overridden",
747 "--config=ui.pager=\"command-arg\"",
748 ]);
749 insta::assert_snapshot!(output, @r#"
750 # ui.pager = "user"
751 # ui.pager = "env-override"
752 ui.pager = "command-arg"
753 [EOF]
754 "#);
755 }
756
757 #[test]
758 fn test_color_ui_messages() {
759 let test_env = TestEnvironment::default();
760 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
761 let work_dir = test_env.work_dir("repo");
762 test_env.add_config("ui.color = 'always'");
763
764 // hint and error
765 let output = test_env.run_jj_in(".", ["-R."]);
766 insta::assert_snapshot!(output, @r#"
767 ------- stderr -------
768 Hint: Use `jj -h` for a list of available commands.
769 Run `jj config set --user ui.default-command log` to disable this message.
770 Error: There is no jj repo in "."
771 [EOF]
772 [exit status: 1]
773 "#);
774
775 // error source
776 let output = work_dir.run_jj(["log", ".."]);
777 insta::assert_snapshot!(output.normalize_backslash(), @r#"
778 ------- stderr -------
779 Error: Failed to parse fileset: Invalid file pattern
780 Caused by:
781 1:  --> 1:1
782  |
783 1 | ..
784  | ^^
785  |
786  = Invalid file pattern
787 2: Path ".." is not in the repo "."
788 3: Invalid component ".." in repo-relative path ".."
789 [EOF]
790 [exit status: 1]
791 "#);
792
793 // warning
794 let output = work_dir.run_jj(["log", "@"]);
795 insta::assert_snapshot!(output, @r#"
796 ------- stderr -------
797 Warning: No matching entries for paths: @
798 Warning: The argument "@" is being interpreted as a fileset expression. To specify a revset, pass -r "@" instead.
799 [EOF]
800 "#);
801
802 // error inlined in template output
803 work_dir.run_jj(["new"]).success();
804 let output = work_dir.run_jj([
805 "log",
806 "-r@|@--",
807 "--config=templates.log_node=commit_id",
808 "-Tdescription",
809 ]);
810 insta::assert_snapshot!(output, @"
811 8afc18ff677d32e40043e1bc8c1683c2f9c2e916
812 <Error: No Commit available> (elided revisions)
813 0000000000000000000000000000000000000000
814 [EOF]
815 ");
816
817 // formatted hint
818 let output = work_dir.run_jj(["edit", ".."]);
819 insta::assert_snapshot!(output, @"
820 ------- stderr -------
821 Error: Revset `..` resolved to more than one revision
822 Hint: The revset `..` resolved to these revisions:
823  mzvwutvl 8afc18ff (empty) (no description set)
824  qpvuntsm e8849ae1 (empty) (no description set)
825 [EOF]
826 [exit status: 1]
827 ");
828
829 // commit_summary template with debugging colors
830 let output = work_dir.run_jj(["st", "--color", "debug"]);
831 insta::assert_snapshot!(output, @"
832 The working copy has no changes.
833 Working copy (@) : <<commit working_copy change_id shortest prefix::m>><<commit working_copy change_id shortest rest::zvwutvl>><<commit working_copy:: >><<commit working_copy commit_id shortest prefix::8>><<commit working_copy commit_id shortest rest::afc18ff>><<commit working_copy:: >><<commit working_copy empty::(empty)>><<commit working_copy:: >><<commit working_copy empty description placeholder::(no description set)>>
834 Parent commit (@-): <<commit change_id shortest prefix::q>><<commit change_id shortest rest::pvuntsm>><<commit:: >><<commit commit_id shortest prefix::e>><<commit commit_id shortest rest::8849ae1>><<commit:: >><<commit empty::(empty)>><<commit:: >><<commit empty description placeholder::(no description set)>>
835 [EOF]
836 ");
837
838 // commit_summary template in transaction
839 let output = work_dir.run_jj(["revert", "--color=debug", "-r@", "-d@"]);
840 insta::assert_snapshot!(output, @r#"
841 ------- stderr -------
842 Reverted 1 commits as follows:
843 <<commit change_id shortest prefix::y>><<commit change_id shortest rest::ostqsxw>><<commit:: >><<commit commit_id shortest prefix::8b>><<commit commit_id shortest rest::f82eec>><<commit:: >><<commit empty::(empty)>><<commit:: >><<commit description first_line::Revert "">>
844 [EOF]
845 "#);
846 }
847
848 #[test]
849 fn test_quiet() {
850 let test_env = TestEnvironment::default();
851 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
852 let work_dir = test_env.work_dir("repo");
853
854 // Can skip message about new working copy with `--quiet`
855 work_dir.write_file("file1", "contents");
856 let output = work_dir.run_jj(["--quiet", "describe", "-m=new description"]);
857 insta::assert_snapshot!(output, @"");
858 }
859
860 #[test]
861 fn test_repeated_args() {
862 // Repeating an argument should be harmless rather than an error, so that an
863 // argument baked into an alias (or a wrapper command) can also be passed
864 // explicitly. See https://github.com/jj-vcs/jj/issues/9859 and
865 // https://github.com/jj-vcs/jj/issues/8101.
866 let test_env = TestEnvironment::default();
867 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
868 let work_dir = test_env.work_dir("repo");
869
870 // `--no-pager` is parsed by the early args parser, `--ignore-working-copy`
871 // is not. The repetition is accepted wherever the occurrences sit relative
872 // to the subcommand.
873 for flag in ["--no-pager", "--ignore-working-copy"] {
874 work_dir.run_jj([flag, flag, "status"]).success();
875 work_dir.run_jj([flag, "status", flag]).success();
876 work_dir.run_jj(["status", flag, flag]).success();
877 }
878
879 // Arguments of subcommands are covered as well.
880 let output = work_dir.run_jj(["log", "--no-graph", "-T=description"]);
881 let repeated_output = work_dir.run_jj(["log", "--no-graph", "--no-graph", "-T=description"]);
882 assert_eq!(repeated_output, output);
883
884 // Repeating a flag must not disable it: `--quiet` twice still suppresses
885 // the message about the new working-copy commit.
886 let output = work_dir.run_jj(["--quiet", "--quiet", "describe", "-m=new description"]);
887 insta::assert_snapshot!(output, @"");
888
889 // For an option that takes a value, the last occurrence wins.
890 work_dir.run_jj(["new", "-m=child"]).success();
891 let output = work_dir.run_jj(["log", "--no-graph", "-T=description", "-n=1", "-n=2"]);
892 insta::assert_snapshot!(output, @r"
893 child
894 new description
895 [EOF]
896 ");
897 work_dir.run_jj(["status", "-R=.", "-R=."]).success();
898
899 // Options that can be specified multiple times keep all of their values.
900 let output = work_dir.run_jj([
901 "--config=user.name=Custom User",
902 "[email protected]",
903 "config",
904 "list",
905 "user",
906 ]);
907 insta::assert_snapshot!(output, @r#"
908 user.name = "Custom User"
909 user.email = "[email protected]"
910 [EOF]
911 "#);
912 let output = work_dir.run_jj(["log", "--no-graph", "-T=description", "-r=@", "-r=@-"]);
913 insta::assert_snapshot!(output, @r"
914 child
915 new description
916 [EOF]
917 ");
918 }
919
920 #[test]
921 fn test_early_args() {
922 // Test that help output parses early args
923 let test_env = TestEnvironment::default();
924
925 // The default is no color.
926 let output = test_env.run_jj_in(".", ["help"]).success();
927 insta::assert_snapshot!(
928 output.stdout.normalized().lines().find(|l| l.contains("Commands:")).unwrap(),
929 @"Commands:");
930
931 // Check that output is colorized.
932 let output = test_env
933 .run_jj_in(".", ["--color=always", "help"])
934 .success();
935 insta::assert_snapshot!(
936 output.stdout.normalized().lines().find(|l| l.contains("Commands:")).unwrap(),
937 @"Commands:");
938
939 // Check that early args are accepted after the help command
940 let output = test_env
941 .run_jj_in(".", ["help", "--color=always"])
942 .success();
943 insta::assert_snapshot!(
944 output.stdout.normalized().lines().find(|l| l.contains("Commands:")).unwrap(),
945 @"Commands:");
946
947 // Check that early args are accepted after -h/--help
948 let output = test_env.run_jj_in(".", ["-h", "--color=always"]).success();
949 insta::assert_snapshot!(
950 output.stdout.normalized().lines().find(|l| l.contains("Usage:")).unwrap(),
951 @"Usage: jj [OPTIONS] <COMMAND>");
952 let output = test_env
953 .run_jj_in(".", ["log", "--help", "--color=always"])
954 .success();
955 insta::assert_snapshot!(
956 output.stdout.normalized().lines().find(|l| l.contains("Usage:")).unwrap(),
957 @"Usage: jj log [OPTIONS] [FILESETS]...");
958
959 // Early args are parsed with clap's ignore_errors(), but there is a known
960 // bug that causes defaults to be unpopulated. Test that the early args are
961 // tolerant of this bug and don't cause a crash.
962 test_env.run_jj_in(".", ["--no-pager", "help"]).success();
963 test_env
964 .run_jj_in(".", ["--config=ui.color=always", "help"])
965 .success();
966 }
967
968 #[test]
969 fn test_config_args() -> TestResult {
970 let test_env = TestEnvironment::default();
971 let list_config = |args: &[&str]| {
972 test_env.run_jj_in(
973 ".",
974 [&["config", "list", "--include-overridden", "test"], args].concat(),
975 )
976 };
977
978 std::fs::write(
979 test_env.env_root().join("file1.toml"),
980 indoc! {"
981 test.key1 = 'file1'
982 test.key2 = 'file1'
983 "},
984 )?;
985 std::fs::write(
986 test_env.env_root().join("file2.toml"),
987 indoc! {"
988 test.key3 = 'file2'
989 "},
990 )?;
991
992 let output = list_config(&["--config=test.key1=arg1"]);
993 insta::assert_snapshot!(output, @r#"
994 test.key1 = "arg1"
995 [EOF]
996 "#);
997 let output = list_config(&["--config-file=file1.toml"]);
998 insta::assert_snapshot!(output, @"
999 test.key1 = 'file1'
1000 test.key2 = 'file1'
1001 [EOF]
1002 ");
1003
1004 // --config items are inserted to a single layer internally
1005 let output = list_config(&[
1006 "--config=test.key1='arg1'",
1007 "--config=test.key2.sub=true",
1008 "--config=test.key1=arg3",
1009 ]);
1010 insta::assert_snapshot!(output, @r#"
1011 test.key1 = "arg3"
1012 test.key2.sub = true
1013 [EOF]
1014 "#);
1015
1016 // --config* arguments are processed in order of appearance
1017 let output = list_config(&[
1018 "--config=test.key1=arg1",
1019 "--config-file=file1.toml",
1020 "--config=test.key2=arg3",
1021 "--config-file=file2.toml",
1022 ]);
1023 insta::assert_snapshot!(output, @r#"
1024 # test.key1 = "arg1"
1025 test.key1 = 'file1'
1026 # test.key2 = 'file1'
1027 test.key2 = "arg3"
1028 test.key3 = 'file2'
1029 [EOF]
1030 "#);
1031
1032 let output = test_env.run_jj_in(".", ["config", "list", "--config=foo"]);
1033 insta::assert_snapshot!(output, @"
1034 ------- stderr -------
1035 Config error: --config must be specified as NAME=VALUE
1036 For help, see https://docs.jj-vcs.dev/latest/config/ or use `jj help -k config`.
1037 [EOF]
1038 [exit status: 1]
1039 ");
1040
1041 let output = test_env.run_jj_in(".", ["config", "list", "--config-file=unknown.toml"]);
1042 insta::with_settings!({
1043 filters => [("(?m)^([2-9]): .*", "$1: <redacted>")],
1044 }, {
1045 insta::assert_snapshot!(output, @"
1046 ------- stderr -------
1047 Config error: Failed to read configuration file
1048 Caused by:
1049 1: Cannot access unknown.toml
1050 2: <redacted>
1051 For help, see https://docs.jj-vcs.dev/latest/config/ or use `jj help -k config`.
1052 [EOF]
1053 [exit status: 1]
1054 ");
1055 });
1056 Ok(())
1057 }
1058
1059 #[test]
1060 fn test_invalid_config() {
1061 // Test that we get a reasonable error if the config is invalid (#55)
1062 let test_env = TestEnvironment::default();
1063
1064 test_env.add_config("[section]key = value-missing-quotes");
1065 let output = test_env.run_jj_in(".", ["git", "init", "repo"]);
1066 insta::assert_snapshot!(output, @"
1067 ------- stderr -------
1068 Config error: Configuration cannot be parsed as TOML document
1069 Caused by: TOML parse error at line 1, column 10
1070 |
1071 1 | [section]key = value-missing-quotes
1072 | ^
1073 unexpected key or value, expected newline, `#`
1074
1075 Hint: Check the config file: $TEST_ENV/config/config0002.toml
1076 For help, see https://docs.jj-vcs.dev/latest/config/ or use `jj help -k config`.
1077 [EOF]
1078 [exit status: 1]
1079 ");
1080 }
1081
1082 #[test]
1083 fn test_invalid_config_value() {
1084 // Test that we get a reasonable error if a config value is invalid
1085 let test_env = TestEnvironment::default();
1086 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
1087 let work_dir = test_env.work_dir("repo");
1088
1089 let output = work_dir.run_jj(["status", "--config=snapshot.auto-track=[0]"]);
1090 insta::assert_snapshot!(output, @"
1091 ------- stderr -------
1092 Config error: Invalid type or value for snapshot.auto-track
1093 Caused by: invalid type: sequence, expected a string
1094
1095 For help, see https://docs.jj-vcs.dev/latest/config/ or use `jj help -k config`.
1096 [EOF]
1097 [exit status: 1]
1098 ");
1099 }
1100
1101 #[test]
1102 #[cfg_attr(windows, ignore = "dirs::home_dir() can't be overridden by $HOME")] // TODO
1103 fn test_conditional_config() {
1104 let test_env = TestEnvironment::default();
1105 test_env
1106 .run_jj_in(test_env.home_dir(), ["git", "init", "repo1"])
1107 .success();
1108 test_env
1109 .run_jj_in(test_env.home_dir(), ["git", "init", "repo2"])
1110 .success();
1111 test_env.add_config(indoc! {"
1112 aliases.foo = ['new', 'root()', '-mglobal']
1113 [[--scope]]
1114 --when.repositories = ['~']
1115 aliases.foo = ['new', 'root()', '-mhome']
1116 [[--scope]]
1117 --when.repositories = ['~/repo1']
1118 aliases.foo = ['new', 'root()', '-mrepo1']
1119 "});
1120
1121 // Sanity check
1122 let output = test_env.run_jj_in(".", ["config", "list", "--include-overridden", "aliases"]);
1123 insta::assert_snapshot!(output, @"
1124 aliases.foo = ['new', 'root()', '-mglobal']
1125 [EOF]
1126 ");
1127 let output = test_env.run_jj_in(
1128 &test_env.home_dir().join("repo1"),
1129 ["config", "list", "--include-overridden", "aliases"],
1130 );
1131 insta::assert_snapshot!(output, @"
1132 # aliases.foo = ['new', 'root()', '-mglobal']
1133 # aliases.foo = ['new', 'root()', '-mhome']
1134 aliases.foo = ['new', 'root()', '-mrepo1']
1135 [EOF]
1136 ");
1137 let output = test_env.run_jj_in(
1138 &test_env.home_dir().join("repo2"),
1139 ["config", "list", "--include-overridden", "aliases"],
1140 );
1141 insta::assert_snapshot!(output, @"
1142 # aliases.foo = ['new', 'root()', '-mglobal']
1143 aliases.foo = ['new', 'root()', '-mhome']
1144 [EOF]
1145 ");
1146
1147 // Aliases can be expanded by using the conditional tables
1148 let output = test_env.run_jj_in(&test_env.home_dir().join("repo1"), ["foo"]);
1149 insta::assert_snapshot!(output, @"
1150 ------- stderr -------
1151 Working copy (@) now at: royxmykx 7c486962 (empty) repo1
1152 Parent commit (@-) : zzzzzzzz 00000000 (empty) (no description set)
1153 [EOF]
1154 ");
1155 let output = test_env.run_jj_in(&test_env.home_dir().join("repo2"), ["foo"]);
1156 insta::assert_snapshot!(output, @"
1157 ------- stderr -------
1158 Working copy (@) now at: yqosqzyt 072741b8 (empty) home
1159 Parent commit (@-) : zzzzzzzz 00000000 (empty) (no description set)
1160 [EOF]
1161 ");
1162 }
1163
1164 #[test]
1165 fn test_conditional_config_environments() {
1166 let mut test_env = TestEnvironment::default();
1167 test_env
1168 .run_jj_in(test_env.home_dir(), ["git", "init", "repo"])
1169 .success();
1170 let repo_dir = test_env.home_dir().join("repo");
1171 test_env.add_config(indoc! {"
1172 ui.pager = 'base'
1173 [[--scope]]
1174 --when.environments = ['MY_JJ_ENV=on']
1175 ui.pager = 'kv-match'
1176 [[--scope]]
1177 --when.environments = ['MY_JJ_ENV']
1178 ui.pager = 'key-exists'
1179 [[--scope]]
1180 --when.environments = ['ABSENT_VAR']
1181 ui.pager = 'absent'
1182 "});
1183
1184 test_env.add_env_var("MY_JJ_ENV", "on");
1185 let output = test_env.run_jj_in(
1186 &repo_dir,
1187 ["config", "list", "--include-overridden", "ui.pager"],
1188 );
1189 insta::assert_snapshot!(output, @r"
1190 # ui.pager = 'base'
1191 # ui.pager = 'kv-match'
1192 ui.pager = 'key-exists'
1193 [EOF]
1194 ");
1195 }
1196
1197 /// Test that `jj` command works with the default configuration.
1198 #[test]
1199 fn test_default_config() -> TestResult {
1200 let mut test_env = TestEnvironment::default();
1201 let config_dir = test_env.env_root().join("empty-config");
1202 std::fs::create_dir(&config_dir)?;
1203 test_env.set_config_path(&config_dir);
1204
1205 let envs_to_drop = test_env
1206 .new_jj_cmd()
1207 .get_envs()
1208 .filter_map(|(name, _)| name.to_str())
1209 .filter(|&name| name.starts_with("JJ_") && name != "JJ_CONFIG")
1210 .map(|name| name.to_owned())
1211 .sorted_unstable()
1212 .collect_vec();
1213 insta::assert_debug_snapshot!(envs_to_drop, @r#"
1214 [
1215 "JJ_EMAIL",
1216 "JJ_OP_HOSTNAME",
1217 "JJ_OP_TIMESTAMP",
1218 "JJ_OP_USERNAME",
1219 "JJ_RANDOMNESS_SEED",
1220 "JJ_TIMESTAMP",
1221 "JJ_TZ_OFFSET_MINS",
1222 "JJ_USER",
1223 ]
1224 "#);
1225 let run_jj = |work_dir: &TestWorkDir, args: &[&str]| {
1226 work_dir.run_jj_with(|cmd| {
1227 for name in &envs_to_drop {
1228 cmd.env_remove(name);
1229 }
1230 cmd.args(args)
1231 })
1232 };
1233
1234 let mut insta_settings = insta::Settings::clone_current();
1235 insta_settings.add_filter(r"\b[a-zA-Z0-9\-._]+@[a-zA-Z0-9\-._]+\b", "<user>@<host>");
1236 insta_settings.add_filter(
1237 r"\b[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}\b",
1238 "<date-time>",
1239 );
1240 insta_settings.add_filter(r"\b[k-z]{8,12}\b", "<change-id>");
1241 insta_settings.add_filter(r"\b[0-9a-f]{8,12}\b", "<id>");
1242 let _guard = insta_settings.bind_to_scope();
1243
1244 let maskable_op_user = {
1245 let maskable_re = Regex::new(r"^[a-zA-Z0-9\-._]+$")?;
1246 let hostname = whoami::hostname().expect("hostname should be set");
1247 let username = whoami::username().expect("username should be set");
1248 maskable_re.is_match(&hostname) && maskable_re.is_match(&username)
1249 };
1250
1251 let output = run_jj(
1252 &test_env.work_dir(""),
1253 &["config", "list", r#"-Tname ++ "\n""#],
1254 );
1255 insta::assert_snapshot!(output, @"
1256 operation.hostname
1257 operation.username
1258 [EOF]
1259 ");
1260
1261 let output = run_jj(&test_env.work_dir(""), &["git", "init", "repo"]);
1262 insta::assert_snapshot!(output, @r#"
1263 ------- stderr -------
1264 Initialized repo in "repo"
1265 [EOF]
1266 "#);
1267
1268 let work_dir = test_env.work_dir("repo");
1269 let output = run_jj(&work_dir, &["new"]);
1270 insta::assert_snapshot!(output, @r#"
1271 ------- stderr -------
1272 Working copy (@) now at: <change-id> <id> (empty) (no description set)
1273 Parent commit (@-) : <change-id> <id> (empty) (no description set)
1274 Warning: Name and email not configured. Until configured, your commits will be created with the empty identity, and can't be pushed to remotes.
1275 Hint: To configure, run:
1276 jj config set --user user.name "Some One"
1277 jj config set --user user.email "<user>@<host>"
1278 [EOF]
1279 "#);
1280
1281 let output = run_jj(&work_dir, &["log"]);
1282 insta::assert_snapshot!(output, @"
1283 @ <change-id> (no email set) <date-time> <id>
1284 │ (empty) (no description set)
1285 ○ <change-id> (no email set) <date-time> <id>
1286 │ (empty) (no description set)
1287 ◆ <change-id> root() <id>
1288 [EOF]
1289 ");
1290
1291 let time_config =
1292 "--config=template-aliases.'format_time_range(t)'='format_timestamp(t.end())'";
1293 let output = run_jj(&work_dir, &["op", "log", time_config]);
1294 if maskable_op_user {
1295 insta::assert_snapshot!(output, @"
1296 @ <id> <user>@<host> default@ <date-time>
1297 │ new empty commit
1298 │ args: jj new
1299 ○ <id> <user>@<host> <date-time>
1300 │ add workspace 'default'
1301 ○ <id> root()
1302 [EOF]
1303 ");
1304 }
1305 Ok(())
1306 }
1307
1308 #[test]
1309 fn test_no_user_configured() {
1310 // Test that the user is reminded if they haven't configured their name or email
1311 let test_env = TestEnvironment::default();
1312 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
1313 let work_dir = test_env.work_dir("repo");
1314
1315 let output = work_dir.run_jj_with(|cmd| {
1316 cmd.args(["describe", "-m", "without name"])
1317 .env_remove("JJ_USER")
1318 });
1319 insta::assert_snapshot!(output, @r#"
1320 ------- stderr -------
1321 Working copy (@) now at: qpvuntsm 7e7014c2 (empty) without name
1322 Parent commit (@-) : zzzzzzzz 00000000 (empty) (no description set)
1323 Warning: Name not configured. Until configured, your commits will be created with the empty identity, and can't be pushed to remotes.
1324 Hint: To configure, run:
1325 jj config set --user user.name "Some One"
1326 [EOF]
1327 "#);
1328 let output = work_dir.run_jj_with(|cmd| {
1329 cmd.args(["describe", "-m", "without email"])
1330 .env_remove("JJ_EMAIL")
1331 });
1332 insta::assert_snapshot!(output, @r#"
1333 ------- stderr -------
1334 Working copy (@) now at: qpvuntsm 876580e3 (empty) without email
1335 Parent commit (@-) : zzzzzzzz 00000000 (empty) (no description set)
1336 Warning: Email not configured. Until configured, your commits will be created with the empty identity, and can't be pushed to remotes.
1337 Hint: To configure, run:
1338 jj config set --user user.email "[email protected]"
1339 [EOF]
1340 "#);
1341 let output = work_dir.run_jj_with(|cmd| {
1342 cmd.args(["describe", "-m", "without name and email"])
1343 .env_remove("JJ_USER")
1344 .env_remove("JJ_EMAIL")
1345 });
1346 insta::assert_snapshot!(output, @r#"
1347 ------- stderr -------
1348 Working copy (@) now at: qpvuntsm f87356a1 (empty) without name and email
1349 Parent commit (@-) : zzzzzzzz 00000000 (empty) (no description set)
1350 Warning: Name and email not configured. Until configured, your commits will be created with the empty identity, and can't be pushed to remotes.
1351 Hint: To configure, run:
1352 jj config set --user user.name "Some One"
1353 jj config set --user user.email "[email protected]"
1354 [EOF]
1355 "#);
1356 }
1357
1358 #[test]
1359 fn test_help() {
1360 // Test that global options are separated out in the help output
1361 let test_env = TestEnvironment::default();
1362
1363 let output = test_env.run_jj_in(".", ["diffedit", "-h"]);
1364 insta::assert_snapshot!(output, @"
1365 Touch up the content changes in a revision with a diff editor
1366
1367 Usage: jj diffedit [OPTIONS] [FILESETS]...
1368
1369 Arguments:
1370 [FILESETS]... Edit only these paths (unmatched paths will remain unchanged)
1371
1372 Options:
1373 -r, --revision <REVSET> The revision to touch up
1374 -f, --from <REVSET> Show changes from this revision
1375 -t, --to <REVSET> Edit changes in this revision
1376 --tool <NAME> Specify diff editor to be used
1377 --restore-descendants Preserve the content (not the diff) when rebasing descendants
1378 -h, --help Print help (see more with '--help')
1379
1380 Global Options:
1381 -R, --repository <REPOSITORY> Path to repository to operate on
1382 --ignore-working-copy Don't snapshot the working copy, and don't update it
1383 --no-integrate-operation Run the command as usual but don't integrate any operations
1384 --ignore-immutable Allow rewriting immutable commits
1385 --at-operation <AT_OPERATION> Operation to load the repo at [alias: --at-op]
1386 --debug Enable debug logging
1387 --color <WHEN> When to colorize output [possible values: always, never, debug,
1388 auto]
1389 --quiet Silence non-primary command output
1390 --no-pager Disable the pager
1391 --config <NAME=VALUE> Additional configuration options (can be repeated)
1392 --config-file <PATH> Additional configuration files (can be repeated)
1393 [EOF]
1394 ");
1395 }
1396
1397 #[test]
1398 fn test_debug_logging_enabled() {
1399 // Test that the debug flag enabled debug logging
1400 let test_env = TestEnvironment::default();
1401
1402 let output = test_env.run_jj_in(".", ["version", "--debug"]).success();
1403 // Split the first log line into a timestamp and the rest.
1404 // The timestamp is constant sized so this is a robust operation.
1405 // Example timestamp: 2022-11-20T06:24:05.477703Z
1406 let (_timestamp, log_line) = output
1407 .stderr
1408 .normalized()
1409 .lines()
1410 .next()
1411 .expect("debug logging on first line")
1412 .split_at(36);
1413 // The log format is currently Pretty so we include the terminal markup.
1414 // Luckily, insta will print this in color when reviewing.
1415 insta::assert_snapshot!(log_line, @" INFO jj_cli::cli_util: debug logging enabled");
1416 }