From b386c1104388961fb168a38736a02f66c65adbfb Mon Sep 17 00:00:00 2001 From: servostar Date: Thu, 2 May 2024 08:26:18 +0200 Subject: [PATCH 1/9] added first test --- .gitignore | 3 ++- CMakeLists.txt | 15 ++++++++++++--- tests/CMakeLists.txt | 9 +++++++++ tests/logging/CMakeLists.txt | 9 +++++++++ tests/logging/check_output.py | 22 ++++++++++++++++++++++ tests/logging/main.c | 16 ++++++++++++++++ 6 files changed, 70 insertions(+), 4 deletions(-) create mode 100644 tests/CMakeLists.txt create mode 100644 tests/logging/CMakeLists.txt create mode 100644 tests/logging/check_output.py create mode 100644 tests/logging/main.c diff --git a/.gitignore b/.gitignore index eee6202..bb676ce 100644 --- a/.gitignore +++ b/.gitignore @@ -13,4 +13,5 @@ Makefile lexer.ll.c parser.tab.c parser.tab.h -build \ No newline at end of file +build +/Testing/ diff --git a/CMakeLists.txt b/CMakeLists.txt index 1cfeefc..079fc47 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -21,6 +21,15 @@ project(gemstone DESCRIPTION "programming language compiler" LANGUAGES C) +set(GEMSTONE_TEST_DIR ${PROJECT_SOURCE_DIR}/tests) +set(GEMSTONE_BINARY_DIR ${PROJECT_SOURCE_DIR}/bin) + +include(CTest) + +if(BUILD_TESTING) + add_subdirectory(tests) +endif() + set(CMAKE_EXPORT_COMPILE_COMMANDS ON) # ------------------------------------------------ # @@ -79,7 +88,7 @@ add_executable(release set_target_properties(release PROPERTIES OUTPUT_NAME "gsc" - RUNTIME_OUTPUT_DIRECTORY "bin/release") + RUNTIME_OUTPUT_DIRECTORY ${GEMSTONE_BINARY_DIR}/release) # FIXME: cannot compile with /O2 because of /RTC1 flag if (MSVC) @@ -111,7 +120,7 @@ add_executable(debug set_target_properties(debug PROPERTIES OUTPUT_NAME "gsc" - RUNTIME_OUTPUT_DIRECTORY "bin/debug") + RUNTIME_OUTPUT_DIRECTORY ${GEMSTONE_BINARY_DIR}/debug) if (MSVC) set(DEBUG_FLAGS /DEBUG) @@ -140,7 +149,7 @@ add_executable(check set_target_properties(check PROPERTIES OUTPUT_NAME "gsc" - RUNTIME_OUTPUT_DIRECTORY "bin/check") + RUNTIME_OUTPUT_DIRECTORY ${GEMSTONE_BINARY_DIR}/check) if (MSVC) set(CHECK_FLAGS /DEBUG /WX) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 0000000..2602430 --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1,9 @@ +include(CTest) + +set(PROJECT_BINARY_DIR bin) +set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/tests) +set(CTEST_BINARY_DIRECTORY ${PROJECT_BINARY_DIR}/tests) + +# Provide test to run here or include another CMakeLists.txt + +add_subdirectory(logging) \ No newline at end of file diff --git a/tests/logging/CMakeLists.txt b/tests/logging/CMakeLists.txt new file mode 100644 index 0000000..ebc9962 --- /dev/null +++ b/tests/logging/CMakeLists.txt @@ -0,0 +1,9 @@ +include(CTest) + +add_executable(logging ${PROJECT_SOURCE_DIR}/src/sys/log.c main.c) +target_include_directories(logging PUBLIC ${PROJECT_SOURCE_DIR}/src) +set_target_properties(logging + PROPERTIES + OUTPUT_NAME "output" + RUNTIME_OUTPUT_DIRECTORY ${GEMSTONE_BINARY_DIR}/tests/logging) +add_test(NAME logging WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} COMMAND python ${GEMSTONE_TEST_DIR}/logging/check_output.py) diff --git a/tests/logging/check_output.py b/tests/logging/check_output.py new file mode 100644 index 0000000..b908cda --- /dev/null +++ b/tests/logging/check_output.py @@ -0,0 +1,22 @@ +import subprocess + + +def run_logger_test(): + p = subprocess.run("bin/tests/logging/output", capture_output=True, text=True) + + # check exit code + if p.returncode != 0: + exit(p.returncode) + + output = p.stderr + + # check if logs appear in default log output (stderr) + + assert "logging some debug..." in output + assert "logging some info..." in output + assert "logging some warning..." in output + assert "logging some error..." in output + + +if __name__ == "__main__": + run_logger_test() diff --git a/tests/logging/main.c b/tests/logging/main.c new file mode 100644 index 0000000..7accfc5 --- /dev/null +++ b/tests/logging/main.c @@ -0,0 +1,16 @@ +// +// Created by servostar on 5/1/24. +// + +#include "sys/log.h" + +int main(void) { + log_init(); + + DEBUG("logging some debug..."); + INFO("logging some info..."); + WARN("logging some warning..."); + ERROR("logging some error..."); + + return 0; +} From 4b24e0645cd9da75c3e42b2b6d5a167fba1cbb84 Mon Sep 17 00:00:00 2001 From: servostar Date: Thu, 2 May 2024 09:49:14 +0200 Subject: [PATCH 2/9] added test for logger panic --- tests/logging/CMakeLists.txt | 24 +++++++++-- tests/logging/check_output.py | 22 ---------- tests/logging/{main.c => output.c} | 0 tests/logging/panic.c | 20 ++++++++++ tests/logging/test_logging.py | 64 ++++++++++++++++++++++++++++++ 5 files changed, 104 insertions(+), 26 deletions(-) delete mode 100644 tests/logging/check_output.py rename tests/logging/{main.c => output.c} (100%) create mode 100644 tests/logging/panic.c create mode 100644 tests/logging/test_logging.py diff --git a/tests/logging/CMakeLists.txt b/tests/logging/CMakeLists.txt index ebc9962..9c90431 100644 --- a/tests/logging/CMakeLists.txt +++ b/tests/logging/CMakeLists.txt @@ -1,9 +1,25 @@ include(CTest) -add_executable(logging ${PROJECT_SOURCE_DIR}/src/sys/log.c main.c) -target_include_directories(logging PUBLIC ${PROJECT_SOURCE_DIR}/src) -set_target_properties(logging +# ------------------------------------------------------- # +# CTEST 1 +# test the default output of the logger + +add_executable(logging_output ${PROJECT_SOURCE_DIR}/src/sys/log.c output.c) +target_include_directories(logging_output PUBLIC ${PROJECT_SOURCE_DIR}/src) +set_target_properties(logging_output PROPERTIES OUTPUT_NAME "output" RUNTIME_OUTPUT_DIRECTORY ${GEMSTONE_BINARY_DIR}/tests/logging) -add_test(NAME logging WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} COMMAND python ${GEMSTONE_TEST_DIR}/logging/check_output.py) +add_test(NAME logging_output WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} COMMAND python ${GEMSTONE_TEST_DIR}/logging/test_logging.py check_output) + +# ------------------------------------------------------- # +# CTEST 1 +# test the panic functionality of the logger + +add_executable(logging_panic ${PROJECT_SOURCE_DIR}/src/sys/log.c panic.c) +target_include_directories(logging_panic PUBLIC ${PROJECT_SOURCE_DIR}/src) +set_target_properties(logging_panic + PROPERTIES + OUTPUT_NAME "panic" + RUNTIME_OUTPUT_DIRECTORY ${GEMSTONE_BINARY_DIR}/tests/logging) +add_test(NAME logging_panic WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} COMMAND python ${GEMSTONE_TEST_DIR}/logging/test_logging.py check_panic) diff --git a/tests/logging/check_output.py b/tests/logging/check_output.py deleted file mode 100644 index b908cda..0000000 --- a/tests/logging/check_output.py +++ /dev/null @@ -1,22 +0,0 @@ -import subprocess - - -def run_logger_test(): - p = subprocess.run("bin/tests/logging/output", capture_output=True, text=True) - - # check exit code - if p.returncode != 0: - exit(p.returncode) - - output = p.stderr - - # check if logs appear in default log output (stderr) - - assert "logging some debug..." in output - assert "logging some info..." in output - assert "logging some warning..." in output - assert "logging some error..." in output - - -if __name__ == "__main__": - run_logger_test() diff --git a/tests/logging/main.c b/tests/logging/output.c similarity index 100% rename from tests/logging/main.c rename to tests/logging/output.c diff --git a/tests/logging/panic.c b/tests/logging/panic.c new file mode 100644 index 0000000..ecd8815 --- /dev/null +++ b/tests/logging/panic.c @@ -0,0 +1,20 @@ +// +// Created by servostar on 5/2/24. +// + +#include "sys/log.h" + +int main(void) { + log_init(); + + // this should appear in stderr + INFO("before exit"); + + PANIC("oooops something happened"); + + // this should NOT appear in stderr + // ^^^ + ERROR("after exit"); + + return 0; +} diff --git a/tests/logging/test_logging.py b/tests/logging/test_logging.py new file mode 100644 index 0000000..da88b19 --- /dev/null +++ b/tests/logging/test_logging.py @@ -0,0 +1,64 @@ +import subprocess +import sys +import logging +from logging import info, error + +BIN_DIR = "bin/tests/logging/" + + +def run_logger_test(): + info("started check output...") + + p = subprocess.run(BIN_DIR + "output", capture_output=True, text=True) + + info("checking exit code...") + + # check exit code + assert p.returncode == 0 + + output = p.stderr + + # check if logs appear in default log output (stderr) + info("checking stderr...") + + assert "logging some debug..." in output + assert "logging some info..." in output + assert "logging some warning..." in output + assert "logging some error..." in output + + +def run_check_panic(): + info("started check panic...") + + p = subprocess.run(BIN_DIR + "panic", capture_output=True, text=True) + + info("checking exit code...") + + # check exit code + assert p.returncode == 1 + + output = p.stderr + + # check if logs appear (not) in default log output (stderr) + info("checking stderr...") + + assert "before exit" in output + assert "oooops something happened" in output + assert "after exit" not in output + + +if __name__ == "__main__": + logging.basicConfig(level=logging.INFO) + + target = sys.argv[1] + + info(f"starting logging test suite with target: {target}") + + match target: + case "check_output": + run_logger_test() + case "check_panic": + run_check_panic() + case _: + error(f"unknown target: {target}") + exit(1) From 7bfe49c8eda7c12b49a63132f77748bbc3651536 Mon Sep 17 00:00:00 2001 From: servostar Date: Thu, 2 May 2024 09:49:50 +0200 Subject: [PATCH 3/9] renamed python test function for logger output --- tests/logging/test_logging.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/logging/test_logging.py b/tests/logging/test_logging.py index da88b19..54f5d75 100644 --- a/tests/logging/test_logging.py +++ b/tests/logging/test_logging.py @@ -6,7 +6,7 @@ from logging import info, error BIN_DIR = "bin/tests/logging/" -def run_logger_test(): +def run_check_output(): info("started check output...") p = subprocess.run(BIN_DIR + "output", capture_output=True, text=True) From dc240acb9dc84cee1f290ae3af3597f2c6c5c919 Mon Sep 17 00:00:00 2001 From: servostar Date: Thu, 2 May 2024 09:59:42 +0200 Subject: [PATCH 4/9] added section about writing tests to README --- README.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/README.md b/README.md index a1e442b..faca249 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,28 @@ Requires: - bison - flex +## Writing Tests + +Since the project is build and configured through CMake it makes sense to rely for tests +on CTest. All tests are located in the subfolder `tests`. In this directory is a CMakeLists.txt which specifies which tests +are to be run. Actual tests are located in folders within tests and contain a final CMakeLists.txt which specifies what to run +for a single test. + +``` +tests + └─ test_group1 + └─ CMakeLists.txt # specify tests in this group + └─ ... # test files of group 1 + + └─ test_group2 + └─ CMakeLists.txt # specify tests in this group + └─ ... # test files of group 2 + + └─ CMakeLists.txt # specify test groups to run + +CMakeLists.txt # build configuration +``` + ## Development with VSCode/Codium Recommended extensions for getting a decent experience are the following: From 138937af57af8ca4d26e002d2881ed29e73e3e96 Mon Sep 17 00:00:00 2001 From: servostar Date: Thu, 2 May 2024 10:04:00 +0200 Subject: [PATCH 5/9] fixed output test and reformatted CMake --- tests/logging/CMakeLists.txt | 20 ++++++++++++++------ tests/logging/test_logging.py | 2 +- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/tests/logging/CMakeLists.txt b/tests/logging/CMakeLists.txt index 9c90431..0db2412 100644 --- a/tests/logging/CMakeLists.txt +++ b/tests/logging/CMakeLists.txt @@ -1,25 +1,33 @@ include(CTest) +include_directories(${PROJECT_SOURCE_DIR}/src) + # ------------------------------------------------------- # # CTEST 1 # test the default output of the logger -add_executable(logging_output ${PROJECT_SOURCE_DIR}/src/sys/log.c output.c) -target_include_directories(logging_output PUBLIC ${PROJECT_SOURCE_DIR}/src) +add_executable(logging_output + ${PROJECT_SOURCE_DIR}/src/sys/log.c + output.c) set_target_properties(logging_output PROPERTIES OUTPUT_NAME "output" RUNTIME_OUTPUT_DIRECTORY ${GEMSTONE_BINARY_DIR}/tests/logging) -add_test(NAME logging_output WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} COMMAND python ${GEMSTONE_TEST_DIR}/logging/test_logging.py check_output) +add_test(NAME logging_output + WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} + COMMAND python ${GEMSTONE_TEST_DIR}/logging/test_logging.py check_output) # ------------------------------------------------------- # # CTEST 1 # test the panic functionality of the logger -add_executable(logging_panic ${PROJECT_SOURCE_DIR}/src/sys/log.c panic.c) -target_include_directories(logging_panic PUBLIC ${PROJECT_SOURCE_DIR}/src) +add_executable(logging_panic + ${PROJECT_SOURCE_DIR}/src/sys/log.c + panic.c) set_target_properties(logging_panic PROPERTIES OUTPUT_NAME "panic" RUNTIME_OUTPUT_DIRECTORY ${GEMSTONE_BINARY_DIR}/tests/logging) -add_test(NAME logging_panic WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} COMMAND python ${GEMSTONE_TEST_DIR}/logging/test_logging.py check_panic) +add_test(NAME logging_panic + WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} + COMMAND python ${GEMSTONE_TEST_DIR}/logging/test_logging.py check_panic) diff --git a/tests/logging/test_logging.py b/tests/logging/test_logging.py index 54f5d75..86816fd 100644 --- a/tests/logging/test_logging.py +++ b/tests/logging/test_logging.py @@ -56,7 +56,7 @@ if __name__ == "__main__": match target: case "check_output": - run_logger_test() + run_check_output() case "check_panic": run_check_panic() case _: From ebf526d6dac1b37171cda151cd2d8ac7e75d2dba Mon Sep 17 00:00:00 2001 From: servostar Date: Thu, 2 May 2024 10:32:27 +0200 Subject: [PATCH 6/9] added test for multiple output logger streams --- tests/logging/CMakeLists.txt | 17 +++++++++++++++- tests/logging/streams.c | 33 +++++++++++++++++++++++++++++++ tests/logging/test_logging.py | 37 +++++++++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 tests/logging/streams.c diff --git a/tests/logging/CMakeLists.txt b/tests/logging/CMakeLists.txt index 0db2412..dee6ddf 100644 --- a/tests/logging/CMakeLists.txt +++ b/tests/logging/CMakeLists.txt @@ -18,7 +18,7 @@ add_test(NAME logging_output COMMAND python ${GEMSTONE_TEST_DIR}/logging/test_logging.py check_output) # ------------------------------------------------------- # -# CTEST 1 +# CTEST 2 # test the panic functionality of the logger add_executable(logging_panic @@ -31,3 +31,18 @@ set_target_properties(logging_panic add_test(NAME logging_panic WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} COMMAND python ${GEMSTONE_TEST_DIR}/logging/test_logging.py check_panic) + +# ------------------------------------------------------- # +# CTEST 3 +# test the ability to write to multiple output streams + +add_executable(logging_streams + ${PROJECT_SOURCE_DIR}/src/sys/log.c + streams.c) +set_target_properties(logging_streams + PROPERTIES + OUTPUT_NAME "stream" + RUNTIME_OUTPUT_DIRECTORY ${GEMSTONE_BINARY_DIR}/tests/logging) +add_test(NAME logging_streams + WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} + COMMAND python ${GEMSTONE_TEST_DIR}/logging/test_logging.py check_stream) diff --git a/tests/logging/streams.c b/tests/logging/streams.c new file mode 100644 index 0000000..97599da --- /dev/null +++ b/tests/logging/streams.c @@ -0,0 +1,33 @@ +// +// Created by servostar on 5/2/24. +// + +#include "sys/log.h" +#include + +static FILE* file; + +void close_file(void) { + if (file != NULL) { + fclose(file); + } +} + +int main(void) { + log_init(); + + // this should appear in stderr + INFO("should only be in stderr"); + + file = fopen("tmp/test.log", "w"); + if (file == NULL) { + PANIC("could not open file"); + } + atexit(close_file); + + log_register_stream(file); + + INFO("should be in both"); + + return 0; +} diff --git a/tests/logging/test_logging.py b/tests/logging/test_logging.py index 86816fd..0d32d48 100644 --- a/tests/logging/test_logging.py +++ b/tests/logging/test_logging.py @@ -2,6 +2,7 @@ import subprocess import sys import logging from logging import info, error +import os BIN_DIR = "bin/tests/logging/" @@ -47,6 +48,40 @@ def run_check_panic(): assert "after exit" not in output +def run_check_stream(): + info("started check panic...") + + info("creating temporary folder...") + + if not os.path.exists("tmp"): + os.mkdir("tmp") + + info("cleaning temporary folder...") + + if os.path.exists("tmp/test.log"): + os.remove("tmp/test.log") + + info("launching test binary...") + + p = subprocess.run(BIN_DIR + "stream", capture_output=True, text=True) + + info("checking exit code...") + + # check exit code + assert p.returncode == 0 + + with open("tmp/test.log", "r") as file: + assert "should be in both" in "".join(file.readlines()) + + output = p.stderr + + # check if logs appear (not) in default log output (stderr) + info("checking stderr...") + + assert "should only be in stderr" in output + assert "should be in both" in output + + if __name__ == "__main__": logging.basicConfig(level=logging.INFO) @@ -59,6 +94,8 @@ if __name__ == "__main__": run_check_output() case "check_panic": run_check_panic() + case "check_stream": + run_check_stream() case _: error(f"unknown target: {target}") exit(1) From c099a60ecf649cd76da74a50b5d83297f9918fde Mon Sep 17 00:00:00 2001 From: servostar Date: Thu, 2 May 2024 10:50:24 +0200 Subject: [PATCH 7/9] added test for compile time log level filter --- tests/logging/CMakeLists.txt | 15 +++++++++++++++ tests/logging/level.c | 18 ++++++++++++++++++ tests/logging/test_logging.py | 23 +++++++++++++++++++++++ 3 files changed, 56 insertions(+) create mode 100644 tests/logging/level.c diff --git a/tests/logging/CMakeLists.txt b/tests/logging/CMakeLists.txt index dee6ddf..0f14079 100644 --- a/tests/logging/CMakeLists.txt +++ b/tests/logging/CMakeLists.txt @@ -46,3 +46,18 @@ set_target_properties(logging_streams add_test(NAME logging_streams WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} COMMAND python ${GEMSTONE_TEST_DIR}/logging/test_logging.py check_stream) + +# ------------------------------------------------------- # +# CTEST 4 +# test compile time log level switch + +add_executable(logging_level + ${PROJECT_SOURCE_DIR}/src/sys/log.c + level.c) +set_target_properties(logging_level + PROPERTIES + OUTPUT_NAME "level" + RUNTIME_OUTPUT_DIRECTORY ${GEMSTONE_BINARY_DIR}/tests/logging) +add_test(NAME logging_level + WORKING_DIRECTORY ${PROJECT_SOURCE_DIR} + COMMAND python ${GEMSTONE_TEST_DIR}/logging/test_logging.py check_level) diff --git a/tests/logging/level.c b/tests/logging/level.c new file mode 100644 index 0000000..6a4f055 --- /dev/null +++ b/tests/logging/level.c @@ -0,0 +1,18 @@ +// +// Created by servostar on 5/2/24. +// + +#include "sys/log.h" + +#define LOG_LEVEL LOG_LEVEL_WARNING + +int main(void) { + log_init(); + + DEBUG("logging some debug..."); + INFO("logging some info..."); + WARN("logging some warning..."); + ERROR("logging some error..."); + + return 0; +} diff --git a/tests/logging/test_logging.py b/tests/logging/test_logging.py index 0d32d48..246a9d5 100644 --- a/tests/logging/test_logging.py +++ b/tests/logging/test_logging.py @@ -28,6 +28,27 @@ def run_check_output(): assert "logging some error..." in output +def run_check_level(): + info("started check level...") + + p = subprocess.run(BIN_DIR + "level", capture_output=True, text=True) + + info("checking exit code...") + + # check exit code + assert p.returncode == 0 + + output = p.stderr + + # check if logs appear in default log output (stderr) + info("checking stderr...") + + assert "logging some debug..." not in output + assert "logging some info..." not in output + assert "logging some warning..." in output + assert "logging some error..." in output + + def run_check_panic(): info("started check panic...") @@ -96,6 +117,8 @@ if __name__ == "__main__": run_check_panic() case "check_stream": run_check_stream() + case "check_level": + run_check_level() case _: error(f"unknown target: {target}") exit(1) From 01cf8345a0ace0624fece387388971922653259a Mon Sep 17 00:00:00 2001 From: servostar Date: Thu, 2 May 2024 12:14:59 +0200 Subject: [PATCH 8/9] reverted change of #aa7c62f0 in devkit added script to run checks and tests in container --- .github/workflows/build-check-sdk.yaml | 4 +-- Dockerfile | 9 ++++-- run-check-test.sh | 40 ++++++++++++++++++++++++++ sdk/Dockerfile | 4 +-- 4 files changed, 50 insertions(+), 7 deletions(-) create mode 100644 run-check-test.sh diff --git a/.github/workflows/build-check-sdk.yaml b/.github/workflows/build-check-sdk.yaml index 5b8f2aa..10a4811 100644 --- a/.github/workflows/build-check-sdk.yaml +++ b/.github/workflows/build-check-sdk.yaml @@ -2,7 +2,7 @@ name: "Build check gemstone in SDK" run-name: SDK build check to ${{ inputs.deploy_target }} by @${{ github.actor }} on: [push, pull_request] env: - SDK: 0.2.1-alpine-3.19.1 + SDK: 0.2.2-alpine-3.19.1 jobs: build-check-sdk: runs-on: ubuntu-latest @@ -11,4 +11,4 @@ jobs: - name: Setup SDK run: docker pull servostar/gemstone:sdk-"$SDK" && docker build --tag gemstone:devkit-"$SDK" . - name: Compile - run: docker run gemstone:devkit-"$SDK" make check + run: docker run gemstone:devkit-"$SDK" sh run-check-test.sh diff --git a/Dockerfile b/Dockerfile index ce2db20..09e34b6 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,9 +1,12 @@ -FROM servostar/gemstone:sdk-0.2.1-alpine-3.19.1 +FROM servostar/gemstone:sdk-0.2.2-alpine-3.19.1 LABEL authors="servostar" -LABEL version="0.2.1" +LABEL version="0.2.2" LABEL description="docker image for setting up the build pipeline on SDK" LABEL website="https://github.com/Servostar/gemstone" -RUN git clone https://github.com/Servostar/gemstone.git /home/lorang +COPY --chown=lorang src /home/lorang/src +COPY --chown=lorang tests /home/lorang/tests +COPY --chown=lorang CMakeLists.txt /home/lorang/ +COPY --chown=lorang run-check-test.sh /home/lorang/ RUN cmake . diff --git a/run-check-test.sh b/run-check-test.sh new file mode 100644 index 0000000..a2a3ab4 --- /dev/null +++ b/run-check-test.sh @@ -0,0 +1,40 @@ +#!/usr/bin/env sh + +# Author: Sven Vogel +# Created: 02.05.2024 +# Description: Builds the project and runs tests +# Returns 0 on success and 1 when something went wrong + +echo "+--------------------------------------+" +echo "| BUILDING all TARGETS |" +echo "+--------------------------------------+" + +make -B +if [ ! $? -eq 0 ]; then + echo "===> failed to build targets" + exit 1 +fi + +echo "+--------------------------------------+" +echo "| RUNNING CODE CHECK |" +echo "+--------------------------------------+" + +make check +if [ ! $? -eq 0 ]; then + echo "===> failed code check..." + exit 1 +fi + +echo "+--------------------------------------+" +echo "| RUNNING TESTS |" +echo "+--------------------------------------+" + +ctest -VV --output-on-failure --schedule-random -j 4 +if [ ! $? -eq 0 ]; then + echo "===> failed tests..." + exit 1 +fi + +echo "+--------------------------------------+" +echo "| COMPLETED CHECK + TESTS SUCCESSFULLY |" +echo "+--------------------------------------+" \ No newline at end of file diff --git a/sdk/Dockerfile b/sdk/Dockerfile index 4dbeb1f..1a6a677 100644 --- a/sdk/Dockerfile +++ b/sdk/Dockerfile @@ -1,11 +1,11 @@ FROM alpine:3.19.1 LABEL authors="servostar" -LABEL version="0.2.1" +LABEL version="0.2.2" LABEL description="base image for building the gemstone programming language compiler" LABEL website="https://github.com/Servostar/gemstone" # install dependencies -RUN apk add build-base gcc make cmake bison flex git +RUN apk add build-base gcc make cmake bison flex git python3 # create user for build RUN adduser --disabled-password lorang From f7a3faad2ea8e25f7bac95cebb34bc9ef1efb39c Mon Sep 17 00:00:00 2001 From: servostar Date: Sat, 4 May 2024 15:33:20 +0200 Subject: [PATCH 9/9] added test for accepting a file as input --- tests/CMakeLists.txt | 3 ++- tests/input_file/CMakeLists.txt | 9 ++++++++ tests/input_file/test.gem | 6 +++++ tests/input_file/test_input_file.py | 36 +++++++++++++++++++++++++++++ 4 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 tests/input_file/CMakeLists.txt create mode 100644 tests/input_file/test.gem create mode 100644 tests/input_file/test_input_file.py diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 2602430..ab8afdc 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -6,4 +6,5 @@ set(CTEST_BINARY_DIRECTORY ${PROJECT_BINARY_DIR}/tests) # Provide test to run here or include another CMakeLists.txt -add_subdirectory(logging) \ No newline at end of file +add_subdirectory(logging) +add_subdirectory(input_file) \ No newline at end of file diff --git a/tests/input_file/CMakeLists.txt b/tests/input_file/CMakeLists.txt new file mode 100644 index 0000000..263e769 --- /dev/null +++ b/tests/input_file/CMakeLists.txt @@ -0,0 +1,9 @@ +include(CTest) + +# ------------------------------------------------------- # +# CTEST 1 +# test if the program accepts a file as input + +add_test(NAME input_file_check + WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}/bin/check + COMMAND python ${GEMSTONE_TEST_DIR}/input_file/test_input_file.py ${GEMSTONE_TEST_DIR}/input_file/test.gem) diff --git a/tests/input_file/test.gem b/tests/input_file/test.gem new file mode 100644 index 0000000..991288d --- /dev/null +++ b/tests/input_file/test.gem @@ -0,0 +1,6 @@ + +import "std.io" + +fun main { + print("Hello, World!!!") +} \ No newline at end of file diff --git a/tests/input_file/test_input_file.py b/tests/input_file/test_input_file.py new file mode 100644 index 0000000..3721dc0 --- /dev/null +++ b/tests/input_file/test_input_file.py @@ -0,0 +1,36 @@ +import os.path +import subprocess +import sys +import logging +from logging import info + + +def check_accept(): + info("testing handling of input file...") + + logging.basicConfig(level=logging.INFO) + + test_file_name = sys.argv[1] + + p = subprocess.run(["./gsc", test_file_name], capture_output=True, text=True) + + assert p.returncode == 0 + + +def check_abort(): + info("testing handling of missing input file...") + + logging.basicConfig(level=logging.INFO) + + p = subprocess.run("./gsc", capture_output=True, text=True) + + assert p.returncode == 1 + + +if __name__ == "__main__": + logging.basicConfig(level=logging.INFO) + info("check if binary exists...") + assert os.path.exists("./gsc") + + check_accept() + check_abort()