Преглед изворни кода

Simplify testing from CMake project to "check" target

Konstantin Podsvirov пре 10 година
родитељ
комит
e301946ca4
2 измењених фајлова са 17 додато и 25 уклоњено
  1. 13 19
      cmake/tests.cmake
  2. 4 6
      src/google/protobuf/compiler/command_line_interface_unittest.cc

+ 13 - 19
cmake/tests.cmake

@@ -2,6 +2,9 @@ if (NOT EXISTS "${PROJECT_SOURCE_DIR}/../gmock/CMakeLists.txt")
   message(FATAL_ERROR "Cannot find gmock directory.")
   message(FATAL_ERROR "Cannot find gmock directory.")
 endif()
 endif()
 
 
+option(protobuf_ABSOLUTE_TEST_PLUGIN_PATH
+  "Using absolute test_plugin path in tests" ON)
+
 include_directories(
 include_directories(
   ${protobuf_source_dir}/gmock
   ${protobuf_source_dir}/gmock
   ${protobuf_source_dir}/gmock/gtest
   ${protobuf_source_dir}/gmock/gtest
@@ -170,7 +173,12 @@ set(tests_files
   ${protobuf_source_dir}/src/google/protobuf/wire_format_unittest.cc
   ${protobuf_source_dir}/src/google/protobuf/wire_format_unittest.cc
 )
 )
 
 
-enable_testing()
+if(protobuf_ABSOLUTE_TEST_PLUGIN_PATH)
+  add_compile_options(-DGOOGLE_PROTOBUF_TEST_PLUGIN_PATH="$<TARGET_FILE:test_plugin>")
+endif()
+
+add_executable(tests ${tests_files} ${common_test_files} ${tests_proto_files} ${lite_test_proto_files})
+target_link_libraries(tests libprotoc libprotobuf gmock_main)
 
 
 set(test_plugin_files
 set(test_plugin_files
   ${protobuf_source_dir}/src/google/protobuf/compiler/mock_code_generator.cc
   ${protobuf_source_dir}/src/google/protobuf/compiler/mock_code_generator.cc
@@ -178,18 +186,15 @@ set(test_plugin_files
   ${protobuf_source_dir}/src/google/protobuf/testing/file.h
   ${protobuf_source_dir}/src/google/protobuf/testing/file.h
   ${protobuf_source_dir}/src/google/protobuf/compiler/test_plugin.cc
   ${protobuf_source_dir}/src/google/protobuf/compiler/test_plugin.cc
 )
 )
+
 add_executable(test_plugin ${test_plugin_files})
 add_executable(test_plugin ${test_plugin_files})
 target_link_libraries(test_plugin libprotoc libprotobuf gmock)
 target_link_libraries(test_plugin libprotoc libprotobuf gmock)
 
 
-add_compile_options(-DGOOGLE_PROTOBUF_TEST_PLUGIN_PATH="$<TARGET_FILE:test_plugin>")
-
 set(lite_test_files
 set(lite_test_files
   ${protobuf_source_dir}/src/google/protobuf/lite_unittest.cc
   ${protobuf_source_dir}/src/google/protobuf/lite_unittest.cc
 )
 )
 add_executable(lite-test ${lite_test_files} ${common_lite_test_files} ${lite_test_proto_files})
 add_executable(lite-test ${lite_test_files} ${common_lite_test_files} ${lite_test_proto_files})
 target_link_libraries(lite-test libprotobuf-lite)
 target_link_libraries(lite-test libprotobuf-lite)
-add_test(NAME lite-test COMMAND lite-test
-  WORKING_DIRECTORY ${protobuf_source_dir})
 
 
 set(lite_arena_test_files
 set(lite_arena_test_files
   ${protobuf_source_dir}/src/google/protobuf/lite_arena_unittest.cc
   ${protobuf_source_dir}/src/google/protobuf/lite_arena_unittest.cc
@@ -197,17 +202,6 @@ set(lite_arena_test_files
 add_executable(lite-arena-test ${lite_arena_test_files} ${common_lite_test_files} ${lite_test_proto_files})
 add_executable(lite-arena-test ${lite_arena_test_files} ${common_lite_test_files} ${lite_test_proto_files})
 target_link_libraries(lite-arena-test libprotobuf-lite gmock_main)
 target_link_libraries(lite-arena-test libprotobuf-lite gmock_main)
 
 
-add_library(libtests STATIC ${common_test_files} ${tests_proto_files} ${lite_test_proto_files})
-
-# Native single tests
-add_executable(tests ${tests_files})
-target_link_libraries(tests libtests libprotoc libprotobuf gmock_main)
-
-# Sparated tests for CTest
-foreach(file ${tests_files})
-  get_filename_component(name ${file} NAME_WE)
-  add_executable(${name} ${file})
-  target_link_libraries(${name} libtests libprotoc libprotobuf gmock_main)
-  add_test(NAME ${name} COMMAND ${name}
-    WORKING_DIRECTORY ${protobuf_source_dir})
-endforeach()
+add_custom_target(check
+  COMMAND tests
+  WORKING_DIRECTORY ${protobuf_source_dir})

+ 4 - 6
src/google/protobuf/compiler/command_line_interface_unittest.cc

@@ -294,6 +294,10 @@ void CommandLineInterfaceTest::Run(const string& command) {
   if (!disallow_plugins_) {
   if (!disallow_plugins_) {
     cli_.AllowPlugins("prefix-");
     cli_.AllowPlugins("prefix-");
 #ifndef GOOGLE_THIRD_PARTY_PROTOBUF
 #ifndef GOOGLE_THIRD_PARTY_PROTOBUF
+    string plugin_path;
+#ifdef GOOGLE_PROTOBUF_TEST_PLUGIN_PATH
+    plugin_path = GOOGLE_PROTOBUF_TEST_PLUGIN_PATH;
+#else
     const char* possible_paths[] = {
     const char* possible_paths[] = {
       // When building with shared libraries, libtool hides the real executable
       // When building with shared libraries, libtool hides the real executable
       // in .libs and puts a fake wrapper in the current directory.
       // in .libs and puts a fake wrapper in the current directory.
@@ -311,12 +315,6 @@ void CommandLineInterfaceTest::Run(const string& command) {
       "test_plugin.exe",        // Other Win32 (MSVC)
       "test_plugin.exe",        // Other Win32 (MSVC)
       "test_plugin",            // Unix
       "test_plugin",            // Unix
     };
     };
-
-    string plugin_path;
-
-#ifdef GOOGLE_PROTOBUF_TEST_PLUGIN_PATH
-    plugin_path = GOOGLE_PROTOBUF_TEST_PLUGIN_PATH;
-#else
     for (int i = 0; i < GOOGLE_ARRAYSIZE(possible_paths); i++) {
     for (int i = 0; i < GOOGLE_ARRAYSIZE(possible_paths); i++) {
       if (access(possible_paths[i], F_OK) == 0) {
       if (access(possible_paths[i], F_OK) == 0) {
         plugin_path = possible_paths[i];
         plugin_path = possible_paths[i];