use std::path::{Path, PathBuf};
use tempfile::TempDir;
pub(crate) struct CrateTestCase {
pub name: &'static str,
#[allow(dead_code)]
path: PathBuf,
_temp_dir: TempDir,
crate_path: PathBuf,
}
impl CrateTestCase {
#[allow(clippy::misnamed_getters)] pub(crate) fn path(&self) -> &Path {
&self.crate_path
}
pub(crate) fn all() -> Vec<Self> {
vec![
Self::os_specific_deps(),
Self::proc_macro_dep(),
Self::simple_bin_no_deps(),
Self::simple_lib_no_deps(),
Self::single_crate_multiple_bins(),
Self::single_crate_multiple_bins_with_default(),
Self::stale_serde(),
Self::thicc(),
Self::timestamp(),
Self::workspace_all_libs(),
Self::workspace_multiple_bin_crates(),
]
}
pub(crate) fn os_specific_deps() -> Self {
Self::load("os-specific-deps")
}
pub(crate) fn proc_macro_dep() -> Self {
Self::load("proc-macro-dep")
}
pub(crate) fn simple_bin_no_deps() -> Self {
Self::load("simple-bin-no-deps")
}
pub(crate) fn simple_lib_no_deps() -> Self {
Self::load("simple-lib-no-deps")
}
pub(crate) fn single_crate_multiple_bins() -> Self {
Self::load("single-crate-multiple-bins")
}
pub(crate) fn single_crate_multiple_bins_with_default() -> Self {
Self::load("single-crate-multiple-bins-with-default")
}
pub(crate) fn stale_serde() -> Self {
Self::load("stale-serde")
}
pub(crate) fn thicc() -> Self {
Self::load("thicc")
}
pub(crate) fn timestamp() -> Self {
Self::load("timestamp")
}
pub(crate) fn workspace_all_libs() -> Self {
Self::load("workspace-all-libs")
}
pub(crate) fn workspace_multiple_bin_crates() -> Self {
Self::load("workspace-multiple-bin-crates")
}
fn load(name: &'static str) -> Self {
const TESTDATA_DIR: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/testdata/crates");
let path = Path::new(TESTDATA_DIR).join(name);
assert!(
path.exists() && path.is_dir(),
"Test case '{name}' doesn't exist: {}",
path.display()
);
let temp_dir = tempfile::tempdir().unwrap();
let crate_path = temp_dir.path().join(name);
crate::helpers::copy_source_tree(&path, &crate_path).unwrap();
let crate_path = std::fs::canonicalize(crate_path).unwrap();
Self {
name,
path,
_temp_dir: temp_dir,
crate_path,
}
}
}
pub(crate) struct ConfigTestCase {
path: PathBuf,
}
impl ConfigTestCase {
pub(crate) fn path(&self) -> &Path {
&self.path
}
fn load(name: &'static str, relative_path: &str, must_exist: bool) -> Self {
const TESTDATA_DIR: &str = concat!(env!("CARGO_MANIFEST_DIR"), "/testdata/configs");
let path = if relative_path.is_empty() {
Path::new(TESTDATA_DIR).to_path_buf()
} else {
Path::new(TESTDATA_DIR).join(relative_path)
};
if must_exist {
assert!(
path.exists(),
"Config test case '{}' doesn't exist: {}",
name,
path.display()
);
}
Self { path }
}
pub(crate) fn hierarchy_root() -> Self {
Self::load("hierarchy_root", "", true)
}
pub(crate) fn hierarchy_work() -> Self {
Self::load("hierarchy_work", "work", true)
}
pub(crate) fn hierarchy_project1() -> Self {
Self::load("hierarchy_project1", "work/project1", true)
}
pub(crate) fn hierarchy_project2() -> Self {
Self::load("hierarchy_project2", "work/project2", true)
}
pub(crate) fn explicit_non_standard_name() -> Self {
Self::load(
"explicit_non_standard_name",
"work/project1/not_called_cgx.toml",
true,
)
}
pub(crate) fn invalid_toml() -> Self {
Self::load("invalid_toml", "invalid_toml.toml", true)
}
pub(crate) fn invalid_options() -> Self {
Self::load("invalid_options", "invalid_config_options.toml", true)
}
pub(crate) fn nonexistent() -> Self {
Self::load("nonexistent", "does_not_exist.toml", false)
}
}