From 253f55dd366af31a8102500b9d447b159f8d9931 Mon Sep 17 00:00:00 2001
From: nekobit <nekobit@noemail.net>
Date: Thu, 28 Jul 2022 03:36:33 +0000
Subject: [PATCH] Improved unit testing

FossilOrigin-Name: 786b4080abc83f076704b33ec884236a3733fef9a5a276d7df54fa35d228015b
---
 .fossil-settings/ignore-glob               |  3 +-
 Makefile                                   | 18 ++++++++--
 test/test.pl                               | 39 ++++++++++++++++++++++
 test/unit/main.c                           | 33 ------------------
 test/unit/{string_test.c => t00_string.c}  |  7 +++-
 test/unit/{mime_multipart.c => t01_mime.c} |  5 ++-
 6 files changed, 66 insertions(+), 39 deletions(-)
 create mode 100755 test/test.pl
 delete mode 100644 test/unit/main.c
 rename test/unit/{string_test.c => t00_string.c} (84%)
 rename test/unit/{mime_multipart.c => t01_mime.c} (96%)

diff --git a/.fossil-settings/ignore-glob b/.fossil-settings/ignore-glob
index 6f37e23..97604c9 100644
--- a/.fossil-settings/ignore-glob
+++ b/.fossil-settings/ignore-glob
@@ -11,4 +11,5 @@ config.h
 treebird
 test/tests
 scripts/*.o
-templates/*.ctt
\ No newline at end of file
+templates/*.ctt
+test/unit/*.bin
\ No newline at end of file
diff --git a/Makefile b/Makefile
index 785e0da..0689bca 100644
--- a/Makefile
+++ b/Makefile
@@ -15,9 +15,14 @@ PAGES_CMP = $(patsubst %.tmpl,%.ctmpl,$(PAGES))
 PAGES_C = $(patsubst %.tmpl, %.c,$(PAGES))
 PAGES_C_OBJ = $(patsubst %.c,%.o,$(PAGES_C))
 TMPLS_C = $(patsubst %.tt,%.ctt,$(TMPLS))
+TEST_DIR = test/unit
+TESTS = $(wildcard $(TEST_DIR)/t*.c)
+UNIT_TESTS = $(patsubst %.c,%.bin,$(TESTS))
 DIST = dist/
 PREFIX ?= /usr/local
 TARGET = treebird
+# For tests
+OBJ_NO_MAIN = $(filter-out src/main.o,$(OBJ))
 
 MASTODONT_URL = https://fossil.nekobit.net/mastodont-c
 
@@ -69,8 +74,9 @@ install: $(TARGET)
 	install -d $(PREFIX)/share/treebird/
 	cp -r dist/ $(PREFIX)/share/treebird/
 
-test:
-	make -C test
+test: all $(UNIT_TESTS)
+	@echo " ... Tests ready"
+	@./test/test.pl
 
 dep_build:
 	make -C $(MASTODONT_DIR)
@@ -78,12 +84,18 @@ dep_build:
 %.o: %.c %.h $(PAGES)
 	$(CC) $(CFLAGS) -c $< -o $@
 
+# For tests
+%.bin: %.c
+	@$(CC) $(CFLAGS) $< -o $@ $(OBJ_NO_MAIN) $(PAGES_C_OBJ) $(LDFLAGS)
+	@echo -n " $@"
+
 clean:
 	rm -f $(OBJ) src/file-to-c/main.o
 	rm -f $(PAGES_CMP)
 	rm -f $(TMPLS_C)
+	rm -f test/unit/*.bin
 	rm -f filec ctemplate
-	rm $(TARGET)
+	rm $(TARGET) || true
 	make -C $(MASTODONT_DIR) clean
 
 clean_deps:
diff --git a/test/test.pl b/test/test.pl
new file mode 100755
index 0000000..faf5cce
--- /dev/null
+++ b/test/test.pl
@@ -0,0 +1,39 @@
+#!/usr/bin/perl
+use strict;
+use warnings;
+use Data::Dumper;
+
+my $FAIL = "\033[91m";
+my $OK = "\033[96m";
+my $SUCCESS = "\033[92m";
+my $ENDC = "\033[0m";
+
+sub read_file
+{
+    my ($filename) = @_;
+    open my $fh, '<', $filename or die "Error opening $filename: $!";
+
+    do { local $/; <$fh> };
+}
+
+my @files_glob = glob('./test/unit/t*.bin');
+my $fail = 0;
+
+foreach my $bin (@files_glob)
+{
+    my $content = read_file(substr($bin, 0, -4) . ".c");
+    my ($name, $valgrind) = $content =~ /\/\*.*INFO name='(.*?)'.*valgrind=(.*?)\s\*\//s;
+
+    
+    if (system(($bin)))
+    {
+        print "$FAIL -- !! -- Test '$name' failed!$ENDC\n";
+        $fail = 1;
+        last;
+    }
+    else {
+        print "$OK -- OK -- Test '$name' passed$ENDC\n";
+    }
+}
+
+print "$SUCCESS -- All Tests passed!$ENDC\n" unless $fail;
diff --git a/test/unit/main.c b/test/unit/main.c
deleted file mode 100644
index ace6823..0000000
--- a/test/unit/main.c
+++ /dev/null
@@ -1,33 +0,0 @@
-/*
- * Treebird - Lightweight frontend for Pleroma
- * Copyright (C) 2022 Nekobit
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program.  If not, see <https://www.gnu.org/licenses/>.
- */
-
-#include <stdio.h>
-#include <stdlib.h>
-#include "../test.h"
-
-// Imports
-#include "mime_multipart.c"
-#include "string_test.c"
-int main()
-{
-    struct test tests[] = {
-        { "Mime multipart parser", mime_multipart_test },
-        { "Strings", string_replace_test }
-    };
-    return iterate_tests(tests, sizeof(tests)/sizeof(tests[0]));
-}
diff --git a/test/unit/string_test.c b/test/unit/t00_string.c
similarity index 84%
rename from test/unit/string_test.c
rename to test/unit/t00_string.c
index 9981849..b68745c 100644
--- a/test/unit/string_test.c
+++ b/test/unit/t00_string.c
@@ -1,7 +1,12 @@
+#include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
+#include <assert.h>
 #include "../../src/string.h"
 
-int string_replace_test(void)
+/* INFO name='Dummy test' valgrind=0 */
+
+int main(void)
 {
     char* res1 = strrepl("hello world", "wo", "swi", STRREPL_ALL);
     assert(strcmp(res1, "hello swirld") == 0);
diff --git a/test/unit/mime_multipart.c b/test/unit/t01_mime.c
similarity index 96%
rename from test/unit/mime_multipart.c
rename to test/unit/t01_mime.c
index 99ba7c5..f3cbf9a 100644
--- a/test/unit/mime_multipart.c
+++ b/test/unit/t01_mime.c
@@ -18,6 +18,9 @@
 
 #include <string.h>
 #include <assert.h>
+#include <stdlib.h>
+
+/* INFO name='Mime checking' valgrind=0 */
 
 void mime_boundary_check(void)
 {
@@ -62,7 +65,7 @@ void form_check(void)
     free(multipart);
 }
 
-int mime_multipart_test(void)
+int main(void)
 {
     mime_boundary_check();
     form_check();
