From 1c9f065dd0a1d9cc732c40d5abd7a7a9a73c8204 Mon Sep 17 00:00:00 2001 From: Andreas Schuh <andreas.schuh.84@gmail.com> Date: Thu, 1 Aug 2013 03:31:47 +0100 Subject: [PATCH] Add negative compilation tests. --- CMakeLists.txt | 34 +++++++++++++++++----------------- test/CMakeLists.txt | 22 ++++++++++++++++++++++ test/gflags_nc.py.in | 33 +++++++++++++++++++++++++++++++++ test/nc/CMakeLists.txt | 16 ++++++++++++++++ test/{ => nc}/gflags_nc.cc | 5 +++++ 5 files changed, 93 insertions(+), 17 deletions(-) create mode 100644 test/gflags_nc.py.in create mode 100644 test/nc/CMakeLists.txt rename test/{ => nc}/gflags_nc.cc (98%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 8a86613..503d83e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -137,20 +137,6 @@ add_library (gflags ${GFLAGS_SRCS}) add_library (gflags_nothreads ${GFLAGS_SRCS}) set_target_properties (gflags_nothreads PROPERTIES COMPILE_DEFINITIONS NO_THREADS) -# ---------------------------------------------------------------------------- -# testing - -# TODO(andreas) Replace Bash scripts such that tests can be run on Windows (e.g., Python). -# The gflags_unittest.sh script should best be replaced by multiple -# add_test commands in the test/CMakeLists.txt file. -if (UNIX) - include (CTest) - if (BUILD_TESTING) - enable_testing () - add_subdirectory (test) - endif () -endif () - # ---------------------------------------------------------------------------- # installation if (WIN32) @@ -165,8 +151,8 @@ else () set (CONFIG_INSTALL_DIR lib/cmake/${PACKAGE_NAME}) endif () -install (TARGETS gflags DESTINATION ${LIBRARY_INSTALL_DIR} EXPORT gflags-lib) -install (FILES ${PUBLIC_HDRS} DESTINATION ${INCLUDE_INSTALL_DIR}/${GFLAGS_NAMESPACE}) +install (TARGETS gflags gflags_nothreads DESTINATION ${LIBRARY_INSTALL_DIR} EXPORT gflags-lib) +install (FILES ${PUBLIC_HDRS} DESTINATION ${INCLUDE_INSTALL_DIR}/${GFLAGS_NAMESPACE}) file (RELATIVE_PATH INSTALL_PREFIX_REL2CONFIG_DIR "${CMAKE_INSTALL_PREFIX}/${CONFIG_INSTALL_DIR}" "${CMAKE_INSTALL_PREFIX}") configure_file (cmake/config.cmake.in "${PROJECT_BINARY_DIR}/${PACKAGE_NAME}-config-install.cmake" @ONLY) @@ -192,5 +178,19 @@ endif () # ---------------------------------------------------------------------------- # support direct use of build tree set (INSTALL_PREFIX_REL2CONFIG_DIR .) -export (TARGETS gflags FILE "${PROJECT_BINARY_DIR}/${PACKAGE_NAME}-export.cmake") +export (TARGETS gflags gflags_nothreads FILE "${PROJECT_BINARY_DIR}/${PACKAGE_NAME}-export.cmake") configure_file (cmake/config.cmake.in "${PROJECT_BINARY_DIR}/${PACKAGE_NAME}-config.cmake" @ONLY) + +# ---------------------------------------------------------------------------- +# testing - MUST follow the generation of the build tree config file + +# TODO(andreas) Replace Bash scripts such that tests can be run on Windows (e.g., Python). +# The gflags_unittest.sh script should best be replaced by multiple +# add_test commands in the test/CMakeLists.txt file. +if (UNIX) + include (CTest) + if (BUILD_TESTING) + enable_testing () + add_subdirectory (test) + endif () +endif () diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 3bd3dfe..bc16eba 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,5 +1,7 @@ ## gflags tests +find_package (PythonInterp) + # ---------------------------------------------------------------------------- # output directories set (CMAKE_RUNTIME_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/Testing/bin") @@ -18,11 +20,31 @@ link_libraries (gflags_nothreads) configure_file (gflags_unittest.cc gflags_unittest-main.cc COPYONLY) configure_file (gflags_unittest.cc gflags_unittest_main.cc COPYONLY) +set (SRCDIR "${CMAKE_CURRENT_SOURCE_DIR}/nc") +configure_file (gflags_nc.py.in "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/nc.py" @ONLY) + add_executable (strip_flags gflags_strip_flags_test.cc) add_executable (unittest gflags_unittest.cc) add_executable (unittest2 gflags_unittest-main.cc) add_executable (unittest3 gflags_unittest_main.cc) +# ---------------------------------------------------------------------------- +# (negative) compilation tests +if (PYTHON_EXECUTABLE) + macro (add_nc_test name) + add_test ( + NAME nc_${name} + COMMAND "${PYTHON_EXECUTABLE}" "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/nc.py" ${name} + ) + endmacro () + + add_nc_test (sanity) + add_nc_test (swapped_args) + add_nc_test (int_instead_of_bool) + add_nc_test (bool_in_quotes) + add_nc_test (define_string_with_0) +endif () + # ---------------------------------------------------------------------------- # test commands add_test ( diff --git a/test/gflags_nc.py.in b/test/gflags_nc.py.in new file mode 100644 index 0000000..7636782 --- /dev/null +++ b/test/gflags_nc.py.in @@ -0,0 +1,33 @@ +#!/usr/bin/env python + +import os +import sys +import subprocess +import shutil + +CMAKE = '@CMAKE_COMMAND@' +TMPDIR = '@TEMPDIR@' +SRCDIR = '@SRCDIR@' +GFLAGS_DIR = '@gflags_BINARY_DIR@' + +if __name__ == "__main__": + if len(sys.argv) != 2: + sys.stderr.write(' '.join(['usage:', sys.argv[0], '<test_name>\n'])) + sys.exit(1) + test_name = sys.argv[1] + bindir = os.path.join(TMPDIR, '_'.join(['nc', test_name])) + if TMPDIR == '': + sys.stderr.write('Temporary directory not set!\n') + sys.exit(1) + # create build directory + if os.path.isdir(bindir): shutil.rmtree(bindir) + os.makedirs(bindir) + # configure the build tree + if subprocess.call([CMAKE, '-Dgflags_DIR:PATH='+GFLAGS_DIR, '-DTEST_NAME:STRING='+test_name, SRCDIR], cwd=bindir) != 0: + sys.stderr.write('Failed to configure the build tree!\n') + sys.exit(1) + # try build, which is supposed to fail (except in case of the sanity check) + if subprocess.call([CMAKE, '--build', bindir], cwd=bindir) == 0 and test_name != 'sanity': + sys.stderr.write('Build expected to fail, but it succeeded!\n') + sys.exit(1) + sys.exit(0) diff --git a/test/nc/CMakeLists.txt b/test/nc/CMakeLists.txt new file mode 100644 index 0000000..823fc67 --- /dev/null +++ b/test/nc/CMakeLists.txt @@ -0,0 +1,16 @@ +## gflags negative compilation tests + +cmake_minimum_required (VERSION 2.8) + +if (NOT TEST_NAME) + message (FATAL_ERROR "Missing TEST_NAME CMake flag") +endif () +string (TOUPPER ${TEST_NAME} TEST_NAME_UPPER) + +project (gflags_nc_${TEST_NAME}) + +find_package (gflags REQUIRED) +include_directories (${gflags_INCLUDE_DIR} "${CMAKE_CURRENT_SOURCE_DIR}/..") +link_libraries (gflags_nothreads) +add_definitions (-DTEST_${TEST_NAME_UPPER}) +add_executable (gflags_nc_${TEST_NAME} gflags_nc.cc) diff --git a/test/gflags_nc.cc b/test/nc/gflags_nc.cc similarity index 98% rename from test/gflags_nc.cc rename to test/nc/gflags_nc.cc index c283612..23398f2 100644 --- a/test/gflags_nc.cc +++ b/test/nc/gflags_nc.cc @@ -66,3 +66,8 @@ DEFINE_string(some_string_flag, "Trying to construct a string by passing 0 would cause a crash."); #endif + +int main(int, char **) +{ + return 0; +} -- GitLab