Skip to content

Commit 7042ff0

Browse files
committed
Ensure that the assertion failure message of a fatal test failure is output even if there are preceding non-fatal errors in the test.
There was a bug in which, if a test had a mix of fatal and non-fatal assertions, and a non-fatal assertion failed, then the test assertion failure message of a subsequent fatal assertion failure would not be output. This did not affect the overall test result but would be confusing and misleading by not including all failure messages. This change fixes the bug and introduces an integration test to protect against it.
1 parent f8e2adc commit 7042ff0

File tree

5 files changed

+65
-10
lines changed

5 files changed

+65
-10
lines changed

googletest/src/internal/test_outcome.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,23 +62,23 @@ impl TestOutcome {
6262
///
6363
/// **For internal use only. API stablility is not guaranteed!**
6464
#[doc(hidden)]
65-
pub fn close_current_test_outcome<E: Display>(result: Result<(), E>) -> Result<(), ()> {
65+
pub fn close_current_test_outcome<E: Display>(inner_result: Result<(), E>) -> Result<(), ()> {
6666
TestOutcome::with_current_test_outcome(|mut outcome| {
67-
let result = match &*outcome {
68-
Some(TestOutcome::Success) => match result {
67+
let outer_result = match &*outcome {
68+
Some(TestOutcome::Success) => match inner_result {
6969
Ok(()) => Ok(()),
70-
Err(f) => {
71-
print!("{}", f);
72-
Err(())
73-
}
70+
Err(_) => Err(()),
7471
},
7572
Some(TestOutcome::Failure) => Err(()),
7673
None => {
7774
panic!("No test context found. This indicates a bug in GoogleTest.")
7875
}
7976
};
77+
if let Err(fatal_assertion_failure) = inner_result {
78+
println!("{fatal_assertion_failure}");
79+
}
8080
*outcome = None;
81-
result
81+
outer_result
8282
})
8383
}
8484

integration_tests/Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,11 @@ name = "failure_due_to_returned_error"
9494
path = "src/failure_due_to_returned_error.rs"
9595
test = false
9696

97+
[[bin]]
98+
name = "fatal_and_non_fatal_failure"
99+
path = "src/fatal_and_non_fatal_failure.rs"
100+
test = false
101+
97102
[[bin]]
98103
name = "first_failure_aborts"
99104
path = "src/first_failure_aborts.rs"
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright 2022 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
fn main() {}
16+
17+
#[cfg(test)]
18+
mod tests {
19+
use googletest::prelude::*;
20+
21+
#[googletest::test]
22+
fn fatal_and_non_fatal_failure() -> Result<()> {
23+
let value = 2;
24+
verify_that!(value, eq(3)).and_log_failure();
25+
verify_that!(value, eq(4))?;
26+
Ok(())
27+
}
28+
}

integration_tests/src/integration_tests.rs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,8 @@ mod tests {
126126
}
127127

128128
#[test]
129-
fn should_output_second_failure_message_on_second_assertion_failure_with_expect_that()
130-
-> Result<()> {
129+
fn should_output_second_failure_message_on_second_assertion_failure_with_expect_that(
130+
) -> Result<()> {
131131
let output = run_external_process_in_tests_directory("two_expect_that_failures")?;
132132

133133
verify_that!(
@@ -220,6 +220,27 @@ mod tests {
220220
)
221221
}
222222

223+
#[test]
224+
fn should_log_fatal_and_non_fatal_errors_to_stdout() -> Result<()> {
225+
let output = run_external_process_in_tests_directory("fatal_and_non_fatal_failure")?;
226+
227+
verify_that!(
228+
output,
229+
all!(
230+
contains_substring(indoc! {"
231+
Expected: is equal to 3
232+
Actual: 2,
233+
which isn't equal to 3
234+
"}),
235+
contains_substring(indoc! {"
236+
Expected: is equal to 4
237+
Actual: 2,
238+
which isn't equal to 4
239+
"})
240+
)
241+
)
242+
}
243+
223244
#[test]
224245
fn should_abort_after_first_failure() -> Result<()> {
225246
let output = run_external_process_in_tests_directory("first_failure_aborts")?;

run_integration_tests.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ INTEGRATION_TEST_BINARIES=(
3535
"failure_due_to_fail_macro_with_empty_message"
3636
"failure_due_to_fail_macro_with_format_arguments"
3737
"failure_due_to_returned_error"
38+
"fatal_and_non_fatal_failure"
3839
"first_failure_aborts"
3940
"google_test_with_rstest"
4041
"non_fatal_failure_in_subroutine"

0 commit comments

Comments
 (0)