ok/jj
1
0
Fork 0
forked from mirrors/jj

cli: allow overwriting non-scalar with jj config set

Before this patch, it was an error to run `jj config set --user foo
'[1]'` twice. But it's only been broken since the previous commit
because '[1]' was interpreted as a string before then.
This commit is contained in:
Martin von Zweigbergk 2023-10-10 22:02:21 -07:00 committed by Martin von Zweigbergk
parent f654801c20
commit ebec82ee0c
2 changed files with 25 additions and 7 deletions

View file

@ -2199,13 +2199,13 @@ pub fn write_config_value_to_file(
))
})?;
}
// Error out if overwriting non-scalar value for key (table or array).
// Error out if overwriting non-scalar value for key (table or array) with
// scalar.
match target_table.get(last_key_part) {
None | Some(toml_edit::Item::None) => {}
Some(toml_edit::Item::Value(val)) if !val.is_array() && !val.is_inline_table() => {}
_ => {
None | Some(toml_edit::Item::None | toml_edit::Item::Value(_)) => {}
Some(toml_edit::Item::Table(_) | toml_edit::Item::ArrayOfTables(_)) => {
return Err(user_error(format!(
"Failed to set {key}: would overwrite entire non-scalar value with scalar"
"Failed to set {key}: would overwrite entire table"
)));
}
}

View file

@ -340,7 +340,7 @@ fn test_config_set_for_repo() {
#[test]
fn test_config_set_toml_types() {
let mut test_env = TestEnvironment::default();
test_env.jj_cmd_success(test_env.env_root(), &["init", "repo", "--git"]);
test_env.jj_cmd_ok(test_env.env_root(), &["init", "repo", "--git"]);
let user_config_path = test_env.config_path().join("config.toml");
test_env.set_config_path(user_config_path.clone());
let repo_path = test_env.env_root().join("repo");
@ -382,8 +382,26 @@ fn test_config_set_type_mismatch() {
&["config", "set", "--user", "test-table", "not-a-table"],
);
insta::assert_snapshot!(stderr, @r###"
Error: Failed to set test-table: would overwrite entire non-scalar value with scalar
Error: Failed to set test-table: would overwrite entire table
"###);
// But it's fine to overwrite arrays and inline tables
test_env.jj_cmd_success(
&repo_path,
&["config", "set", "--user", "test-table.array", "[1,2,3]"],
);
test_env.jj_cmd_success(
&repo_path,
&["config", "set", "--user", "test-table.array", "[4,5,6]"],
);
test_env.jj_cmd_success(
&repo_path,
&["config", "set", "--user", "test-table.inline", "{ x = 42}"],
);
test_env.jj_cmd_success(
&repo_path,
&["config", "set", "--user", "test-table.inline", "42"],
);
}
#[test]