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

tests: pass string instead of bytes to add_config()

I don't think need to write non-UTF8 bytes to our config files. If we
ever do (maybe to test that we give the user a reasonable error
message), we add a custom function for that.
This commit is contained in:
Martin von Zweigbergk 2023-01-26 11:26:18 -08:00 committed by Martin von Zweigbergk
parent e1d49cc67f
commit 3ccdd1f98a
12 changed files with 36 additions and 44 deletions

View file

@ -115,7 +115,7 @@ impl TestEnvironment {
&self.config_dir
}
pub fn add_config(&self, content: &[u8]) {
pub fn add_config(&self, content: &str) {
// Concatenating two valid TOML files does not (generally) result in a valid
// TOML file, so we use create a new file every time instead.
let mut config_file_number = self.config_file_number.borrow_mut();
@ -142,9 +142,8 @@ impl TestEnvironment {
// in it
let escaped_editor_path = editor_path.to_str().unwrap().replace('\\', r"\\");
self.add_env_var("EDITOR", &escaped_editor_path);
self.add_config(
format!(
r###"
self.add_config(&format!(
r###"
[ui]
merge-editor = "fake-editor"
@ -152,9 +151,7 @@ impl TestEnvironment {
fake-editor.program="{escaped_editor_path}"
fake-editor.merge-args = ["$output"]
"###
)
.as_bytes(),
);
));
let edit_script = self.env_root().join("edit_script");
std::fs::write(&edit_script, "").unwrap();
self.add_env_var("EDIT_SCRIPT", edit_script.to_str().unwrap());
@ -169,7 +166,7 @@ impl TestEnvironment {
// Simplified TOML escaping, hoping that there are no '"' or control characters
// in it
let escaped_diff_editor_path = diff_editor_path.to_str().unwrap().replace('\\', r"\\");
self.add_config(format!(r#"ui.diff-editor = "{escaped_diff_editor_path}""#).as_bytes());
self.add_config(&format!(r#"ui.diff-editor = "{escaped_diff_editor_path}""#));
let edit_script = self.env_root().join("diff_edit_script");
std::fs::write(&edit_script, "").unwrap();
self.add_env_var("DIFF_EDIT_SCRIPT", edit_script.to_str().unwrap());

View file

@ -24,7 +24,7 @@ fn test_alias_basic() {
test_env.jj_cmd_success(test_env.env_root(), &["init", "repo", "--git"]);
let repo_path = test_env.env_root().join("repo");
test_env.add_config(br#"alias.b = ["log", "-r", "@", "-T", "branches"]"#);
test_env.add_config(r#"alias.b = ["log", "-r", "@", "-T", "branches"]"#);
test_env.jj_cmd_success(&repo_path, &["branch", "create", "my-branch"]);
let stdout = test_env.jj_cmd_success(&repo_path, &["b"]);
insta::assert_snapshot!(stdout, @r###"
@ -55,7 +55,7 @@ fn test_alias_calls_unknown_command() {
test_env.jj_cmd_success(test_env.env_root(), &["init", "repo", "--git"]);
let repo_path = test_env.env_root().join("repo");
test_env.add_config(br#"alias.foo = ["nonexistent"]"#);
test_env.add_config(r#"alias.foo = ["nonexistent"]"#);
let stderr = test_env.jj_cmd_cli_error(&repo_path, &["foo"]);
insta::assert_snapshot!(stderr, @r###"
error: The subcommand 'nonexistent' wasn't recognized
@ -72,7 +72,7 @@ fn test_alias_calls_command_with_invalid_option() {
test_env.jj_cmd_success(test_env.env_root(), &["init", "repo", "--git"]);
let repo_path = test_env.env_root().join("repo");
test_env.add_config(br#"alias.foo = ["log", "--nonexistent"]"#);
test_env.add_config(r#"alias.foo = ["log", "--nonexistent"]"#);
let stderr = test_env.jj_cmd_cli_error(&repo_path, &["foo"]);
insta::assert_snapshot!(stderr, @r###"
error: Found argument '--nonexistent' which wasn't expected, or isn't valid in this context
@ -90,7 +90,7 @@ fn test_alias_calls_help() {
let test_env = TestEnvironment::default();
test_env.jj_cmd_success(test_env.env_root(), &["init", "repo", "--git"]);
let repo_path = test_env.env_root().join("repo");
test_env.add_config(br#"alias.h = ["--help"]"#);
test_env.add_config(r#"alias.h = ["--help"]"#);
let stdout = test_env.jj_cmd_success(&repo_path, &["h"]);
insta::assert_snapshot!(stdout.lines().take(5).join("\n"), @r###"
Jujutsu (An experimental VCS)
@ -107,7 +107,7 @@ fn test_alias_cannot_override_builtin() {
test_env.jj_cmd_success(test_env.env_root(), &["init", "repo", "--git"]);
let repo_path = test_env.env_root().join("repo");
test_env.add_config(br#"alias.log = ["rebase"]"#);
test_env.add_config(r#"alias.log = ["rebase"]"#);
// Alias should be ignored
let stdout = test_env.jj_cmd_success(&repo_path, &["log", "-r", "root"]);
insta::assert_snapshot!(stdout, @r###"
@ -123,7 +123,7 @@ fn test_alias_recursive() {
let repo_path = test_env.env_root().join("repo");
test_env.add_config(
br#"[alias]
r#"[alias]
foo = ["foo"]
bar = ["baz"]
baz = ["bar"]
@ -146,7 +146,7 @@ fn test_alias_global_args_before_and_after() {
let test_env = TestEnvironment::default();
test_env.jj_cmd_success(test_env.env_root(), &["init", "repo", "--git"]);
let repo_path = test_env.env_root().join("repo");
test_env.add_config(br#"alias.l = ["log", "-T", "commit_id", "-r", "all()"]"#);
test_env.add_config(r#"alias.l = ["log", "-T", "commit_id", "-r", "all()"]"#);
// Test the setup
let stdout = test_env.jj_cmd_success(&repo_path, &["l"]);
insta::assert_snapshot!(stdout, @r###"
@ -181,8 +181,8 @@ fn test_alias_global_args_in_definition() {
let test_env = TestEnvironment::default();
test_env.jj_cmd_success(test_env.env_root(), &["init", "repo", "--git"]);
let repo_path = test_env.env_root().join("repo");
test_env.add_config(
br#"alias.l = ["log", "-T", "commit_id", "--at-op", "@-", "-r", "all()", "--color=always"]"#,
test_env.add_config(
r#"alias.l = ["log", "-T", "commit_id", "--at-op", "@-", "-r", "all()", "--color=always"]"#,
);
// The global argument in the alias is respected
@ -197,7 +197,7 @@ fn test_alias_invalid_definition() {
let test_env = TestEnvironment::default();
test_env.add_config(
br#"[alias]
r#"[alias]
non-list = 5
non-string-list = [[]]
"#,

View file

@ -26,8 +26,7 @@ fn test_config_list_single() {
r###"
[test-table]
somekey = "some value"
"###
.as_bytes(),
"###,
);
let stdout = test_env.jj_cmd_success(
@ -48,8 +47,7 @@ fn test_config_list_table() {
x = true
y.foo = "abc"
y.bar = 123
"###
.as_bytes(),
"###,
);
let stdout = test_env.jj_cmd_success(test_env.env_root(), &["config", "list", "test-table"]);
insta::assert_snapshot!(
@ -67,8 +65,7 @@ fn test_config_list_array() {
test_env.add_config(
r###"
test-array = [1, "b", 3.4]
"###
.as_bytes(),
"###,
);
let stdout = test_env.jj_cmd_success(test_env.env_root(), &["config", "list", "test-array"]);
insta::assert_snapshot!(stdout, @r###"
@ -85,8 +82,7 @@ fn test_config_list_inline_table() {
x = 1
[[test-table]]
y = ["z"]
"###
.as_bytes(),
"###,
);
let stdout = test_env.jj_cmd_success(test_env.env_root(), &["config", "list", "test-table"]);
insta::assert_snapshot!(stdout, @r###"
@ -104,8 +100,7 @@ fn test_config_list_all() {
x = true
y.foo = "abc"
y.bar = 123
"###
.as_bytes(),
"###,
);
let stdout = test_env.jj_cmd_success(test_env.env_root(), &["config", "list"]);
insta::assert_snapshot!(
@ -132,7 +127,7 @@ fn test_config_layer_override_default() {
"###);
// User
test_env.add_config(format!("{config_key} = {value:?}\n", value = "user").as_bytes());
test_env.add_config(&format!("{config_key} = {value:?}\n", value = "user"));
let stdout = test_env.jj_cmd_success(&repo_path, &["config", "list", config_key]);
insta::assert_snapshot!(stdout, @r###"
merge-tools.vimdiff.program="user"
@ -180,7 +175,7 @@ fn test_config_layer_override_env() {
"###);
// User
test_env.add_config(format!("{config_key} = {value:?}\n", value = "user").as_bytes());
test_env.add_config(&format!("{config_key} = {value:?}\n", value = "user"));
let stdout = test_env.jj_cmd_success(&repo_path, &["config", "list", config_key]);
insta::assert_snapshot!(stdout, @r###"
ui.editor="user"

View file

@ -125,7 +125,7 @@ fn test_describe() {
assert!(get_stderr_string(&assert).contains("bad-editor-from-visual-env"));
// `ui.editor` config overrides `$VISUAL`
test_env.add_config(br#"ui.editor = "bad-editor-from-config""#);
test_env.add_config(r#"ui.editor = "bad-editor-from-config""#);
let assert = test_env
.jj_cmd(&repo_path, &["describe"])
.env("VISUAL", "bad-editor-from-visual-env")

View file

@ -187,7 +187,7 @@ fn test_color_config() {
"###);
// Test that color is used if it's requested in the config file
test_env.add_config(br#"ui.color="always""#);
test_env.add_config(r#"ui.color="always""#);
let stdout = test_env.jj_cmd_success(&repo_path, &["log", "-T", "commit_id"]);
insta::assert_snapshot!(stdout, @r###"
@ 230dd059e1b059aefc0da06a2e5a7dbf22362f22
@ -291,7 +291,7 @@ fn test_invalid_config() {
// Test that we get a reasonable error if the config is invalid (#55)
let test_env = TestEnvironment::default();
test_env.add_config(b"[section]key = value-missing-quotes");
test_env.add_config("[section]key = value-missing-quotes");
let stderr = test_env.jj_cmd_failure(test_env.env_root(), &["init", "repo"]);
insta::assert_snapshot!(stderr.replace('\\', "/"), @r###"
Config error: expected newline, found an identifier at line 1 column 10 in config/config0001.toml

View file

@ -196,7 +196,7 @@ fn test_init_local_disallowed() {
#[test]
fn test_init_local() {
let test_env = TestEnvironment::default();
test_env.add_config(br#"ui.allow-init-native = true"#);
test_env.add_config(r#"ui.allow-init-native = true"#);
let stdout = test_env.jj_cmd_success(test_env.env_root(), &["init", "repo"]);
insta::assert_snapshot!(stdout, @r###"
Initialized repo in "repo"

View file

@ -603,7 +603,7 @@ fn test_default_revset() {
test_env.jj_cmd_success(&repo_path, &["describe", "-m", "add a file"]);
// Set configuration to only show the root commit.
test_env.add_config(br#"ui.default-revset = "root""#);
test_env.add_config(r#"ui.default-revset = "root""#);
// Log should only contain one line (for the root commit), and not show the
// commit created above.
@ -657,7 +657,7 @@ fn test_graph_template_color() {
test_env.jj_cmd_success(&repo_path, &["new", "-m", "single line"]);
test_env.add_config(
br#"[colors]
r#"[colors]
description = "red"
"working_copy description" = "green"
"#,

View file

@ -127,7 +127,7 @@ fn test_op_log() {
fn test_op_log_configurable() {
let test_env = TestEnvironment::default();
test_env.add_config(
br#"operation.hostname = "my-hostname"
r#"operation.hostname = "my-hostname"
operation.username = "my-username"
"#,
);

View file

@ -147,7 +147,7 @@ fn test_alias() {
let repo_path = test_env.env_root().join("repo");
test_env.add_config(
br###"
r###"
[revset-aliases]
'my-root' = 'root'
'syntax-error' = 'whatever &'
@ -249,7 +249,7 @@ fn test_bad_alias_decl() {
let repo_path = test_env.env_root().join("repo");
test_env.add_config(
br###"
r###"
[revset-aliases]
'my-root' = 'root'
'"bad"' = 'root'

View file

@ -41,7 +41,7 @@ fn test_show_relative_timestamps() {
test_env.jj_cmd_success(test_env.env_root(), &["init", "repo", "--git"]);
let repo_path = test_env.env_root().join("repo");
test_env.add_config(br#"ui.relative-timestamps = true"#);
test_env.add_config(r#"ui.relative-timestamps = true"#);
let stdout = test_env.jj_cmd_success(&repo_path, &["show"]);
let timestamp_re = Regex::new(r"\([0-9]+ years ago\)").unwrap();

View file

@ -21,7 +21,7 @@ pub mod common;
#[test]
fn test_untrack() {
let test_env = TestEnvironment::default();
test_env.add_config(br#"ui.allow-init-native = true"#);
test_env.add_config(r#"ui.allow-init-native = true"#);
test_env.jj_cmd_success(test_env.env_root(), &["init", "repo"]);
let repo_path = test_env.env_root().join("repo");
@ -101,7 +101,7 @@ fn test_untrack() {
#[test]
fn test_untrack_sparse() {
let test_env = TestEnvironment::default();
test_env.add_config(br#"ui.allow-init-native = true"#);
test_env.add_config(r#"ui.allow-init-native = true"#);
test_env.jj_cmd_success(test_env.env_root(), &["init", "repo"]);
let repo_path = test_env.env_root().join("repo");

View file

@ -215,10 +215,10 @@ fn test_list_workspaces_template() {
let test_env = TestEnvironment::default();
test_env.jj_cmd_success(test_env.env_root(), &["init", "--git", "main"]);
test_env.add_config(
br###"
r#"
template.commit_summary = """commit_id.short() " " description.first_line()
if(current_working_copy, " (current)")"""
"###,
"#,
);
let main_path = test_env.env_root().join("main");
let secondary_path = test_env.env_root().join("secondary");