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

cli: accept TOML arrays and tables to jj config set

This commit is contained in:
Martin von Zweigbergk 2023-10-10 20:47:46 -07:00 committed by Martin von Zweigbergk
parent 679a591a22
commit f654801c20
3 changed files with 34 additions and 6 deletions

View file

@ -21,6 +21,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Status messages are now printed to stderr.
* `jj config set` now interprets the value as TOML also if it's a valid TOML
array or table. For example, `jj config set --user 'aliases.n' '["new"]'`
### New features
### Fixed bugs

View file

@ -2178,13 +2178,10 @@ pub fn write_config_value_to_file(
})?;
// Apply config value
// Interpret value as string unless it's another simple scalar type.
// Interpret value as string if it can't be parsed as a TOML value.
// TODO(#531): Infer types based on schema (w/ --type arg to override).
let item = match toml_edit::Value::from_str(value_str) {
Ok(value @ toml_edit::Value::Boolean(..))
| Ok(value @ toml_edit::Value::Integer(..))
| Ok(value @ toml_edit::Value::Float(..))
| Ok(value @ toml_edit::Value::String(..)) => toml_edit::value(value),
Ok(value) => toml_edit::value(value),
_ => toml_edit::value(value_str),
};
let mut target_table = doc.as_table_mut();
@ -2209,7 +2206,7 @@ pub fn write_config_value_to_file(
_ => {
return Err(user_error(format!(
"Failed to set {key}: would overwrite entire non-scalar value with scalar"
)))
)));
}
}
target_table[last_key_part] = item;

View file

@ -337,6 +337,34 @@ 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"]);
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");
let set_value = |key, value| {
test_env.jj_cmd_success(&repo_path, &["config", "set", "--user", key, value]);
};
set_value("test-table.integer", "42");
set_value("test-table.float", "3.14");
set_value("test-table.array", r#"["one", "two"]"#);
set_value("test-table.boolean", "true");
set_value("test-table.string", r#""foo""#);
set_value("test-table.invalid", r"a + b");
insta::assert_snapshot!(std::fs::read_to_string(&user_config_path).unwrap(), @r###"
[test-table]
integer = 42
float = 3.14
array = ["one", "two"]
boolean = true
string = "foo"
invalid = "a + b"
"###);
}
#[test]
fn test_config_set_type_mismatch() {
let mut test_env = TestEnvironment::default();