From 004c1d3b39f045f0f3935f6ca7cb9cb1960573ea Mon Sep 17 00:00:00 2001 From: Chris Wilson Date: Sun, 13 Dec 2015 23:44:05 +0000 Subject: Add CTest configurations to CMake. Make some tests work on Windows/MSVC. --- infrastructure/cmake/CMakeLists.txt | 17 +++++++++-- lib/common/Test.cpp | 59 ++++++++++++++++++++++++++++++++++++- test/bbackupd/testbbackupd.cpp | 4 +++ 3 files changed, 76 insertions(+), 4 deletions(-) diff --git a/infrastructure/cmake/CMakeLists.txt b/infrastructure/cmake/CMakeLists.txt index 8af365eb..805568da 100644 --- a/infrastructure/cmake/CMakeLists.txt +++ b/infrastructure/cmake/CMakeLists.txt @@ -1,5 +1,6 @@ cmake_minimum_required(VERSION 2.6) project(BoxBackup) +enable_testing() set(base_dir ${CMAKE_SOURCE_DIR}/../..) @@ -61,7 +62,7 @@ if(NOT status EQUAL 0) "status ${status}: ${command_output}") endif() -# Parsing Makefile.extra files in CMake script is a pain, so the relevant rules for +# Parsing Makefile.extra files in CMake script is a pain, so the relevant rules for # code-generating Perl scripts are hard-coded here. set(exception_files @@ -166,10 +167,18 @@ foreach(module_dep string(REGEX REPLACE " $" "" dependencies "${dependencies}") if(module_name MATCHES "^bin_") + string(REGEX MATCH "^bin_(.*)" valid_exe ${module_name}) + set(bin_name ${CMAKE_MATCH_1}) if(DEBUG) message(STATUS "add executable '${module_name}': '${module_files}'") endif() add_executable(${module_name} ${module_files}) + + # We need to install binaries in specific places so that tests + # can find them: + install(FILES "$" + DESTINATION "${base_dir}/debug/${module_dir}" + RENAME "${bin_name}${CMAKE_EXECUTABLE_SUFFIX}") elseif(module_name MATCHES "^test_") string(REGEX MATCH "^test_(.*)" valid_test ${module_name}) set(test_name ${CMAKE_MATCH_1}) @@ -182,6 +191,8 @@ foreach(module_dep file(WRITE "${module_path}/_main.cpp" "${test_main}") add_executable(${module_name} ${module_files} "${module_path}/_main.cpp") + add_test(NAME ${test_name} + COMMAND ${PERL_EXECUTABLE} ${base_dir}/runtest.pl ${test_name}) elseif(module_name MATCHES "^(lib_.*|qdbm)$") if(DEBUG) message(STATUS "add library '${module_name}': '${module_files}'") @@ -191,7 +202,7 @@ foreach(module_dep message(FATAL_ERROR "Unsupported module type: " ${module_name}) endif() - target_compile_definitions(${module_name} PRIVATE -DBOX_MODULE="${module_dir}") + target_compile_definitions(${module_name} PRIVATE -DBOX_MODULE="${module_name}") if(dependencies) string(REGEX REPLACE "[ ]+" ";" dependency_list "${dependencies}") @@ -212,7 +223,7 @@ foreach(module_dep get_property(dep_include_dirs TARGET ${dependency} PROPERTY INTERFACE_INCLUDE_DIRECTORIES) - target_include_directories(${module_name} + target_include_directories(${module_name} PUBLIC ${dep_include_dirs}) endif() endforeach() diff --git a/lib/common/Test.cpp b/lib/common/Test.cpp index 9385d8c3..df2c3bc6 100644 --- a/lib/common/Test.cpp +++ b/lib/common/Test.cpp @@ -22,7 +22,9 @@ #endif #include "BoxTime.h" +#include "FileStream.h" #include "Test.h" +#include "Utils.h" int num_tests_selected = 0; int num_failures = 0; @@ -81,6 +83,60 @@ bool setUp(const char* function_name) } } +#ifdef _MSC_VER + DIR* pDir = opendir("testfiles"); + if(!pDir) + { + THROW_SYS_FILE_ERROR("Failed to open test temporary directory", + "testfiles", CommonException, Internal); + } + struct dirent* pEntry; + for(pEntry = readdir(pDir); pEntry; pEntry = readdir(pDir)) + { + std::string filename = pEntry->d_name; + if(StartsWith("TestDir", filename) || + StartsWith("0_", filename) || + filename == "accounts.txt" || + StartsWith("file", filename) || + StartsWith("notifyran", filename) || + StartsWith("notifyscript.tag", filename) || + StartsWith("restore", filename) || + filename == "bbackupd-data" || + filename == "syncallowscript.control" || + StartsWith("syncallowscript.notifyran.", filename) || + filename == "test2.downloaded") + { + int filetype = ObjectExists(std::string("testfiles/") + filename); + if(filetype == ObjectExists_File) + { + if(!::unlink(filename.c_str())) + { + TEST_FAIL_WITH_MESSAGE(BOX_SYS_ERROR_MESSAGE("Failed to delete " + "test fixture file: unlink(\"" << filename << "\")")); + } + } + else if(filetype == ObjectExists_Dir) + { + std::string cmd = "rmdir /s /q testfiles\\" + filename; + int status = system(cmd.c_str()); + if(status != 0) + { + TEST_FAIL_WITH_MESSAGE("Failed to delete test fixture " + "file: command '" << cmd << "' exited with " + "status " << status); + } + } + else + { + TEST_FAIL_WITH_MESSAGE("Don't know how to delete file " << filename << + " of type " << filetype); + } + } + } + closedir(pDir); + FileStream touch("testfiles/accounts.txt", O_WRONLY | O_CREAT | O_TRUNC, + S_IRUSR | S_IWUSR); +#else TEST_THAT_THROWONFAIL(system( "rm -rf testfiles/TestDir* testfiles/0_0 testfiles/0_1 " "testfiles/0_2 testfiles/accounts.txt " // testfiles/test* .tgz! @@ -91,11 +147,12 @@ bool setUp(const char* function_name) "testfiles/syncallowscript.notifyran.* " "testfiles/test2.downloaded" ) == 0); + TEST_THAT_THROWONFAIL(system("touch testfiles/accounts.txt") == 0); +#endif TEST_THAT_THROWONFAIL(mkdir("testfiles/0_0", 0755) == 0); TEST_THAT_THROWONFAIL(mkdir("testfiles/0_1", 0755) == 0); TEST_THAT_THROWONFAIL(mkdir("testfiles/0_2", 0755) == 0); TEST_THAT_THROWONFAIL(mkdir("testfiles/bbackupd-data", 0755) == 0); - TEST_THAT_THROWONFAIL(system("touch testfiles/accounts.txt") == 0); return true; } diff --git a/test/bbackupd/testbbackupd.cpp b/test/bbackupd/testbbackupd.cpp index 4b843e6a..a9855250 100644 --- a/test/bbackupd/testbbackupd.cpp +++ b/test/bbackupd/testbbackupd.cpp @@ -35,6 +35,10 @@ #include #endif +#ifdef WIN32 + #include +#endif + #include #ifdef HAVE_SYSCALL -- cgit v1.2.3