Skip to content

Commit

Permalink
tests: Remove dependency on ignore
Browse files Browse the repository at this point in the history
  • Loading branch information
taiki-e committed Oct 10, 2023
1 parent 66a05d5 commit d87e60b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 12 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ walkdir = "2.2.3"

[dev-dependencies]
easy-ext = "1"
ignore = "0.4"
rustversion = "1"
tempfile = "3"

Expand Down
42 changes: 33 additions & 9 deletions tests/auxiliary/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ use std::{
mem,
path::{Path, PathBuf},
process::{Command, ExitStatus, Stdio},
str,
sync::OnceLock,
};

use anyhow::{Context as _, Result};
use anyhow::{bail, Context as _, Result};
use camino::Utf8Path;
use easy_ext::ext;
use fs_err as fs;
Expand Down Expand Up @@ -116,20 +117,43 @@ pub fn test_project(model: &str) -> Result<TempDir> {
let workspace_root = tmpdir.path();
let model_path = fixtures_path().join("crates").join(model);

for entry in ignore::WalkBuilder::new(&model_path).hidden(false).build().filter_map(Result::ok)
{
let from = entry.path();
let to = &workspace_root.join(from.strip_prefix(&model_path)?);
if from.is_dir() {
fs::create_dir_all(to)?;
} else {
fs::copy(from, to)?;
for (file_name, from) in git_ls_files(model_path.as_std_path(), &[])? {
let to = &workspace_root.join(file_name);
if !to.parent().unwrap().is_dir() {
fs::create_dir_all(to.parent().unwrap())?;
}
fs::copy(from, to)?;
}

Ok(tmpdir)
}

fn git_ls_files(dir: &Path, filters: &[&str]) -> Result<Vec<(String, PathBuf)>> {
let output = Command::new("git")
.arg("ls-files")
.args(filters)
.current_dir(dir)
.output()
.with_context(|| format!("failed to run `git ls-files {filters:?}`"))?;
if !output.status.success() {
bail!("failed to run `git ls-files {filters:?}`");
}
Ok(str::from_utf8(&output.stdout)?
.lines()
.map(str::trim)
.filter_map(|f| {
if f.is_empty() {
return None;
}
let p = dir.join(f);
if !p.exists() {
return None;
}
Some((f.to_owned(), p))
})
.collect())
}

pub fn perturb_one_header(workspace_root: &Path) -> Result<Option<PathBuf>> {
let target_dir = workspace_root.join("target").join("llvm-cov-target");
let path = fs::read_dir(target_dir)?.filter_map(Result::ok).find_map(|entry| {
Expand Down
4 changes: 2 additions & 2 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#![warn(rust_2018_idioms)]
#![cfg(not(miri))] // Miri doesn't support file with non-default mode: https://github.com/rust-lang/miri/pull/2720

mod auxiliary;

use anyhow::Context as _;
use auxiliary::{
assert_output, cargo_llvm_cov, fixtures_path, normalize_output, perturb_one_header,
Expand All @@ -12,8 +14,6 @@ use camino::Utf8Path;
use fs_err as fs;
use tempfile::tempdir;

mod auxiliary;

const SUBCOMMANDS: &[&str] = &["", "run", "report", "clean", "show-env", "nextest"];

fn test_set() -> Vec<(&'static str, &'static [&'static str])> {
Expand Down

0 comments on commit d87e60b

Please sign in to comment.