Skip to content

Commit

Permalink
tests: Minor cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
taiki-e committed Oct 20, 2024
1 parent 47b418a commit 04cc2f6
Showing 1 changed file with 18 additions and 15 deletions.
33 changes: 18 additions & 15 deletions tests/auxiliary/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::{
sync::OnceLock,
};

use anyhow::{bail, Context as _, Result};
use anyhow::Context as _;
pub(crate) use build_context::TARGET;
use easy_ext::ext;
use fs_err as fs;
Expand Down Expand Up @@ -80,7 +80,7 @@ impl Command {
}
_ => {}
}
let (_test_project, cur_dir) = test_project(test_model).unwrap();
let (_test_project, cur_dir) = test_project(test_model);
let output =
self.current_dir(cur_dir).output().context("could not execute process").unwrap();
AssertOutput(Some(AssertOutputInner {
Expand Down Expand Up @@ -239,8 +239,9 @@ impl AssertOutput {
}
}

fn test_project(model: &str) -> Result<(tempfile::TempDir, PathBuf)> {
let tmpdir = tempfile::tempdir()?;
#[track_caller]
fn test_project(model: &str) -> (tempfile::TempDir, PathBuf) {
let tmpdir = tempfile::tempdir().unwrap();
let tmpdir_path = tmpdir.path();

let model_path;
Expand All @@ -255,30 +256,32 @@ fn test_project(model: &str) -> Result<(tempfile::TempDir, PathBuf)> {
workspace_root = tmpdir_path.to_path_buf();
}

for (file_name, from) in git_ls_files(&model_path, &[])? {
for (file_name, from) in git_ls_files(&model_path, &[]) {
let to = &tmpdir_path.join(file_name);
if !to.parent().unwrap().is_dir() {
fs::create_dir_all(to.parent().unwrap())?;
fs::create_dir_all(to.parent().unwrap()).unwrap();
}
fs::copy(from, to)?;
fs::copy(from, to).unwrap();
}

Ok((tmpdir, workspace_root))
(tmpdir, workspace_root)
}

fn git_ls_files(dir: &Path, filters: &[&str]) -> Result<Vec<(String, PathBuf)>> {
#[track_caller]
fn git_ls_files(dir: &Path, filters: &[&str]) -> Vec<(String, PathBuf)> {
let mut cmd = Command::new("git");
cmd.arg("ls-files").args(filters).current_dir(dir);
let output = cmd.output().with_context(|| format!("could not execute process `{cmd:?}`"))?;
if !output.status.success() {
bail!(
let output =
cmd.output().unwrap_or_else(|e| panic!("could not execute process `{cmd:?}`: {e}"));
assert!(
output.status.success(),
"process didn't exit successfully: `{cmd:?}`:\n\nSTDOUT:\n{0}\n{1}\n{0}\n\nSTDERR:\n{0}\n{2}\n{0}\n",
"-".repeat(60),
String::from_utf8_lossy(&output.stdout),
String::from_utf8_lossy(&output.stderr),
);
}
Ok(str::from_utf8(&output.stdout)?
str::from_utf8(&output.stdout)
.unwrap()
.lines()
.map(str::trim)
.filter_map(|f| {
Expand All @@ -291,5 +294,5 @@ fn git_ls_files(dir: &Path, filters: &[&str]) -> Result<Vec<(String, PathBuf)>>
}
Some((f.to_owned(), p))
})
.collect())
.collect()
}

0 comments on commit 04cc2f6

Please sign in to comment.