diff --git a/ChangeLog.txt b/ChangeLog.txt index 25eed6ba..1b179dda 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -7,6 +7,7 @@ Fixed bugs: Other: * Updated to crypto++ 8.1 +* Unit tests can now be run from any directory Version 0.10.0 diff --git a/appveyor.yml b/appveyor.yml index 9f2857ae..7085040e 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -36,7 +36,7 @@ build_script: - cmd: cmake --build . --config %CONFIGURATION% - cmd: .\test\gitversion\gitversion-test.exe # cpp-utils-test disables ThreadDebuggingTest_ThreadName.*_thenIsCorrect because the appveyor image is too old to support the API needed for that - - cmd: cd .\test\cpp-utils\ && .\cpp-utils-test.exe --gtest_filter=-ThreadDebuggingTest_ThreadName.*_thenIsCorrect && cd ..\.. + - cmd: .\test\cpp-utils\cpp-utils-test.exe --gtest_filter=-ThreadDebuggingTest_ThreadName.*_thenIsCorrect #- cmd: .\test\fspp\fspp-test.exe - cmd: .\test\parallelaccessstore\parallelaccessstore-test.exe - cmd: .\test\blockstore\blockstore-test.exe diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index d8d77fa8..4879389e 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,6 +1,7 @@ if (BUILD_TESTING) include_directories(../src) + add_subdirectory(my-gtest-main) add_subdirectory(gitversion) add_subdirectory(cpp-utils) if (NOT MSVC) diff --git a/test/blobstore/CMakeLists.txt b/test/blobstore/CMakeLists.txt index f9eeb8d7..05e98b8d 100644 --- a/test/blobstore/CMakeLists.txt +++ b/test/blobstore/CMakeLists.txt @@ -27,7 +27,7 @@ set(SOURCES ) add_executable(${PROJECT_NAME} ${SOURCES}) -target_link_libraries(${PROJECT_NAME} googletest blobstore) +target_link_libraries(${PROJECT_NAME} my-gtest-main googletest blobstore) add_test(${PROJECT_NAME} ${PROJECT_NAME}) target_enable_style_warnings(${PROJECT_NAME}) diff --git a/test/blockstore/CMakeLists.txt b/test/blockstore/CMakeLists.txt index 54167aef..ca63acce 100644 --- a/test/blockstore/CMakeLists.txt +++ b/test/blockstore/CMakeLists.txt @@ -42,7 +42,7 @@ set(SOURCES ) add_executable(${PROJECT_NAME} ${SOURCES}) -target_link_libraries(${PROJECT_NAME} googletest blockstore) +target_link_libraries(${PROJECT_NAME} my-gtest-main googletest blockstore) add_test(${PROJECT_NAME} ${PROJECT_NAME}) target_enable_style_warnings(${PROJECT_NAME}) diff --git a/test/cpp-utils/CMakeLists.txt b/test/cpp-utils/CMakeLists.txt index 831683c9..9a0c71cd 100644 --- a/test/cpp-utils/CMakeLists.txt +++ b/test/cpp-utils/CMakeLists.txt @@ -69,7 +69,7 @@ target_activate_cpp14(${PROJECT_NAME}_exit_signal) target_link_libraries(${PROJECT_NAME}_exit_signal cpp-utils) add_executable(${PROJECT_NAME} ${SOURCES}) -target_link_libraries(${PROJECT_NAME} googletest cpp-utils) +target_link_libraries(${PROJECT_NAME} my-gtest-main googletest cpp-utils) add_dependencies(${PROJECT_NAME} ${PROJECT_NAME}_exit_status ${PROJECT_NAME}_exit_signal) add_test(${PROJECT_NAME} ${PROJECT_NAME}) diff --git a/test/cpp-utils/assert/backtrace_test.cpp b/test/cpp-utils/assert/backtrace_test.cpp index 769ae8a2..77d18bc1 100644 --- a/test/cpp-utils/assert/backtrace_test.cpp +++ b/test/cpp-utils/assert/backtrace_test.cpp @@ -2,18 +2,24 @@ #include #include "cpp-utils/assert/backtrace.h" #include "cpp-utils/process/subprocess.h" +#include +#include "my-gtest-main.h" using std::string; using testing::HasSubstr; +namespace bf = boost::filesystem; namespace { std::string call_process_exiting_with(const std::string& kind, const std::string& signal = "") { #if defined(_MSC_VER) - constexpr const char* executable = "cpp-utils-test_exit_signal.exe"; + auto executable = get_executable().parent_path() / "cpp-utils-test_exit_signal.exe"; #else - constexpr const char* executable = "./test/cpp-utils/cpp-utils-test_exit_signal"; + auto executable = get_executable().parent_path() / "cpp-utils-test_exit_signal"; #endif - const std::string command = std::string(executable) + " \"" + kind + "\" \"" + signal + "\" 2>&1"; + if (!bf::exists(executable)) { + throw std::runtime_error(executable.string() + " not found."); + } + const std::string command = executable.string() + " \"" + kind + "\" \"" + signal + "\" 2>&1"; auto result = cpputils::Subprocess::call(command); return result.output; } diff --git a/test/cpp-utils/process/SubprocessTest.cpp b/test/cpp-utils/process/SubprocessTest.cpp index 9a1c857c..c9ed0a83 100644 --- a/test/cpp-utils/process/SubprocessTest.cpp +++ b/test/cpp-utils/process/SubprocessTest.cpp @@ -1,19 +1,26 @@ #include #include +#include #include +#include "my-gtest-main.h" using cpputils::Subprocess; using cpputils::SubprocessError; +using std::string; +namespace bf = boost::filesystem; namespace { std::string exit_with_message_and_status(const char* message, int status) { #if defined(_MSC_VER) - constexpr const char* executable = "cpp-utils-test_exit_status.exe"; + auto executable = get_executable().parent_path() / "cpp-utils-test_exit_status.exe"; #else - constexpr const char* executable = "./test/cpp-utils/cpp-utils-test_exit_status"; + auto executable = get_executable().parent_path() / "cpp-utils-test_exit_status"; #endif - return std::string(executable) + " \"" + message + "\" " + std::to_string(status); + if (!bf::exists(executable)) { + throw std::runtime_error(executable.string() + " not found."); + } + return executable.string() + " \"" + message + "\" " + std::to_string(status); } } diff --git a/test/cryfs-cli/CMakeLists.txt b/test/cryfs-cli/CMakeLists.txt index b120419a..2d0b38c5 100644 --- a/test/cryfs-cli/CMakeLists.txt +++ b/test/cryfs-cli/CMakeLists.txt @@ -16,7 +16,7 @@ set(SOURCES ) add_executable(${PROJECT_NAME} ${SOURCES}) -target_link_libraries(${PROJECT_NAME} googletest cryfs-cli cryfs-unmount fspp-fuse) +target_link_libraries(${PROJECT_NAME} my-gtest-main googletest cryfs-cli cryfs-unmount fspp-fuse) add_test(${PROJECT_NAME} ${PROJECT_NAME}) target_enable_style_warnings(${PROJECT_NAME}) diff --git a/test/cryfs/CMakeLists.txt b/test/cryfs/CMakeLists.txt index 463f13e6..77a025f4 100644 --- a/test/cryfs/CMakeLists.txt +++ b/test/cryfs/CMakeLists.txt @@ -24,7 +24,7 @@ set(SOURCES ) add_executable(${PROJECT_NAME} ${SOURCES}) -target_link_libraries(${PROJECT_NAME} googletest cryfs) +target_link_libraries(${PROJECT_NAME} my-gtest-main googletest cryfs) add_test(${PROJECT_NAME} ${PROJECT_NAME}) target_enable_style_warnings(${PROJECT_NAME}) diff --git a/test/fspp/CMakeLists.txt b/test/fspp/CMakeLists.txt index 8f94cf2b..dabff1f1 100644 --- a/test/fspp/CMakeLists.txt +++ b/test/fspp/CMakeLists.txt @@ -102,7 +102,7 @@ set(SOURCES testutils/OpenFileHandle.cpp testutils/OpenFileHandle.h) add_executable(${PROJECT_NAME} ${SOURCES}) -target_link_libraries(${PROJECT_NAME} googletest fspp-interface fspp-fuse) +target_link_libraries(${PROJECT_NAME} my-gtest-main googletest fspp-interface fspp-fuse) add_test(${PROJECT_NAME} ${PROJECT_NAME}) target_enable_style_warnings(${PROJECT_NAME}) diff --git a/test/gitversion/CMakeLists.txt b/test/gitversion/CMakeLists.txt index ea3221a6..51a5ccc1 100644 --- a/test/gitversion/CMakeLists.txt +++ b/test/gitversion/CMakeLists.txt @@ -6,7 +6,7 @@ set(SOURCES ) add_executable(${PROJECT_NAME} ${SOURCES}) -target_link_libraries(${PROJECT_NAME} googletest gitversion) +target_link_libraries(${PROJECT_NAME} my-gtest-main googletest gitversion) add_test(${PROJECT_NAME} ${PROJECT_NAME}) target_enable_style_warnings(${PROJECT_NAME}) diff --git a/test/my-gtest-main/CMakeLists.txt b/test/my-gtest-main/CMakeLists.txt new file mode 100644 index 00000000..1d1e7e08 --- /dev/null +++ b/test/my-gtest-main/CMakeLists.txt @@ -0,0 +1,13 @@ +project (my-gtest-main) + +set(SOURCES + my-gtest-main.cpp +) + +add_library(${PROJECT_NAME} STATIC ${SOURCES}) +target_link_libraries(${PROJECT_NAME} PUBLIC googletest cpp-utils) +target_add_boost(${PROJECT_NAME} filesystem system) +target_include_directories(${PROJECT_NAME} PUBLIC .) + +target_enable_style_warnings(${PROJECT_NAME}) +target_activate_cpp14(${PROJECT_NAME}) diff --git a/test/my-gtest-main/my-gtest-main.cpp b/test/my-gtest-main/my-gtest-main.cpp new file mode 100644 index 00000000..c526e1a4 --- /dev/null +++ b/test/my-gtest-main/my-gtest-main.cpp @@ -0,0 +1,27 @@ +#include "my-gtest-main.h" + +#include "gmock/gmock.h" +#include "gtest/gtest.h" + +#include +#include + +namespace { + boost::optional executable; +} + +const boost::filesystem::path& get_executable() { + ASSERT(executable != boost::none, "Executable path not set"); + return *executable; +} + + +int main(int argc, char** argv) { + executable = boost::filesystem::path(argv[0]); + + // Since Google Mock depends on Google Test, InitGoogleMock() is + // also responsible for initializing Google Test. Therefore there's + // no need for calling testing::InitGoogleTest() separately. + testing::InitGoogleMock(&argc, argv); + return RUN_ALL_TESTS(); +} diff --git a/test/my-gtest-main/my-gtest-main.h b/test/my-gtest-main/my-gtest-main.h new file mode 100644 index 00000000..d77b379f --- /dev/null +++ b/test/my-gtest-main/my-gtest-main.h @@ -0,0 +1,5 @@ +#pragma once + +#include + +const boost::filesystem::path& get_executable(); diff --git a/test/parallelaccessstore/CMakeLists.txt b/test/parallelaccessstore/CMakeLists.txt index 0ec0b5e2..16170d17 100644 --- a/test/parallelaccessstore/CMakeLists.txt +++ b/test/parallelaccessstore/CMakeLists.txt @@ -6,7 +6,7 @@ set(SOURCES ) add_executable(${PROJECT_NAME} ${SOURCES}) -target_link_libraries(${PROJECT_NAME} googletest parallelaccessstore) +target_link_libraries(${PROJECT_NAME} my-gtest-main googletest parallelaccessstore) add_test(${PROJECT_NAME} ${PROJECT_NAME}) target_enable_style_warnings(${PROJECT_NAME}) diff --git a/vendor/googletest/CMakeLists.txt b/vendor/googletest/CMakeLists.txt index 2b4b5d24..6c7e98ae 100644 --- a/vendor/googletest/CMakeLists.txt +++ b/vendor/googletest/CMakeLists.txt @@ -10,7 +10,6 @@ if (BUILD_TESTING) project (googletest) add_library(${PROJECT_NAME} dummy.cpp) target_link_libraries(${PROJECT_NAME} PUBLIC gtest gmock) - target_link_libraries(${PROJECT_NAME} PRIVATE gmock_main) target_include_directories(${PROJECT_NAME} SYSTEM INTERFACE ${gtest_INCLUDE_DIRS}/include SYSTEM ${gmock_INCLUDE_DIRS}/include) # Disable "missing override" warning because gmock MOCK_METHOD() don't use override :(