Skip to content

Commit

Permalink
Introduce constant dist dirs for testing
Browse files Browse the repository at this point in the history
* Rename setup() to test() as it only returns after the test has happened.
* Separate tracking the mutable dist servers from the new const dist server
* Permit starting a test with no dist server
* add with_scenario to construct a specific dist server on-demand, but also cache it for other tests
* Migrate cli-exact to use with_scenario
* Remove some stale no longer relvant end of test checks
* Cached dist servers are generated new per test run for now, and are
  unique per test binary. They leak at the end of the test, but aren't
  very large. Clean your target :).
* Simplify many tests that had unnecessary work in them (work that wasn't being tested)
  • Loading branch information
rbtcollins authored Feb 22, 2023
2 parents 5b36ca2 + 099b2d5 commit b9b3973
Show file tree
Hide file tree
Showing 15 changed files with 1,963 additions and 1,568 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ stack backtrace:
8: core::panicking::panic_fmt
at /rustc/de02101e6d949c4a9040211e9ce8c488a997497e\/src\libcore\panicking.rs:85
9: core::result::unwrap_failed
10: cli_v1::mock::clitools::setup
10: cli_v1::mock::clitools::test
11: alloc::boxed::{{impl}}::call_once<(),FnOnce<()>>
at /rustc/de02101e6d949c4a9040211e9ce8c488a997497e\src\liballoc\boxed.rs:746
12: panic_unwind::__rust_maybe_catch_panic
Expand Down
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ version = "0.3"

[dev-dependencies]
walkdir = "2"
once_cell = "1.17.0"
enum-map = "2.4.2"

[build-dependencies]
lazy_static = "1"
Expand Down
38 changes: 35 additions & 3 deletions src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,52 @@ impl Env for HashMap<String, String> {
}
}

/// Returns a tempdir for running tests in
pub fn test_dir() -> io::Result<tempfile::TempDir> {
/// The path to a dir for this test binaries state
fn exe_test_dir() -> io::Result<PathBuf> {
let current_exe_path = env::current_exe().unwrap();
let mut exe_dir = current_exe_path.parent().unwrap();
if exe_dir.ends_with("deps") {
exe_dir = exe_dir.parent().unwrap();
}
let test_dir = exe_dir.parent().unwrap().join("tests");
Ok(exe_dir.parent().unwrap().to_owned())
}

/// Returns a tempdir for running tests in
pub fn test_dir() -> io::Result<tempfile::TempDir> {
let exe_dir = exe_test_dir()?;
let test_dir = exe_dir.join("tests");
fs::create_dir_all(&test_dir).unwrap();
tempfile::Builder::new()
.prefix("running-test-")
.tempdir_in(test_dir)
}

/// Returns a directory for storing immutable distributions in
pub fn const_dist_dir() -> io::Result<tempfile::TempDir> {
// TODO: do something smart, like managing garbage collection or something.
let exe_dir = exe_test_dir()?;
let dists_dir = exe_dir.join("dists");
fs::create_dir_all(&dists_dir)?;
let current_exe = env::current_exe().unwrap();
let current_exe_name = current_exe.file_name().unwrap();
tempfile::Builder::new()
.prefix(&format!(
"dist-for-{}-",
Path::new(current_exe_name).display()
))
.tempdir_in(dists_dir)
}

/// Returns a tempdir for storing test-scoped distributions in
pub fn test_dist_dir() -> io::Result<tempfile::TempDir> {
let exe_dir = exe_test_dir()?;
let test_dir = exe_dir.join("tests");
fs::create_dir_all(&test_dir).unwrap();
tempfile::Builder::new()
.prefix("test-dist-dir-")
.tempdir_in(test_dir)
}

/// Makes persistent unique directory inside path.
///
/// Should only be used with path=a tempdir that will be cleaned up, as the
Expand Down
Loading

0 comments on commit b9b3973

Please sign in to comment.