From 650853401d3613da6e9ef67f681cc5048e9f28f1 Mon Sep 17 00:00:00 2001 From: Martin von Zweigbergk Date: Thu, 3 Nov 2022 13:14:23 -0700 Subject: [PATCH] tests: replace test env path in output by `$TEST_ENV` Mercurial's test runner does something like this. I considered replacing `\` by `/` everywhere, but we use `\` in graph-log output quite frequently, so it doesn't seem worth it. --- tests/common/mod.rs | 18 ++++++++++++++++-- tests/test_git_clone.rs | 14 +++++++------- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/tests/common/mod.rs b/tests/common/mod.rs index 9ab4a1fdd..d129425f3 100644 --- a/tests/common/mod.rs +++ b/tests/common/mod.rs @@ -17,6 +17,7 @@ use std::collections::HashMap; use std::path::{Path, PathBuf}; use jujutsu_lib::testutils; +use regex::{Captures, Regex}; use tempfile::TempDir; pub struct TestEnvironment { @@ -77,14 +78,14 @@ impl TestEnvironment { /// Run a `jj` command, check that it was successful, and return its stdout pub fn jj_cmd_success(&self, current_dir: &Path, args: &[&str]) -> String { let assert = self.jj_cmd(current_dir, args).assert().success().stderr(""); - get_stdout_string(&assert) + self.normalize_output(get_stdout_string(&assert)) } /// Run a `jj` command, check that it failed with code 1, and return its /// stderr pub fn jj_cmd_failure(&self, current_dir: &Path, args: &[&str]) -> String { let assert = self.jj_cmd(current_dir, args).assert().code(1).stdout(""); - get_stderr_string(&assert) + self.normalize_output(get_stderr_string(&assert)) } /// Run a `jj` command and check that it failed with code 2 (for invalid @@ -154,6 +155,19 @@ impl TestEnvironment { self.add_env_var("DIFF_EDIT_SCRIPT", edit_script.to_str().unwrap()); edit_script } + + pub fn normalize_output(&self, text: String) -> String { + let regex = Regex::new(&format!( + r"{}(\S+)", + regex::escape(&self.env_root.display().to_string()) + )) + .unwrap(); + regex + .replace_all(&text, |caps: &Captures| { + format!("$TEST_ENV{}", caps[1].replace('\\', "/")) + }) + .to_string() + } } pub fn get_stdout_string(assert: &assert_cmd::assert::Assert) -> String { diff --git a/tests/test_git_clone.rs b/tests/test_git_clone.rs index d5c84ca9f..28ad12f2e 100644 --- a/tests/test_git_clone.rs +++ b/tests/test_git_clone.rs @@ -24,8 +24,8 @@ fn test_git_clone() { // Clone an empty repo let stdout = test_env.jj_cmd_success(test_env.env_root(), &["git", "clone", "source", "empty"]); - insta::assert_snapshot!(stdout.replace(test_env.env_root().join("empty").to_str().unwrap(), ""), @r###" - Fetching into new repo in "" + insta::assert_snapshot!(stdout, @r###" + Fetching into new repo in "$TEST_ENV/empty" Nothing changed. "###); @@ -51,8 +51,8 @@ fn test_git_clone() { .unwrap(); git_repo.set_head("refs/heads/main").unwrap(); let stdout = test_env.jj_cmd_success(test_env.env_root(), &["git", "clone", "source", "clone"]); - insta::assert_snapshot!(stdout.replace(test_env.env_root().join("clone").to_str().unwrap(), ""), @r###" - Fetching into new repo in "" + insta::assert_snapshot!(stdout, @r###" + Fetching into new repo in "$TEST_ENV/clone" Working copy now at: 1f0b881a057d (no description set) Added 1 files, modified 0 files, removed 0 files "###); @@ -60,21 +60,21 @@ fn test_git_clone() { // Try cloning into an existing workspace let stderr = test_env.jj_cmd_failure(test_env.env_root(), &["git", "clone", "source", "clone"]); - insta::assert_snapshot!(stderr.replace(test_env.env_root().join("clone").to_str().unwrap(), ""), @r###" + insta::assert_snapshot!(stderr, @r###" Error: Destination path exists and is not an empty directory "###); // Try cloning into an existing file std::fs::write(test_env.env_root().join("file"), "contents").unwrap(); let stderr = test_env.jj_cmd_failure(test_env.env_root(), &["git", "clone", "source", "file"]); - insta::assert_snapshot!(stderr.replace(test_env.env_root().join("file").to_str().unwrap(), ""), @r###" + insta::assert_snapshot!(stderr, @r###" Error: Destination path exists and is not an empty directory "###); // Try cloning into non-empty, non-workspace directory std::fs::remove_dir_all(test_env.env_root().join("clone").join(".jj")).unwrap(); let stderr = test_env.jj_cmd_failure(test_env.env_root(), &["git", "clone", "source", "clone"]); - insta::assert_snapshot!(stderr.replace(test_env.env_root().join("clone").to_str().unwrap(), ""), @r###" + insta::assert_snapshot!(stderr, @r###" Error: Destination path exists and is not an empty directory "###); }