Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove need to use tempfile #57

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .cargo/config.toml

This file was deleted.

9 changes: 2 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "steamlocate"
version = "2.0.0-beta.0"
version = "2.0.0-beta.1"
authors = ["William Venner <william@venner.io>"]
edition = "2018"
repository = "https://github.com/WilliamVenner/steamlocate-rs"
Expand All @@ -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

Expand All @@ -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 }
Expand All @@ -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"
2 changes: 0 additions & 2 deletions src/tests/legacy.rs
Original file line number Diff line number Diff line change
@@ -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},
Expand Down
60 changes: 31 additions & 29 deletions src/tests/temp.rs
Original file line number Diff line number Diff line change
@@ -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<TempDir, crate::tests::TestError> {
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<path::PathBuf>);

#[cfg(any(steamlocate_doctest, test))]
mod real {
pub struct TempDir(tempfile::TempDir);
impl TempDir {
pub fn new() -> Result<Self, TestError> {
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<Self, crate::tests::TestError> {
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(),
))
}