Skip to content

Commit

Permalink
Move brittle test into a standalone file. Fixes #40
Browse files Browse the repository at this point in the history
  • Loading branch information
dimo414 committed Jan 24, 2024
1 parent 59aa082 commit 218d954
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 35 deletions.
35 changes: 0 additions & 35 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 35 additions & 0 deletions tests/cwd.rs
Original file line number Diff line number Diff line change
@@ -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");
}
}

0 comments on commit 218d954

Please sign in to comment.