kyclark / command-line-rust

Code for Command-Line Rust (O'Reilly, 2024, ISBN 9781098109417)
https://learning.oreilly.com/library/view/command-line-rust/9781098109424/
MIT License
1.55k stars 252 forks source link

Test: Add serial_test crate to resolve issue with prallel tests #20

Open Forest0923 opened 6 months ago

Forest0923 commented 6 months ago

Overview

I fixed an occasional test failure case in the 07-findr test.

Error Log

% cargo test
    Finished test [unoptimized + debuginfo] target(s) in 0.02s
     Running unittests src/main.rs (target/debug/deps/findr-ac989d0767a0cedd)

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

     Running tests/cli.rs (target/debug/deps/cli-53ada2b900d3192b)

running 28 tests
test dies_bad_name ... ok
test path_a_b_d ... ok
test name_a ... ok
test path1 ... ok
test dies_bad_type ... ok
test path_a ... ok
test path_a_b ... ok
test path_d ... ok
test name_csv ... ok
test type_d_path_a_b ... ok
test path_g ... ok
test type_d_path_a_b_d ... ok
test name_txt_path_a_d ... ok
test type_d_path_a ... ok
test type_f ... ok
test name_csv_mp3 ... ok
test type_d_name_a ... ok
test type_d_path_d ... ok
test type_d ... FAILED
test type_f_l ... ok
test type_f_name_a ... ok
test type_f_path_d ... ok
test skips_bad_dir ... ok
test type_f_path_a_b ... ok
test type_f_path_a_b_d ... ok
test type_l ... ok
test type_f_path_a ... ok
test unreadable_dir ... ok

failures:

---- type_d stdout ----
thread 'type_d' panicked at tests/cli.rs:90:5:
assertion failed: `(left == right)`

Diff < left / right > :
 [
     "tests/inputs",
     "tests/inputs/a",
     "tests/inputs/a/b",
     "tests/inputs/a/b/c",
<    "tests/inputs/cant-touch-this",
     "tests/inputs/d",
     "tests/inputs/d/e",
     "tests/inputs/f",
 ]

note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

failures:
    type_d

test result: FAILED. 27 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

error: test failed, to rerun pass `--test cli`

Cause

The issue occurs when tests with --type d are executed at the moment when the cant-touch-this directory is created in unreadable_dir(), causing tests/inputs/cant-touch-this to be output.

Resolution

Modified test/cli.rs to use serial_test.

Changing the directory name could also resolve the issue, but while investigating whether specific tests could be executed serially, I found a suitable library, so I used that instead.