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.
This commit is contained in:
Martin von Zweigbergk 2022-11-03 13:14:23 -07:00 committed by Martin von Zweigbergk
parent 1c4888f769
commit 650853401d
2 changed files with 23 additions and 9 deletions

View file

@ -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 {

View file

@ -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(), "<dest>"), @r###"
Fetching into new repo in "<dest>"
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(), "<dest>"), @r###"
Fetching into new repo in "<dest>"
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(), "<dest>"), @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(), "<dest>"), @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(), "<dest>"), @r###"
insta::assert_snapshot!(stderr, @r###"
Error: Destination path exists and is not an empty directory
"###);
}