Use paths relative to executable location to find subprocess executables

This commit is contained in:
Sebastian Messmer 2019-03-23 22:01:26 -07:00
parent 9ee345e16a
commit 2150446a2c
17 changed files with 75 additions and 16 deletions

View File

@ -7,6 +7,7 @@ Fixed bugs:
Other: Other:
* Updated to crypto++ 8.1 * Updated to crypto++ 8.1
* Unit tests can now be run from any directory
Version 0.10.0 Version 0.10.0

View File

@ -36,7 +36,7 @@ build_script:
- cmd: cmake --build . --config %CONFIGURATION% - cmd: cmake --build . --config %CONFIGURATION%
- cmd: .\test\gitversion\gitversion-test.exe - 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 # 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\fspp\fspp-test.exe
- cmd: .\test\parallelaccessstore\parallelaccessstore-test.exe - cmd: .\test\parallelaccessstore\parallelaccessstore-test.exe
- cmd: .\test\blockstore\blockstore-test.exe - cmd: .\test\blockstore\blockstore-test.exe

View File

@ -1,6 +1,7 @@
if (BUILD_TESTING) if (BUILD_TESTING)
include_directories(../src) include_directories(../src)
add_subdirectory(my-gtest-main)
add_subdirectory(gitversion) add_subdirectory(gitversion)
add_subdirectory(cpp-utils) add_subdirectory(cpp-utils)
if (NOT MSVC) if (NOT MSVC)

View File

@ -27,7 +27,7 @@ set(SOURCES
) )
add_executable(${PROJECT_NAME} ${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}) add_test(${PROJECT_NAME} ${PROJECT_NAME})
target_enable_style_warnings(${PROJECT_NAME}) target_enable_style_warnings(${PROJECT_NAME})

View File

@ -42,7 +42,7 @@ set(SOURCES
) )
add_executable(${PROJECT_NAME} ${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}) add_test(${PROJECT_NAME} ${PROJECT_NAME})
target_enable_style_warnings(${PROJECT_NAME}) target_enable_style_warnings(${PROJECT_NAME})

View File

@ -69,7 +69,7 @@ target_activate_cpp14(${PROJECT_NAME}_exit_signal)
target_link_libraries(${PROJECT_NAME}_exit_signal cpp-utils) target_link_libraries(${PROJECT_NAME}_exit_signal cpp-utils)
add_executable(${PROJECT_NAME} ${SOURCES}) 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_dependencies(${PROJECT_NAME} ${PROJECT_NAME}_exit_status ${PROJECT_NAME}_exit_signal)
add_test(${PROJECT_NAME} ${PROJECT_NAME}) add_test(${PROJECT_NAME} ${PROJECT_NAME})

View File

@ -2,18 +2,24 @@
#include <csignal> #include <csignal>
#include "cpp-utils/assert/backtrace.h" #include "cpp-utils/assert/backtrace.h"
#include "cpp-utils/process/subprocess.h" #include "cpp-utils/process/subprocess.h"
#include <boost/filesystem.hpp>
#include "my-gtest-main.h"
using std::string; using std::string;
using testing::HasSubstr; using testing::HasSubstr;
namespace bf = boost::filesystem;
namespace { namespace {
std::string call_process_exiting_with(const std::string& kind, const std::string& signal = "") { std::string call_process_exiting_with(const std::string& kind, const std::string& signal = "") {
#if defined(_MSC_VER) #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 #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 #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); auto result = cpputils::Subprocess::call(command);
return result.output; return result.output;
} }

View File

@ -1,19 +1,26 @@
#include <cpp-utils/process/subprocess.h> #include <cpp-utils/process/subprocess.h>
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <boost/filesystem.hpp>
#include <cpp-utils/lock/ConditionBarrier.h> #include <cpp-utils/lock/ConditionBarrier.h>
#include "my-gtest-main.h"
using cpputils::Subprocess; using cpputils::Subprocess;
using cpputils::SubprocessError; using cpputils::SubprocessError;
using std::string;
namespace bf = boost::filesystem;
namespace { namespace {
std::string exit_with_message_and_status(const char* message, int status) { std::string exit_with_message_and_status(const char* message, int status) {
#if defined(_MSC_VER) #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 #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 #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);
} }
} }

View File

@ -16,7 +16,7 @@ set(SOURCES
) )
add_executable(${PROJECT_NAME} ${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}) add_test(${PROJECT_NAME} ${PROJECT_NAME})
target_enable_style_warnings(${PROJECT_NAME}) target_enable_style_warnings(${PROJECT_NAME})

View File

@ -24,7 +24,7 @@ set(SOURCES
) )
add_executable(${PROJECT_NAME} ${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}) add_test(${PROJECT_NAME} ${PROJECT_NAME})
target_enable_style_warnings(${PROJECT_NAME}) target_enable_style_warnings(${PROJECT_NAME})

View File

@ -102,7 +102,7 @@ set(SOURCES
testutils/OpenFileHandle.cpp testutils/OpenFileHandle.h) testutils/OpenFileHandle.cpp testutils/OpenFileHandle.h)
add_executable(${PROJECT_NAME} ${SOURCES}) 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}) add_test(${PROJECT_NAME} ${PROJECT_NAME})
target_enable_style_warnings(${PROJECT_NAME}) target_enable_style_warnings(${PROJECT_NAME})

View File

@ -6,7 +6,7 @@ set(SOURCES
) )
add_executable(${PROJECT_NAME} ${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}) add_test(${PROJECT_NAME} ${PROJECT_NAME})
target_enable_style_warnings(${PROJECT_NAME}) target_enable_style_warnings(${PROJECT_NAME})

View File

@ -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})

View File

@ -0,0 +1,27 @@
#include "my-gtest-main.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include <boost/optional.hpp>
#include <cpp-utils/assert/assert.h>
namespace {
boost::optional<boost::filesystem::path> 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();
}

View File

@ -0,0 +1,5 @@
#pragma once
#include <boost/filesystem/path.hpp>
const boost::filesystem::path& get_executable();

View File

@ -6,7 +6,7 @@ set(SOURCES
) )
add_executable(${PROJECT_NAME} ${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}) add_test(${PROJECT_NAME} ${PROJECT_NAME})
target_enable_style_warnings(${PROJECT_NAME}) target_enable_style_warnings(${PROJECT_NAME})

View File

@ -10,7 +10,6 @@ if (BUILD_TESTING)
project (googletest) project (googletest)
add_library(${PROJECT_NAME} dummy.cpp) add_library(${PROJECT_NAME} dummy.cpp)
target_link_libraries(${PROJECT_NAME} PUBLIC gtest gmock) 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) 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 :( # Disable "missing override" warning because gmock MOCK_METHOD() don't use override :(