diff --git a/lcthw-remnants-2/c-skeleton/.gitignore b/lcthw-remnants-2/c-skeleton/.gitignore index 9b6cf51..0c5003d 100644 --- a/lcthw-remnants-2/c-skeleton/.gitignore +++ b/lcthw-remnants-2/c-skeleton/.gitignore @@ -1 +1,2 @@ +tests/runtests tests/your_library_tests diff --git a/lcthw-remnants-2/c-skeleton/Makefile b/lcthw-remnants-2/c-skeleton/Makefile index 70e2e98..fae7993 100644 --- a/lcthw-remnants-2/c-skeleton/Makefile +++ b/lcthw-remnants-2/c-skeleton/Makefile @@ -34,8 +34,8 @@ build: # The Unit Tests .PHONY: tests tests: LDLIBS += -static -l$(LIBNAME) -tests: $(TESTS) - sh ./tests/runtests.sh +tests: ./tests/runtests $(TESTS) + ./tests/runtests valgrind: VALGRIND="valgrind --log-file=/tmp/valgrind-%p.log" $(MAKE) @@ -43,7 +43,7 @@ valgrind: # The Cleaner clean: rm -rf build $(OBJECTS) $(TESTS) - rm -f tests/tests.log + rm -f tests/tests.log tests/runtests find . -name "*.gc*" -exec rm {} \; rm -rf `find . -name "*.dSYM" -print` diff --git a/lcthw-remnants-2/c-skeleton/tests/runtests.c b/lcthw-remnants-2/c-skeleton/tests/runtests.c new file mode 100644 index 0000000..82068ae --- /dev/null +++ b/lcthw-remnants-2/c-skeleton/tests/runtests.c @@ -0,0 +1,94 @@ +#include +#include +#include +#include +#include +#include +#include + +int test_selector(const struct dirent *ep) +{ + return fnmatch("*_tests", ep->d_name, 0) == 0; +} + +int run_tests_file(char *filename) +{ + if(access(filename, R_OK | X_OK) == 0) { + return system(filename); + } + + return -1; +} + +int main(int argc, char *argv[]) +{ + int entcount = 0; + int rc = 0; + int i = 0; + int ntests = 0; + char *testsdir = getenv("TESTS"); + char *valgrind = getenv("VALGRIND"); + struct dirent **namelist = NULL; + struct dirent *ep = NULL; + + if(!testsdir) { + testsdir = "./tests"; + } + + entcount = scandir((const char *)testsdir, &namelist, test_selector, alphasort); + check(entcount > -1, "Failed to scan tests dir"); + + for(i = 0; i < entcount; i++) { + ep = namelist[i]; + check(ep, "Dirent is missing."); + + char filename[256]; + rc = sprintf(filename, "%s/%s", testsdir, ep->d_name); + check(rc > -1, "Failed to build filename."); + debug("Found filename '%s'", filename); + + free(ep); + + if(valgrind) { + char command[1024]; + rc = sprintf(command, "%s %s", valgrind, filename); + check(rc > -1, "Failed to build command with valgrind."); + rc = run_tests_file(command); + } else { + rc = run_tests_file(filename); + } + + if(rc > 0) { + debug("Skipping '%s'", filename); + continue; + } + + ntests++; + + if(rc == 0) { + printf("%s PASS\n", filename); + continue; + } + + printf("ERROR in test %s: here's tests/tests.log\n", filename); + printf("------\n"); + system("tail tests/tests.log"); + goto error; + } + + printf("------\n"); + printf("Total of %d test files run.\n", ntests); + + if(namelist) { + free(namelist); + } + + return 0; + +error: + if(namelist) { + free(namelist); + } + + return 1; +} diff --git a/lcthw-remnants-2/c-skeleton/tests/runtests.sh b/lcthw-remnants-2/c-skeleton/tests/runtests.sh deleted file mode 100644 index 2976086..0000000 --- a/lcthw-remnants-2/c-skeleton/tests/runtests.sh +++ /dev/null @@ -1,19 +0,0 @@ -echo "Running unit tests:" - -for i in tests/*_tests -do - if test -f $i - then - if $VALGRIND ./$i 2>> tests/tests.log - then - echo $i PASS - else - echo "ERROR in test $i: here's tests/tests.log" - echo "------" - tail tests/tests.log - exit 1 - fi - fi -done - -echo ""