From 3e8061a6184233059b9d0ef2a7dc6f697d1beaeb Mon Sep 17 00:00:00 2001 From: Andrea Date: Wed, 27 Aug 2025 08:36:27 +0200 Subject: [PATCH] chore: update tests --- Makefile | 32 ++++++++++++++++++++++++++------ README.md | 8 ++++++++ compile_flags.txt | 5 +++++ tests/.gitkeep | 0 tests/test_main.c | 7 +++++++ 5 files changed, 46 insertions(+), 6 deletions(-) create mode 100644 compile_flags.txt delete mode 100644 tests/.gitkeep create mode 100644 tests/test_main.c diff --git a/Makefile b/Makefile index 89b28d3..8215e07 100644 --- a/Makefile +++ b/Makefile @@ -1,17 +1,25 @@ CC := gcc -CFLAGS := -Wall -Wextra -Iheaders +CFLAGS := -Wall -Wextra -Iinclude -Icommon -std=c23 PROG_NAME := program SRC_DIR := src BIN_DIR := bin -INC_DIR := include +TEST_DIR := tests TARGET := $(BIN_DIR)/$(PROG_NAME) SRCS := $(wildcard $(SRC_DIR)/*.c) OBJS := $(SRCS:$(SRC_DIR)/%.c=$(BIN_DIR)/%.o) -all: $(TARGET) +TEST_SRCS := $(wildcard $(TEST_DIR)/*.c) +TEST_OBJS := $(TEST_SRCS:$(TEST_DIR)/%.c=$(BIN_DIR)/%.test.o) +TEST_BIN := $(BIN_DIR)/tests + +all: compile_flags.txt $(TARGET) + +compile_flags.txt: Makefile + @echo "Generating compile_flags.txt from CFLAGS..." + @echo $(CFLAGS) | tr ' ' '\n' > compile_flags.txt $(TARGET): $(OBJS) | $(BIN_DIR) $(CC) $(OBJS) -o $@ @@ -20,7 +28,19 @@ $(TARGET): $(OBJS) | $(BIN_DIR) $(BIN_DIR)/%.o: $(SRC_DIR)/%.c | $(BIN_DIR) $(CC) $(CFLAGS) -c $< -o $@ -clean: - rm -rf $(BIN_DIR) +test: $(TEST_BIN) + @echo "Running tests..." + @$(TEST_BIN) -.PHONY: all clean +$(TEST_BIN): $(TEST_OBJS) $(filter-out $(BIN_DIR)/main.o,$(OBJS)) | $(BIN_DIR) + $(CC) $^ -o $@ + +$(BIN_DIR)/%.test.o: $(TEST_DIR)/%.c | $(BIN_DIR) + $(CC) $(CFLAGS) -c $< -o $@ + +clean: + rm -f $(BIN_DIR)/*.o + rm -f $(TEST_BIN) $(BIN_DIR)/*test + rm -f $(TARGET) + +.PHONY: all clean test diff --git a/README.md b/README.md index 4a2eb2b..32dbade 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,11 @@ # C Project A little template for C projects. + +## Testing + +In order to test the project run: + +```sh +make test +``` diff --git a/compile_flags.txt b/compile_flags.txt new file mode 100644 index 0000000..3cd72e5 --- /dev/null +++ b/compile_flags.txt @@ -0,0 +1,5 @@ +-Wall +-Wextra +-Iinclude +-Icommon +-std=c23 diff --git a/tests/.gitkeep b/tests/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/tests/test_main.c b/tests/test_main.c new file mode 100644 index 0000000..dc0f8ef --- /dev/null +++ b/tests/test_main.c @@ -0,0 +1,7 @@ +#include + +int main(void) { + assert(true); + + return 0; +} \ No newline at end of file