Skip to content

Commit

Permalink
Get rid of absolute paths in transcript tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jpikl committed Apr 5, 2024
1 parent 826cecf commit aae3a15
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
2 changes: 1 addition & 1 deletion snapshots/error.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 8 additions & 7 deletions src/commands/x.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,15 +410,16 @@ fn format_command(command: &Command) -> Result<String> {
write!(&mut output, "{key}={val:?} ",)?;
}

// We want to obfuscate program path to make "transcript" tests reproducible.
if cfg!(debug_assertions) && env::var_os("NEXTEST").is_some() {
let program = Path::new(command.get_program())
let program = if cfg!(debug_assertions) && env::var_os("NEXTEST").is_some() {
// We want to obfuscate program path to make "transcript" tests reproducible.
Path::new(command.get_program())
.file_stem()
.unwrap_or_default();
write!(&mut output, "{program:?}")?;
.unwrap_or_default()
} else {
write!(&mut output, "{:?}", command.get_program())?;
}
command.get_program()
};

write!(&mut output, "{program:?}")?;

for arg in command.get_args() {
write!(&mut output, " {arg:?}")?;
Expand Down
18 changes: 17 additions & 1 deletion tests/transcript.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use assert_cmd::crate_name;
use std::env;
use std::ffi::OsString;
use std::path::Path;
use std::path::PathBuf;
use term_transcript::svg::NamedPalette;
use term_transcript::svg::Template;
use term_transcript::svg::TemplateOptions;
Expand Down Expand Up @@ -38,7 +40,9 @@ const STATUS_COMMAND: &str = "echo %errorlevel%";

fn transcript(snapshot: &str, args: &str) {
let shell_options = ShellOptions::default()
.with_cargo_path()
// `.with_cargo_path()` is useless because it appends the cargo target path at the end.
// We need it first, so it's prioritized over the other paths which might contain binary with the same name.
.with_env("PATH", custom_path_env())
.with_env("CLICOLOR_FORCE", "1")
.with_current_dir(env!("CARGO_MANIFEST_DIR"))
.with_status_check(STATUS_COMMAND, |output| {
Expand All @@ -59,3 +63,15 @@ fn transcript(snapshot: &str, args: &str) {
.with_template(Template::new(template_options))
.test(path, [input.as_ref()]);
}

fn custom_path_env() -> OsString {
let path_env = env::var_os("PATH").unwrap_or_default();
let mut paths = env::split_paths(&path_env).collect::<Vec<_>>();
paths.insert(0, bin_directory());
env::join_paths(paths).unwrap()
}

fn bin_directory() -> PathBuf {
let bin_path = env!(concat!("CARGO_BIN_EXE_", crate_name!()));
Path::new(&bin_path).parent().unwrap().to_owned()
}

0 comments on commit aae3a15

Please sign in to comment.