Debian Codegraph @

cli/tests/test_config_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 std::env::join_paths;
16 use std::path::Path;
17 use std::path::PathBuf;
18
19 use indoc::indoc;
20 use itertools::Itertools as _;
21 use regex::Regex;
22 use testutils::TestResult;
23
24 use crate::common::TestEnvironment;
25 use crate::common::default_config_from_schema;
26 use crate::common::fake_editor_path;
27 use crate::common::force_interactive;
28 use crate::common::to_toml_value;
29
30 #[test]
31 fn test_config_list_single() {
32 let test_env = TestEnvironment::default();
33 test_env.add_config(
34 r#"
35 [test-table]
36 somekey = "some value"
37 "#,
38 );
39
40 let output = test_env.run_jj_in(".", ["config", "list", "test-table.somekey"]);
41 insta::assert_snapshot!(output, @r#"
42 test-table.somekey = "some value"
43 [EOF]
44 "#);
45
46 let output = test_env.run_jj_in(
47 ".",
48 ["config", "list", r#"-Tname ++ "\n""#, "test-table.somekey"],
49 );
50 insta::assert_snapshot!(output, @"
51 test-table.somekey
52 [EOF]
53 ");
54 }
55
56 #[test]
57 fn test_config_list_nonexistent() {
58 let test_env = TestEnvironment::default();
59 let output = test_env.run_jj_in(".", ["config", "list", "nonexistent-test-key"]);
60 insta::assert_snapshot!(output, @r"
61 ------- stderr -------
62 Warning: No matching config key for: nonexistent-test-key
63 [EOF]
64 ");
65
66 let output = test_env.run_jj_in(".", ["config", "list", "--repo"]);
67 insta::assert_snapshot!(output, @r"
68 ------- stderr -------
69 Warning: No config to list.
70 [EOF]
71 ");
72 }
73
74 #[test]
75 fn test_config_list_table() {
76 let test_env = TestEnvironment::default();
77 test_env.add_config(
78 r#"
79 [test-table]
80 x = true
81 y.foo = "abc"
82 y.bar = 123
83 "z"."with space"."function()" = 5
84 "#,
85 );
86 let output = test_env.run_jj_in(".", ["config", "list", "test-table"]);
87 insta::assert_snapshot!(output, @r#"
88 test-table.x = true
89 test-table.y.foo = "abc"
90 test-table.y.bar = 123
91 test-table.z."with space"."function()" = 5
92 [EOF]
93 "#);
94 }
95
96 #[test]
97 fn test_config_list_inline_table() {
98 let test_env = TestEnvironment::default();
99 test_env.add_config(
100 r#"
101 test-table = { x = true, y = 1 }
102 "#,
103 );
104 // Inline tables are expanded
105 let output = test_env.run_jj_in(".", ["config", "list", "test-table"]);
106 insta::assert_snapshot!(output, @"
107 test-table.x = true
108 test-table.y = 1
109 [EOF]
110 ");
111 // Inner value can also be addressed by a dotted name path
112 let output = test_env.run_jj_in(".", ["config", "list", "test-table.x"]);
113 insta::assert_snapshot!(output, @"
114 test-table.x = true
115 [EOF]
116 ");
117 }
118
119 #[test]
120 fn test_config_list_array() {
121 let test_env = TestEnvironment::default();
122 test_env.add_config(
123 r#"
124 test-array = [1, "b", 3.4]
125 "#,
126 );
127 let output = test_env.run_jj_in(".", ["config", "list", "test-array"]);
128 insta::assert_snapshot!(output, @r#"
129 test-array = [1, "b", 3.4]
130 [EOF]
131 "#);
132 }
133
134 #[test]
135 fn test_config_list_array_of_tables() {
136 let test_env = TestEnvironment::default();
137 test_env.add_config(
138 r#"
139 [[test-table]]
140 x = 1
141 [[test-table]]
142 y = ["z"]
143 z."key=with whitespace" = []
144 "#,
145 );
146 // Array is a value, so is array of tables
147 let output = test_env.run_jj_in(".", ["config", "list", "test-table"]);
148 insta::assert_snapshot!(output, @r#"
149 test-table = [{ x = 1 }, { y = ["z"], z = { "key=with whitespace" = [] } }]
150 [EOF]
151 "#);
152 }
153
154 #[test]
155 fn test_config_list_all() {
156 let test_env = TestEnvironment::default();
157 test_env.add_config(
158 r#"
159 test-val = [1, 2, 3]
160 [test-table]
161 x = true
162 y.foo = "abc"
163 y.bar = 123
164 "#,
165 );
166
167 let output = test_env.run_jj_in(".", ["config", "list"]);
168 insta::assert_snapshot!(
169 output.normalize_stdout_with(|s| find_stdout_lines(r"(test-val|test-table\b[^=]*)", &s)), @r#"
170 test-val = [1, 2, 3]
171 test-table.x = true
172 test-table.y.foo = "abc"
173 test-table.y.bar = 123
174 [EOF]
175 "#);
176 }
177
178 #[test]
179 fn test_config_list_multiline_string() {
180 let test_env = TestEnvironment::default();
181 test_env.add_config(
182 r#"
183 multiline = '''
184 foo
185 bar
186 '''
187 "#,
188 );
189
190 let output = test_env.run_jj_in(".", ["config", "list", "multiline"]);
191 insta::assert_snapshot!(output, @"
192 multiline = '''
193 foo
194 bar
195 '''
196 [EOF]
197 ");
198
199 let output = test_env.run_jj_in(
200 ".",
201 [
202 "config",
203 "list",
204 "multiline",
205 "--include-overridden",
206 "--config=multiline='single'",
207 ],
208 );
209 insta::assert_snapshot!(output, @"
210 # multiline = '''
211 # foo
212 # bar
213 # '''
214 multiline = 'single'
215 [EOF]
216 ");
217 }
218
219 #[test]
220 fn test_config_list_layer() {
221 let mut test_env = TestEnvironment::default();
222 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
223 // Test with fresh new config file
224 let user_config_path = test_env.config_path().join("config.toml");
225 test_env.set_config_path(&user_config_path);
226 let work_dir = test_env.work_dir("repo");
227
228 // User
229 work_dir
230 .run_jj(["config", "set", "--user", "test-key", "test-val"])
231 .success();
232
233 work_dir
234 .run_jj([
235 "config",
236 "set",
237 "--user",
238 "test-layered-key",
239 "test-original-val",
240 ])
241 .success();
242
243 let output = work_dir.run_jj(["config", "list", "--user"]);
244 insta::assert_snapshot!(output, @r#"
245 test-key = "test-val"
246 test-layered-key = "test-original-val"
247 [EOF]
248 "#);
249
250 // Repo
251 work_dir
252 .run_jj([
253 "config",
254 "set",
255 "--repo",
256 "test-layered-key",
257 "test-layered-val",
258 ])
259 .success();
260
261 let output = work_dir.run_jj(["config", "list", "--user"]);
262 insta::assert_snapshot!(output, @r#"
263 test-key = "test-val"
264 [EOF]
265 "#);
266
267 let output = work_dir.run_jj(["config", "list", "--repo"]);
268 insta::assert_snapshot!(output, @r#"
269 test-layered-key = "test-layered-val"
270 [EOF]
271 "#);
272
273 // Workspace (new scope takes precedence over repo)
274 // Add a workspace-level setting
275 work_dir
276 .run_jj([
277 "config",
278 "set",
279 "--workspace",
280 "test-layered-wks-key",
281 "ws-val",
282 ])
283 .success();
284
285 // Listing user shouldn't include workspace
286 let output = work_dir.run_jj(["config", "list", "--user"]);
287 insta::assert_snapshot!(output, @r#"
288 test-key = "test-val"
289 [EOF]
290 "#);
291
292 // Workspace
293 let output = work_dir.run_jj(["config", "list", "--workspace"]);
294 insta::assert_snapshot!(output, @r#"
295 test-layered-wks-key = "ws-val"
296 [EOF]
297 "#);
298 }
299
300 #[test]
301 fn test_config_list_origin() {
302 let mut test_env = TestEnvironment::default();
303 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
304 // Test with fresh new config file
305 let user_config_path = test_env.config_path().join("config.toml");
306 test_env.set_config_path(&user_config_path);
307 let work_dir = test_env.work_dir("repo");
308
309 // User
310 work_dir
311 .run_jj(["config", "set", "--user", "test-key", "test-val"])
312 .success();
313
314 work_dir
315 .run_jj([
316 "config",
317 "set",
318 "--user",
319 "test-layered-key",
320 "test-original-val",
321 ])
322 .success();
323
324 // Repo
325 work_dir
326 .run_jj([
327 "config",
328 "set",
329 "--repo",
330 "test-layered-key",
331 "test-layered-val",
332 ])
333 .success();
334
335 let output = work_dir.run_jj([
336 "config",
337 "list",
338 "-Tbuiltin_config_list_detailed",
339 "--config",
340 "test-cli-key=test-cli-val",
341 ]);
342 insta::assert_snapshot!(output, @r#"
343 test-key = "test-val" # user $TEST_ENV/config/config.toml
344 test-layered-key = "test-layered-val" # repo $TEST_ENV/home/.config/jj/repos/0757f5ec8418b4f0983d/config.toml
345 user.name = "Test User" # env
346 user.email = "[email protected]" # env
347 debug.commit-timestamp = "2001-02-03T04:05:11+07:00" # env
348 debug.randomness-seed = 5 # env
349 debug.operation-timestamp = "2001-02-03T04:05:11+07:00" # env
350 operation.hostname = "host.example.com" # env
351 operation.username = "test-username" # env
352 test-cli-key = "test-cli-val" # cli
353 [EOF]
354 "#);
355
356 let output = work_dir.run_jj([
357 "config",
358 "list",
359 "-Tbuiltin_config_list_detailed",
360 "--color=debug",
361 "--include-defaults",
362 "--include-overridden",
363 "--config=test-key=test-cli-val",
364 "test-key",
365 ]);
366 insta::assert_snapshot!(output, @r#"
367 <<config_list overridden name::# test-key>><<config_list overridden:: = >><<config_list overridden value::"test-val">><<config_list overridden:: # >><<config_list overridden source::user>><<config_list overridden:: >><<config_list overridden path::$TEST_ENV/config/config.toml>><<config_list overridden::>>
368 <<config_list name::test-key>><<config_list:: = >><<config_list value::"test-cli-val">><<config_list:: # >><<config_list source::cli>><<config_list::>>
369 [EOF]
370 "#);
371
372 let output = work_dir.run_jj([
373 "config",
374 "list",
375 r#"-Tjson(self) ++ "\n""#,
376 "--include-defaults",
377 "--include-overridden",
378 "--config=test-key=test-cli-val",
379 "test-key",
380 ]);
381 insta::with_settings!({
382 // Windows paths will be escaped in JSON syntax, which cannot be
383 // normalized as $TEST_ENV.
384 filters => [(r#""path":"[^"]*","#, r#""path":"<redacted>","#)],
385 }, {
386 insta::assert_snapshot!(output, @r#"
387 {"name":"test-key","value":"test-val","source":"user","path":"<redacted>","is_overridden":true}
388 {"name":"test-key","value":"test-cli-val","source":"cli","path":null,"is_overridden":false}
389 [EOF]
390 "#);
391 });
392 }
393
394 #[test]
395 fn test_config_layer_override_default() {
396 let test_env = TestEnvironment::default();
397 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
398 let work_dir = test_env.work_dir("repo");
399 let config_key = "merge-tools.vimdiff.program";
400
401 // Default
402 let output = work_dir.run_jj(["config", "list", config_key, "--include-defaults"]);
403 insta::assert_snapshot!(output, @r#"
404 merge-tools.vimdiff.program = "vim"
405 [EOF]
406 "#);
407
408 // User
409 test_env.add_config(format!(
410 "{config_key} = {value}\n",
411 value = to_toml_value("user")
412 ));
413 let output = work_dir.run_jj(["config", "list", config_key]);
414 insta::assert_snapshot!(output, @r#"
415 merge-tools.vimdiff.program = "user"
416 [EOF]
417 "#);
418
419 // Repo
420 work_dir
421 .run_jj(["config", "set", "--repo", config_key, "repo"])
422 .success();
423 let output = work_dir.run_jj(["config", "list", config_key]);
424 insta::assert_snapshot!(output, @r#"
425 merge-tools.vimdiff.program = "repo"
426 [EOF]
427 "#);
428
429 // Command argument
430 let output = work_dir.run_jj([
431 "config",
432 "list",
433 config_key,
434 "--config",
435 &format!("{config_key}={value}", value = to_toml_value("command-arg")),
436 ]);
437 insta::assert_snapshot!(output, @r#"
438 merge-tools.vimdiff.program = "command-arg"
439 [EOF]
440 "#);
441
442 // Allow printing overridden values
443 let output = work_dir.run_jj([
444 "config",
445 "list",
446 config_key,
447 "--include-overridden",
448 "--config",
449 &format!("{config_key}={value}", value = to_toml_value("command-arg")),
450 ]);
451 insta::assert_snapshot!(output, @r#"
452 # merge-tools.vimdiff.program = "user"
453 # merge-tools.vimdiff.program = "repo"
454 merge-tools.vimdiff.program = "command-arg"
455 [EOF]
456 "#);
457
458 let output = work_dir.run_jj([
459 "config",
460 "list",
461 "--color=always",
462 config_key,
463 "--include-overridden",
464 ]);
465 insta::assert_snapshot!(output, @r#"
466 # merge-tools.vimdiff.program = "user"
467 merge-tools.vimdiff.program = "repo"
468 [EOF]
469 "#);
470 }
471
472 #[test]
473 fn test_config_layer_override_env() {
474 let mut test_env = TestEnvironment::default();
475 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
476 let config_key = "ui.editor";
477
478 // Environment base
479 test_env.add_env_var("EDITOR", "env-base");
480 let work_dir = test_env.work_dir("repo");
481 let output = work_dir.run_jj(["config", "list", config_key]);
482 insta::assert_snapshot!(output, @r#"
483 ui.editor = "env-base"
484 [EOF]
485 "#);
486
487 // User
488 test_env.add_config(format!(
489 "{config_key} = {value}\n",
490 value = to_toml_value("user")
491 ));
492 let output = work_dir.run_jj(["config", "list", config_key]);
493 insta::assert_snapshot!(output, @r#"
494 ui.editor = "user"
495 [EOF]
496 "#);
497
498 // Repo
499 work_dir
500 .run_jj(["config", "set", "--repo", config_key, "repo"])
501 .success();
502 let output = work_dir.run_jj(["config", "list", config_key]);
503 insta::assert_snapshot!(output, @r#"
504 ui.editor = "repo"
505 [EOF]
506 "#);
507
508 // Environment override
509 test_env.add_env_var("JJ_EDITOR", "env-override");
510 let work_dir = test_env.work_dir("repo");
511 let output = work_dir.run_jj(["config", "list", config_key]);
512 insta::assert_snapshot!(output, @r#"
513 ui.editor = "env-override"
514 [EOF]
515 "#);
516
517 // Command argument
518 let output = work_dir.run_jj([
519 "config",
520 "list",
521 config_key,
522 "--config",
523 &format!("{config_key}={value}", value = to_toml_value("command-arg")),
524 ]);
525 insta::assert_snapshot!(output, @r#"
526 ui.editor = "command-arg"
527 [EOF]
528 "#);
529
530 // Allow printing overridden values
531 let output = work_dir.run_jj([
532 "config",
533 "list",
534 config_key,
535 "--include-overridden",
536 "--config",
537 &format!("{config_key}={value}", value = to_toml_value("command-arg")),
538 ]);
539 insta::assert_snapshot!(output, @r#"
540 # ui.editor = "env-base"
541 # ui.editor = "user"
542 # ui.editor = "repo"
543 # ui.editor = "env-override"
544 ui.editor = "command-arg"
545 [EOF]
546 "#);
547 }
548
549 #[test]
550 fn test_config_layer_workspace() {
551 let test_env = TestEnvironment::default();
552 test_env.run_jj_in(".", ["git", "init", "main"]).success();
553 let main_dir = test_env.work_dir("main");
554 let secondary_dir = test_env.work_dir("secondary");
555 let config_key = "ui.editor";
556
557 main_dir.write_file("file", "contents");
558 main_dir.run_jj(["new"]).success();
559 main_dir
560 .run_jj(["workspace", "add", "--name", "second", "../secondary"])
561 .success();
562
563 // Repo
564 main_dir
565 .run_jj(["config", "set", "--repo", config_key, "main-repo"])
566 .success();
567 let output = main_dir.run_jj(["config", "list", config_key]);
568 insta::assert_snapshot!(output, @r#"
569 ui.editor = "main-repo"
570 [EOF]
571 "#);
572 let output = secondary_dir.run_jj(["config", "list", config_key]);
573 insta::assert_snapshot!(output, @r#"
574 ui.editor = "main-repo"
575 [EOF]
576 "#);
577 }
578
579 #[test]
580 fn test_config_set_bad_opts() {
581 let test_env = TestEnvironment::default();
582 let output = test_env.run_jj_in(".", ["config", "set"]);
583 insta::assert_snapshot!(output, @"
584 ------- stderr -------
585 error: the following required arguments were not provided:
586 <--user|--repo|--workspace|--file <PATH>>
587 <NAME>
588 <VALUE>
589
590 Usage: jj config set <--user|--repo|--workspace|--file <PATH>> <NAME> <VALUE>
591
592 For more information, try '--help'.
593 [EOF]
594 [exit status: 2]
595 ");
596
597 let output = test_env.run_jj_in(".", ["config", "set", "--user", "", "x"]);
598 insta::assert_snapshot!(output, @"
599 ------- stderr -------
600 error: invalid value '' for '<NAME>': TOML parse error at line 1, column 1
601 |
602 1 |
603 | ^
604 unquoted keys cannot be empty, expected letters, numbers, `-`, `_`
605
606
607 For more information, try '--help'.
608 [EOF]
609 [exit status: 2]
610 ");
611
612 let output = test_env.run_jj_in(".", ["config", "set", "--user", "x", "['typo'}"]);
613 insta::assert_snapshot!(output, @"
614 ------- stderr -------
615 error: invalid value '['typo'}' for '<VALUE>': TOML parse error at line 1, column 8
616 |
617 1 | ['typo'}
618 | ^
619 missing comma between array elements, expected `,`
620
621
622 For more information, try '--help'.
623 [EOF]
624 [exit status: 2]
625 ");
626 }
627
628 #[test]
629 fn test_config_set_for_user() {
630 let mut test_env = TestEnvironment::default();
631 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
632 // Test with fresh new config file
633 let user_config_path = test_env.config_path().join("config.toml");
634 test_env.set_config_path(&user_config_path);
635 let work_dir = test_env.work_dir("repo");
636
637 work_dir
638 .run_jj(["config", "set", "--user", "test-key", "test-val"])
639 .success();
640 work_dir
641 .run_jj(["config", "set", "--user", "test-table.foo", "true"])
642 .success();
643 work_dir
644 .run_jj(["config", "set", "--user", "test-table.'bar()'", "0"])
645 .success();
646
647 // Ensure test-key successfully written to user config.
648 let user_config_toml = std::fs::read_to_string(&user_config_path)
649 .unwrap_or_else(|_| panic!("Failed to read file {}", user_config_path.display()));
650 insta::assert_snapshot!(user_config_toml, @r#"
651 #:schema https://docs.jj-vcs.dev/latest/config-schema.json
652
653 test-key = "test-val"
654
655 [test-table]
656 foo = true
657 'bar()' = 0
658 "#);
659 }
660
661 #[test]
662 fn test_config_set_for_user_directory() -> TestResult {
663 let test_env = TestEnvironment::default();
664
665 test_env
666 .run_jj_in(".", ["config", "set", "--user", "test-key", "test-val"])
667 .success();
668 insta::assert_snapshot!(
669 std::fs::read_to_string(test_env.last_config_file_path())?,
670 @r#"
671 test-key = "test-val"
672
673 [template-aliases]
674 'format_time_range(time_range)' = 'time_range.start() ++ " - " ++ time_range.end()'
675
676 [git]
677 colocate = false
678 "#);
679
680 // Add one more config file to the directory
681 test_env.add_config("");
682 let output = test_env.run_jj_in(
683 ".",
684 ["config", "set", "--user", "test-key", "test-other-val"],
685 );
686 insta::assert_snapshot!(output, @"");
687
688 insta::assert_snapshot!(
689 std::fs::read_to_string(test_env.first_config_file_path())?,
690 @r#"
691 test-key = "test-other-val"
692
693 [template-aliases]
694 'format_time_range(time_range)' = 'time_range.start() ++ " - " ++ time_range.end()'
695
696 [git]
697 colocate = false
698 "#);
699
700 insta::assert_snapshot!(
701 std::fs::read_to_string(test_env.last_config_file_path())?,
702 @"");
703 Ok(())
704 }
705
706 #[test]
707 fn test_config_set_for_repo() -> TestResult {
708 let test_env = TestEnvironment::default();
709 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
710 let work_dir = test_env.work_dir("repo");
711 work_dir
712 .run_jj(["config", "set", "--repo", "test-key", "test-val"])
713 .success();
714 work_dir
715 .run_jj(["config", "set", "--repo", "test-table.foo", "true"])
716 .success();
717 // Ensure test-key successfully written to user config.
718 let config_dir = test_env.work_dir("home/.config/jj/repos/8e4fac809cbb3b162c95");
719 let repo_config_toml = config_dir.read_file("config.toml");
720 insta::assert_snapshot!(repo_config_toml, @r#"
721 #:schema https://docs.jj-vcs.dev/latest/config-schema.json
722
723 test-key = "test-val"
724
725 [test-table]
726 foo = true
727 "#);
728
729 std::fs::remove_dir_all(config_dir.root())?;
730 let output = work_dir.run_jj(["config", "path", "--repo"]);
731 insta::assert_snapshot!(output, @"
732 $TEST_ENV/home/.config/jj/repos/8e4fac809cbb3b162c95/config.toml
733 [EOF]
734 ------- stderr -------
735 Warning: Per-repo config not found. Generating an empty one.
736 Per-repo config is stored in the same directory as your user config for security reasons.
737 If you work across multiple computers, you may want to keep your user config directory in sync.
738 [EOF]
739 ");
740
741 // Check that it's regenerated the metadata.
742 assert!(config_dir.root().join("metadata.binpb").is_file());
743 // But not the config file itself.
744 assert!(!config_dir.root().join("config.toml").is_file());
745 Ok(())
746 }
747
748 #[test]
749 fn test_config_set_for_workspace() {
750 let test_env = TestEnvironment::default();
751 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
752 let work_dir = test_env.work_dir("repo");
753 work_dir.run_jj(["new"]).success();
754 work_dir
755 .run_jj(["workspace", "add", "--name", "second", "../secondary"])
756 .success();
757 let work_dir = test_env.work_dir("secondary");
758
759 // set in workspace
760 work_dir
761 .run_jj(["config", "set", "--workspace", "test-key", "ws-val"])
762 .success();
763
764 // Read workspace config
765 let workspace_config = &test_env
766 .work_dir("home/.config/jj/workspaces/0757f5ec8418b4f0983d")
767 .read_file("config.toml");
768 insta::assert_snapshot!(workspace_config, @r#"
769 #:schema https://docs.jj-vcs.dev/latest/config-schema.json
770
771 test-key = "ws-val"
772 "#);
773 }
774
775 #[test]
776 fn test_config_set_for_file() -> TestResult {
777 let test_env = TestEnvironment::default();
778
779 // Create a second config file
780 test_env.add_config("");
781 let first_path = test_env.first_config_file_path();
782 let last_path = test_env.last_config_file_path();
783
784 // Setting a key with --file shouldn't prompt even if multiple files exist
785 let output = test_env.run_jj_in(
786 ".",
787 [
788 "config",
789 "set",
790 "--file",
791 last_path.to_str().unwrap(),
792 "test-key",
793 "test-val",
794 ],
795 );
796 insta::assert_snapshot!(output, @"");
797
798 insta::assert_snapshot!(
799 std::fs::read_to_string(last_path)?,
800 @r#"test-key = "test-val""#);
801
802 // Verify the first file remained unchanged
803 insta::assert_snapshot!(
804 std::fs::read_to_string(first_path)?,
805 @r#"
806
807 [template-aliases]
808 'format_time_range(time_range)' = 'time_range.start() ++ " - " ++ time_range.end()'
809
810 [git]
811 colocate = false
812 "#);
813
814 Ok(())
815 }
816
817 #[test]
818 fn test_config_set_file_with_existing_scopes() -> TestResult {
819 let mut test_env = TestEnvironment::default();
820 let conf_d = test_env.env_root().join("conf.d");
821 std::fs::create_dir_all(&conf_d)?;
822 let user_config_path = join_paths([test_env.config_path(), &conf_d])?;
823 test_env.set_config_path(&user_config_path);
824
825 let work_conf = conf_d.join("work.toml");
826 std::fs::write(
827 &work_conf,
828 indoc! {"
829 --when.repositories = ['some/path']
830 work_user = 'Work User'
831 "},
832 )?;
833 let output = test_env.run_jj_in(
834 ".",
835 [
836 "config",
837 "set",
838 "--file",
839 work_conf.to_str().unwrap(),
840 "shared_key",
841 "shared_val",
842 ],
843 );
844 insta::assert_snapshot!(output, @"");
845 insta::assert_snapshot!(
846 std::fs::read_to_string(&work_conf)?,
847 @r#"
848 --when.repositories = ['some/path']
849 work_user = 'Work User'
850 shared_key = "shared_val"
851 "#
852 );
853
854 Ok(())
855 }
856
857 #[test]
858 fn test_config_set_toml_types() -> TestResult {
859 let mut test_env = TestEnvironment::default();
860 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
861 // Test with fresh new config file
862 let user_config_path = test_env.config_path().join("config.toml");
863 test_env.set_config_path(&user_config_path);
864 let work_dir = test_env.work_dir("repo");
865
866 let set_value = |key, value| {
867 work_dir
868 .run_jj(["config", "set", "--user", key, value])
869 .success();
870 };
871 set_value("test-table.integer", "42");
872 set_value("test-table.float", "3.14");
873 set_value("test-table.array", r#"["one", "two"]"#);
874 set_value("test-table.boolean", "true");
875 set_value("test-table.string", r#""foo""#);
876 set_value("test-table.invalid", r"a + b");
877 insta::assert_snapshot!(std::fs::read_to_string(&user_config_path)?, @r#"
878 #:schema https://docs.jj-vcs.dev/latest/config-schema.json
879
880 [test-table]
881 integer = 42
882 float = 3.14
883 array = ["one", "two"]
884 boolean = true
885 string = "foo"
886 invalid = "a + b"
887 "#);
888 Ok(())
889 }
890
891 #[test]
892 fn test_config_set_type_mismatch() {
893 let test_env = TestEnvironment::default();
894 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
895 let work_dir = test_env.work_dir("repo");
896
897 work_dir
898 .run_jj(["config", "set", "--user", "test-table.foo", "test-val"])
899 .success();
900 let output = work_dir.run_jj(["config", "set", "--user", "test-table", "not-a-table"]);
901 insta::assert_snapshot!(output, @"
902 ------- stderr -------
903 Error: Failed to set test-table
904 Caused by: Would overwrite entire table test-table
905 [EOF]
906 [exit status: 1]
907 ");
908
909 // But it's fine to overwrite arrays and inline tables
910 work_dir
911 .run_jj(["config", "set", "--user", "test-table.array", "[1,2,3]"])
912 .success();
913 work_dir
914 .run_jj(["config", "set", "--user", "test-table.array", "[4,5,6]"])
915 .success();
916 work_dir
917 .run_jj(["config", "set", "--user", "test-table.inline", "{ x = 42}"])
918 .success();
919 work_dir
920 .run_jj(["config", "set", "--user", "test-table.inline", "42"])
921 .success();
922 }
923
924 #[test]
925 fn test_config_set_nontable_parent() {
926 let test_env = TestEnvironment::default();
927 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
928 let work_dir = test_env.work_dir("repo");
929
930 work_dir
931 .run_jj(["config", "set", "--user", "test-nontable", "test-val"])
932 .success();
933 let output = work_dir.run_jj(["config", "set", "--user", "test-nontable.foo", "test-val"]);
934 insta::assert_snapshot!(output, @"
935 ------- stderr -------
936 Error: Failed to set test-nontable.foo
937 Caused by: Would overwrite non-table value with parent table test-nontable
938 [EOF]
939 [exit status: 1]
940 ");
941 }
942
943 #[test]
944 fn test_config_unset_non_existent_key() {
945 let test_env = TestEnvironment::default();
946 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
947 let work_dir = test_env.work_dir("repo");
948
949 let output = work_dir.run_jj(["config", "unset", "--user", "nonexistent"]);
950 insta::assert_snapshot!(output, @r#"
951 ------- stderr -------
952 Error: "nonexistent" doesn't exist
953 [EOF]
954 [exit status: 1]
955 "#);
956 }
957
958 #[test]
959 fn test_config_unset_inline_table_key() -> TestResult {
960 let mut test_env = TestEnvironment::default();
961 // Test with fresh new config file
962 let user_config_path = test_env.config_path().join("config.toml");
963 test_env.set_config_path(&user_config_path);
964 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
965 let work_dir = test_env.work_dir("repo");
966
967 work_dir
968 .run_jj(["config", "set", "--user", "inline-table", "{ foo = true }"])
969 .success();
970 work_dir
971 .run_jj(["config", "unset", "--user", "inline-table.foo"])
972 .success();
973 let user_config_toml = std::fs::read_to_string(&user_config_path)?;
974 insta::assert_snapshot!(user_config_toml, @"
975 #:schema https://docs.jj-vcs.dev/latest/config-schema.json
976
977 inline-table = {}
978 ");
979 Ok(())
980 }
981
982 #[test]
983 fn test_config_unset_table_like() -> TestResult {
984 let mut test_env = TestEnvironment::default();
985 // Test with fresh new config file
986 let user_config_path = test_env.config_path().join("config.toml");
987 test_env.set_config_path(&user_config_path);
988
989 std::fs::write(
990 &user_config_path,
991 indoc! {b"
992 inline-table = { foo = true }
993 [non-inline-table]
994 foo = true
995 "},
996 )?;
997
998 // Inline table is syntactically a "value", so it can be deleted.
999 test_env
1000 .run_jj_in(".", ["config", "unset", "--user", "inline-table"])
1001 .success();
1002 // Non-inline table cannot be deleted.
1003 let output = test_env.run_jj_in(".", ["config", "unset", "--user", "non-inline-table"]);
1004 insta::assert_snapshot!(output, @"
1005 ------- stderr -------
1006 Error: Failed to unset non-inline-table
1007 Caused by: Would delete entire table non-inline-table
1008 [EOF]
1009 [exit status: 1]
1010 ");
1011
1012 let user_config_toml = std::fs::read_to_string(&user_config_path)?;
1013 insta::assert_snapshot!(user_config_toml, @"
1014 [non-inline-table]
1015 foo = true
1016 ");
1017 Ok(())
1018 }
1019
1020 #[test]
1021 fn test_config_unset_for_user() -> TestResult {
1022 let mut test_env = TestEnvironment::default();
1023 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
1024 // Test with fresh new config file
1025 let user_config_path = test_env.config_path().join("config.toml");
1026 test_env.set_config_path(&user_config_path);
1027 let work_dir = test_env.work_dir("repo");
1028
1029 work_dir
1030 .run_jj(["config", "set", "--user", "foo", "true"])
1031 .success();
1032 work_dir
1033 .run_jj(["config", "unset", "--user", "foo"])
1034 .success();
1035
1036 work_dir
1037 .run_jj(["config", "set", "--user", "table.foo", "true"])
1038 .success();
1039 work_dir
1040 .run_jj(["config", "unset", "--user", "table.foo"])
1041 .success();
1042
1043 work_dir
1044 .run_jj(["config", "set", "--user", "table.inline", "{ foo = true }"])
1045 .success();
1046 work_dir
1047 .run_jj(["config", "unset", "--user", "table.inline"])
1048 .success();
1049
1050 let user_config_toml = std::fs::read_to_string(&user_config_path)?;
1051 insta::assert_snapshot!(user_config_toml, @"[table]");
1052 Ok(())
1053 }
1054
1055 #[test]
1056 fn test_config_unset_for_repo() {
1057 let test_env = TestEnvironment::default();
1058 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
1059 let work_dir = test_env.work_dir("repo");
1060
1061 work_dir
1062 .run_jj(["config", "set", "--repo", "test-key", "test-val"])
1063 .success();
1064 work_dir
1065 .run_jj(["config", "unset", "--repo", "test-key"])
1066 .success();
1067
1068 let repo_config_toml = &test_env
1069 .work_dir("home/.config/jj/repos/8e4fac809cbb3b162c95")
1070 .read_file("config.toml");
1071 insta::assert_snapshot!(repo_config_toml, @"");
1072 }
1073
1074 #[test]
1075 fn test_config_unset_for_workspace() {
1076 let test_env = TestEnvironment::default();
1077 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
1078 let work_dir = test_env.work_dir("repo");
1079 work_dir.run_jj(["new"]).success();
1080 work_dir
1081 .run_jj(["workspace", "add", "--name", "second", "../secondary"])
1082 .success();
1083 let work_dir = test_env.work_dir("secondary");
1084
1085 // set then unset
1086 work_dir
1087 .run_jj(["config", "set", "--workspace", "foo", "bar"])
1088 .success();
1089 work_dir
1090 .run_jj(["config", "unset", "--workspace", "foo"])
1091 .success();
1092
1093 let workspace_config = &test_env
1094 .work_dir("home/.config/jj/workspaces/0757f5ec8418b4f0983d")
1095 .read_file("config.toml");
1096 insta::assert_snapshot!(workspace_config, @"");
1097 }
1098
1099 #[test]
1100 fn test_config_unset_for_file() -> TestResult {
1101 let test_env = TestEnvironment::default();
1102 test_env.add_config("");
1103 let last_path = test_env.last_config_file_path();
1104
1105 test_env
1106 .run_jj_in(
1107 ".",
1108 [
1109 "config",
1110 "set",
1111 "--file",
1112 last_path.to_str().unwrap(),
1113 "foo",
1114 "true",
1115 ],
1116 )
1117 .success();
1118
1119 let output = test_env.run_jj_in(
1120 ".",
1121 [
1122 "config",
1123 "unset",
1124 "--file",
1125 last_path.to_str().unwrap(),
1126 "foo",
1127 ],
1128 );
1129 insta::assert_snapshot!(output, @"");
1130
1131 insta::assert_snapshot!(
1132 std::fs::read_to_string(last_path)?,
1133 @"");
1134
1135 Ok(())
1136 }
1137
1138 #[test]
1139 fn test_config_edit_missing_opt() {
1140 let test_env = TestEnvironment::default();
1141 let output = test_env.run_jj_in(".", ["config", "edit"]);
1142 insta::assert_snapshot!(output, @"
1143 ------- stderr -------
1144 error: the following required arguments were not provided:
1145 <--user|--repo|--workspace|--file <PATH>>
1146
1147 Usage: jj config edit <--user|--repo|--workspace|--file <PATH>>
1148
1149 For more information, try '--help'.
1150 [EOF]
1151 [exit status: 2]
1152 ");
1153 }
1154
1155 #[test]
1156 fn test_config_edit_user() -> TestResult {
1157 let mut test_env = TestEnvironment::default();
1158 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
1159 // Remove one of the config file to disambiguate
1160 std::fs::remove_file(test_env.last_config_file_path())?;
1161 let edit_script = test_env.set_up_fake_editor();
1162 let work_dir = test_env.work_dir("repo");
1163
1164 std::fs::write(edit_script, "dump-path path")?;
1165 work_dir.run_jj(["config", "edit", "--user"]).success();
1166
1167 let edited_path = PathBuf::from(std::fs::read_to_string(test_env.env_root().join("path"))?);
1168 assert_eq!(
1169 edited_path,
1170 dunce::simplified(&test_env.last_config_file_path())
1171 );
1172 Ok(())
1173 }
1174
1175 #[test]
1176 fn test_config_edit_user_new_file() {
1177 let mut test_env = TestEnvironment::default();
1178 let user_config_path = test_env.config_path().join("config").join("file.toml");
1179 test_env.set_up_fake_editor(); // set $EDIT_SCRIPT, but added configuration is ignored
1180 test_env.add_env_var("EDITOR", fake_editor_path());
1181 test_env.set_config_path(&user_config_path);
1182 assert!(!user_config_path.exists());
1183
1184 test_env
1185 .run_jj_in(".", ["config", "edit", "--user"])
1186 .success();
1187 assert!(
1188 user_config_path.exists(),
1189 "new file and directory should be created"
1190 );
1191 }
1192
1193 #[test]
1194 fn test_config_edit_file() -> TestResult {
1195 let mut test_env = TestEnvironment::default();
1196 test_env.add_config("");
1197 let last_path = test_env.last_config_file_path();
1198 let edit_script = test_env.set_up_fake_editor();
1199
1200 std::fs::write(edit_script, "dump-path path")?;
1201 test_env
1202 .run_jj_in(
1203 ".",
1204 ["config", "edit", "--file", last_path.to_str().unwrap()],
1205 )
1206 .success();
1207
1208 let edited_path = PathBuf::from(std::fs::read_to_string(test_env.env_root().join("path"))?);
1209 assert_eq!(edited_path, dunce::simplified(&last_path));
1210 Ok(())
1211 }
1212
1213 #[test]
1214 fn test_config_edit_repo() -> TestResult {
1215 let mut test_env = TestEnvironment::default();
1216 let edit_script = test_env.set_up_fake_editor();
1217 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
1218 let repo_config_dir = test_env.home_dir().join(".config/jj/repos");
1219 assert!(!repo_config_dir.is_dir());
1220
1221 std::fs::write(edit_script, "dump-path path")?;
1222 let work_dir = test_env.work_dir("repo");
1223 work_dir.run_jj(["config", "edit", "--repo"]).success();
1224
1225 let repo_config_path = test_env
1226 .work_dir("home/.config/jj/repos/8e4fac809cbb3b162c95")
1227 .root()
1228 .join("config.toml");
1229
1230 let edited_path = PathBuf::from(std::fs::read_to_string(test_env.env_root().join("path"))?);
1231 assert!(repo_config_dir.is_dir());
1232 assert_eq!(edited_path, dunce::simplified(&repo_config_path));
1233 assert!(repo_config_path.exists(), "new file should be created");
1234 Ok(())
1235 }
1236
1237 #[test]
1238 fn test_config_edit_invalid_config() -> TestResult {
1239 let mut test_env = TestEnvironment::default();
1240 let edit_script = test_env.set_up_fake_editor();
1241
1242 // Test re-edit
1243 std::fs::write(
1244 &edit_script,
1245 "write\ninvalid config here\0next invocation\n\0write\ntest=\"success\"",
1246 )?;
1247 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
1248 let work_dir = test_env.work_dir("repo");
1249 let output = work_dir.run_jj_with(|cmd| {
1250 force_interactive(cmd)
1251 .args(["config", "edit", "--repo"])
1252 .write_stdin("Y\n")
1253 });
1254 insta::assert_snapshot!(output, @"
1255 ------- stderr -------
1256 Editing file: $TEST_ENV/home/.config/jj/repos/8e4fac809cbb3b162c95/config.toml
1257 Warning: An error has been found inside the config:
1258 Caused by:
1259 1: Configuration cannot be parsed as TOML document
1260 2: TOML parse error at line 1, column 9
1261 |
1262 1 | invalid config here
1263 | ^
1264 key with no value, expected `=`
1265
1266 Do you want to keep editing the file? If not, previous config will be restored. (Yn): [EOF]
1267 ");
1268
1269 let output = work_dir.run_jj(["config", "get", "test"]);
1270 insta::assert_snapshot!(output, @"
1271 success
1272 [EOF]
1273 "
1274 );
1275
1276 // Test the restore previous config
1277 std::fs::write(&edit_script, "write\ninvalid config here")?;
1278 let work_dir = test_env.work_dir("repo");
1279 let output = work_dir.run_jj_with(|cmd| {
1280 force_interactive(cmd)
1281 .args(["config", "edit", "--repo"])
1282 .write_stdin("n\n")
1283 });
1284 insta::assert_snapshot!(output, @"
1285 ------- stderr -------
1286 Editing file: $TEST_ENV/home/.config/jj/repos/8e4fac809cbb3b162c95/config.toml
1287 Warning: An error has been found inside the config:
1288 Caused by:
1289 1: Configuration cannot be parsed as TOML document
1290 2: TOML parse error at line 1, column 9
1291 |
1292 1 | invalid config here
1293 | ^
1294 key with no value, expected `=`
1295
1296 Do you want to keep editing the file? If not, previous config will be restored. (Yn): [EOF]
1297 ");
1298
1299 let output = work_dir.run_jj(["config", "get", "test"]);
1300 insta::assert_snapshot!(output, @"
1301 success
1302 [EOF]
1303 "
1304 );
1305 Ok(())
1306 }
1307
1308 #[test]
1309 fn test_config_path() {
1310 let mut test_env = TestEnvironment::default();
1311 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
1312 let work_dir = test_env.work_dir("repo");
1313
1314 let user_config_path = test_env.env_root().join("config.toml");
1315 let repo_config_path = work_dir
1316 .root()
1317 .join(PathBuf::from_iter([".jj", "repo", "config.toml"]));
1318 let ws_config_path = work_dir
1319 .root()
1320 .join(PathBuf::from_iter([".jj", "workspace-config.toml"]));
1321 test_env.set_config_path(&user_config_path);
1322 let work_dir = test_env.work_dir("repo");
1323
1324 insta::assert_snapshot!(work_dir.run_jj(["config", "path", "--user"]), @"
1325 $TEST_ENV/config.toml
1326 [EOF]
1327 ");
1328 assert!(
1329 !user_config_path.exists(),
1330 "jj config path shouldn't create new file"
1331 );
1332
1333 insta::assert_snapshot!(work_dir.run_jj(["config", "path", "--repo"]), @"
1334 $TEST_ENV/home/.config/jj/repos/ffdaa62087a280bddc5e/config.toml
1335 [EOF]
1336 ");
1337 assert!(
1338 !repo_config_path.exists(),
1339 "jj config path shouldn't create new file"
1340 );
1341
1342 insta::assert_snapshot!(test_env.run_jj_in(".", ["config", "path", "--repo"]), @"
1343 ------- stderr -------
1344 Error: No repo config path found
1345 [EOF]
1346 [exit status: 1]
1347 ");
1348
1349 insta::assert_snapshot!(work_dir.run_jj(["config", "path", "--workspace"]), @"
1350 $TEST_ENV/home/.config/jj/workspaces/d043564ef93650b06a70/config.toml
1351 [EOF]
1352 ");
1353 assert!(
1354 !ws_config_path.exists(),
1355 "jj config path shouldn't create new file"
1356 );
1357
1358 insta::assert_snapshot!(test_env.run_jj_in(".", ["config", "path", "--workspace"]), @"
1359 ------- stderr -------
1360 Error: No workspace config path found
1361 [EOF]
1362 [exit status: 1]
1363 ");
1364 }
1365
1366 #[test]
1367 fn test_config_path_multiple() -> TestResult {
1368 let mut test_env = TestEnvironment::default();
1369 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
1370 let config_path = test_env.config_path().join("config.toml");
1371 let work_config_path = test_env.config_path().join("conf.d");
1372 let user_config_path = join_paths([config_path, work_config_path])?;
1373 test_env.set_config_path(&user_config_path);
1374 let work_dir = test_env.work_dir("repo");
1375 insta::assert_snapshot!(work_dir.run_jj(["config", "path", "--user"]), @"
1376 $TEST_ENV/config/config.toml
1377 $TEST_ENV/config/conf.d
1378 [EOF]
1379 ");
1380 Ok(())
1381 }
1382
1383 #[test]
1384 fn test_config_only_loads_toml_files() -> TestResult {
1385 let mut test_env = TestEnvironment::default();
1386 test_env.set_up_fake_editor();
1387 std::fs::File::create(test_env.config_path().join("is-not.loaded"))?;
1388 insta::assert_snapshot!(test_env.run_jj_in(".", ["config", "edit", "--user"]), @"
1389 ------- stderr -------
1390 Editing file: $TEST_ENV/config/config0001.toml
1391 [EOF]
1392 ");
1393 Ok(())
1394 }
1395
1396 #[test]
1397 fn test_config_edit_repo_outside_repo() {
1398 let test_env = TestEnvironment::default();
1399 let output = test_env.run_jj_in(".", ["config", "edit", "--repo"]);
1400 insta::assert_snapshot!(output, @"
1401 ------- stderr -------
1402 Error: No repo config path found to edit
1403 [EOF]
1404 [exit status: 1]
1405 ");
1406 }
1407
1408 #[test]
1409 fn test_config_get() {
1410 let test_env = TestEnvironment::default();
1411 test_env.add_config(
1412 r#"
1413 [table]
1414 string = "some value 1"
1415 int = 123
1416 list = ["list", "value"]
1417 overridden = "foo"
1418 "#,
1419 );
1420 test_env.add_config(
1421 r#"
1422 [table]
1423 overridden = "bar"
1424 "#,
1425 );
1426
1427 let output = test_env.run_jj_in(".", ["config", "get", "nonexistent"]);
1428 insta::assert_snapshot!(output, @"
1429 ------- stderr -------
1430 Config error: Value not found for nonexistent
1431 For help, see https://docs.jj-vcs.dev/latest/config/ or use `jj help -k config`.
1432 [EOF]
1433 [exit status: 1]
1434 ");
1435
1436 let output = test_env.run_jj_in(".", ["config", "get", "table.string"]);
1437 insta::assert_snapshot!(output, @"
1438 some value 1
1439 [EOF]
1440 ");
1441
1442 let output = test_env.run_jj_in(".", ["config", "get", "table.int"]);
1443 insta::assert_snapshot!(output, @"
1444 123
1445 [EOF]
1446 ");
1447
1448 let output = test_env.run_jj_in(".", ["config", "get", "table.list"]);
1449 insta::assert_snapshot!(output, @r#"
1450 ["list", "value"]
1451 [EOF]
1452 "#);
1453
1454 let output = test_env.run_jj_in(".", ["config", "get", "table"]);
1455 insta::assert_snapshot!(output, @r#"
1456 { string = "some value 1", int = 123, list = ["list", "value"], overridden = "bar" }
1457 [EOF]
1458 "#);
1459
1460 let output = test_env.run_jj_in(".", ["config", "get", "table.overridden"]);
1461 insta::assert_snapshot!(output, @"
1462 bar
1463 [EOF]
1464 ");
1465 }
1466
1467 #[test]
1468 fn test_config_get_yields_values_consistent_with_schema_defaults() -> TestResult {
1469 let mut test_env = TestEnvironment::default();
1470
1471 // The default test environment may already contain configuration that's
1472 // different from the true default, e.g. `git.colocate = false`. So we
1473 // explicitly set the config to an empty one in order to test the true
1474 // default config values.
1475 let config_dir = test_env.env_root().join("empty-config");
1476 std::fs::create_dir(&config_dir)?;
1477 test_env.set_config_path(&config_dir);
1478
1479 let get_true_default = move |key: &str| {
1480 let output = test_env.run_jj_in(".", ["config", "get", key]).success();
1481 let output_doc = toml_edit::Document::parse(format!("test={}", output.stdout.normalized()))
1482 .unwrap_or_else(|_| {
1483 // Unfortunately for this test, `config get` is "lossy" and does not print
1484 // quoted strings. This means that e.g. `false` and `"false"` are not
1485 // distinguishable. If value couldn't be parsed, it's probably a string, so
1486 // let's parse its Debug string instead.
1487 toml_edit::Document::parse(format!("test={:?}", output.stdout.normalized().trim()))
1488 .unwrap()
1489 });
1490 output_doc.get("test").unwrap().as_value().unwrap().clone()
1491 };
1492
1493 let mut schema_defaults = toml_edit::ser::to_document(&default_config_from_schema())?;
1494
1495 // Ensure that `get_values()` flattens the entire configuration.
1496 struct SetDotted;
1497 impl toml_edit::visit_mut::VisitMut for SetDotted {
1498 fn visit_table_like_mut(&mut self, node: &mut dyn toml_edit::TableLike) {
1499 node.set_dotted(true);
1500 toml_edit::visit_mut::visit_table_like_mut(self, node);
1501 }
1502 }
1503 toml_edit::visit_mut::visit_document_mut(&mut SetDotted, &mut schema_defaults);
1504
1505 for (key, schema_default) in schema_defaults.into_table().get_values() {
1506 let key = key.iter().join(".");
1507 match key.as_str() {
1508 // These keys technically don't have a default value, but they exhibit a default
1509 // behavior consistent with the value claimed by the schema. When these defaults are
1510 // used, a hint is printed to stdout.
1511 "ui.default-command" => insta::assert_snapshot!(schema_default, @r#""log""#),
1512 "ui.diff-editor" => insta::assert_snapshot!(schema_default, @r#"":builtin""#),
1513 "ui.merge-editor" => insta::assert_snapshot!(schema_default, @r#"":builtin""#),
1514 "git.fetch" => insta::assert_snapshot!(schema_default, @r#""origin""#),
1515 "git.push" => insta::assert_snapshot!(schema_default, @r#""origin""#),
1516
1517 // When no `short-prefixes` revset is explicitly configured, the revset for `log` is
1518 // used instead, even if that has a value different from the default. The schema
1519 // represents this behavior with a symbolic default value.
1520 "revsets.short-prefixes" => {
1521 insta::assert_snapshot!(schema_default, @r#""<revsets.log>""#);
1522 }
1523
1524 // The default for `ui.pager` is a table; `ui.pager.command` is an array and `jj config
1525 // get` currently cannot print that. The schema default omits the env variable
1526 // `LESSCHARSET` and gives the default as a plain string.
1527 "ui.pager" => insta::assert_snapshot!(schema_default, @r#""less -FRXK""#),
1528
1529 // The `immutable_heads()` revset actually defaults to `builtin_immutable_heads()` but
1530 // this would be a poor starting point for a custom revset, so the schema "inlines"
1531 // `builtin_immutable_heads()`.
1532 r#"revset-aliases."immutable_heads()""# => {
1533 let builtin_default =
1534 get_true_default("revset-aliases.'builtin_immutable_heads()'");
1535 assert!(
1536 builtin_default.to_string() == schema_default.to_string(),
1537 "{key}: the schema claims a default ({schema_default}) which is different \
1538 from what builtin_immutable_heads() resolves to ({builtin_default})"
1539 );
1540 }
1541
1542 _ => {
1543 let true_default = get_true_default(&key);
1544 assert!(
1545 true_default.to_string() == schema_default.to_string(),
1546 "{key}: true default value ({true_default}) is not consistent with default \
1547 claimed by schema ({schema_default})"
1548 );
1549 }
1550 }
1551 }
1552 Ok(())
1553 }
1554
1555 #[test]
1556 fn test_config_path_syntax() {
1557 let test_env = TestEnvironment::default();
1558 test_env.add_config(
1559 r#"
1560 a.'b()' = 0
1561 'b c'.d = 1
1562 'b c'.e.'f[]' = 2
1563 - = 3
1564 _ = 4
1565 '.' = 5
1566 "#,
1567 );
1568
1569 let output = test_env.run_jj_in(".", ["config", "list", "a.'b()'"]);
1570 insta::assert_snapshot!(output, @"
1571 a.'b()' = 0
1572 [EOF]
1573 ");
1574 let output = test_env.run_jj_in(".", ["config", "list", "'b c'"]);
1575 insta::assert_snapshot!(output, @r#"
1576 'b c'.d = 1
1577 'b c'.e."f[]" = 2
1578 [EOF]
1579 "#);
1580 let output = test_env.run_jj_in(".", ["config", "list", "'b c'.d"]);
1581 insta::assert_snapshot!(output, @"
1582 'b c'.d = 1
1583 [EOF]
1584 ");
1585 let output = test_env.run_jj_in(".", ["config", "list", "'b c'.e.'f[]'"]);
1586 insta::assert_snapshot!(output, @"
1587 'b c'.e.'f[]' = 2
1588 [EOF]
1589 ");
1590 let output = test_env.run_jj_in(".", ["config", "get", "'b c'.e.'f[]'"]);
1591 insta::assert_snapshot!(output, @"
1592 2
1593 [EOF]
1594 ");
1595
1596 // Not a table
1597 let output = test_env.run_jj_in(".", ["config", "list", "a.'b()'.x"]);
1598 insta::assert_snapshot!(output, @r"
1599 ------- stderr -------
1600 Warning: No matching config key for: a.'b()'.x
1601 [EOF]
1602 ");
1603 let output = test_env.run_jj_in(".", ["config", "get", "a.'b()'.x"]);
1604 insta::assert_snapshot!(output, @"
1605 ------- stderr -------
1606 Config error: Value not found for a.'b()'.x
1607 For help, see https://docs.jj-vcs.dev/latest/config/ or use `jj help -k config`.
1608 [EOF]
1609 [exit status: 1]
1610 ");
1611
1612 // "-" and "_" are valid TOML keys
1613 let output = test_env.run_jj_in(".", ["config", "list", "-"]);
1614 insta::assert_snapshot!(output, @"
1615 - = 3
1616 [EOF]
1617 ");
1618 let output = test_env.run_jj_in(".", ["config", "list", "_"]);
1619 insta::assert_snapshot!(output, @"
1620 _ = 4
1621 [EOF]
1622 ");
1623
1624 // "." requires quoting
1625 let output = test_env.run_jj_in(".", ["config", "list", "'.'"]);
1626 insta::assert_snapshot!(output, @"
1627 '.' = 5
1628 [EOF]
1629 ");
1630 let output = test_env.run_jj_in(".", ["config", "get", "'.'"]);
1631 insta::assert_snapshot!(output, @"
1632 5
1633 [EOF]
1634 ");
1635 let output = test_env.run_jj_in(".", ["config", "get", "."]);
1636 insta::assert_snapshot!(output, @"
1637 ------- stderr -------
1638 error: invalid value '.' for '<NAME>': TOML parse error at line 1, column 1
1639 |
1640 1 | .
1641 | ^
1642 unquoted keys cannot be empty, expected letters, numbers, `-`, `_`
1643
1644
1645 For more information, try '--help'.
1646 [EOF]
1647 [exit status: 2]
1648 ");
1649
1650 // Invalid TOML keys
1651 let output = test_env.run_jj_in(".", ["config", "list", "b c"]);
1652 insta::assert_snapshot!(output, @"
1653 ------- stderr -------
1654 error: invalid value 'b c' for '[NAME]': TOML parse error at line 1, column 3
1655 |
1656 1 | b c
1657 | ^
1658 unexpected content, expected nothing
1659
1660
1661 For more information, try '--help'.
1662 [EOF]
1663 [exit status: 2]
1664 ");
1665 let output = test_env.run_jj_in(".", ["config", "list", ""]);
1666 insta::assert_snapshot!(output, @"
1667 ------- stderr -------
1668 error: invalid value '' for '[NAME]': TOML parse error at line 1, column 1
1669 |
1670 1 |
1671 | ^
1672 unquoted keys cannot be empty, expected letters, numbers, `-`, `_`
1673
1674
1675 For more information, try '--help'.
1676 [EOF]
1677 [exit status: 2]
1678 ");
1679 }
1680
1681 #[test]
1682 #[cfg_attr(windows, ignore = "dirs::home_dir() can't be overridden by $HOME")] // TODO
1683 fn test_config_conditional() -> TestResult {
1684 let mut test_env = TestEnvironment::default();
1685 let home_dir = test_env.work_dir(test_env.home_dir());
1686 home_dir.run_jj(["git", "init", "repo1"]).success();
1687 home_dir.run_jj(["git", "init", "repo2"]).success();
1688 // Test with fresh new config file
1689 let user_config_path = test_env.env_root().join("config.toml");
1690 test_env.set_config_path(&user_config_path);
1691 std::fs::write(
1692 &user_config_path,
1693 indoc! {"
1694 foo = 'global'
1695 baz = 'global'
1696 qux = 'global'
1697
1698 [[--scope]]
1699 --when.repositories = ['~/repo1']
1700 foo = 'repo1'
1701 [[--scope]]
1702 --when.repositories = ['~/repo2']
1703 foo = 'repo2'
1704 [[--scope]]
1705 --when.workspaces = ['~/repo2']
1706 foo2 = 'repo2'
1707 [[--scope]]
1708 --when.workspaces = ['~/repo2_1']
1709 foo2 = 'repo2_1'
1710
1711 [[--scope]]
1712 --when.commands = ['config']
1713 baz = 'config'
1714 [[--scope]]
1715 --when.commands = ['config get']
1716 qux = 'get'
1717 [[--scope]]
1718 --when.commands = ['config list']
1719 qux = 'list'
1720 "},
1721 )?;
1722 let home_dir = test_env.work_dir(test_env.home_dir());
1723 let work_dir1 = home_dir.dir("repo1");
1724 let work_dir2 = home_dir.dir("repo2");
1725 let work_dir2_1 = home_dir.dir("repo2_1");
1726 work_dir2
1727 .run_jj(&["workspace", "add", "../repo2_1"])
1728 .success();
1729
1730 // get and list should refer to the resolved config
1731 let output = test_env.run_jj_in(".", ["config", "get", "foo"]);
1732 insta::assert_snapshot!(output, @"
1733 global
1734 [EOF]
1735 ");
1736 let output = work_dir1.run_jj(["config", "get", "foo"]);
1737 insta::assert_snapshot!(output, @"
1738 repo1
1739 [EOF]
1740 ");
1741 // baz should be the same for `jj config get` and `jj config list`
1742 // qux should be different
1743 let output = work_dir1.run_jj(["config", "get", "baz"]);
1744 insta::assert_snapshot!(output, @"
1745 config
1746 [EOF]
1747 ");
1748 let output = work_dir1.run_jj(["config", "get", "qux"]);
1749 insta::assert_snapshot!(output, @"
1750 get
1751 [EOF]
1752 ");
1753 let output = test_env.run_jj_in(".", ["config", "list", "--user"]);
1754 insta::assert_snapshot!(output, @"
1755 foo = 'global'
1756 baz = 'config'
1757 qux = 'list'
1758 [EOF]
1759 ");
1760 let output = work_dir1.run_jj(["config", "list", "--user"]);
1761 insta::assert_snapshot!(output, @"
1762 foo = 'repo1'
1763 baz = 'config'
1764 qux = 'list'
1765 [EOF]
1766 ");
1767 let output = work_dir2.run_jj(["config", "list", "--user"]);
1768 insta::assert_snapshot!(output, @"
1769 foo = 'repo2'
1770 foo2 = 'repo2'
1771 baz = 'config'
1772 qux = 'list'
1773 [EOF]
1774 ");
1775 let output = work_dir2_1.run_jj(["config", "list", "--user"]);
1776 insta::assert_snapshot!(output, @"
1777 foo = 'repo2'
1778 foo2 = 'repo2_1'
1779 baz = 'config'
1780 qux = 'list'
1781 [EOF]
1782 ");
1783
1784 // relative workspace path
1785 let output = work_dir2.run_jj(["config", "list", "--user", "-R../repo1"]);
1786 insta::assert_snapshot!(output, @"
1787 foo = 'repo1'
1788 baz = 'config'
1789 qux = 'list'
1790 [EOF]
1791 ");
1792
1793 // set and unset should refer to the source config
1794 // (there's no option to update scoped table right now.)
1795 let output = test_env.run_jj_in(".", ["config", "set", "--user", "bar", "new value"]);
1796 insta::assert_snapshot!(output, @"");
1797 let config_contents = std::fs::read_to_string(&user_config_path)?;
1798 insta::assert_snapshot!(config_contents, @r#"
1799 foo = 'global'
1800 baz = 'global'
1801 qux = 'global'
1802 bar = "new value"
1803
1804 [[--scope]]
1805 --when.repositories = ['~/repo1']
1806 foo = 'repo1'
1807 [[--scope]]
1808 --when.repositories = ['~/repo2']
1809 foo = 'repo2'
1810 [[--scope]]
1811 --when.workspaces = ['~/repo2']
1812 foo2 = 'repo2'
1813 [[--scope]]
1814 --when.workspaces = ['~/repo2_1']
1815 foo2 = 'repo2_1'
1816
1817 [[--scope]]
1818 --when.commands = ['config']
1819 baz = 'config'
1820 [[--scope]]
1821 --when.commands = ['config get']
1822 qux = 'get'
1823 [[--scope]]
1824 --when.commands = ['config list']
1825 qux = 'list'
1826 "#);
1827 let output = work_dir1.run_jj(["config", "unset", "--user", "foo"]);
1828 insta::assert_snapshot!(output, @"");
1829 let config_contents = std::fs::read_to_string(&user_config_path)?;
1830 insta::assert_snapshot!(config_contents, @r#"
1831 baz = 'global'
1832 qux = 'global'
1833 bar = "new value"
1834
1835 [[--scope]]
1836 --when.repositories = ['~/repo1']
1837 foo = 'repo1'
1838 [[--scope]]
1839 --when.repositories = ['~/repo2']
1840 foo = 'repo2'
1841 [[--scope]]
1842 --when.workspaces = ['~/repo2']
1843 foo2 = 'repo2'
1844 [[--scope]]
1845 --when.workspaces = ['~/repo2_1']
1846 foo2 = 'repo2_1'
1847
1848 [[--scope]]
1849 --when.commands = ['config']
1850 baz = 'config'
1851 [[--scope]]
1852 --when.commands = ['config get']
1853 qux = 'get'
1854 [[--scope]]
1855 --when.commands = ['config list']
1856 qux = 'list'
1857 "#);
1858 Ok(())
1859 }
1860
1861 // Minimal test for Windows where the home directory can't be switched.
1862 // (Can be removed if test_config_conditional() is enabled on Windows.)
1863 #[test]
1864 fn test_config_conditional_without_home_dir() -> TestResult {
1865 let mut test_env = TestEnvironment::default();
1866 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
1867 // Test with fresh new config file
1868 let user_config_path = test_env.env_root().join("config.toml");
1869 test_env.set_config_path(&user_config_path);
1870 let work_dir = test_env.work_dir("repo");
1871 std::fs::write(
1872 &user_config_path,
1873 format!(
1874 indoc! {"
1875 foo = 'global'
1876 [[--scope]]
1877 --when.repositories = [{repo_path}]
1878 foo = 'repo'
1879 "},
1880 // "\\?\" paths shouldn't be required on Windows
1881 repo_path = to_toml_value(dunce::simplified(work_dir.root()).to_str().unwrap())
1882 ),
1883 )?;
1884
1885 let output = test_env.run_jj_in(".", ["config", "get", "foo"]);
1886 insta::assert_snapshot!(output, @"
1887 global
1888 [EOF]
1889 ");
1890 let output = work_dir.run_jj(["config", "get", "foo"]);
1891 insta::assert_snapshot!(output, @"
1892 repo
1893 [EOF]
1894 ");
1895 Ok(())
1896 }
1897
1898 #[test]
1899 fn test_config_show_paths() {
1900 let test_env = TestEnvironment::default();
1901 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
1902 let work_dir = test_env.work_dir("repo");
1903
1904 work_dir
1905 .run_jj(["config", "set", "--user", "ui.paginate", ":builtin"])
1906 .success();
1907 let output = test_env.run_jj_in(".", ["st"]);
1908 insta::assert_snapshot!(output, @"
1909 ------- stderr -------
1910 Config error: Invalid type or value for ui.paginate
1911 Caused by: unknown variant `:builtin`, expected `never` or `auto`
1912
1913 Hint: Check the config file: $TEST_ENV/config/config0001.toml
1914 For help, see https://docs.jj-vcs.dev/latest/config/ or use `jj help -k config`.
1915 [EOF]
1916 [exit status: 1]
1917 ");
1918 }
1919
1920 #[test]
1921 fn test_config_author_change_warning() {
1922 let test_env = TestEnvironment::default();
1923 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
1924 let work_dir = test_env.work_dir("repo");
1925 let output = work_dir.run_jj(["config", "set", "--repo", "user.email", "'Foo'"]);
1926 insta::assert_snapshot!(output, @r#"
1927 ------- stderr -------
1928 Warning: This setting will only impact future commits.
1929 The author of the working copy will stay "Test User <[email protected]>".
1930 To change the working copy author, use "jj metaedit --update-author".
1931 [EOF]
1932 "#);
1933
1934 // test_env.run_jj*() resets state for every invocation
1935 // for this test, the state (user.email) is needed
1936 work_dir
1937 .run_jj_with(|cmd| {
1938 cmd.args(["metaedit", "--update-author"])
1939 .env_remove("JJ_EMAIL")
1940 })
1941 .success();
1942
1943 let output = work_dir.run_jj(["show"]);
1944 insta::assert_snapshot!(output, @"
1945 Commit ID: c2090b51d7ecd861e83a677fdf5e9d855efd14fa
1946 Change ID: qpvuntsmwlqtpsluzzsnyyzlmlwvmlnu
1947 Author : Test User <Foo> (2001-02-03 08:05:07)
1948 Committer: Test User <Foo> (2001-02-03 08:05:09)
1949
1950 (no description set)
1951
1952 [EOF]
1953 ");
1954
1955 let output = work_dir.run_jj(["config", "set", "--repo", "user.name", "'Bar'"]);
1956 insta::assert_snapshot!(output, @r#"
1957 ------- stderr -------
1958 Warning: This setting will only impact future commits.
1959 The author of the working copy will stay "Test User <Foo>".
1960 To change the working copy author, use "jj metaedit --update-author".
1961 [EOF]
1962 "#);
1963
1964 // test_env.run_jj*() resets state for every invocation
1965 // for this test, both user.name and user.email are needed
1966 work_dir
1967 .run_jj_with(|cmd| {
1968 cmd.args(["metaedit", "--update-author"])
1969 .env_remove("JJ_EMAIL")
1970 .env_remove("JJ_USER")
1971 })
1972 .success();
1973
1974 let output = work_dir.run_jj(["show"]);
1975 insta::assert_snapshot!(output, @"
1976 Commit ID: cab1afa907c193ca2db14f6bc971fde030e3970a
1977 Change ID: qpvuntsmwlqtpsluzzsnyyzlmlwvmlnu
1978 Author : Bar <Foo> (2001-02-03 08:05:07)
1979 Committer: Bar <Foo> (2001-02-03 08:05:12)
1980
1981 (no description set)
1982
1983 [EOF]
1984 ");
1985
1986 // no warnings if no change
1987 let output = work_dir.run_jj(["config", "set", "--repo", "user.name", "'Bar'"]);
1988 insta::assert_snapshot!(output, @"");
1989 }
1990
1991 #[test]
1992 fn test_config_author_change_warning_root_env() {
1993 let test_env = TestEnvironment::default();
1994 let output = test_env.run_jj_in(".", ["config", "set", "--user", "user.email", "'Foo'"]);
1995 insta::assert_snapshot!(output, @"");
1996 }
1997
1998 fn find_stdout_lines(keyname_pattern: &str, stdout: &str) -> String {
1999 let key_line_re = Regex::new(&format!(r"(?m)^{keyname_pattern} = .*\n")).unwrap();
2000 key_line_re.find_iter(stdout).map(|m| m.as_str()).collect()
2001 }
2002
2003 /// Set up a per-repo config directory by running `config set --repo`. Returns
2004 /// the path to the per-repo config directory
2005 /// (e.g. `~/.config/jj/repos/<config_id>/`).
2006 fn create_repo_with_config(test_env: &mut TestEnvironment, repo_name: &str) -> TestResult<PathBuf> {
2007 test_env
2008 .run_jj_in(".", ["git", "init", repo_name])
2009 .success();
2010 let work_dir = test_env.work_dir(repo_name);
2011 work_dir
2012 .run_jj(["config", "set", "--repo", "user.name", "test"])
2013 .success();
2014 let output = work_dir.run_jj(["config", "path", "--repo"]).success();
2015 let config_file = Path::new(output.stdout.raw().trim_end_matches('\n'));
2016 Ok(config_file.parent().unwrap().to_path_buf())
2017 }
2018
2019 #[test]
2020 fn test_config_gc_no_repos_dir() {
2021 let test_env = TestEnvironment::default();
2022 // No repo config dir created at all.
2023 let output = test_env.run_jj_in(".", ["config", "gc"]);
2024 insta::assert_snapshot!(output, @"
2025 ------- stderr -------
2026 Missing repo configs (repo path no longer exists):
2027 (none)
2028 [EOF]
2029 ");
2030 }
2031
2032 #[test]
2033 fn test_config_gc_all_existing() -> TestResult {
2034 let mut test_env = TestEnvironment::default();
2035 create_repo_with_config(&mut test_env, "repo")?;
2036
2037 let output = test_env.run_jj_in(".", ["config", "gc"]);
2038 insta::assert_snapshot!(output, @"
2039 ------- stderr -------
2040 Missing repo configs (repo path no longer exists):
2041 (none)
2042 [EOF]
2043 ");
2044 Ok(())
2045 }
2046
2047 #[test]
2048 fn test_config_gc_missing_default_no() -> TestResult {
2049 let mut test_env = TestEnvironment::default();
2050 let config_dir = create_repo_with_config(&mut test_env, "repo")?;
2051 // Remove the repo so its metadata path no longer exists.
2052 std::fs::remove_dir_all(test_env.env_root().join("repo"))?;
2053
2054 // Non-interactive: the prompt auto-answers with the default ("no").
2055 let output = test_env.run_jj_in(".", ["config", "gc"]);
2056 insta::assert_snapshot!(output, @"
2057 ------- stderr -------
2058 Missing repo configs (repo path no longer exists):
2059 $TEST_ENV/home/.config/jj/repos/8e4fac809cbb3b162c95
2060 repo path: $TEST_ENV/repo/.jj/repo
2061 Delete 1 missing repo config directories? (yN): n
2062 Aborted; nothing was deleted.
2063 [EOF]
2064 ");
2065 // The directory should still be there.
2066 assert!(config_dir.is_dir());
2067 Ok(())
2068 }
2069
2070 #[test]
2071 fn test_config_gc_missing_confirmed() -> TestResult {
2072 let mut test_env = TestEnvironment::default();
2073 let config_dir = create_repo_with_config(&mut test_env, "repo")?;
2074 std::fs::remove_dir_all(test_env.env_root().join("repo"))?;
2075
2076 let output = test_env.work_dir("").run_jj_with(|cmd| {
2077 force_interactive(cmd)
2078 .args(["config", "gc"])
2079 .write_stdin("y\n")
2080 });
2081 insta::assert_snapshot!(output, @"
2082 ------- stderr -------
2083 Missing repo configs (repo path no longer exists):
2084 $TEST_ENV/home/.config/jj/repos/8e4fac809cbb3b162c95
2085 repo path: $TEST_ENV/repo/.jj/repo
2086 Delete 1 missing repo config directories? (yN): Deleted 1 config directories.
2087 [EOF]
2088 ");
2089 assert!(!config_dir.exists());
2090 Ok(())
2091 }
2092
2093 #[test]
2094 fn test_config_gc_missing_with_extra_file() -> TestResult {
2095 let mut test_env = TestEnvironment::default();
2096 let config_dir = create_repo_with_config(&mut test_env, "repo")?;
2097 std::fs::remove_dir_all(test_env.env_root().join("repo"))?;
2098 // An unrelated file in the per-repo config directory should prevent us
2099 // from deleting it: we only remove the known jj-managed files and then
2100 // try to rmdir the (hopefully empty) directory.
2101 let extra_file = config_dir.join("unexpected.txt");
2102 std::fs::write(&extra_file, b"hello")?;
2103
2104 let output = test_env.work_dir("").run_jj_with(|cmd| {
2105 force_interactive(cmd)
2106 .args(["config", "gc"])
2107 .write_stdin("y\n")
2108 });
2109 insta::assert_snapshot!(output.normalize_stderr_with(|s| {
2110 // The OS-specific "directory not empty" message varies, so strip the
2111 // trailing detail after the path for a stable snapshot.
2112 regex::Regex::new(r"(?m)(Failed to delete \S+):.*$")
2113 .unwrap()
2114 .replace_all(&s, "$1: <directory not empty>")
2115 .into_owned()
2116 }), @"
2117 ------- stderr -------
2118 Missing repo configs (repo path no longer exists):
2119 $TEST_ENV/home/.config/jj/repos/8e4fac809cbb3b162c95
2120 repo path: $TEST_ENV/repo/.jj/repo
2121 Delete 1 missing repo config directories? (yN): Warning: Failed to delete $TEST_ENV/home/.config/jj/repos/8e4fac809cbb3b162c95: <directory not empty>
2122 Deleted 0 config directories.
2123 [EOF]
2124 ");
2125 // The directory must still be there (with the unrelated file intact).
2126 assert!(config_dir.is_dir());
2127 assert!(extra_file.is_file());
2128 // The known jj-managed files should be gone.
2129 assert!(!config_dir.join("config.toml").exists());
2130 assert!(!config_dir.join("metadata.binpb").exists());
2131 Ok(())
2132 }
2133
2134 #[test]
2135 fn test_config_file_validation() -> TestResult {
2136 let test_env = TestEnvironment::default();
2137 test_env.run_jj_in(".", ["git", "init", "repo"]).success();
2138 let work_dir = test_env.work_dir("repo");
2139
2140 // Rejects random non-jj paths
2141 let output = work_dir.run_jj(["config", "set", "--file", "abc/random/path.toml", "k", "v"]);
2142 insta::assert_snapshot!(output, @r"
2143 ------- stderr -------
2144 Error: Configuration file '$TEST_ENV/repo/abc/random/path.toml' is not a valid jj configuration file location
2145 Hint: Valid config locations include user configs (`~/.config/jj/config.toml` or `conf.d/*.toml`), repo/workspace configs, or files loaded with the global flag `--config-file <PATH>`.
2146 [EOF]
2147 [exit status: 1]
2148 ");
2149
2150 // Rejects typos like .config/jj/config.toml relative to cwd
2151 let output = work_dir.run_jj([
2152 "config",
2153 "set",
2154 "--file",
2155 ".config/jj/config.toml",
2156 "k",
2157 "v",
2158 ]);
2159 insta::assert_snapshot!(output, @r"
2160 ------- stderr -------
2161 Error: Configuration file '$TEST_ENV/repo/.config/jj/config.toml' is not a valid jj configuration file location
2162 Hint: Valid config locations include user configs (`~/.config/jj/config.toml` or `conf.d/*.toml`), repo/workspace configs, or files loaded with the global flag `--config-file <PATH>`.
2163 [EOF]
2164 [exit status: 1]
2165 ");
2166
2167 // Rejects non-toml files even under a valid config directory
2168 let invalid_ext_file = test_env.config_path().join("notes.txt");
2169 let output = work_dir.run_jj([
2170 "config",
2171 "set",
2172 "--file",
2173 invalid_ext_file.to_str().unwrap(),
2174 "k",
2175 "v",
2176 ]);
2177 insta::assert_snapshot!(output, @r"
2178 ------- stderr -------
2179 Error: Configuration file '$TEST_ENV/config/notes.txt' is not a valid jj configuration file location
2180 Hint: Valid config locations include user configs (`~/.config/jj/config.toml` or `conf.d/*.toml`), repo/workspace configs, or files loaded with the global flag `--config-file <PATH>`.
2181 [EOF]
2182 [exit status: 1]
2183 ");
2184
2185 // Allows creating a new .toml file under a valid config directory
2186 let valid_file = test_env.config_path().join("custom.toml");
2187 let output = work_dir.run_jj([
2188 "config",
2189 "set",
2190 "--file",
2191 valid_file.to_str().unwrap(),
2192 "custom-key",
2193 "custom-val",
2194 ]);
2195 insta::assert_snapshot!(output, @"");
2196 assert!(valid_file.exists());
2197
2198 // Allows targeting a valid config file via relative path
2199 let output = work_dir.run_jj([
2200 "config",
2201 "set",
2202 "--file",
2203 "../config/relative.toml",
2204 "rel-key",
2205 "rel-val",
2206 ]);
2207 insta::assert_snapshot!(output, @"");
2208 insta::assert_snapshot!(
2209 std::fs::read_to_string(test_env.config_path().join("relative.toml"))?,
2210 @r#"
2211 #:schema https://docs.jj-vcs.dev/latest/config-schema.json
2212
2213 rel-key = "rel-val"
2214 "#
2215 );
2216
2217 // Files loaded via the global --config-file option are also recognized as
2218 // loaded layers and can be targeted with --file. This behavior can be
2219 // removed if necessary.
2220 let custom_file = test_env.env_root().join("outside.toml");
2221 std::fs::write(&custom_file, "")?;
2222 let output = work_dir.run_jj([
2223 "--config-file",
2224 custom_file.to_str().unwrap(),
2225 "config",
2226 "set",
2227 "--file",
2228 custom_file.to_str().unwrap(),
2229 "outside-key",
2230 "outside-val",
2231 ]);
2232 insta::assert_snapshot!(output, @"");
2233 assert_eq!(
2234 std::fs::read_to_string(&custom_file)?,
2235 "outside-key = \"outside-val\"\n"
2236 );
2237
2238 Ok(())
2239 }