From f9771a998340ff2dd74e195a36d1e3330038895d Mon Sep 17 00:00:00 2001 From: Luke Boswell Date: Mon, 15 Apr 2024 20:30:34 +1000 Subject: [PATCH] split test_utils --- Cargo.lock | 15 ++++++-- Cargo.toml | 1 + crates/compiler/load/Cargo.toml | 2 +- crates/compiler/load/tests/test_reporting.rs | 3 +- crates/compiler/load_internal/Cargo.toml | 2 +- .../compiler/load_internal/tests/test_load.rs | 3 +- crates/compiler/test_syntax/Cargo.toml | 1 + crates/compiler/test_syntax/tests/test_fmt.rs | 3 +- crates/test_utils/Cargo.toml | 1 - crates/test_utils/src/lib.rs | 37 ------------------- crates/test_utils_dir/Cargo.toml | 13 +++++++ crates/test_utils_dir/src/lib.rs | 36 ++++++++++++++++++ 12 files changed, 70 insertions(+), 47 deletions(-) create mode 100644 crates/test_utils_dir/Cargo.toml create mode 100644 crates/test_utils_dir/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 81d67ce88c..651f8c17e5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -40,9 +40,9 @@ dependencies = [ [[package]] name = "aligned" -version = "0.4.1" +version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "80a21b9440a626c7fc8573a9e3d3a06b75c7c97754c2949bc7857b90353ca655" +checksum = "377e4c0ba83e4431b10df45c1d4666f178ea9c552cac93e60c3a88bf32785923" dependencies = [ "as-slice", ] @@ -2743,7 +2743,7 @@ dependencies = [ "roc_solve", "roc_solve_problem", "roc_target", - "roc_test_utils", + "roc_test_utils_dir", "roc_types", "ven_pretty", ] @@ -2778,7 +2778,7 @@ dependencies = [ "roc_solve", "roc_solve_problem", "roc_target", - "roc_test_utils", + "roc_test_utils_dir", "roc_tracing", "roc_types", "roc_unify", @@ -3129,6 +3129,12 @@ name = "roc_test_utils" version = "0.0.1" dependencies = [ "pretty_assertions", +] + +[[package]] +name = "roc_test_utils_dir" +version = "0.0.1" +dependencies = [ "remove_dir_all 0.8.2", ] @@ -3867,6 +3873,7 @@ dependencies = [ "roc_parse", "roc_region", "roc_test_utils", + "roc_test_utils_dir", "walkdir", ] diff --git a/Cargo.toml b/Cargo.toml index e10289933f..d2dc077e8c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,6 +17,7 @@ members = [ "crates/repl_expect", "crates/roc_std", "crates/test_utils", + "crates/test_utils_dir", "crates/valgrind", "crates/tracing", "crates/utils/*", diff --git a/crates/compiler/load/Cargo.toml b/crates/compiler/load/Cargo.toml index b55099dabb..57a4131d18 100644 --- a/crates/compiler/load/Cargo.toml +++ b/crates/compiler/load/Cargo.toml @@ -40,7 +40,7 @@ roc_problem = { path = "../problem" } roc_region = { path = "../region" } roc_solve_problem = { path = "../solve_problem" } ven_pretty = { path = "../../vendor/pretty" } -roc_test_utils = { path = "../../test_utils" } +roc_test_utils_dir = { path = "../../test_utils_dir" } indoc.workspace = true insta.workspace = true diff --git a/crates/compiler/load/tests/test_reporting.rs b/crates/compiler/load/tests/test_reporting.rs index 9cd33334e9..3996258d3b 100644 --- a/crates/compiler/load/tests/test_reporting.rs +++ b/crates/compiler/load/tests/test_reporting.rs @@ -28,6 +28,7 @@ mod test_reporting { use roc_reporting::report::{RocDocAllocator, RocDocBuilder}; use roc_solve::FunctionKind; use roc_solve_problem::TypeError; + use roc_test_utils_dir::TmpDir; use roc_types::subs::Subs; use std::path::PathBuf; @@ -115,7 +116,7 @@ mod test_reporting { // We can't have all tests use "tmp" because tests run in parallel, // so append the test name to the tmp path. let tmp = format!("tmp/{subdir}"); - let dir = roc_test_utils::TmpDir::new(&tmp); + let dir = TmpDir::new(&tmp); let filename = PathBuf::from("Test.roc"); let file_path = dir.path().join(filename); diff --git a/crates/compiler/load_internal/Cargo.toml b/crates/compiler/load_internal/Cargo.toml index eec4cd8ed7..e5437947c8 100644 --- a/crates/compiler/load_internal/Cargo.toml +++ b/crates/compiler/load_internal/Cargo.toml @@ -40,7 +40,7 @@ parking_lot.workspace = true tempfile.workspace = true [dev-dependencies] -roc_test_utils = { path = "../../test_utils" } +roc_test_utils_dir = { path = "../../test_utils_dir" } indoc.workspace = true maplit.workspace = true diff --git a/crates/compiler/load_internal/tests/test_load.rs b/crates/compiler/load_internal/tests/test_load.rs index 80f1b45681..5454e697ce 100644 --- a/crates/compiler/load_internal/tests/test_load.rs +++ b/crates/compiler/load_internal/tests/test_load.rs @@ -32,6 +32,7 @@ use roc_reporting::report::{can_problem, DEFAULT_PALETTE}; use roc_reporting::report::{strip_colors, RenderTarget}; use roc_solve::FunctionKind; use roc_target::Target; +use roc_test_utils_dir::TmpDir; use roc_types::pretty_print::name_and_print_var; use roc_types::pretty_print::DebugPrint; use std::collections::HashMap; @@ -153,7 +154,7 @@ fn multiple_modules_help<'a>( // We can't have all tests use "tmp" because tests run in parallel, // so append the test name to the tmp path. let tmp = format!("tmp/{subdir}"); - let dir = roc_test_utils::TmpDir::new(&tmp); + let dir = TmpDir::new(&tmp); let app_module = files.pop().unwrap(); diff --git a/crates/compiler/test_syntax/Cargo.toml b/crates/compiler/test_syntax/Cargo.toml index cb22da5c1f..7c873e0969 100644 --- a/crates/compiler/test_syntax/Cargo.toml +++ b/crates/compiler/test_syntax/Cargo.toml @@ -18,6 +18,7 @@ roc_module = { path = "../module" } roc_parse = { path = "../parse" } roc_region = { path = "../region" } roc_test_utils = { path = "../../test_utils" } +roc_test_utils_dir = { path = "../../test_utils_dir" } [dev-dependencies] indoc.workspace = true diff --git a/crates/compiler/test_syntax/tests/test_fmt.rs b/crates/compiler/test_syntax/tests/test_fmt.rs index ff51b9c3fd..51479a76a3 100644 --- a/crates/compiler/test_syntax/tests/test_fmt.rs +++ b/crates/compiler/test_syntax/tests/test_fmt.rs @@ -11,7 +11,8 @@ mod test_fmt { use roc_parse::module::{self, module_defs}; use roc_parse::parser::Parser; use roc_parse::state::State; - use roc_test_utils::{assert_multiline_str_eq, workspace_root}; + use roc_test_utils::assert_multiline_str_eq; + use roc_test_utils_dir::workspace_root; use test_syntax::test_helpers::Input; fn check_formatting(expected: &'_ str) -> impl Fn(Input) + '_ { diff --git a/crates/test_utils/Cargo.toml b/crates/test_utils/Cargo.toml index 1d70df053d..5bcb8cd4c8 100644 --- a/crates/test_utils/Cargo.toml +++ b/crates/test_utils/Cargo.toml @@ -9,6 +9,5 @@ version.workspace = true [dependencies] pretty_assertions.workspace = true -remove_dir_all.workspace = true [dev-dependencies] diff --git a/crates/test_utils/src/lib.rs b/crates/test_utils/src/lib.rs index f0e99282c3..85683f14c8 100644 --- a/crates/test_utils/src/lib.rs +++ b/crates/test_utils/src/lib.rs @@ -1,5 +1,4 @@ //! Provides testing utility functions for use throughout the Rust code base. -use std::path::PathBuf; #[doc(hidden)] pub use pretty_assertions::assert_eq as _pretty_assert_eq; @@ -19,39 +18,3 @@ macro_rules! assert_multiline_str_eq { $crate::_pretty_assert_eq!($crate::DebugAsDisplay($a), $crate::DebugAsDisplay($b)) }; } - -/** - * Creates a temporary empty directory that gets deleted when this goes out of scope. - */ -pub struct TmpDir { - path: std::path::PathBuf, -} - -impl TmpDir { - pub fn new(dir: &str) -> TmpDir { - let path = std::path::Path::new(dir); - // ensure_empty_dir will fail if the dir doesn't already exist - std::fs::create_dir_all(path).unwrap(); - remove_dir_all::ensure_empty_dir(path).unwrap(); - - let mut pathbuf = std::path::PathBuf::new(); - pathbuf.push(path); - TmpDir { path: pathbuf } - } - - pub fn path(&self) -> &std::path::Path { - self.path.as_path() - } -} - -impl Drop for TmpDir { - fn drop(&mut self) { - // we "discard" the Result because there is no problem when a dir was already removed before we call remove_dir_all - let _ = remove_dir_all::remove_dir_all(&self.path); - } -} - -pub fn workspace_root() -> PathBuf { - let root = std::env::var("ROC_WORKSPACE_DIR").expect("Can't find the ROC_WORKSPACE_DIR variable expected to be set in .cargo/config.toml. Are you running tests outside of cargo?"); - PathBuf::from(root) -} diff --git a/crates/test_utils_dir/Cargo.toml b/crates/test_utils_dir/Cargo.toml new file mode 100644 index 0000000000..5a0f52e814 --- /dev/null +++ b/crates/test_utils_dir/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "roc_test_utils_dir" +description = "Utility functions used all over the code base." + +authors.workspace = true +edition.workspace = true +license.workspace = true +version.workspace = true + +[dependencies] +remove_dir_all.workspace = true + +[dev-dependencies] diff --git a/crates/test_utils_dir/src/lib.rs b/crates/test_utils_dir/src/lib.rs new file mode 100644 index 0000000000..e04024ce39 --- /dev/null +++ b/crates/test_utils_dir/src/lib.rs @@ -0,0 +1,36 @@ +/** + * Creates a temporary empty directory that gets deleted when this goes out of scope. + */ +use std::path::PathBuf; +pub struct TmpDir { + path: std::path::PathBuf, +} + +impl TmpDir { + pub fn new(dir: &str) -> TmpDir { + let path = std::path::Path::new(dir); + // ensure_empty_dir will fail if the dir doesn't already exist + std::fs::create_dir_all(path).unwrap(); + remove_dir_all::ensure_empty_dir(path).unwrap(); + + let mut pathbuf = std::path::PathBuf::new(); + pathbuf.push(path); + TmpDir { path: pathbuf } + } + + pub fn path(&self) -> &std::path::Path { + self.path.as_path() + } +} + +impl Drop for TmpDir { + fn drop(&mut self) { + // we "discard" the Result because there is no problem when a dir was already removed before we call remove_dir_all + let _ = remove_dir_all::remove_dir_all(&self.path); + } +} + +pub fn workspace_root() -> PathBuf { + let root = std::env::var("ROC_WORKSPACE_DIR").expect("Can't find the ROC_WORKSPACE_DIR variable expected to be set in .cargo/config.toml. Are you running tests outside of cargo?"); + PathBuf::from(root) +}