From 3ccdd1f98a4fa0a90b1e82e6c8ff3ad9cc03e936 Mon Sep 17 00:00:00 2001 From: Martin von Zweigbergk Date: Thu, 26 Jan 2023 11:26:18 -0800 Subject: [PATCH] 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. --- tests/common/mod.rs | 13 +++++-------- tests/test_alias.rs | 20 ++++++++++---------- tests/test_config_command.rs | 19 +++++++------------ tests/test_describe_command.rs | 2 +- tests/test_global_opts.rs | 4 ++-- tests/test_init_command.rs | 2 +- tests/test_log_command.rs | 4 ++-- tests/test_operations.rs | 2 +- tests/test_revset_output.rs | 4 ++-- tests/test_show_command.rs | 2 +- tests/test_untrack_command.rs | 4 ++-- tests/test_workspaces.rs | 4 ++-- 12 files changed, 36 insertions(+), 44 deletions(-) diff --git a/tests/common/mod.rs b/tests/common/mod.rs index 66f061602..63fad0ad1 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -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()); diff --git a/tests/test_alias.rs b/tests/test_alias.rs index dc2dbbd86..b7939af57 100644 --- a/tests/test_alias.rs +++ b/tests/test_alias.rs @@ -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 = [[]] "#, diff --git a/tests/test_config_command.rs b/tests/test_config_command.rs index 59dc6794d..c79f816f0 100644 --- a/tests/test_config_command.rs +++ b/tests/test_config_command.rs @@ -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" diff --git a/tests/test_describe_command.rs b/tests/test_describe_command.rs index 2691f3a30..6953b4d3b 100644 --- a/tests/test_describe_command.rs +++ b/tests/test_describe_command.rs @@ -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") diff --git a/tests/test_global_opts.rs b/tests/test_global_opts.rs index bdcb87f59..ef6ff70f0 100644 --- a/tests/test_global_opts.rs +++ b/tests/test_global_opts.rs @@ -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 diff --git a/tests/test_init_command.rs b/tests/test_init_command.rs index cc2529444..dd0f535dd 100644 --- a/tests/test_init_command.rs +++ b/tests/test_init_command.rs @@ -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" diff --git a/tests/test_log_command.rs b/tests/test_log_command.rs index 52eaa9351..127afd0a2 100644 --- a/tests/test_log_command.rs +++ b/tests/test_log_command.rs @@ -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" "#, diff --git a/tests/test_operations.rs b/tests/test_operations.rs index e3d9118d6..105c5003a 100644 --- a/tests/test_operations.rs +++ b/tests/test_operations.rs @@ -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" "#, ); diff --git a/tests/test_revset_output.rs b/tests/test_revset_output.rs index c2834c312..b0609eb95 100644 --- a/tests/test_revset_output.rs +++ b/tests/test_revset_output.rs @@ -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' diff --git a/tests/test_show_command.rs b/tests/test_show_command.rs index 87fc9413e..1ca805d10 100644 --- a/tests/test_show_command.rs +++ b/tests/test_show_command.rs @@ -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(); diff --git a/tests/test_untrack_command.rs b/tests/test_untrack_command.rs index 8ea184a68..cb18a1ff8 100644 --- a/tests/test_untrack_command.rs +++ b/tests/test_untrack_command.rs @@ -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"); diff --git a/tests/test_workspaces.rs b/tests/test_workspaces.rs index 9655b8750..cd6f55d61 100644 --- a/tests/test_workspaces.rs +++ b/tests/test_workspaces.rs @@ -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");