Skip to content

Commit

Permalink
Port helper/test-advise.c to unit-tests/t-advise.c
Browse files Browse the repository at this point in the history
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
achluma committed Jan 10, 2024
1 parent a54a84b commit dba26db
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 58 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -783,7 +783,6 @@ X =

PROGRAMS += $(patsubst %.o,git-%$X,$(PROGRAM_OBJS))

TEST_BUILTINS_OBJS += test-advise.o
TEST_BUILTINS_OBJS += test-bitmap.o
TEST_BUILTINS_OBJS += test-bloom.o
TEST_BUILTINS_OBJS += test-bundle-uri.o
Expand Down Expand Up @@ -1342,6 +1341,7 @@ THIRD_PARTY_SOURCES += sha1dc/%
UNIT_TEST_PROGRAMS += t-basic
UNIT_TEST_PROGRAMS += t-mem-pool
UNIT_TEST_PROGRAMS += t-strbuf
UNIT_TEST_PROGRAMS += t-advise
UNIT_TEST_PROGS = $(patsubst %,$(UNIT_TEST_BIN)/%$X,$(UNIT_TEST_PROGRAMS))
UNIT_TEST_OBJS = $(patsubst %,$(UNIT_TEST_DIR)/%.o,$(UNIT_TEST_PROGRAMS))
UNIT_TEST_OBJS += $(UNIT_TEST_DIR)/test-lib.o
Expand Down
22 changes: 0 additions & 22 deletions t/helper/test-advise.c

This file was deleted.

1 change: 0 additions & 1 deletion t/helper/test-tool.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ static const char * const test_tool_usage[] = {
};

static struct test_cmd cmds[] = {
{ "advise", cmd__advise_if_enabled },
{ "bitmap", cmd__bitmap },
{ "bloom", cmd__bloom },
{ "bundle-uri", cmd__bundle_uri },
Expand Down
1 change: 0 additions & 1 deletion t/helper/test-tool.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

#include "git-compat-util.h"

int cmd__advise_if_enabled(int argc, const char **argv);
int cmd__bitmap(int argc, const char **argv);
int cmd__bloom(int argc, const char **argv);
int cmd__bundle_uri(int argc, const char **argv);
Expand Down
33 changes: 0 additions & 33 deletions t/t0018-advice.sh

This file was deleted.

56 changes: 56 additions & 0 deletions t/unit-tests/t-advise.c
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();
}

0 comments on commit dba26db

Please sign in to comment.