#106 Add test of gflags-config.cmake package configuration
authorAndreas Schuh <andreas.schuh.84@gmail.com>
Wed, 25 Mar 2015 13:54:49 +0000 (13:54 +0000)
committerAndreas Schuh <andreas.schuh.84@gmail.com>
Wed, 25 Mar 2015 13:54:49 +0000 (13:54 +0000)
CMakeLists.txt
INSTALL.md
test/CMakeLists.txt
test/config/CMakeLists.txt [new file with mode: 0644]
test/config/main.cc [new file with mode: 0644]
test/gflags_build.py.in [new file with mode: 0644]
test/gflags_nc.py.in [deleted file]
test/nc/CMakeLists.txt
test/nc/gflags_nc.cc

index 4d5d843..778bb86 100644 (file)
@@ -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
index 3d36903..b89d86d 100644 (file)
@@ -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".
index bb189af..77b582f 100644 (file)
@@ -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 ()
diff --git a/test/config/CMakeLists.txt b/test/config/CMakeLists.txt
new file mode 100644 (file)
index 0000000..b5bc282
--- /dev/null
@@ -0,0 +1,10 @@
+## 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)
diff --git a/test/config/main.cc b/test/config/main.cc
new file mode 100644 (file)
index 0000000..9b2b898
--- /dev/null
@@ -0,0 +1,12 @@
+#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;
+}
diff --git a/test/gflags_build.py.in b/test/gflags_build.py.in
new file mode 100644 (file)
index 0000000..a8cba2b
--- /dev/null
@@ -0,0 +1,43 @@
+#!/usr/bin/env python
+
+import os
+import sys
+import subprocess
+import shutil
+
+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) != 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]
+  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)
+  # create build directory
+  if os.path.isdir(bindir): shutil.rmtree(bindir)
+  os.makedirs(bindir)
+  # configure the build tree
+  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)
+  # 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)
diff --git a/test/gflags_nc.py.in b/test/gflags_nc.py.in
deleted file mode 100644 (file)
index 9c6054c..0000000
+++ /dev/null
@@ -1,33 +0,0 @@
-#!/usr/bin/env python
-
-import os
-import sys
-import subprocess
-import shutil
-
-CMAKE      = '@CMAKE_COMMAND@'
-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']))
-    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)
index 663c210..e425d91 100644 (file)
@@ -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})
index 23398f2..1990c30 100644 (file)
 
 #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,