From d6d9c0a0020383c5bb011640310b2b9adb8736cc Mon Sep 17 00:00:00 2001 From: Taiki Endo Date: Tue, 10 Oct 2023 21:05:46 +0900 Subject: [PATCH] tests: Remove dependency on ignore --- .github/workflows/ci.yml | 2 +- Cargo.toml | 1 - tests/auxiliary/mod.rs | 44 ++++++++++++++++++++++++++++++---------- 3 files changed, 34 insertions(+), 13 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1188cb1e..43d33bea 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -117,7 +117,7 @@ jobs: with: persist-credentials: false - name: Install Rust - run: apk --no-cache add bash cargo + run: apk --no-cache add bash cargo git shell: sh - run: cargo test --workspace --all-features - run: | diff --git a/Cargo.toml b/Cargo.toml index 77e3e3e5..0b0b3d91 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -41,7 +41,6 @@ toml_edit = "0.20" build-context = "0.1" easy-ext = "1" fs-err = "2" -ignore = "0.4" tempfile = "3" [profile.release] diff --git a/tests/auxiliary/mod.rs b/tests/auxiliary/mod.rs index 389c67af..af2af2d4 100644 --- a/tests/auxiliary/mod.rs +++ b/tests/auxiliary/mod.rs @@ -5,10 +5,11 @@ use std::{ ffi::OsStr, path::{Path, PathBuf}, process::{Command, ExitStatus}, + str, sync::OnceLock, }; -use anyhow::{Context as _, Result}; +use anyhow::{bail, Context as _, Result}; pub use build_context::TARGET; use easy_ext::ext; use fs_err as fs; @@ -240,18 +241,39 @@ fn test_project(model: &str) -> Result<(TempDir, PathBuf)> { workspace_root = tmpdir_path.to_path_buf(); } - for entry in ignore::WalkBuilder::new(&model_path).hidden(false).build().filter_map(Result::ok) - { - let path = entry.path(); - let tmp_path = &tmpdir_path.join(path.strip_prefix(&model_path)?); - if !tmp_path.exists() { - if path.is_dir() { - fs::create_dir_all(tmp_path)?; - } else { - fs::copy(path, tmp_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::copy(from, to)?; } Ok((tmpdir, workspace_root)) } + +fn git_ls_files(dir: &Path, filters: &[&str]) -> Result> { + 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()) +}