Skip to content

Commit

Permalink
Fix tests that fail with feature="debug" and add CI coverage.
Browse files Browse the repository at this point in the history
  • Loading branch information
dimo414 committed Apr 2, 2023
1 parent d341bd2 commit f06e6a4
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 8 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ jobs:
run: cargo doc --no-deps --document-private-items
- name: Tests
run: cargo test --verbose
- name: Tests (features=debug)
run: cargo test --verbose --features debug
- name: Benchmark
run: |
cargo build --release
Expand Down
33 changes: 33 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,5 @@ version = "1.0"

[dev-dependencies]
filetime = "0.2"
regex = "1.7"
test_dir = "0.2"
46 changes: 38 additions & 8 deletions tests/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ mod cli {
if !result.err.is_empty() { eprintln!("stderr:\n{}", result.err); }
} else {
// debug writes to stderr, so don't bother checking it in that mode
// TODO annotate any tests that aren't debug-compatible and add CI coverage of debug
assert_eq!(result.err, "");
}
assert_eq!(result.status, Some(0));
Expand Down Expand Up @@ -128,7 +127,12 @@ mod cli {

for _ in 1..3 {
let subsequent_result = run(bkt(dir.path("cache")).args(args));
assert_eq!(first_result, subsequent_result);
if cfg!(feature="debug") {
assert_eq!(first_result.status, subsequent_result.status);
assert_eq!(first_result.out, subsequent_result.out);
} else {
assert_eq!(first_result, subsequent_result);
}
}
}

Expand Down Expand Up @@ -220,11 +224,14 @@ mod cli {
let cmd = format!("{} false;", COUNT_INVOCATIONS);
let args = ["--discard-failures", "--", "bash", "-c", &cmd, "arg0", file.to_str().unwrap()];

assert_eq!(run(bkt(dir.path("cache")).args(args)),
CmdResult { out: "1".into(), err: "".into(), status: Some(1) });
let result = run(bkt(dir.path("cache")).args(args));
assert_eq!(result.out, "1");
assert_eq!(result.status, Some(1));

// Not cached
assert_eq!(run(bkt(dir.path("cache")).args(args)),
CmdResult { out: "2".into(), err: "".into(), status: Some(1) });
let result = run(bkt(dir.path("cache")).args(args));
assert_eq!(result.out, "2");
assert_eq!(result.status, Some(1));
}

#[test]
Expand Down Expand Up @@ -406,6 +413,7 @@ mod cli {
}

#[test]
#[cfg(not(feature="debug"))]
fn no_debug_output() {
let dir = TestDir::temp();
let args = ["--", "bash", "-c", "true"];
Expand All @@ -418,6 +426,27 @@ mod cli {
CmdResult { out: "".into(), err: "".into(), status: Some(0) });
}

#[test]
#[cfg(feature="debug")]
fn debug_output() {
fn starts_with_bkt(s: &str) -> bool { s.lines().all(|l| l.starts_with("bkt: ")) }

let miss_debug_re = regex::Regex::new(
"bkt: state: \nbkt: lookup .* not found\nbkt: cleanup data .*\nbkt: cleanup keys .*\nbkt: store data .*\nbkt: store key .*\n").unwrap();
let hit_debug_re = regex::Regex::new("bkt: lookup .* found\n").unwrap();

let dir = TestDir::temp();
let args = ["--", "bash", "-c", PRINT_ARGS, "arg0"];

let miss = run(bkt(dir.path("cache")).args(args));
assert!(starts_with_bkt(&miss.err), "{}", miss.err);
assert!(miss_debug_re.is_match(&miss.err), "{}", miss.err);

let hit = run(bkt(dir.path("cache")).args(args));
assert!(starts_with_bkt(&hit.err), "{}", hit.err);
assert!(hit_debug_re.is_match(&hit.err), "{}", hit.err);
}

#[test]
fn output_preserved() {
let dir = TestDir::temp();
Expand All @@ -437,6 +466,7 @@ mod cli {
}

#[test]
#[cfg(not(feature="debug"))]
fn sensitive_output() {
let dir = TestDir::temp();
let args = ["--", "bash", "-c", r"printf 'foo\0bar'; printf 'bar\0baz\n' >&2"];
Expand Down Expand Up @@ -515,10 +545,10 @@ mod cli {
let proc1 = bkt(dir.path("cache")).args(args).stdout(Stdio::piped()).stderr(Stdio::piped()).spawn().unwrap();
let proc2 = bkt(dir.path("cache")).args(args).stdout(Stdio::piped()).stderr(Stdio::piped()).spawn().unwrap();
let result1: CmdResult = proc1.wait_with_output().unwrap().into();
assert_eq!(result1.err, "");
if !cfg!(feature="debug") { assert_eq!(result1.err, ""); }
assert_eq!(result1.status, Some(0));
let result2: CmdResult = proc2.wait_with_output().unwrap().into();
assert_eq!(result2.err, "");
if !cfg!(feature="debug") { assert_eq!(result2.err, ""); }
assert_eq!(result2.status, Some(0));

assert_eq!(std::fs::read_to_string(&file).unwrap(), "..");
Expand Down

0 comments on commit f06e6a4

Please sign in to comment.