Skip to content

Commit bedf351

Browse files
committed
fix integration tests on CI
1 parent 2613669 commit bedf351

File tree

2 files changed

+100
-40
lines changed

2 files changed

+100
-40
lines changed

bear/tests/cases/exit_codes.rs

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,21 @@ use std::time::Instant;
1212

1313
#[test]
1414
fn test_exit_code_for_empty_arguments() {
15+
// Executing Bear with no arguments should return a non-zero exit code,
16+
// and print usage information.
1517
Command::cargo_bin(BEAR_BIN)
1618
.unwrap()
19+
.env("RUST_LOG", "debug")
20+
.env("RUST_BACKTRACE", "1")
1721
.assert()
1822
.failure()
1923
.stderr(predicate::str::contains("Usage: bear"));
2024
}
2125

2226
#[test]
2327
fn test_exit_code_for_help() {
28+
// Executing help and subcommand help should always has zero exit code,
29+
// and print out usage information
2430
Command::cargo_bin(BEAR_BIN)
2531
.unwrap()
2632
.arg("--help")
@@ -46,70 +52,93 @@ fn test_exit_code_for_help() {
4652
}
4753

4854
#[test]
49-
fn test_exit_code_for_invalid_command() {
55+
fn test_exit_code_for_invalid_argument() {
56+
// Executing Bear with an invalid argument should always has non-zero exit code,
57+
// and print relevant information about the reason about the failure.
5058
Command::cargo_bin(BEAR_BIN)
5159
.unwrap()
52-
.arg("invalid_command")
60+
.arg("invalid_argument")
61+
.env("RUST_LOG", "debug")
62+
.env("RUST_BACKTRACE", "1")
5363
.assert()
5464
.failure()
5565
.stderr(predicate::str::contains("error: unexpected argument"));
5666
}
5767

5868
#[test]
69+
#[cfg(target_os = "linux")] // FIXME: compiler wrappers does not work yet
70+
fn test_exit_code_for_non_existing_command() {
71+
// Executing a non-existing command should always has non-zero exit code,
72+
// and print relevant information about the reason about the failure.
73+
Command::cargo_bin(BEAR_BIN)
74+
.unwrap()
75+
.args(["--", "invalid_command"])
76+
.env("RUST_LOG", "debug")
77+
.env("RUST_BACKTRACE", "1")
78+
.assert()
79+
.failure()
80+
.stderr(predicate::str::contains(
81+
"Failed to execute the build command",
82+
));
83+
}
84+
85+
#[test]
86+
#[cfg(target_os = "linux")] // FIXME: compiler wrappers does not work yet
5987
#[cfg(has_executable_true)]
6088
fn test_exit_code_for_true() {
89+
// When the executed command returns successfully, Bear exit code should be zero.
6190
Command::cargo_bin(BEAR_BIN)
6291
.unwrap()
6392
.arg("--")
6493
.arg(TRUE_PATH)
94+
.env("RUST_LOG", "debug")
95+
.env("RUST_BACKTRACE", "1")
6596
.assert()
6697
.success();
6798
}
6899

69100
#[test]
70101
#[cfg(has_executable_false)]
71102
fn test_exit_code_for_false() {
103+
// When the executed command returns unsuccessfully, Bear exit code should be non-zero.
72104
Command::cargo_bin(BEAR_BIN)
73105
.unwrap()
74106
.arg("--")
75107
.arg(FALSE_PATH)
108+
.env("RUST_LOG", "debug")
109+
.env("RUST_BACKTRACE", "1")
76110
.assert()
77111
.failure();
78112
}
79113

80114
#[test]
81115
#[cfg(has_executable_sleep)]
82116
fn test_exit_code_when_signaled() {
83-
// Prepare the command
117+
// When the bear process is signaled, Bear exit code should be non-zero.
118+
// And should terminate the child process and return immediately.
119+
84120
let mut cmd = StdCommand::new(cargo_bin(BEAR_BIN));
85121
cmd.arg("--")
86122
.arg(SLEEP_PATH)
87123
.arg("10")
124+
.env("RUST_LOG", "debug")
125+
.env("RUST_BACKTRACE", "1")
88126
.stdout(Stdio::null())
89127
.stderr(Stdio::null());
90128

91-
// Start the command
92129
let mut child = cmd.spawn().expect("Failed to spawn command");
93130

94131
// Wait 200ms to ensure that the sleep command was also executed
95132
std::thread::sleep(std::time::Duration::from_millis(200));
96133

97-
// Send a termination signal to the process and record the time
98134
let kill_time = Instant::now();
99135
child.kill().expect("Failed to signal the process");
100-
101-
// Wait for the process to complete and record the time
102136
let status = child.wait().expect("Failed to wait for command");
103137
let wait_end = Instant::now();
104138

105-
// Assert that the exit status is not zero
106139
assert!(!status.success());
107-
108-
// Assert that the process stopped right after the kill call (less than 1 second)
109-
let time_diff = wait_end.duration_since(kill_time);
110140
assert!(
111-
time_diff.as_secs() < 1,
112-
"Process took too long to terminate: {:?}",
113-
time_diff
141+
wait_end.duration_since(kill_time).as_secs() < 1,
142+
"Process took too long to terminate.",
114143
);
115144
}

bear/tests/cases/test_fixtures.rs

Lines changed: 57 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,96 +6,127 @@ use assert_cmd::Command;
66

77
#[cfg(has_executable_true)]
88
#[test]
9-
fn test_true_help() {
9+
fn test_true_works() {
1010
Command::new(TRUE_PATH).assert().success();
1111
}
1212

1313
#[cfg(has_executable_false)]
1414
#[test]
15-
fn test_false_help() {
15+
fn test_false_works() {
1616
Command::new(FALSE_PATH).assert().failure();
1717
}
1818

1919
#[cfg(has_executable_echo)]
2020
#[test]
21-
fn test_echo_help() {
22-
Command::new(ECHO_PATH).arg("--help").assert().success();
21+
fn test_echo_works() {
22+
// Testing echo with as executing to print out a value.
23+
// Testing with `--help` or `--version` is not a portable test.
24+
Command::new(ECHO_PATH).arg("testing").assert().success();
2325
}
2426

2527
#[cfg(has_executable_sleep)]
2628
#[test]
27-
fn test_sleep_help() {
28-
Command::new(SLEEP_PATH).arg("--help").assert().success();
29+
fn test_sleep_works() {
30+
// Testing sleep with a zero second value.
31+
// Testing with `--help` or `--version` is not a portable test.
32+
Command::new(SLEEP_PATH).arg("0").assert().success();
2933
}
3034

3135
#[cfg(has_executable_shell)]
3236
#[test]
33-
fn test_shell_help() {
34-
Command::new(SHELL_PATH).arg("--help").assert().success();
37+
fn test_shell_works() {
38+
// Testing shell to execute a built it function.
39+
// Testing with `--help` or `--version` is not a portable test. Debian `dash` is failing with those arguments.
40+
Command::new(SHELL_PATH)
41+
.args(["-c", "true"])
42+
.assert()
43+
.success();
3544
}
3645

3746
#[cfg(has_executable_make)]
3847
#[test]
39-
fn test_make_help() {
40-
Command::new(MAKE_PATH).arg("--help").assert().success();
48+
fn test_make_works() {
49+
// Testing make by querying its version.
50+
Command::new(MAKE_PATH).arg("--version").assert().success();
4151
}
4252

4353
#[cfg(has_executable_compiler_c)]
4454
#[test]
45-
fn test_compiler_c_help() {
55+
fn test_compiler_c_works() {
56+
// Testing compiler by querying its version.
4657
Command::new(COMPILER_C_PATH)
47-
.arg("--help")
58+
.arg("--version")
4859
.assert()
4960
.success();
5061
}
5162

5263
#[cfg(has_executable_compiler_cxx)]
5364
#[test]
54-
fn test_compiler_cxx_help() {
65+
fn test_compiler_cxx_works() {
66+
// Testing compiler by querying its version.
5567
Command::new(COMPILER_CXX_PATH)
56-
.arg("--help")
68+
.arg("--version")
5769
.assert()
5870
.success();
5971
}
6072

6173
#[cfg(has_executable_compiler_fortran)]
6274
#[test]
63-
fn test_compiler_fortran_help() {
75+
fn test_compiler_fortran_works() {
76+
// Testing compiler by querying its version.
6477
Command::new(COMPILER_FORTRAN_PATH)
65-
.arg("--help")
78+
.arg("--version")
6679
.assert()
6780
.success();
6881
}
6982

7083
#[cfg(has_executable_compiler_cuda)]
7184
#[test]
72-
fn test_compiler_cuda_help() {
85+
fn test_compiler_cuda_works() {
86+
// Testing compiler by querying its version.
7387
Command::new(COMPILER_CUDA_PATH)
74-
.arg("--help")
88+
.arg("--version")
7589
.assert()
7690
.success();
7791
}
7892

93+
#[cfg(not(target_os = "macos"))]
7994
#[cfg(has_executable_libtool)]
8095
#[test]
81-
fn test_libtool_help() {
82-
Command::new(LIBTOOL_PATH).arg("--help").assert().success();
96+
fn test_libtool_works() {
97+
// Testing libtool by querying its version.
98+
// FIXME: libtool does not have version or help parameters on macOS
99+
Command::new(LIBTOOL_PATH)
100+
.arg("--version")
101+
.assert()
102+
.success();
83103
}
84104

85105
#[cfg(has_executable_fakeroot)]
86106
#[test]
87-
fn test_fakeroot_help() {
88-
Command::new(FAKEROOT_PATH).arg("--help").assert().success();
107+
fn test_fakeroot_works() {
108+
// Testing fakeroot by querying its version.
109+
Command::new(FAKEROOT_PATH)
110+
.arg("--version")
111+
.assert()
112+
.success();
89113
}
90114

91115
#[cfg(has_executable_valgrind)]
92116
#[test]
93-
fn test_valgrind_help() {
94-
Command::new(VALGRIND_PATH).arg("--help").assert().success();
117+
fn test_valgrind_works() {
118+
// Testing valgrind by querying its version.
119+
Command::new(VALGRIND_PATH)
120+
.arg("--version")
121+
.assert()
122+
.success();
95123
}
96124

125+
#[cfg(not(target_os = "macos"))]
97126
#[cfg(has_executable_ar)]
98127
#[test]
99-
fn test_ar_help() {
100-
Command::new(AR_PATH).arg("--help").assert().success();
128+
fn test_ar_works() {
129+
// Testing ar by querying its version.
130+
// FIXME: ar does not have version or help parameters on macOS
131+
Command::new(AR_PATH).arg("--version").assert().success();
101132
}

0 commit comments

Comments
 (0)