temp_testdir-0.2.3/.gitignore010066400017500001750000000001051354535025300144330ustar0000000000000000/.idea /target **/*.rs.bk this_test_root temp_testdir.iml Cargo.lock temp_testdir-0.2.3/CHANGELOG.md010066400017500001750000000001461354536022400142600ustar0000000000000000# Changelog ## [0.2.3] 2019-10-03 ### Fixed - Create an empty directory without remove it (See #1) temp_testdir-0.2.3/Cargo.toml.orig010066400017500001750000000007501354535746200153510ustar0000000000000000[package] name = "temp_testdir" version = "0.2.3" authors = ["Michele d'Amico "] homepage = "https://github.com/la10736/temp_testdir" description = """ Little crate to use temp directory in crate. You can chose if delete it after use or not to debugging purpose. """ repository = "https://github.com/la10736/temp_testdir.git" license = "MIT/Apache-2.0" keywords = ["test", "temp"] categories = ["development-tools::testing"] readme = "README.md" [dependencies] temp_testdir-0.2.3/Cargo.toml0000644000000017620000000000000116040ustar00# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies # # If you believe there's an error in this file please file an # issue against the rust-lang/cargo repository. If you're # editing this file be aware that the upstream Cargo.toml # will likely look very different (and much more reasonable) [package] name = "temp_testdir" version = "0.2.3" authors = ["Michele d'Amico "] description = "Little crate to use temp directory in crate. You can chose\nif delete it after use or not to debugging purpose.\n" homepage = "https://github.com/la10736/temp_testdir" readme = "README.md" keywords = ["test", "temp"] categories = ["development-tools::testing"] license = "MIT/Apache-2.0" repository = "https://github.com/la10736/temp_testdir.git" [dependencies] temp_testdir-0.2.3/README.md010066400017500001750000000025041354535025300137270ustar0000000000000000# Working with temporary directories in Rust test A zero dependencies crate to deal with temporary directories in tests. To use it add ``` [dev-dependencies] temp_testdir = "0.2" ``` ## How to use ```rust #[test] fn should_delete_temp_dir() { let temp = TempDir::default(); // You can use `temp` as a `Path` let mut file_path = PathBuf::from(temp.as_ref()); file_path.push("hello.txt"); let mut f = File::create(file_path.clone()).unwrap(); f.write_all("Hello World!".as_bytes()); my_app.process(&file_path); // Temp dir will be deleted at the end of the test } ``` If you need to not delete the dir when test is done you can use ```rust let temp = TempDir::default().permanent(); ``` ## Where the dirs are All dirs will be in your system standard temp dir follow by `rstest.` where `nr` is the lowest integer that can be used to crate it. You can change this behaviour by two envirorment variables: - `RSTEST_TEMP_DIR_ROOT`: root of all temp dir (default system temp dir) - `RSTEST_TEMP_DIR_ROOT_NAME`: prefix dir name (default system `rstest`) ## License Licensed under either of * Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0) * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT) at your option. temp_testdir-0.2.3/src/lib.rs010066400017500001750000000113741354536001600143560ustar0000000000000000use std::path::PathBuf; use std::path::Path; use std::ffi::OsStr; use std::ops::Deref; use std::ffi::OsString; /// Create a dir that will be removed. pub struct TempDir { path: PathBuf, destroy: bool, } impl Default for TempDir { fn default() -> Self { Self::new(Self::default_path(), true) } } impl Deref for TempDir { type Target = Path; fn deref(&self) -> &Self::Target { &self.path } } impl AsRef for TempDir { fn as_ref(&self) -> &Path { self.path.as_path() } } pub static RSTEST_TEMP_DIR_ROOT_DEFAULT: &'static str = "rstest"; pub static ENV_RSTEST_TEMP_DIR_ROOT_NAME: &'static str = "RSTEST_TEMP_DIR_ROOT_NAME"; pub static ENV_RSTEST_TEMP_DIR_ROOT: &'static str = "RSTEST_TEMP_DIR_ROOT"; impl TempDir { /// Prevent dir delete pub fn permanent(mut self) -> Self { self.destroy = false; self } /// New Temp dir. pub fn new>(path: P, destroy: bool) -> Self { let mut path = PathBuf::from(path.as_ref()); Self::create_root(&path); while std::fs::create_dir(&path).is_err() { let val = { path.extension().unwrap_or(OsStr::new("")) .to_str() .and_then(|v| v.parse::().ok()) .unwrap_or(0) + 1 }; path.set_extension(val.to_string()); } TempDir { path, destroy } } fn create_root(path: &Path) { if let Some(parent) = path.parent() { std::fs::create_dir_all(&parent).expect("Should create the parent dir"); } } fn default_path() -> PathBuf { let mut path = root(); path.push(root_name()); path } } fn rm>(path: P) { let _ = std::fs::remove_dir_all(path.as_ref()); } impl Drop for TempDir { fn drop(&mut self) { if self.destroy { rm(&self.path); } } } fn root_name() -> OsString { std::env::var_os(ENV_RSTEST_TEMP_DIR_ROOT_NAME) .unwrap_or(OsString::from(RSTEST_TEMP_DIR_ROOT_DEFAULT)) } fn root() -> PathBuf { std::env::var_os(ENV_RSTEST_TEMP_DIR_ROOT) .map(|p| PathBuf::from(p)) .unwrap_or(std::env::temp_dir()) } #[cfg(test)] mod test { use super::*; use std::fs::File; #[test] fn default_tempdir_should_create_a_directory() { let temp = TempDir::default(); assert!(temp.as_ref().is_dir()) } #[test] fn default_tempdir_should_destroy_directory_after_go_out_of_scope() { let path= { let temp = TempDir::default(); temp.as_ref().to_owned() }; assert!(!path.exists()) } #[test] fn tempdir_permanent_should_do_not_remove_dir() { let path; { let temp = TempDir::default().permanent(); path = temp.as_ref().to_owned() } assert!(path.is_dir()); rm(&path) } #[test] fn two_temp_dir_should_have_different_path() { let t1 = TempDir::default(); let t2 = TempDir::default(); assert_ne!(t1.as_ref(), t2.as_ref()); } #[test] fn default_temp_should_destroy_also_content() { let path; { let temp = TempDir::default(); path = temp.as_ref().to_owned(); File::create(temp.join("somefile")).expect("Should create dir"); } assert!(!path.exists()); } #[derive(Default)] struct EnvState(std::collections::HashSet); impl EnvState { pub fn add(&mut self, key: &str, value: &str) { std::env::set_var(key, value); self.0.insert(String::from(key)); } } impl Drop for EnvState { fn drop(&mut self) { for k in &self.0 { std::env::remove_var(k); } } } #[test] fn should_resolve_rstest_temp_dir_root_name_in_env() { let mut env = EnvState::default(); let new_root = "other_rstest_root"; env.add(ENV_RSTEST_TEMP_DIR_ROOT_NAME, new_root); let last = OsString::from(TempDir::default().components().last().unwrap().as_os_str()); assert!(last.into_string().unwrap().starts_with(new_root)); } #[test] fn should_resolve_root_in_env() { let mut env = EnvState::default(); let new_root = "this_test_root"; env.add(ENV_RSTEST_TEMP_DIR_ROOT, new_root); let first = OsString::from(TempDir::default().components().nth(0).unwrap().as_os_str()); assert_eq!(first.into_string().unwrap(), new_root); rm(new_root); } #[test] fn should_not_leave_a_dangling_empty_directory() { let root = "__should_be_destroy__"; TempDir::new(root, true); assert!(!Path::new(root).exists()) } } temp_testdir-0.2.3/tests/integration.rs010066400017500001750000000020511354535025300165000ustar0000000000000000extern crate temp_testdir; use temp_testdir::TempDir; use std::path::PathBuf; use std::fs::File; use std::io::Write; use std::io::Read; #[test] fn should_delete_temp_dir() { let mut file_path; { let temp = TempDir::default(); file_path = PathBuf::from(temp.as_ref()); file_path.push("hello.txt"); let mut f = File::create(file_path.clone()).unwrap(); f.write_all("Hello World!".as_bytes()).unwrap(); } // Should be deleted assert!(!file_path.is_file()); assert!(!file_path.parent().unwrap().is_dir()); } #[test] fn should_not_delete_temp_dir() { let mut file_path; { let temp = TempDir::default().permanent(); file_path = PathBuf::from(temp.as_ref()); file_path.push("hello.txt"); let mut f = File::create(file_path.clone()).unwrap(); f.write_all("Hello World!".as_bytes()).unwrap(); } let mut content = String::new(); File::open(file_path).unwrap().read_to_string(&mut content).unwrap(); assert_eq!("Hello World!", &content); } temp_testdir-0.2.3/.cargo_vcs_info.json0000644000000001120000000000000135720ustar00{ "git": { "sha1": "643283eb30b882dccf5f0d3a3a27150fe6272970" } }