From ea0ef9b5007eee811de805c261775072691e6317 Mon Sep 17 00:00:00 2001 From: Nazmul Idris Date: Fri, 6 Dec 2024 08:07:54 -0600 Subject: [PATCH] [test_fixtures] Add temp_dir fixture - Easily create temp dirs for tests - Auto cleanup and delete temp dir when dropped (out of scope) Fixes #371 --- CHANGELOG.md | 135 +++++++++++++++++++++++++++++++++- Cargo.lock | 1 + test_fixtures/Cargo.toml | 3 + test_fixtures/src/lib.rs | 2 + test_fixtures/src/temp_dir.rs | 66 +++++++++++++++++ 5 files changed, 205 insertions(+), 2 deletions(-) create mode 100644 test_fixtures/src/temp_dir.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index ec8028f1..5a4b0f0b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,84 @@ - [global-config](#global-config) - [2024-12-04](#2024-12-04) +- [r3bl-cmdr](#r3bl-cmdr) + - [v_next-release-cmdr](#v_next-release-cmdr) + - [v0.0.16 2024-09-13](#v0016-2024-09-13) + - [v0.0.15 2024-09-12](#v0015-2024-09-12) + - [v0.0.14 2024-06-29](#v0014-2024-06-29) + - [v0.0.13 2024-05-20](#v0013-2024-05-20) + - [v0.0.12 2024-05-12](#v0012-2024-05-12) + - [v0.0.11 2024-01-14](#v0011-2024-01-14) + - [v0.0.10 2024-01-02](#v0010-2024-01-02) + - [v0.0.9 2023-12-31](#v009-2023-12-31) + - [v0.0.8 2023-12-22](#v008-2023-12-22) +- [r3bl_tuify](#r3bl_tuify) + - [v0.2.0 2024-10-21](#v020-2024-10-21) + - [v0.1.27 2024-09-12](#v0127-2024-09-12) + - [v0.1.26 2024-04-15](#v0126-2024-04-15) + - [v0.1.25 2024-01-14](#v0125-2024-01-14) + - [v0.1.24 2023-12-31](#v0124-2023-12-31) + - [v0.1.23 2023-12-22](#v0123-2023-12-22) + - [v0.1.22 2023-12-20](#v0122-2023-12-20) + - [v0.1.21 2023-10-21](#v0121-2023-10-21) + - [v0.1.20 2023-10-21](#v0120-2023-10-21) + - [v0.1.19 2023-10-17](#v0119-2023-10-17) + - [v0.1.18 2023-10-17](#v0118-2023-10-17) + - [v0.1.17 2023-10-14](#v0117-2023-10-14) +- [r3bl_tui](#r3bl_tui) + - [v0.6.0 2024-10-21](#v060-2024-10-21) + - [v0.5.9 2024-09-12](#v059-2024-09-12) + - [v0.5.8 2024-09-07](#v058-2024-09-07) + - [v0.5.7 2024-08-13](#v057-2024-08-13) + - [v0.5.6 2024-06-29](#v056-2024-06-29) + - [v0.5.5 2024-05-20](#v055-2024-05-20) + - [v0.5.4 2024-05-20](#v054-2024-05-20) + - [v0.5.3 2024-04-15](#v053-2024-04-15) + - [v0.5.2 2024-01-14](#v052-2024-01-14) + - [v0.5.1 2024-01-09](#v051-2024-01-09) + - [v0.5.0 2023-12-31](#v050-2023-12-31) + - [v0.4.0 2023-12-22](#v040-2023-12-22) + - [v0.3.10 2023-10-29](#v0310-2023-10-29) + - [v0.3.9 2023-10-29](#v039-2023-10-29) + - [v0.3.7 2023-10-21](#v037-2023-10-21) + - [v0.3.6 2023-10-17](#v036-2023-10-17) + - [v0.3.5 2023-10-14](#v035-2023-10-14) + - [v0.3.3 2023-04-20](#v033-2023-04-20) + - [v0.3.2 2023-03-06](#v032-2023-03-06) + - [v0.3.1 2023-03-06](#v031-2023-03-06) +- [r3bl_core](#r3bl_core) + - [v_next_release_r3bl_core](#v_next_release_r3bl_core) + - [v0.10.0 2024-10-20](#v0100-2024-10-20) +- [r3bl_analytics_schema](#r3bl_analytics_schema) + - [v0.0.2 2024-09-12](#v002-2024-09-12) + - [v0.0.1 2023-12-31](#v001-2023-12-31) +- [r3bl_macro](#r3bl_macro) + - [v0.10.0 2024-10-20](#v0100-2024-10-20) +- [r3bl_test_fixtures](#r3bl_test_fixtures) + - [v_next_release_r3bl_test_fixtures](#v_next_release_r3bl_test_fixtures) + - [v0.1.0 2024-10-21](#v010-2024-10-21) + - [v0.0.3 2024-09-12](#v003-2024-09-12) + - [v0.0.2 2024-07-13](#v002-2024-07-13) + - [v0.0.1 2024-07-12](#v001-2024-07-12) +- [r3bl_terminal_async](#r3bl_terminal_async) + - [v0.6.0 2024-10-21](#v060-2024-10-21) + - [v0.5.7 2024-09-12](#v057-2024-09-12) + - [v0.5.6 2024-08-13](#v056-2024-08-13) + - [v0.5.5 2024-07-13](#v055-2024-07-13) + - [v0.5.4 2024-07-12](#v054-2024-07-12) + - [v0.5.3 2024-05-22](#v053-2024-05-22) + - [v0.5.2 2020-05-06](#v052-2020-05-06) + - [v0.5.1 2024-04-28](#v051-2024-04-28) + - [v0.5.0 2024-04-22](#v050-2024-04-22) + - [v0.4.0 2024-04-21](#v040-2024-04-21) + - [v0.3.1 2024-04-17](#v031-2024-04-17) + - [v0.3.0 2024-04-15](#v030-2024-04-15) +- [r3bl_ansi_color](#r3bl_ansi_color) + - [v0.7.0 2024-10-18](#v070-2024-10-18) + + +- [global-config](#global-config) + - [-12-04](#-12-04) - [r3bl-cmdr](#r3bl-cmdr) - [next-release-cmdr](#next-release-cmdr) - [v0.0.16 2024-09-13](#v0016-2024-09-13) @@ -128,6 +206,50 @@ - [v0.9.11 2023-10-28](#v0911-2023-10-28) - [v0.9.10 2023-10-21](#v0910-2023-10-21) - [v0.9.9](#v099) + +12-22](#v098-2023-12-22) + - [v0.9.7 2023-10-21](#v097-2023-10-21) + - [v0.9.6 2023-10-17](#v096-2023-10-17) + - [v0.9.5 2023-10-14](#v095-2023-10-14) +- [r3bl_rs_utils_core](#r3bl_rs_utils_core) + - [Rename to r3bl_core](#rename-to-r3bl_core) + - [v0.9.16 2024-09-12](#v0916-2024-09-12) + - [v0.9.15 2024-09-07](#v0915-2024-09-07) + - [v0.9.14 2024-08-13](#v0914-2024-08-13) + - [v0.9.13 2024-04-15](#v0913-2024-04-15) + - [v0.9.12 2024-01-07](#v0912-2024-01-07) + - [v0.9.11 2024-01-02](#v0911-2024-01-02) + - [v0.9.10 2023-12-22](#v0910-2023-12-22) + - [v0.9.9 2023-10-21](#v099-2023-10-21) + - [v0.9.8 2023-10-21](#v098-2023-10-21) + - [v0.9.7 2023-10-17](#v097-2023-10-17) + - [v0.9.6 2023-10-17](#v096-2023-10-17) + - [v0.9.5 2023-10-14](#v095-2023-10-14) + - [v0.9.1 2023-03-06](#v091-2023-03-06) +- [r3bl_simple_logger](#r3bl_simple_logger) + - [Archived 2024-09-27](#archived-2024-09-27) + - [v0.1.4 2024-09-12](#v014-2024-09-12) + - [v0.1.3 2023-10-21](#v013-2023-10-21) + - [v0.1.2 2023-10-21](#v012-2023-10-21) + - [v0.1.1 2023-10-17](#v011-2023-10-17) + - [v0.1.0 2023-10-14](#v010-2023-10-14) +- [r3bl_redux](#r3bl_redux) + - [Archived 2024-09-29](#archived-2024-09-29) + - [v0.2.8 2024-09-12](#v028-2024-09-12) + - [v0.2.7 2024-09-07](#v027-2024-09-07) + - [v0.2.6 2023-10-21](#v026-2023-10-21) + - [v0.2.5 2023-10-17](#v025-2023-10-17) + - [v0.2.4 2023-10-14](#v024-2023-10-14) +- [r3bl_rs_utils](#r3bl_rs_utils) + - [Archived 2024-09-30](#archived-2024-09-30) + - [v0.9.16 2024-09-12](#v0916-2024-09-12) + - [v0.9.15 2023-12-22](#v0915-2023-12-22) + - [v0.9.14 2023-10-29](#v0914-2023-10-29) + - [v0.9.13 2023-10-29](#v0913-2023-10-29) + - [v0.9.12 2023-10-29](#v0912-2023-10-29) + - [v0.9.11 2023-10-28](#v0911-2023-10-28) + - [v0.9.10 2023-10-21](#v0910-2023-10-21) + - [v0.9.9](#v099) - [Renamed](#renamed) - [r3bl_rs_utils_macro](#r3bl_rs_utils_macro) - [r3bl_rs_utils_core](#r3bl_rs_utils_core) @@ -167,7 +289,7 @@ the following: ## `r3bl-cmdr` -### next-release-cmdr +### v_next-release-cmdr This is part of a total reorganization of the `r3bl-open-core` repo. This is a breaking change for almost every crate in the repo. This @@ -756,7 +878,7 @@ exhaustively tested and is able to handle many more corner cases. ## `r3bl_core` -### next_release_r3bl_core +### v_next_release_r3bl_core This release does not have any major changes. 1. Add a new declarative macro to effortlessly create global mutable thread safe @@ -905,6 +1027,15 @@ Deleted: ## `r3bl_test_fixtures` +### v_next_release_r3bl_test_fixtures + +This release adds a new fixture to make it easy to create temporary directories for tests. + +Added: + - Add a new fixture `temp_dir::create_temp_dir()` to make it easy to create temporary + directories for tests. Any temporary directories created are automatically cleaned up + after the test is done. + ### v0.1.0 (2024-10-21) This is part of a total reorganization of the `r3bl-open-core` repo. This is a breaking diff --git a/Cargo.lock b/Cargo.lock index af3e5214..efc877be 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1792,6 +1792,7 @@ name = "r3bl_test_fixtures" version = "0.1.0" dependencies = [ "async-stream", + "crossterm", "futures-core", "futures-util", "miette", diff --git a/test_fixtures/Cargo.toml b/test_fixtures/Cargo.toml index 816f64ff..f25fb8b1 100644 --- a/test_fixtures/Cargo.toml +++ b/test_fixtures/Cargo.toml @@ -45,3 +45,6 @@ tracing-subscriber = "0.3.18" tracing-appender = "0.2.3" tracing-core = "0.1.32" tokio-test = "0.4.4" + +# Colorful console output (for tests). +crossterm = { version = "0.28.1", features = ["event-stream"] } # EventStream diff --git a/test_fixtures/src/lib.rs b/test_fixtures/src/lib.rs index 8ebf75e2..55a69e01 100644 --- a/test_fixtures/src/lib.rs +++ b/test_fixtures/src/lib.rs @@ -197,8 +197,10 @@ pub mod input_device_fixtures; pub mod output_device_fixtures; pub mod tcp_stream_fixtures; +pub mod temp_dir; // Re-export. pub use input_device_fixtures::*; pub use output_device_fixtures::*; pub use tcp_stream_fixtures::*; +pub use temp_dir::*; diff --git a/test_fixtures/src/temp_dir.rs b/test_fixtures/src/temp_dir.rs new file mode 100644 index 00000000..c25eb447 --- /dev/null +++ b/test_fixtures/src/temp_dir.rs @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2024 R3BL LLC + * All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +use miette::IntoDiagnostic; +use r3bl_core::friendly_random_id; + +pub struct TempDir { + pub path: std::path::PathBuf, +} + +/// Create a temporary directory. The directory is automatically deleted when the +/// [TempDir] struct is dropped. +pub fn create_temp_dir() -> miette::Result { + let root = std::env::temp_dir(); + let new_temp_dir = root.join(friendly_random_id::generate_friendly_random_id()); + std::fs::create_dir(&new_temp_dir).into_diagnostic()?; + Ok(TempDir { path: new_temp_dir }) +} + +impl Drop for TempDir { + fn drop(&mut self) { std::fs::remove_dir_all(&self.path).unwrap(); } +} + +#[cfg(test)] +mod tests { + use crossterm::style::Stylize as _; + + use super::*; + + #[test] + fn test_temp_dir() { + let temp_dir = create_temp_dir().unwrap(); + println!( + "Temp dir: {}", + temp_dir.path.display().to_string().magenta() + ); + + assert!(temp_dir.path.exists()); + } + + #[test] + fn test_temp_dir_drop() { + let temp_dir = create_temp_dir().unwrap(); + + let copy_of_path = temp_dir.path.clone(); + println!("Temp dir: {}", copy_of_path.display().to_string().magenta()); + + drop(temp_dir); + + assert!(!copy_of_path.exists()); + } +}