// Copyright 2022 The Jujutsu Authors//// Licensed under the Apache License, Version 2.0 (the "License");// you may not use this file except in compliance with the License.// You may obtain a copy of the License at//// https://www.apache.org/licenses/LICENSE-2.0//// Unless required by applicable law or agreed to in writing, software// distributed under the License is distributed on an "AS IS" BASIS,// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.// See the License for the specific language governing permissions and// limitations under the License. use std::env::join_paths;use std::path::PathBuf; use indoc::indoc;use itertools::Itertools as _;use regex::Regex; use crate::common::TestEnvironment;use crate::common::default_config_from_schema;use crate::common::fake_editor_path;use crate::common::force_interactive;use crate::common::to_toml_value; #[test]fn test_config_list_single() { let test_env = TestEnvironment::default(); test_env.add_config( r#" [test-table] somekey = "some value" "#, ); let output = test_env.run_jj_in(".", ["config", "list", "test-table.somekey"]); insta::assert_snapshot!(output, @r#" test-table.somekey = "some value" [EOF] "#); let output = test_env.run_jj_in( ".", ["config", "list", r#"-Tname ++ "\n""#, "test-table.somekey"], ); insta::assert_snapshot!(output, @r" test-table.somekey [EOF] ");} #[test]fn test_config_list_nonexistent() { let test_env = TestEnvironment::default(); let output = test_env.run_jj_in(".", ["config", "list", "nonexistent-test-key"]); insta::assert_snapshot!(output, @r" ------- stderr ------- Warning: No matching config key for nonexistent-test-key [EOF] ");} #[test]fn test_config_list_table() { let test_env = TestEnvironment::default(); test_env.add_config( r#" [test-table] x = true y.foo = "abc" y.bar = 123 "z"."with space"."function()" = 5 "#, ); let output = test_env.run_jj_in(".", ["config", "list", "test-table"]); insta::assert_snapshot!(output, @r#" test-table.x = true test-table.y.foo = "abc" test-table.y.bar = 123 test-table.z."with space"."function()" = 5 [EOF] "#);} #[test]fn test_config_list_inline_table() { let test_env = TestEnvironment::default(); test_env.add_config( r#" test-table = { x = true, y = 1 } "#, ); // Inline tables are expanded let output = test_env.run_jj_in(".", ["config", "list", "test-table"]); insta::assert_snapshot!(output, @r" test-table.x = true test-table.y = 1 [EOF] "); // Inner value can also be addressed by a dotted name path let output = test_env.run_jj_in(".", ["config", "list", "test-table.x"]); insta::assert_snapshot!(output, @r" test-table.x = true [EOF] ");} #[test]fn test_config_list_array() { let test_env = TestEnvironment::default(); test_env.add_config( r#" test-array = [1, "b", 3.4] "#, ); let output = test_env.run_jj_in(".", ["config", "list", "test-array"]); insta::assert_snapshot!(output, @r#" test-array = [1, "b", 3.4] [EOF] "#);} #[test]fn test_config_list_array_of_tables() { let test_env = TestEnvironment::default(); test_env.add_config( r#" [[test-table]] x = 1 [[test-table]] y = ["z"] z."key=with whitespace" = [] "#, ); // Array is a value, so is array of tables let output = test_env.run_jj_in(".", ["config", "list", "test-table"]); insta::assert_snapshot!(output, @r#" test-table = [{ x = 1 }, { y = ["z"], z = { "key=with whitespace" = [] } }] [EOF] "#);} #[test]fn test_config_list_all() { let test_env = TestEnvironment::default(); test_env.add_config( r#" test-val = [1, 2, 3] [test-table] x = true y.foo = "abc" y.bar = 123 "#, ); let output = test_env.run_jj_in(".", ["config", "list"]); insta::assert_snapshot!( output.normalize_stdout_with(|s| find_stdout_lines(r"(test-val|test-table\b[^=]*)", &s)), @r#" test-val = [1, 2, 3] test-table.x = true test-table.y.foo = "abc" test-table.y.bar = 123 [EOF] "#);} #[test]fn test_config_list_multiline_string() { let test_env = TestEnvironment::default(); test_env.add_config( r#" multiline = '''foobar''' "#, ); let output = test_env.run_jj_in(".", ["config", "list", "multiline"]); insta::assert_snapshot!(output, @r" multiline = ''' foo bar ''' [EOF] "); let output = test_env.run_jj_in( ".", [ "config", "list", "multiline", "--include-overridden", "--config=multiline='single'", ], ); insta::assert_snapshot!(output, @r" # multiline = ''' # foo # bar # ''' multiline = 'single' [EOF] ");} #[test]fn test_config_list_layer() { let mut test_env = TestEnvironment::default(); test_env.run_jj_in(".", ["git", "init", "repo"]).success(); // Test with fresh new config file let user_config_path = test_env.config_path().join("config.toml"); test_env.set_config_path(&user_config_path); let work_dir = test_env.work_dir("repo"); // User work_dir .run_jj(["config", "set", "--user", "test-key", "test-val"]) .success(); work_dir .run_jj([ "config", "set", "--user", "test-layered-key", "test-original-val", ]) .success(); let output = work_dir.run_jj(["config", "list", "--user"]); insta::assert_snapshot!(output, @r#" test-key = "test-val" test-layered-key = "test-original-val" [EOF] "#); // Repo work_dir .run_jj([ "config", "set", "--repo", "test-layered-key", "test-layered-val", ]) .success(); let output = work_dir.run_jj(["config", "list", "--user"]); insta::assert_snapshot!(output, @r#" test-key = "test-val" [EOF] "#); let output = work_dir.run_jj(["config", "list", "--repo"]); insta::assert_snapshot!(output, @r#" test-layered-key = "test-layered-val" [EOF] "#);} #[test]fn test_config_list_origin() { let mut test_env = TestEnvironment::default(); test_env.run_jj_in(".", ["git", "init", "repo"]).success(); // Test with fresh new config file let user_config_path = test_env.config_path().join("config.toml"); test_env.set_config_path(&user_config_path); let work_dir = test_env.work_dir("repo"); // User work_dir .run_jj(["config", "set", "--user", "test-key", "test-val"]) .success(); work_dir .run_jj([ "config", "set", "--user", "test-layered-key", "test-original-val", ]) .success(); // Repo work_dir .run_jj([ "config", "set", "--repo", "test-layered-key", "test-layered-val", ]) .success(); let output = work_dir.run_jj([ "config", "list", "-Tbuiltin_config_list_detailed", "--config", "test-cli-key=test-cli-val", ]); insta::assert_snapshot!(output, @r#" test-key = "test-val" # user $TEST_ENV/config/config.toml test-layered-key = "test-layered-val" # repo $TEST_ENV/repo/.jj/repo/config.toml user.name = "Test User" # env user.email = "test.user@example.com" # env debug.commit-timestamp = "2001-02-03T04:05:11+07:00" # env debug.randomness-seed = 5 # env debug.operation-timestamp = "2001-02-03T04:05:11+07:00" # env operation.hostname = "host.example.com" # env operation.username = "test-username" # env test-cli-key = "test-cli-val" # cli [EOF] "#); let output = work_dir.run_jj([ "config", "list", "-Tbuiltin_config_list_detailed", "--color=debug", "--include-defaults", "--include-overridden", "--config=test-key=test-cli-val", "test-key", ]); insta::assert_snapshot!(output, @r#" [38;5;8m<<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::>>[39m [38;5;2m<<config_list name::test-key>>[39m<<config_list:: = >>[38;5;3m<<config_list value::"test-cli-val">>[39m<<config_list:: # >>[38;5;4m<<config_list source::cli>>[39m<<config_list::>> [EOF] "#); let output = work_dir.run_jj([ "config", "list", r#"-Tjson(self) ++ "\n""#, "--include-defaults", "--include-overridden", "--config=test-key=test-cli-val", "test-key", ]); insta::with_settings!({ // Windows paths will be escaped in JSON syntax, which cannot be // normalized as $TEST_ENV. filters => [(r#""path":"[^"]*","#, r#""path":"<redacted>","#)], }, { insta::assert_snapshot!(output, @r#" {"name":"test-key","value":"test-val","source":"user","path":"<redacted>","is_overridden":true} {"name":"test-key","value":"test-cli-val","source":"cli","path":null,"is_overridden":false} [EOF] "#); });} #[test]fn test_config_layer_override_default() { let test_env = TestEnvironment::default(); test_env.run_jj_in(".", ["git", "init", "repo"]).success(); let work_dir = test_env.work_dir("repo"); let config_key = "merge-tools.vimdiff.program"; // Default let output = work_dir.run_jj(["config", "list", config_key, "--include-defaults"]); insta::assert_snapshot!(output, @r#" merge-tools.vimdiff.program = "vim" [EOF] "#); // User test_env.add_config(format!( "{config_key} = {value}\n", value = to_toml_value("user") )); let output = work_dir.run_jj(["config", "list", config_key]); insta::assert_snapshot!(output, @r#" merge-tools.vimdiff.program = "user" [EOF] "#); // Repo work_dir.write_file( ".jj/repo/config.toml", format!("{config_key} = {value}\n", value = to_toml_value("repo")), ); let output = work_dir.run_jj(["config", "list", config_key]); insta::assert_snapshot!(output, @r#" merge-tools.vimdiff.program = "repo" [EOF] "#); // Command argument let output = work_dir.run_jj([ "config", "list", config_key, "--config", &format!("{config_key}={value}", value = to_toml_value("command-arg")), ]); insta::assert_snapshot!(output, @r#" merge-tools.vimdiff.program = "command-arg" [EOF] "#); // Allow printing overridden values let output = work_dir.run_jj([ "config", "list", config_key, "--include-overridden", "--config", &format!("{config_key}={value}", value = to_toml_value("command-arg")), ]); insta::assert_snapshot!(output, @r##" # merge-tools.vimdiff.program = "user" # merge-tools.vimdiff.program = "repo" merge-tools.vimdiff.program = "command-arg" [EOF] "##); let output = work_dir.run_jj([ "config", "list", "--color=always", config_key, "--include-overridden", ]); insta::assert_snapshot!(output, @r#" [38;5;8m# merge-tools.vimdiff.program = "user"[39m [38;5;2mmerge-tools.vimdiff.program[39m = [38;5;3m"repo"[39m [EOF] "#);} #[test]fn test_config_layer_override_env() { let mut test_env = TestEnvironment::default(); test_env.run_jj_in(".", ["git", "init", "repo"]).success(); let config_key = "ui.editor"; // Environment base test_env.add_env_var("EDITOR", "env-base"); let work_dir = test_env.work_dir("repo"); let output = work_dir.run_jj(["config", "list", config_key]); insta::assert_snapshot!(output, @r#" ui.editor = "env-base" [EOF] "#); // User test_env.add_config(format!( "{config_key} = {value}\n", value = to_toml_value("user") )); let output = work_dir.run_jj(["config", "list", config_key]); insta::assert_snapshot!(output, @r#" ui.editor = "user" [EOF] "#); // Repo work_dir.write_file( ".jj/repo/config.toml", format!("{config_key} = {value}\n", value = to_toml_value("repo")), ); let output = work_dir.run_jj(["config", "list", config_key]); insta::assert_snapshot!(output, @r#" ui.editor = "repo" [EOF] "#); // Environment override test_env.add_env_var("JJ_EDITOR", "env-override"); let work_dir = test_env.work_dir("repo"); let output = work_dir.run_jj(["config", "list", config_key]); insta::assert_snapshot!(output, @r#" ui.editor = "env-override" [EOF] "#); // Command argument let output = work_dir.run_jj([ "config", "list", config_key, "--config", &format!("{config_key}={value}", value = to_toml_value("command-arg")), ]); insta::assert_snapshot!(output, @r#" ui.editor = "command-arg" [EOF] "#); // Allow printing overridden values let output = work_dir.run_jj([ "config", "list",Lines 1–500 of 1744. Load more
// Copyright 2022 The Jujutsu Authors // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // https://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. use std::env::join_paths; use std::path::PathBuf; use indoc::indoc; use itertools::Itertools as _; use regex::Regex; use crate::common::TestEnvironment; use crate::common::default_config_from_schema; use crate::common::fake_editor_path; use crate::common::force_interactive; use crate::common::to_toml_value; #[test] fn test_config_list_single() { let test_env = TestEnvironment::default(); test_env.add_config( r#" [test-table] somekey = "some value" "#, ); let output = test_env.run_jj_in(".", ["config", "list", "test-table.somekey"]); insta::assert_snapshot!(output, @r#" test-table.somekey = "some value" [EOF] "#); let output = test_env.run_jj_in( ".", ["config", "list", r#"-Tname ++ "\n""#, "test-table.somekey"], ); insta::assert_snapshot!(output, @r" test-table.somekey [EOF] "); } #[test] fn test_config_list_nonexistent() { let test_env = TestEnvironment::default(); let output = test_env.run_jj_in(".", ["config", "list", "nonexistent-test-key"]); insta::assert_snapshot!(output, @r" ------- stderr ------- Warning: No matching config key for nonexistent-test-key [EOF] "); } #[test] fn test_config_list_table() { let test_env = TestEnvironment::default(); test_env.add_config( r#" [test-table] x = true y.foo = "abc" y.bar = 123 "z"."with space"."function()" = 5 "#, ); let output = test_env.run_jj_in(".", ["config", "list", "test-table"]); insta::assert_snapshot!(output, @r#" test-table.x = true test-table.y.foo = "abc" test-table.y.bar = 123 test-table.z."with space"."function()" = 5 [EOF] "#); } #[test] fn test_config_list_inline_table() { let test_env = TestEnvironment::default(); test_env.add_config( r#" test-table = { x = true, y = 1 } "#, ); // Inline tables are expanded let output = test_env.run_jj_in(".", ["config", "list", "test-table"]); insta::assert_snapshot!(output, @r" test-table.x = true test-table.y = 1 [EOF] "); // Inner value can also be addressed by a dotted name path let output = test_env.run_jj_in(".", ["config", "list", "test-table.x"]); insta::assert_snapshot!(output, @r" test-table.x = true [EOF] "); } #[test] fn test_config_list_array() { let test_env = TestEnvironment::default(); test_env.add_config( r#" test-array = [1, "b", 3.4] "#, ); let output = test_env.run_jj_in(".", ["config", "list", "test-array"]); insta::assert_snapshot!(output, @r#" test-array = [1, "b", 3.4] [EOF] "#); } #[test] fn test_config_list_array_of_tables() { let test_env = TestEnvironment::default(); test_env.add_config( r#" [[test-table]] x = 1 [[test-table]] y = ["z"] z."key=with whitespace" = [] "#, ); // Array is a value, so is array of tables let output = test_env.run_jj_in(".", ["config", "list", "test-table"]); insta::assert_snapshot!(output, @r#" test-table = [{ x = 1 }, { y = ["z"], z = { "key=with whitespace" = [] } }] [EOF] "#); } #[test] fn test_config_list_all() { let test_env = TestEnvironment::default(); test_env.add_config( r#" test-val = [1, 2, 3] [test-table] x = true y.foo = "abc" y.bar = 123 "#, ); let output = test_env.run_jj_in(".", ["config", "list"]); insta::assert_snapshot!( output.normalize_stdout_with(|s| find_stdout_lines(r"(test-val|test-table\b[^=]*)", &s)), @r#" test-val = [1, 2, 3] test-table.x = true test-table.y.foo = "abc" test-table.y.bar = 123 [EOF] "#); } #[test] fn test_config_list_multiline_string() { let test_env = TestEnvironment::default(); test_env.add_config( r#" multiline = ''' foo bar ''' "#, ); let output = test_env.run_jj_in(".", ["config", "list", "multiline"]); insta::assert_snapshot!(output, @r" multiline = ''' foo bar ''' [EOF] "); let output = test_env.run_jj_in( ".", [ "config", "list", "multiline", "--include-overridden", "--config=multiline='single'", ], ); insta::assert_snapshot!(output, @r" # multiline = ''' # foo # bar # ''' multiline = 'single' [EOF] "); } #[test] fn test_config_list_layer() { let mut test_env = TestEnvironment::default(); test_env.run_jj_in(".", ["git", "init", "repo"]).success(); // Test with fresh new config file let user_config_path = test_env.config_path().join("config.toml"); test_env.set_config_path(&user_config_path); let work_dir = test_env.work_dir("repo"); // User work_dir .run_jj(["config", "set", "--user", "test-key", "test-val"]) .success(); work_dir .run_jj([ "config", "set", "--user", "test-layered-key", "test-original-val", ]) .success(); let output = work_dir.run_jj(["config", "list", "--user"]); insta::assert_snapshot!(output, @r#" test-key = "test-val" test-layered-key = "test-original-val" [EOF] "#); // Repo work_dir .run_jj([ "config", "set", "--repo", "test-layered-key", "test-layered-val", ]) .success(); let output = work_dir.run_jj(["config", "list", "--user"]); insta::assert_snapshot!(output, @r#" test-key = "test-val" [EOF] "#); let output = work_dir.run_jj(["config", "list", "--repo"]); insta::assert_snapshot!(output, @r#" test-layered-key = "test-layered-val" [EOF] "#); } #[test] fn test_config_list_origin() { let mut test_env = TestEnvironment::default(); test_env.run_jj_in(".", ["git", "init", "repo"]).success(); // Test with fresh new config file let user_config_path = test_env.config_path().join("config.toml"); test_env.set_config_path(&user_config_path); let work_dir = test_env.work_dir("repo"); // User work_dir .run_jj(["config", "set", "--user", "test-key", "test-val"]) .success(); work_dir .run_jj([ "config", "set", "--user", "test-layered-key", "test-original-val", ]) .success(); // Repo work_dir .run_jj([ "config", "set", "--repo", "test-layered-key", "test-layered-val", ]) .success(); let output = work_dir.run_jj([ "config", "list", "-Tbuiltin_config_list_detailed", "--config", "test-cli-key=test-cli-val", ]); insta::assert_snapshot!(output, @r#" test-key = "test-val" # user $TEST_ENV/config/config.toml test-layered-key = "test-layered-val" # repo $TEST_ENV/repo/.jj/repo/config.toml user.name = "Test User" # env user.email = "test.user@example.com" # env debug.commit-timestamp = "2001-02-03T04:05:11+07:00" # env debug.randomness-seed = 5 # env debug.operation-timestamp = "2001-02-03T04:05:11+07:00" # env operation.hostname = "host.example.com" # env operation.username = "test-username" # env test-cli-key = "test-cli-val" # cli [EOF] "#); let output = work_dir.run_jj([ "config", "list", "-Tbuiltin_config_list_detailed", "--color=debug", "--include-defaults", "--include-overridden", "--config=test-key=test-cli-val", "test-key", ]); insta::assert_snapshot!(output, @r#" [38;5;8m<<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::>>[39m [38;5;2m<<config_list name::test-key>>[39m<<config_list:: = >>[38;5;3m<<config_list value::"test-cli-val">>[39m<<config_list:: # >>[38;5;4m<<config_list source::cli>>[39m<<config_list::>> [EOF] "#); let output = work_dir.run_jj([ "config", "list", r#"-Tjson(self) ++ "\n""#, "--include-defaults", "--include-overridden", "--config=test-key=test-cli-val", "test-key", ]); insta::with_settings!({ // Windows paths will be escaped in JSON syntax, which cannot be // normalized as $TEST_ENV. filters => [(r#""path":"[^"]*","#, r#""path":"<redacted>","#)], }, { insta::assert_snapshot!(output, @r#" {"name":"test-key","value":"test-val","source":"user","path":"<redacted>","is_overridden":true} {"name":"test-key","value":"test-cli-val","source":"cli","path":null,"is_overridden":false} [EOF] "#); }); } #[test] fn test_config_layer_override_default() { let test_env = TestEnvironment::default(); test_env.run_jj_in(".", ["git", "init", "repo"]).success(); let work_dir = test_env.work_dir("repo"); let config_key = "merge-tools.vimdiff.program"; // Default let output = work_dir.run_jj(["config", "list", config_key, "--include-defaults"]); insta::assert_snapshot!(output, @r#" merge-tools.vimdiff.program = "vim" [EOF] "#); // User test_env.add_config(format!( "{config_key} = {value}\n", value = to_toml_value("user") )); let output = work_dir.run_jj(["config", "list", config_key]); insta::assert_snapshot!(output, @r#" merge-tools.vimdiff.program = "user" [EOF] "#); // Repo work_dir.write_file( ".jj/repo/config.toml", format!("{config_key} = {value}\n", value = to_toml_value("repo")), ); let output = work_dir.run_jj(["config", "list", config_key]); insta::assert_snapshot!(output, @r#" merge-tools.vimdiff.program = "repo" [EOF] "#); // Command argument let output = work_dir.run_jj([ "config", "list", config_key, "--config", &format!("{config_key}={value}", value = to_toml_value("command-arg")), ]); insta::assert_snapshot!(output, @r#" merge-tools.vimdiff.program = "command-arg" [EOF] "#); // Allow printing overridden values let output = work_dir.run_jj([ "config", "list", config_key, "--include-overridden", "--config", &format!("{config_key}={value}", value = to_toml_value("command-arg")), ]); insta::assert_snapshot!(output, @r##" # merge-tools.vimdiff.program = "user" # merge-tools.vimdiff.program = "repo" merge-tools.vimdiff.program = "command-arg" [EOF] "##); let output = work_dir.run_jj([ "config", "list", "--color=always", config_key, "--include-overridden", ]); insta::assert_snapshot!(output, @r#" [38;5;8m# merge-tools.vimdiff.program = "user"[39m [38;5;2mmerge-tools.vimdiff.program[39m = [38;5;3m"repo"[39m [EOF] "#); } #[test] fn test_config_layer_override_env() { let mut test_env = TestEnvironment::default(); test_env.run_jj_in(".", ["git", "init", "repo"]).success(); let config_key = "ui.editor"; // Environment base test_env.add_env_var("EDITOR", "env-base"); let work_dir = test_env.work_dir("repo"); let output = work_dir.run_jj(["config", "list", config_key]); insta::assert_snapshot!(output, @r#" ui.editor = "env-base" [EOF] "#); // User test_env.add_config(format!( "{config_key} = {value}\n", value = to_toml_value("user") )); let output = work_dir.run_jj(["config", "list", config_key]); insta::assert_snapshot!(output, @r#" ui.editor = "user" [EOF] "#); // Repo work_dir.write_file( ".jj/repo/config.toml", format!("{config_key} = {value}\n", value = to_toml_value("repo")), ); let output = work_dir.run_jj(["config", "list", config_key]); insta::assert_snapshot!(output, @r#" ui.editor = "repo" [EOF] "#); // Environment override test_env.add_env_var("JJ_EDITOR", "env-override"); let work_dir = test_env.work_dir("repo"); let output = work_dir.run_jj(["config", "list", config_key]); insta::assert_snapshot!(output, @r#" ui.editor = "env-override" [EOF] "#); // Command argument let output = work_dir.run_jj([ "config", "list", config_key, "--config", &format!("{config_key}={value}", value = to_toml_value("command-arg")), ]); insta::assert_snapshot!(output, @r#" ui.editor = "command-arg" [EOF] "#); // Allow printing overridden values let output = work_dir.run_jj([ "config", "list",