aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--include/winapi/file.hpp3
-rw-r--r--src/file.cpp16
-rw-r--r--test/unit_tests/CMakeLists.txt5
-rw-r--r--test/unit_tests/fixtures.hpp61
-rw-r--r--test/unit_tests/process.cpp40
5 files changed, 89 insertions, 36 deletions
diff --git a/include/winapi/file.hpp b/include/winapi/file.hpp
index fd34783..3dcd0d1 100644
--- a/include/winapi/file.hpp
+++ b/include/winapi/file.hpp
@@ -22,6 +22,9 @@ public:
static Handle open_w(const std::string&);
static Handle open_w(const CanonicalPath&);
+ static void remove(const std::string&);
+ static void remove(const CanonicalPath&);
+
using Handle::close;
using Handle::read;
diff --git a/src/file.cpp b/src/file.cpp
index c6a4ff4..165f36e 100644
--- a/src/file.cpp
+++ b/src/file.cpp
@@ -69,6 +69,14 @@ Handle open_file(const std::wstring& path, const CreateFileParams& params) {
return Handle{handle};
}
+void remove_file(const std::wstring& path) {
+ const auto ret = ::DeleteFileW(path.c_str());
+
+ if (!ret) {
+ throw error::windows(GetLastError(), "DeleteFileW");
+ }
+}
+
} // namespace
Handle File::open_r(const std::string& path) {
@@ -87,4 +95,12 @@ Handle File::open_w(const CanonicalPath& path) {
return open_file(to_system_path(path), CreateFileParams::write());
}
+void File::remove(const std::string& path) {
+ remove_file(to_system_path(path));
+}
+
+void File::remove(const CanonicalPath& path) {
+ remove_file(to_system_path(path));
+}
+
} // namespace winapi
diff --git a/test/unit_tests/CMakeLists.txt b/test/unit_tests/CMakeLists.txt
index 4580a57..2470954 100644
--- a/test/unit_tests/CMakeLists.txt
+++ b/test/unit_tests/CMakeLists.txt
@@ -1,5 +1,6 @@
-file(GLOB unit_tests_src "*.cpp")
-add_executable(unit_tests ${unit_tests_src})
+file(GLOB unit_tests_cpp "*.cpp")
+file(GLOB unit_tests_hpp "*.hpp")
+add_executable(unit_tests ${unit_tests_cpp} ${unit_tests_hpp})
target_link_libraries(unit_tests PRIVATE winapi_common winapi_utf8)
set_target_properties(unit_tests PROPERTIES OUTPUT_NAME winapi-common-unit-tests)
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;
+};
diff --git a/test/unit_tests/process.cpp b/test/unit_tests/process.cpp
index 957a507..ced11ab 100644
--- a/test/unit_tests/process.cpp
+++ b/test/unit_tests/process.cpp
@@ -3,6 +3,8 @@
// For details, see https://github.com/egor-tensin/winapi-common.
// Distributed under the MIT License.
+#include "fixtures.hpp"
+
#include <winapi/cmd_line.hpp>
#include <winapi/file.hpp>
#include <winapi/path.hpp>
@@ -12,43 +14,11 @@
#include <boost/test/unit_test.hpp>
-#include <stdexcept>
-#include <string>
#include <utility>
using namespace winapi;
using namespace winapi::process;
-namespace {
-
-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 = 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};
- }
-
- const std::string m_echo_exe;
-};
-
-} // namespace
-
BOOST_AUTO_TEST_SUITE(process_tests)
BOOST_FIXTURE_TEST_CASE(echo, WithEchoExe) {
@@ -81,11 +51,13 @@ BOOST_FIXTURE_TEST_CASE(echo_stdout_to_pipe, WithEchoExe) {
BOOST_FIXTURE_TEST_CASE(echo_stdout_to_file, WithEchoExe) {
const CommandLine cmd_line{get_echo_exe(), {"XXX", "YYY", "ZZZ"}};
Process::IO io;
- io.std_out = Stdout{CanonicalPath{"test.txt"}};
+ const CanonicalPath stdout_path{"test.txt"};
+ const RemoveFileGuard remove_stdout_file{stdout_path};
+ io.std_out = Stdout{stdout_path};
const auto process = Process::create(cmd_line, std::move(io));
process.wait();
BOOST_TEST(process.get_exit_code() == 0);
- const auto output = File::open_r(CanonicalPath{"test.txt"}).read();
+ const auto output = File::open_r(stdout_path).read();
const auto utf8 = narrow(output);
BOOST_TEST(utf8 == "XXX\r\nYYY\r\nZZZ\r\n");
}