diff --git a/.cargo/config.toml b/.cargo/config.toml deleted file mode 100644 index 8d3cf49..0000000 --- a/.cargo/config.toml +++ /dev/null @@ -1,2 +0,0 @@ -[build] -rustflags = ["--cfg", "steamlocate_doctest"] diff --git a/Cargo.toml b/Cargo.toml index 5e4a826..1fb5ef9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "steamlocate" -version = "2.0.0-beta.0" +version = "2.0.0-beta.1" authors = ["William Venner "] edition = "2018" repository = "https://github.com/WilliamVenner/steamlocate-rs" @@ -12,7 +12,7 @@ categories = ["os", "hardware-support", "filesystem", "accessibility"] [package.metadata.docs.rs] all-features = true -rustdoc-args = ["--cfg", "docsrs", "--cfg", "steamlocate_doctest"] +rustdoc-args = ["--cfg", "docsrs"] # TODO: test more features in CI @@ -26,10 +26,6 @@ keyvalues-parser = "0.2" keyvalues-serde = "0.2" serde = { version = "1.0", features = ["derive"] } -# Custom cfg used to enable a dependency only needed for doctests -[target."cfg(steamlocate_doctest)".dependencies] -tempfile = "3.8.1" - # Platform-specific dependencies used for locating the steam dir [target."cfg(target_os=\"windows\")".dependencies] locate_backend = { package = "winreg", version = "0.51", optional = true } @@ -38,4 +34,3 @@ locate_backend = { package = "dirs", version = "5", optional = true } [dev-dependencies] insta = { version = "1.34.0", features = ["ron"] } -tempfile = "3.8.1" diff --git a/src/tests/legacy.rs b/src/tests/legacy.rs index ab344c2..480a96b 100644 --- a/src/tests/legacy.rs +++ b/src/tests/legacy.rs @@ -1,5 +1,3 @@ -// TODO: steamlocate_tempfile cfg for docs. Otherwise rely on a env var to get passed in - use crate::{ tests::{ helpers::{expect_test_env, SampleApp}, diff --git a/src/tests/temp.rs b/src/tests/temp.rs index 3973a85..bb6197b 100644 --- a/src/tests/temp.rs +++ b/src/tests/temp.rs @@ -1,38 +1,40 @@ -#[cfg(not(any(steamlocate_doctest, test)))] -pub use fake::TempDir; -#[cfg(any(steamlocate_doctest, test))] -pub use real::TempDir; +//! `TempDir` at home +//! +//! I want to use temporary directories in doctests, but that works against your public API. +//! Luckily all the functionality we need is very easy to replicate -#[cfg(not(any(steamlocate_doctest, test)))] -mod fake { - pub struct TempDir; +use std::{collections, env, fs, hash, path}; - impl TempDir { - // TODO: I think that this can be added to a `.cargo` config file for this project? - pub fn new() -> Result { - unimplemented!("Pass RUSTFLAGS='--cfg steamlocate_doctest' to run doctests"); - } +use crate::tests::TestError; - pub fn path(&self) -> &std::path::Path { - panic!(); - } - } -} +#[derive(Debug)] +pub struct TempDir(Option); -#[cfg(any(steamlocate_doctest, test))] -mod real { - pub struct TempDir(tempfile::TempDir); +impl TempDir { + pub fn new() -> Result { + let mut dir = env::temp_dir(); + let random_name = format!("steamlocate-test-{:x}", random_seed()); + dir.push(random_name); + // TODO: could retry on failure + fs::create_dir_all(&dir)?; + Ok(Self(Some(dir))) + } - impl TempDir { - pub fn new() -> Result { - let temp_dir = tempfile::Builder::new() - .prefix("steamlocate-test-") - .tempdir()?; - Ok(Self(temp_dir)) - } + pub fn path(&self) -> &path::Path { + self.0.as_deref().unwrap() + } +} - pub fn path(&self) -> &std::path::Path { - self.0.path() +impl Drop for TempDir { + fn drop(&mut self) { + if let Some(path) = self.0.take() { + let _ = fs::remove_dir_all(path); } } } + +fn random_seed() -> u64 { + hash::Hasher::finish(&hash::BuildHasher::build_hasher( + &collections::hash_map::RandomState::new(), + )) +}