From 319c85d5a753ac0cb62dc77660373dea6d8ff375 Mon Sep 17 00:00:00 2001 From: Cosmic Horror Date: Sat, 16 Dec 2023 14:59:17 -0700 Subject: [PATCH 1/2] Remove need to use `tempfile` --- .cargo/config.toml | 2 -- Cargo.toml | 7 +----- src/tests/legacy.rs | 2 -- src/tests/temp.rs | 60 +++++++++++++++++++++++---------------------- 4 files changed, 32 insertions(+), 39 deletions(-) delete mode 100644 .cargo/config.toml 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..810f2dd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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(), + )) +} From 8326b9de22fcb53309b225aef2d83edd9b0a6e66 Mon Sep 17 00:00:00 2001 From: Cosmic Horror Date: Sat, 16 Dec 2023 15:06:44 -0700 Subject: [PATCH 2/2] Bump version to v2.0.0-beta.1 --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 810f2dd..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"