-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(#513) Refactor into set_log_streams_for_tests() to manage unit-test …
…outputs. This commit refactors existing chunks of code that exists in different unit- test sources to manage output file handles to a common function, now defined in new file unit/unit_tests_common.c ; set_log_streams_for_tests() Tests that check error-raising behaviour will now need to call set_log_streams_for_neg_tests(), to manage output streams. Minor correction to TEST_DB_NAME; change it to conform to the r.e. defined in .gitignore to suppress listing this in 'git status' output.
- Loading branch information
Showing
9 changed files
with
107 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Copyright 2022 VMware, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
/* | ||
* ----------------------------------------------------------------------------- | ||
* unit_tests_common.c | ||
* | ||
* Common functions shared across unit test sources. | ||
* ----------------------------------------------------------------------------- | ||
*/ | ||
#include "ctest.h" // This is required for all test-case files. | ||
#include "unit_tests.h" | ||
|
||
/* | ||
* Setup function to manage manage output log streams from most (unit) tests. | ||
* By default, there will be no output. Info / error messages are only printed | ||
* when the caller sets the VERBOSE env var to opt-in. | ||
*/ | ||
void | ||
set_log_streams_for_tests() | ||
{ | ||
if (Ctest_verbose) { | ||
platform_set_log_streams(stdout, stderr); | ||
} | ||
} | ||
|
||
/* | ||
* Setup function is provided to manage output log streams from (unit) tests. | ||
* that are negative tests, checking error conditions / messages. | ||
* | ||
* Some unit tests exercise error cases, so even when everything succeeds, they | ||
* generate lots of "error" messages. By default, that would go to stderr, which | ||
* would pollute test output. This function sets up output file handles such | ||
* that those expected error messages are only printed when the caller sets the | ||
* VERBOSE env var to opt-in. By default, execution from unit-tests will be | ||
* silent, redirecting output handles to /dev/null. | ||
* | ||
* Returns: Output file handles (out/error) if caller has requested for them. | ||
*/ | ||
void | ||
set_log_streams_for_neg_tests(platform_log_handle **log_stdout, | ||
platform_log_handle **log_stderr) | ||
{ | ||
// Here we ensure those expected error messages are only printed | ||
// when the caller sets the VERBOSE env var to opt-in. | ||
if (Ctest_verbose) { | ||
platform_set_log_streams(stdout, stderr); | ||
if (log_stdout) { | ||
*log_stdout = stdout; | ||
} | ||
if (log_stderr) { | ||
*log_stderr = stderr; | ||
} | ||
CTEST_LOG_INFO("\nVerbose mode on. This test exercises an error case, " | ||
"so on sucess it " | ||
"will print a message that appears to be an error.\n"); | ||
} else { | ||
FILE *dev_null = fopen("/dev/null", "w"); | ||
ASSERT_NOT_NULL(dev_null); | ||
platform_set_log_streams(dev_null, dev_null); | ||
if (log_stdout) { | ||
*log_stdout = dev_null; | ||
} | ||
if (log_stderr) { | ||
*log_stderr = dev_null; | ||
} | ||
} | ||
} |