Commit 16a16876 authored by Andreas Schuh's avatar Andreas Schuh

#106 Add test of gflags-config.cmake package configuration

parent f4f524df
......@@ -63,13 +63,11 @@ option (BUILD_gflags_LIB "Request build of the multi-threaded gflags l
option (BUILD_gflags_nothreads_LIB "Request build of the single-threaded gflags library." ON)
option (BUILD_PACKAGING "Enable build of distribution packages using CPack." OFF)
option (BUILD_TESTING "Enable build of the unit tests and their execution using CTest." OFF)
option (BUILD_NC_TESTS "Request addition of negative compilation tests." OFF)
option (INSTALL_HEADERS "Request packaging of headers and other development files." ON)
mark_as_advanced (CLEAR CMAKE_INSTALL_PREFIX)
mark_as_advanced (CMAKE_CONFIGURATION_TYPES
BUILD_STATIC_LIBS
BUILD_NC_TESTS
INSTALL_HEADERS)
if (APPLE)
mark_as_advanced(CMAKE_OSX_ARCHITECTURES
......
......@@ -57,6 +57,7 @@ BUILD_STATIC_LIBS | Request build of static link libraries. Implied if
BUILD_PACKAGING | Enable binary package generation using CPack.
BUILD_TESTING | Build tests for execution by CTest.
BUILD_NC_TESTS | Request inclusion of negative compilation tests (requires Python).
BUILD_CONFIG_TESTS | Request inclusion of package configuration tests (requires Python).
BUILD_gflags_LIBS | Request build of multi-threaded gflags libraries (if threading library found).
BUILD_gflags_nothreads_LIBS | Request build of single-threaded gflags libraries.
GFLAGS_NAMESPACE | Name of the C++ namespace to be used by the gflags library. Note that the public source header files are installed in a subdirectory named after this namespace. To maintain backwards compatibility with the Google Commandline Flags, set this variable to "google". The default is "gflags".
......
......@@ -165,25 +165,41 @@ add_test(NAME gflags_declare COMMAND gflags_declare_test --message "Hello gflags
set_tests_properties(gflags_declare PROPERTIES PASS_REGULAR_EXPRESSION "Hello gflags!")
# ----------------------------------------------------------------------------
# (negative) compilation tests
if (BUILD_NC_TESTS)
# configure Python script which configures and builds a test project
if (BUILD_NC_TESTS OR BUILD_CONFIG_TESTS)
find_package (PythonInterp)
if (NOT PYTHON_EXECUTABLE)
message (FATAL_ERROR "No Python installation found! It is required by the negative compilation tests."
" Either install Python or set BUILD_NC_TESTS to FALSE and try again.")
message (FATAL_ERROR "No Python installation found! It is required by the (negative) compilation tests."
" Either install Python or set BUILD_NC_TESTS and BUILD_CONFIG_TESTS to FALSE.")
endif ()
set (SRCDIR "${CMAKE_CURRENT_SOURCE_DIR}/nc")
set (TMPDIR "${PROJECT_BINARY_DIR}/Testing/Temporary")
configure_file (gflags_nc.py.in "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/nc.py" @ONLY)
macro (add_gflags_nc_test name)
configure_file (gflags_build.py.in "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/build.py" @ONLY)
function (add_gflags_build_test name srcdir expect_fail)
set (srcdir "${CMAKE_CURRENT_SOURCE_DIR}/${srcdir}")
add_test (
NAME nc_${name}
COMMAND "${PYTHON_EXECUTABLE}" "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/nc.py" ${name}
NAME "${name}"
COMMAND "${PYTHON_EXECUTABLE}" "${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/build.py"
${name} ${srcdir} ${expect_fail}
)
endmacro ()
add_gflags_nc_test (sanity)
add_gflags_nc_test (swapped_args)
add_gflags_nc_test (int_instead_of_bool)
add_gflags_nc_test (bool_in_quotes)
add_gflags_nc_test (define_string_with_0)
endfunction ()
endif ()
# ----------------------------------------------------------------------------
# negative compilation tests
option (BUILD_NC_TESTS "Request addition of negative compilation tests." OFF)
mark_as_advanced (BUILD_NC_TESTS)
if (BUILD_NC_TESTS)
add_gflags_build_test (nc_sanity nc 0)
add_gflags_build_test (nc_swapped_args nc 1)
add_gflags_build_test (nc_int_instead_of_bool nc 1)
add_gflags_build_test (nc_bool_in_quotes nc 1)
add_gflags_build_test (nc_define_string_with_0 nc 1)
endif ()
# ----------------------------------------------------------------------------
# build configuration test
option (BUILD_CONFIG_TESTS "Request addition of package configuration tests." OFF)
mark_as_advanced (BUILD_CONFIG_TESTS)
if (BUILD_CONFIG_TESTS)
add_gflags_build_test (cmake_config config 0)
endif ()
## gflags package configuration tests
cmake_minimum_required (VERSION 2.8)
project (gflags_${TEST_NAME})
find_package (gflags REQUIRED)
add_executable (foo main.cc)
target_link_libraries (foo gflags)
#include <iostream>
#include <gflags/gflags.h>
DEFINE_string(message, "Hello World!", "The message to print");
int main(int argc, char **argv)
{
gflags::SetUsageMessage("Test CMake configuration of gflags library (gflags-config.cmake)");
gflags::ParseCommandLineFlags(&argc, &argv, true);
std::cout << FLAGS_message << std::endl;
return 0;
}
......@@ -5,17 +5,20 @@ import sys
import subprocess
import shutil
CMAKE = '@CMAKE_COMMAND@'
TMPDIR = '@TMPDIR@'
SRCDIR = '@SRCDIR@'
GFLAGS_DIR = '@gflags_BINARY_DIR@'
CMAKE = '@CMAKE_COMMAND@'
CMAKE_BUILD_TYPE = '@CMAKE_BUILD_TYPE@'
TMPDIR = '@TMPDIR@'
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']))
if len(sys.argv) != 4:
sys.stderr.write(' '.join(['usage:', sys.argv[0], '<test_name> <srcdir> <expect_fail:0|1>\n']))
sys.exit(1)
test_name = sys.argv[1]
bindir = os.path.join(TMPDIR, '_'.join(['nc', test_name]))
test_name = sys.argv[1]
srcdir = sys.argv[2]
expect_fail = (sys.argv[3].lower() in ['true', 'yes', 'on', '1'])
bindir = os.path.join(TMPDIR, test_name)
if TMPDIR == '':
sys.stderr.write('Temporary directory not set!\n')
sys.exit(1)
......@@ -23,11 +26,18 @@ if __name__ == "__main__":
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:
if subprocess.call([CMAKE, '-DCMAKE_BUILD_TYPE:STRING='+CMAKE_BUILD_TYPE,
'-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)
# build the test project
exit_code = subprocess.call([CMAKE, '--build', bindir, '--config', CMAKE_BUILD_TYPE], cwd=bindir)
if expect_fail == True:
if exit_code == 0:
sys.stderr.write('Build expected to fail, but it succeeded!\n')
sys.exit(1)
else:
sys.stderr.write('Build failed as expected\n')
exit_code = 0
sys.exit(exit_code)
......@@ -7,10 +7,10 @@ if (NOT TEST_NAME)
endif ()
string (TOUPPER ${TEST_NAME} TEST_NAME_UPPER)
project (gflags_nc_${TEST_NAME})
project (gflags_${TEST_NAME})
find_package (gflags REQUIRED)
include_directories ("${CMAKE_CURRENT_SOURCE_DIR}/..")
add_definitions (-DTEST_${TEST_NAME_UPPER})
add_executable (gflags_nc_${TEST_NAME} gflags_nc.cc)
target_link_libraries(gflags_nc_${TEST_NAME} ${gflags_LIBRARIES})
add_executable (gflags_${TEST_NAME} gflags_nc.cc)
target_link_libraries(gflags_${TEST_NAME} ${gflags_LIBRARIES})
......@@ -33,33 +33,33 @@
#include <gflags/gflags.h>
#if defined(TEST_SWAPPED_ARGS)
#if defined(TEST_NC_SWAPPED_ARGS)
DEFINE_bool(some_bool_flag,
"the default value should go here, not the description",
false);
#elif defined(TEST_INT_INSTEAD_OF_BOOL)
#elif defined(TEST_NC_INT_INSTEAD_OF_BOOL)
DEFINE_bool(some_bool_flag_2,
0,
"should have been an int32 flag but mistakenly used bool instead");
#elif defined(TEST_BOOL_IN_QUOTES)
#elif defined(TEST_NC_BOOL_IN_QUOTES)
DEFINE_bool(some_bool_flag_3,
"false",
"false in in quotes, which is wrong");
#elif defined(TEST_SANITY)
#elif defined(TEST_NC_SANITY)
DEFINE_bool(some_bool_flag_4,
true,
"this is the correct usage of DEFINE_bool");
#elif defined(TEST_DEFINE_STRING_WITH_0)
#elif defined(TEST_NC_DEFINE_STRING_WITH_0)
DEFINE_string(some_string_flag,
0,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment