diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index ed39df0..3ba5ba9 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -18,4 +18,4 @@ build-and-test: # Build and check if the program works stage: build-and-test environment: production script: - - make && . ./test.sh + - make && make test diff --git a/Makefile b/Makefile index ba2fefa..f5b57f9 100644 --- a/Makefile +++ b/Makefile @@ -12,7 +12,8 @@ SRCDIR = ./src INCDIR = ./include OBJDIR = ./obj BINDIR = ./bin -TESTDIR = ./test_files/corrupted +TESTDIR_SAMPLES = ./test_files +TESTDIR = $(TESTDIR_SAMPLES)/corrupted SRC = $(wildcard $(SRCDIR)/*.c) OBJ = $(SRC:$(SRCDIR)/%.c=$(OBJDIR)/%.o) @@ -88,7 +89,38 @@ clean: distclean: $(call rmdirs,$(DIRECTORIES_TO_REMOVE)) +# Variables for testing +EXEC_LOG = ./exec.log +PROGRAM_ARGS := -p 60 -t 2 -n 2 --noconfirm -c -s 999999999 + +test: + @if [ ! -e "$(EXECPATH)" ]; then \ + printf -- "[!] Polonium executable (\`$(EXECUTABLE)\`) is not \ +available, attempting to build\n"; \ + $(MAKE) all; \ + exit 0; \ + fi + + @printf -- "[*] Generating corrupted versions of test files\n" + @mkdir -p $(TESTDIR) + @find $(TESTDIR_SAMPLES) -maxdepth 1 -type f -name 'test.*' | while IFS= \ + read -r file; do \ + basename=$$(basename "$$file"); \ + workfile="$(TESTDIR)/$$basename"; \ + cp "$$file" "$$workfile" || exit 1; \ + printf -- "--- [*] $(EXECUTABLE) $(basename "$2")$(PROGRAM_ARGS)\n"; \ + $(EXECPATH) "$$workfile" $(PROGRAM_ARGS) > $(EXEC_LOG) 2>&1; \ + if [ "$$?" -ne "0" ]; then \ + printf -- "[!] Polonium failed:\n"; \ + cat $(EXEC_LOG); \ + rm $(EXEC_LOG); \ + exit 1; \ + fi; \ + rm $(EXEC_LOG); \ + done + @printf "[>] All done!\n" + testclean: $(call rmdirs,$(TESTDIR)) -.PHONY: all clean distclean testclean +.PHONY: all clean distclean test testclean diff --git a/test.sh b/test.sh deleted file mode 100644 index 5d21c3a..0000000 --- a/test.sh +++ /dev/null @@ -1,51 +0,0 @@ -#!/bin/sh - -set +o histexpand # Disable parsing "!" as the history expansion character -set +e # Continue script execution even if a command fails - -mkdir_if_not_exists() { - [ -d "$1" ] || mkdir "$1" -} - -exec_name='polonium' -exec_file="$(pwd)/bin/$exec_name" -exec_log="$(pwd)/exec.log" - -corrupt() { - cp "$1" "$2" || return 1 - - file_name="$(pwd)/$2" - - args=("-p" "60" "-t" "2" "-n" "2" "--noconfirm" "-c" "-s" "999999999") - - printf -- "--- [*] $exec_name $(basename "$2") ${args[*]}\n" - - "$exec_file" "$file_name" "${args[@]}" > "$exec_log" 2>&1 - return $? -} - -iterate_on_test_files() { - find "$1" -maxdepth 1 -type f -name 'test.*' | while IFS= read -r file; do - basename="$(basename "$file")" - corrupt "$file" "./corrupted/$basename" - if [ "$?" -ne "0" ]; then - printf "[!] Polonium failed:\n" - cat "$exec_log" - rm "$exec_log" - return 1 - fi - - rm "$exec_log" - return 0 - done -} - -cd ./test_files || exit 1 -printf "[*] Generating corrupted versions of test files\n" -mkdir_if_not_exists ./corrupted/ -iterate_on_test_files . -if [ "$?" -ne "0" ]; then - exit 1 -fi - -printf "[>] All done!\n"