diff options
author | Egor Tensin <Egor.Tensin@gmail.com> | 2020-10-17 02:56:39 +0300 |
---|---|---|
committer | Egor Tensin <Egor.Tensin@gmail.com> | 2020-10-17 02:56:39 +0300 |
commit | 83efec86e7d23d02160719229df4790805432c35 (patch) | |
tree | 8d87a40484514043f50d092636fcf9340c06edc4 /test/unit_tests/fixtures.hpp | |
parent | CommandLine: refactoring (diff) | |
download | winapi-common-83efec86e7d23d02160719229df4790805432c35.tar.gz winapi-common-83efec86e7d23d02160719229df4790805432c35.zip |
process_tests: remove temp files
Diffstat (limited to '')
-rw-r--r-- | test/unit_tests/fixtures.hpp | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/test/unit_tests/fixtures.hpp b/test/unit_tests/fixtures.hpp new file mode 100644 index 0000000..293ad35 --- /dev/null +++ b/test/unit_tests/fixtures.hpp @@ -0,0 +1,61 @@ +// Copyright (c) 2020 Egor Tensin <Egor.Tensin@gmail.com> +// This file is part of the "winapi-common" project. +// For details, see https://github.com/egor-tensin/winapi-common. +// Distributed under the MIT License. + +#pragma once + +#include <winapi/cmd_line.hpp> +#include <winapi/file.hpp> +#include <winapi/path.hpp> + +#include <boost/test/unit_test.hpp> + +#include <exception> +#include <stdexcept> +#include <string> + +class RemoveFileGuard { +public: + explicit RemoveFileGuard(const winapi::CanonicalPath& path) : m_path(path) {} + + ~RemoveFileGuard() { + try { + winapi::File::remove(m_path); + } catch (const std::exception& e) { + BOOST_TEST_MESSAGE("Couldn't remove file " << m_path.get() << ": " << e.what()); + } + } + +private: + winapi::CanonicalPath m_path; + + RemoveFileGuard(const RemoveFileGuard&) = delete; + RemoveFileGuard& operator=(const RemoveFileGuard&) = delete; +}; + +class WithEchoExe { +public: + WithEchoExe() : m_echo_exe(find_echo_exe()) {} + + const std::string& get_echo_exe() { return m_echo_exe; } + +private: + static std::string find_echo_exe() { + static const std::string prefix{"--echo_exe="}; + return find_param_value(prefix); + } + + static std::string find_param_value(const std::string& param_prefix) { + const auto cmd_line = winapi::CommandLine::query(); + const auto& args = cmd_line.get_args(); + for (const auto& arg : args) { + if (arg.rfind(param_prefix, 0) == 0) { + return arg.substr(param_prefix.length()); + } + } + throw std::runtime_error{"couldn't find parameter " + param_prefix}; + } + + std::string m_echo_exe; +}; |