From 4db1714f55c8b84d00fd1edf560d5bc32537f96b Mon Sep 17 00:00:00 2001 From: Joachim Schiele Date: Wed, 14 Jul 2021 16:47:58 +0200 Subject: [PATCH] CMake: Adding full CMake support for building and testing --- CMakeLists.txt | 24 +++++++++++++++++ test/CMakeLists.txt | 14 ++++++++++ test/run_tests.py | 64 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 102 insertions(+) create mode 100644 CMakeLists.txt create mode 100644 test/CMakeLists.txt create mode 100755 test/run_tests.py diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..4d1e04b --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,24 @@ +project(gherkin-cpp) +cmake_minimum_required(VERSION 3.0) + +option(BUILD_SHARED_LIBS "Build shared library." OFF) + +LIST(APPEND GHERKIN_CPP_SRS + src/compiler.cpp + src/parser.cpp +) + +ADD_LIBRARY(gherkin-cpp ${GHERKIN_CPP_SRS}) +ADD_LIBRARY(gherkin-cpp::gherkin-cpp ALIAS gherkin-cpp) + +TARGET_INCLUDE_DIRECTORIES(gherkin-cpp + PUBLIC ${PROJECT_SOURCE_DIR}/include +) + +ADD_SUBDIRECTORY(libs/fmem) +TARGET_LINK_LIBRARIES(gherkin-cpp PUBLIC fmem) + +ADD_SUBDIRECTORY(libs/gherkin-c) +TARGET_LINK_LIBRARIES(gherkin-cpp PUBLIC gherkin) + +ADD_SUBDIRECTORY(test) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt new file mode 100644 index 0000000..a8812d1 --- /dev/null +++ b/test/CMakeLists.txt @@ -0,0 +1,14 @@ +ADD_EXECUTABLE(gherkin-cpp-test main.cpp) +TARGET_LINK_LIBRARIES(gherkin-cpp-test PUBLIC gherkin-cpp) + +ADD_CUSTOM_TARGET(gherkin-cpp-test.run DEPENDS testgherkin-cpp-) + +FILE(COPY testdata DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) + +ADD_CUSTOM_COMMAND( + TARGET gherkin-cpp-test.run + POST_BUILD + COMMAND ${CMAKE_CURRENT_SOURCE_DIR}/run_tests.py + WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR} + COMMENT "Running the tests" +) \ No newline at end of file diff --git a/test/run_tests.py b/test/run_tests.py new file mode 100755 index 0000000..e0e0575 --- /dev/null +++ b/test/run_tests.py @@ -0,0 +1,64 @@ +#!/usr/bin/python3 +import subprocess +import os + +# prevent stack-trace on ctrl+c +import signal +import sys +signal.signal(signal.SIGINT, lambda x, y: sys.exit(0)) + +import filecmp + +class bcolors: + OKGREEN = '\033[32m' + FAIL = '\033[91m' + ENDC = '\033[0m' + +targets = [] + +# collect the list of all tests +for root, dirs, files in os.walk("."): + for file in files: + if file.endswith(".feature"): + targets.append(os.path.join(root, file)) + +overal_result = True + +for target in targets: + #print(target) + target_result = target+".result" + outfile = open(target_result, "w") + run_result = subprocess.run(["valgrind", "--leak-check=full", "--error-exitcode=-1", "./test", target], stdout=outfile, stderr=subprocess.DEVNULL) + #print(run_result.returncode) + + + # quick hack to keep the .out files untouched but still run from the test folder + fin = open(target_result, "rt") + data = fin.read() + data = data.replace('"uri":"./testdata/', '"uri":"test/testdata/') + fin.close() + fin = open(target_result, "wt") + fin.write(data) + fin.close() + + target_out = target + ".out" + comp = filecmp.cmp(target_out, target_result) + #print("Test run result: " + str(comp)) + + fixed_target_string = "{0:>70}".format(target) + print(fixed_target_string + " -> ", end='', flush=True) + + if comp == True: + colored_status_code = bcolors.OKGREEN + 'OK' + bcolors.ENDC + else: + colored_status_code = bcolors.FAIL + 'FAIL' + bcolors.ENDC + overal_result = False + print(colored_status_code) + +if overal_result == True: + os._exit(0) +else: + print("Compare the *.feature.out to the *.feature.result for each failed test") + print("----------------------------------------------------------------------") + print("At least one test failed, so this script exits with an error (exit code 1)") + os._exit(1)