git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH RFC v2 4/4] unit test: add basic example and build rules

From
steadmon@google.com <steadmon@google.com>
Date
May 17, 2023, 23:56 UTC
Message-ID
<20230517-unit-tests-v2-v2-4-21b5b60f4b32@google.com>
In-Reply-To
<20230517-unit-tests-v2-v2-0-21b5b60f4b32@google.com>
Integrate a simple strbuf unit test with Git's Makefiles.

You can build and run the unit tests with `make unit-tests` (or just build them with `make build-unit-tests`). By default we use the basic test runner from the C-TAP project, but users who prefer prove as a test runner can set `DEFAULT_UNIT_TEST_TARGET=prove-unit-tests` instead.

We modify the `#include`s in the C TAP libraries so that we can build them without having to include the t/ directory in our include search path.

Signed-off-by: Calvin Wan <calvinwan@google.com>
Signed-off-by: Josh Steadmon <steadmon@google.com>
Change-Id: Ie61eafd2bd8f8dc5b30449af1e436889f91da3b7
---
 .gitignore      |  2 ++
 Makefile        | 24 +++++++++++++++++++++++-
 t/Makefile      | 10 ++++++++++
 t/strbuf-test.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 t/tap/basic.c   |  2 +-
 t/tap/basic.h   |  2 +-
 6 files changed, 91 insertions(+), 3 deletions(-)
diff --git a/.gitignore b/.gitignore
index e875c59054..464e301345 100644
--- a/.gitignore
+++ b/.gitignore
@@ -245,3 +245,5 @@ Release/
 /git.VC.db
 *.dSYM
 /contrib/buildsystems/out
+/t/runtests
+/t/unit-tests/
diff --git a/Makefile b/Makefile
index 8ee7c7e5a8..aa94e3ba45 100644
--- a/Makefile
+++ b/Makefile
@@ -661,6 +661,7 @@ BUILTIN_OBJS =
 BUILT_INS =
 COMPAT_CFLAGS =
 COMPAT_OBJS =
+CTAP_OBJS =
 XDIFF_OBJS =
 GENERATED_H =
 EXTRA_CPPFLAGS =
@@ -682,6 +683,8 @@ TEST_BUILTINS_OBJS =
 TEST_OBJS =
 TEST_PROGRAMS_NEED_X =
 THIRD_PARTY_SOURCES =
+UNIT_TEST_PROGRAMS =
+UNIT_TEST_DIR = t/unit-tests
 
 # Having this variable in your environment would break pipelines because
 # you cause "cd" to echo its destination to stdout.  It can also take
@@ -1318,6 +1321,10 @@ BUILTIN_OBJS += builtin/verify-tag.o
 BUILTIN_OBJS += builtin/worktree.o
 BUILTIN_OBJS += builtin/write-tree.o
 
+CTAP_OBJS += t/tap/basic.o
+UNIT_TEST_RUNNER = t/runtests
+UNIT_TEST_PROGRAMS += $(UNIT_TEST_DIR)/strbuf-test-t
+
 # THIRD_PARTY_SOURCES is a list of patterns compatible with the
 # $(filter) and $(filter-out) family of functions. They specify source
 # files which are taken from some third-party source where we want to be
@@ -2673,6 +2680,7 @@ OBJECTS += $(TEST_OBJS)
 OBJECTS += $(XDIFF_OBJS)
 OBJECTS += $(FUZZ_OBJS)
 OBJECTS += $(REFTABLE_OBJS) $(REFTABLE_TEST_OBJS)
+OBJECTS += $(CTAP_OBJS)
 
 ifndef NO_CURL
 	OBJECTS += http.o http-walker.o remote-curl.o
@@ -3654,7 +3662,7 @@ clean: profile-clean coverage-clean cocciclean
 	$(RM) $(OBJECTS)
 	$(RM) $(LIB_FILE) $(XDIFF_LIB) $(REFTABLE_LIB) $(REFTABLE_TEST_LIB)
 	$(RM) $(ALL_PROGRAMS) $(SCRIPT_LIB) $(BUILT_INS) $(OTHER_PROGRAMS)
-	$(RM) $(TEST_PROGRAMS)
+	$(RM) $(TEST_PROGRAMS) $(UNIT_TEST_RUNNER) $(UNIT_TEST_PROGRAMS)
 	$(RM) $(FUZZ_PROGRAMS)
 	$(RM) $(SP_OBJ)
 	$(RM) $(HCC)
@@ -3832,3 +3840,17 @@ $(FUZZ_PROGRAMS): all
 		$(XDIFF_OBJS) $(EXTLIBS) git.o $@.o $(LIB_FUZZING_ENGINE) -o $@
 
 fuzz-all: $(FUZZ_PROGRAMS)
+
+$(UNIT_TEST_DIR):
+	$(QUIET)mkdir $(UNIT_TEST_DIR)
+
+$(UNIT_TEST_PROGRAMS): $(UNIT_TEST_DIR) $(CTAP_OBJS) $(GITLIBS)
+	$(QUIET_CC)$(CC) -o $@ t/$(patsubst %-t,%,$(notdir $@)).c $(CTAP_OBJS) $(LIBS)
+
+$(UNIT_TEST_RUNNER): $(patsubst %,%.c,$(UNIT_TEST_RUNNER))
+	$(QUIET_CC)$(CC) -o $@ $^
+
+.PHONY: build-unit-tests unit-tests
+build-unit-tests: $(UNIT_TEST_PROGRAMS)
+unit-tests: $(UNIT_TEST_PROGRAMS) $(UNIT_TEST_RUNNER)
+	$(MAKE) -C t/ unit-tests
diff --git a/t/Makefile b/t/Makefile
index 3e00cdd801..9df1a4e34b 100644
--- a/t/Makefile
+++ b/t/Makefile
@@ -17,6 +17,7 @@ TAR ?= $(TAR)
 RM ?= rm -f
 PROVE ?= prove
 DEFAULT_TEST_TARGET ?= test
+DEFAULT_UNIT_TEST_TARGET ?= run-unit-tests
 TEST_LINT ?= test-lint
 
 ifdef TEST_OUTPUT_DIRECTORY
@@ -41,6 +42,7 @@ TPERF = $(sort $(wildcard perf/p[0-9][0-9][0-9][0-9]-*.sh))
 TINTEROP = $(sort $(wildcard interop/i[0-9][0-9][0-9][0-9]-*.sh))
 CHAINLINTTESTS = $(sort $(patsubst chainlint/%.test,%,$(wildcard chainlint/*.test)))
 CHAINLINT = '$(PERL_PATH_SQ)' chainlint.pl
+UNIT_TESTS = $(sort $(wildcard unit-tests/*))
 
 # `test-chainlint` (which is a dependency of `test-lint`, `test` and `prove`)
 # checks all tests in all scripts via a single invocation, so tell individual
@@ -65,6 +67,14 @@ prove: pre-clean check-chainlint $(TEST_LINT)
 $(T):
 	@echo "*** $@ ***"; '$(TEST_SHELL_PATH_SQ)' $@ $(GIT_TEST_OPTS)
 
+unit-tests: $(DEFAULT_UNIT_TEST_TARGET)
+
+run-unit-tests:
+	./runtests $(UNIT_TESTS)
+
+prove-unit-tests:
+	@echo "*** prove - unit tests ***"; $(PROVE) $(GIT_PROVE_OPTS) $(UNIT_TESTS)
+
 pre-clean:
 	$(RM) -r '$(TEST_RESULTS_DIRECTORY_SQ)'
 
diff --git a/t/strbuf-test.c b/t/strbuf-test.c
new file mode 100644
index 0000000000..8f8d4e11db
--- /dev/null
+++ b/t/strbuf-test.c
@@ -0,0 +1,54 @@
+#include "tap/basic.h"
+
+#include "../git-compat-util.h"
+#include "../strbuf.h"
+
+int strbuf_init_test()
+{
+	struct strbuf *buf = malloc(sizeof(void*));
+	strbuf_init(buf, 0);
+
+	if (buf->buf[0] != '\0')
+		return 0;
+	if (buf->alloc != 0)
+		return 0;
+	if (buf->len != 0)
+		return 0;
+	return 1;
+}
+
+int strbuf_init_test2() {
+	struct strbuf *buf = malloc(sizeof(void*));
+	strbuf_init(buf, 100);
+
+	if (buf->buf[0] != '\0')
+		return 0;
+	if (buf->alloc != 101)
+		return 0;
+	if (buf->len != 0)
+		return 0;
+	return 1;
+}
+
+
+int strbuf_grow_test() {
+	struct strbuf *buf = malloc(sizeof(void*));
+	strbuf_grow(buf, 100);
+
+	if (buf->buf[0] != '\0')
+		return 0;
+	if (buf->alloc != 101)
+		return 0;
+	if (buf->len != 0)
+		return 0;
+	return 1;
+}
+
+int main(void)
+{
+	plan(3);
+	ok(strbuf_init_test(), "strbuf_init initializes properly");
+	ok(strbuf_init_test2(), "strbuf_init with hint initializes properly");
+	ok(strbuf_grow_test(), "strbuf_grow grows properly");
+	return 0;
+}
diff --git a/t/tap/basic.c b/t/tap/basic.c
index 704282b9c1..37c2d6f082 100644
--- a/t/tap/basic.c
+++ b/t/tap/basic.c
@@ -52,7 +52,7 @@
 #include <sys/types.h>
 #include <unistd.h>
 
-#include <tap/basic.h>
+#include "basic.h"
 
 /* Windows provides mkdir and rmdir under different names. */
 #ifdef _WIN32
diff --git a/t/tap/basic.h b/t/tap/basic.h
index afea8cb210..a0c0ef2c87 100644
--- a/t/tap/basic.h
+++ b/t/tap/basic.h
@@ -36,7 +36,7 @@
 #include <stdarg.h> /* va_list */
 #include <stddef.h> /* size_t */
 #include <stdlib.h> /* free */
-#include <tap/macros.h>
+#include "macros.h"
 
 /*
  * Used for iterating through arrays.  ARRAY_SIZE returns the number of
-- 
2.40.1.606.ga4b1b128d6-goog
Previous: Josh SteadmonNext: Phillip Wood
Message 15 of 32 in “Add an external testing library for unit tests”
  1. 0/4 Add an external testing library for unit testssteadmon@google.com, May 17, 2023
  2. 1/4 common-main: split common_exit() into a new filesteadmon@google.com, May 17, 2023
  3. Junio C HamanoMay 18, 2023
  4. Splitting common-main (Was: Re: [PATCH RFC v2 1/4] common-main: split common_exit() into a new file)Josh Steadmon, Jul 14, 2023
  5. Junio C HamanoJul 15, 2023
  6. Jeff HostetlerAug 14, 2023
  7. 2/4 unit tests: Add a project plan documentsteadmon@google.com, May 17, 2023
  8. Phillip WoodMay 18, 2023
  9. Glen ChooMay 18, 2023
  10. Josh SteadmonMay 24, 2023
  11. Phillip WoodJun 1, 2023
  12. 3/4 Add C TAP harnesssteadmon@google.com, May 17, 2023
  13. Phillip WoodMay 18, 2023
  14. Josh SteadmonMay 18, 2023
  15. 4/4 unit test: add basic example and build rulessteadmon@google.com, May 17, 2023
  16. Phillip WoodMay 18, 2023
  17. 0/1 Add a project document for adding unit testsJosh Steadmon, Jun 9, 2023
  18. 1/1 unit tests: Add a project plan documentJosh Steadmon, Jun 9, 2023
  19. Junio C HamanoJun 13, 2023
  20. Josh SteadmonJun 30, 2023
  21. Linus ArverJun 29, 2023
  22. Josh SteadmonJun 29, 2023
  23. Linus ArverJun 30, 2023
  24. Glen ChooJul 6, 2023
  25. Junio C HamanoJul 6, 2023
  26. Glen ChooJul 6, 2023
  27. Josh SteadmonJun 30, 2023
  28. Junio C HamanoJun 29, 2023
  29. Linus ArverJun 30, 2023
  30. Phillip WoodJun 30, 2023
  31. K WanJun 30, 2023
  32. Josh SteadmonJun 30, 2023

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.