diff options
| -rw-r--r-- | Cargo.lock | 1 | ||||
| -rw-r--r-- | Cargo.toml | 3 | ||||
| -rw-r--r-- | src/tests/mod.rs | 48 |
3 files changed, 29 insertions, 23 deletions
@@ -1560,6 +1560,7 @@ dependencies = [ "num_cpus", "once_cell", "serde", + "tempfile", "thiserror", "tokio", "tracing", @@ -57,5 +57,8 @@ xdg = "2" # terminal coloring ansi_term = "0.12" +[dev-dependencies] +tempfile = "3" + [features] default = [] diff --git a/src/tests/mod.rs b/src/tests/mod.rs index 3e0f7af..ca25c1f 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -1,13 +1,9 @@ +use std::path::{Path, PathBuf}; + use crate::repo::*; use anyhow::Result; -use git2::Repository; - -thread_local! { - static TEST_DIR: std::path::PathBuf = std::env::current_exe() - .unwrap() - .join(std::path::Path::new("../../tmp")); -} +use tracing::debug; const REPOS: [&str; 5] = [ "repos/site/group/repo1", @@ -17,43 +13,49 @@ const REPOS: [&str; 5] = [ "repos/site/group/subgroup/subsubgroup/repo4", ]; -fn prepare_repos() -> Result<()> { +fn prepare_repos(path: &Path) -> Result<()> { REPOS.iter().try_for_each(|repo| { - let path = format!("{:?}/{}", TEST_DIR, repo); + let path = build_path(path, repo); std::fs::create_dir_all(&path)?; - let _repo = Repository::init(&path)?; + let _repo = gix::init(&path)?; Ok::<(), anyhow::Error>(()) }) } -fn clean_repos() -> Result<()> { - REPOS.iter().try_for_each(|repo| { - let path = format!("{:?}/{}", TEST_DIR, repo); - std::fs::remove_dir_all(&path)?; +fn build_path(path: &Path, repo: &str) -> PathBuf { + let mut path = path.to_owned(); + path.push(repo); + return path; +} - Ok::<(), anyhow::Error>(()) - }) +fn build_path_string(path: &Path, repo: &str) -> String { + let mut path = path.to_owned(); + path.push(repo); + return path.into_os_string().into_string().unwrap(); } #[tokio::test] async fn search_repos() -> Result<()> { tracing_subscriber::fmt::init(); - prepare_repos()?; + let test_dir = tempfile::tempdir()?; + let test_path = test_dir.path(); + debug!("test files placed in: {:?}", test_dir.path()); + + prepare_repos(test_path)?; let mut left: Vec<String> = vec![ - format!("{:?}/repos/site/group/repo1", TEST_DIR), - format!("{:?}/repos/site/group/repo2", TEST_DIR), - format!("{:?}/repos/site/group/subgroup/repo3", TEST_DIR), - format!("{:?}/repos/site/group/subgroup/subsubgroup/repo4", TEST_DIR), + build_path_string(test_path, "repos/site/group/repo1"), + build_path_string(test_path, "repos/site/group/repo2"), + build_path_string(test_path, "repos/site/group/subgroup/repo3"), + build_path_string(test_path, "repos/site/group/subgroup/subsubgroup/repo4"), ]; - let right = Repos::from_local(&format!("{:?}/repos", TEST_DIR), ""); + let right = Repos::from_local(&build_path_string(test_path, "repos"), ""); let mut right: Vec<&str> = right.iter().map(|x| x.0.as_str()).collect(); assert_eq!(left.sort(), right.sort_unstable()); - clean_repos()?; Ok(()) } |
