From 218d954ebcff521e35bb8077562ceb9b1ba3e48d Mon Sep 17 00:00:00 2001 From: Michael Diamond Date: Wed, 24 Jan 2024 01:06:21 -0800 Subject: [PATCH] Move brittle test into a standalone file. Fixes #40 --- src/lib.rs | 35 ----------------------------------- tests/cwd.rs | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 35 deletions(-) create mode 100644 tests/cwd.rs diff --git a/src/lib.rs b/src/lib.rs index 33de670..32c05ea 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1497,41 +1497,6 @@ mod bkt_tests { assert!(status.is_miss()); } - #[test] - fn cwd_and_working_dir_share_cache() { - let old_cwd = std::env::current_dir().unwrap(); - - let dir = TestDir::temp().create("wd", FileType::Dir); - let wd = dir.path("wd"); - let bkt = Bkt::create(dir.path("cache")).unwrap(); - // Note we haven't changed the cwd yet - use_cwd() shouldn't read it - let cmd = CommandDesc::new(["bash", "-c", "pwd; echo '.' > file"]).with_cwd(); - // Now the cwd is captured, but overwritten by with_working_dir() - let state = cmd.capture_state().unwrap().with_working_dir(&wd); - let (result, status) = bkt.retrieve(state, Duration::from_secs(10)).unwrap(); - assert_eq!(result.stdout_utf8(), format!("{}\n", wd.to_str().unwrap())); - assert_eq!(result.stderr_utf8(), ""); - assert_eq!(result.exit_code(), 0); - assert!(status.is_miss()); - - // now change the cwd and see it get captured lazily - std::env::set_current_dir(&wd).unwrap(); - let (result, status) = bkt.retrieve(&cmd, Duration::from_secs(10)).unwrap(); - assert_eq!(result.stdout_utf8(), format!("{}\n", wd.to_str().unwrap())); - assert_eq!(result.stderr_utf8(), ""); - assert_eq!(result.exit_code(), 0); - assert!(status.is_hit()); - - // and the file was only written to once, hence the cache was shared - assert_eq!(std::fs::read_to_string(wd.join("file")).unwrap(), ".\n"); - - // Restore the original cwd - // NB this could fail to be reached if the test fails, which could cause other confusing - // errors. An RAII pattern using Drop, similar to absl::Cleanup, would be nicer but I'm not - // aware of a standard pattern for this atm. - std::env::set_current_dir(old_cwd).unwrap(); - } - #[test] // TODO the JSON serializer doesn't support OsString keys, CommandState needs a custom // Serializer (for feature="debug", at least) - see https://stackoverflow.com/q/51276896 diff --git a/tests/cwd.rs b/tests/cwd.rs new file mode 100644 index 0000000..5bdaca2 --- /dev/null +++ b/tests/cwd.rs @@ -0,0 +1,35 @@ +mod cwd { + use std::time::Duration; + use test_dir::{TestDir, FileType, DirBuilder}; + use bkt::{Bkt, CacheStatus, CommandDesc}; + + // This test is pulled out from the unit tests into a separate file to avoid racing with other + // tests that depend on the cwd. See #40 for more. If we need to add more tests like this consider + // https://docs.rs/serial_test/ + #[test] + fn cwd_and_working_dir_share_cache() { + let dir = TestDir::temp().create("wd", FileType::Dir); + let wd = dir.path("wd"); + let bkt = Bkt::create(dir.path("cache")).unwrap(); + // Note we haven't changed the cwd yet - with_cwd() doesn't read it + let cmd = CommandDesc::new(["bash", "-c", "pwd; echo '.' > file"]).with_cwd(); + // The initial cwd is captured, but it's overwritten by with_working_dir() + let state = cmd.capture_state().unwrap().with_working_dir(&wd); + let (result, status) = bkt.retrieve(state, Duration::from_secs(10)).unwrap(); + assert_eq!(result.stdout_utf8(), format!("{}\n", wd.to_str().unwrap())); + assert_eq!(result.stderr_utf8(), ""); + assert_eq!(result.exit_code(), 0); + assert!(matches!(status, CacheStatus::Miss(_))); + + // now change the cwd and see it get captured lazily + std::env::set_current_dir(&wd).unwrap(); + let (result, status) = bkt.retrieve(&cmd, Duration::from_secs(10)).unwrap(); + assert_eq!(result.stdout_utf8(), format!("{}\n", wd.to_str().unwrap())); + assert_eq!(result.stderr_utf8(), ""); + assert_eq!(result.exit_code(), 0); + assert!(matches!(status, CacheStatus::Hit(_))); + + // and the file was only written to once, hence the cache was shared + assert_eq!(std::fs::read_to_string(wd.join("file")).unwrap(), ".\n"); + } +}