-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Port helper/test-advise.c to unit-tests/t-advise.c
In the recent codebase update (8bf6fbd (Merge branch 'js/doc-unit-tests', 2023-12-09)), a new unit testing framework was merged, providing a standardized approach for testing C code. Prior to this update, some unit tests relied on the test helper mechanism, lacking a dedicated unit testing framework. It's more natural to perform these unit tests using the new unit test framework. This commit migrates the unit tests for advise_if_enabled functionality from the legacy approach using the test-tool command `test-tool advise` in t/helper/test-advise.c to the new unit testing framework (t/unit-tests/test-lib.h). The migration involves refactoring the tests to utilize the testing macros provided by the framework (TEST() and check_*()). Mentored-by: Christian Couder <[email protected]> Signed-off-by: Achu Luma <[email protected]>
- Loading branch information
Showing
6 changed files
with
57 additions
and
58 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 was deleted.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
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,56 @@ | ||
#include "test-lib.h" | ||
#include "advice.h" | ||
#include "config.h" | ||
#include "setup.h" | ||
#include "strbuf.h" | ||
|
||
static const char expect_advice_msg[] = "hint: This is a piece of advice\n" | ||
"hint: Disable this message with \"git config advice.nestedTag false\"\n"; | ||
static const char advice_msg[] = "This is a piece of advice"; | ||
static const char out_file[] = "./output.txt"; | ||
|
||
|
||
static void check_advise_if_enabled(const char *argv, const char *conf_val, const char *expect) { | ||
FILE *file; | ||
struct strbuf actual = STRBUF_INIT; | ||
|
||
if (conf_val) | ||
git_default_advice_config("advice.nestedTag", conf_val); | ||
|
||
file = freopen(out_file, "w", stderr); | ||
if (!check(!!file)) { | ||
test_msg("Error opening file %s", out_file); | ||
return; | ||
} | ||
|
||
advise_if_enabled(ADVICE_NESTED_TAG, "%s", argv); | ||
fclose(file); | ||
|
||
if (!check(strbuf_read_file(&actual, out_file, 0) >= 0)) { | ||
test_msg("Error reading file %s", out_file); | ||
strbuf_release(&actual); | ||
return; | ||
} | ||
|
||
check_str(actual.buf, expect); | ||
strbuf_release(&actual); | ||
|
||
// Delete the output.txt file after the check is done | ||
if (!check(remove(out_file) == 0)) | ||
test_msg("Error deleting %s", out_file); | ||
} | ||
|
||
int cmd_main(int argc, const char **argv) { | ||
setenv("GIT_DISCOVERY_ACROSS_FILESYSTEM", "1", 1); | ||
setenv("TERM", "dumb", 1); | ||
setup_git_directory(); | ||
|
||
TEST(check_advise_if_enabled(advice_msg, NULL, expect_advice_msg), | ||
"advice should be printed when config variable is unset"); | ||
TEST(check_advise_if_enabled(advice_msg, "true", expect_advice_msg), | ||
"advice should be printed when config variable is set to true"); | ||
TEST(check_advise_if_enabled(advice_msg, "false", ""), | ||
"advice should not be printed when config variable is set to false"); | ||
|
||
return test_done(); | ||
} |