aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/test/unit_tests/fixtures.hpp
blob: aaf6571b7aba2f1747ad52ef3fe6d1f172df8791 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// 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 "shared/command.hpp"

#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 WithParam {
public:
    WithParam(const std::string& param_prefix) : m_value(find_param_value(param_prefix)) {}

    const std::string& get_value() { return m_value; }

private:
    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_value;
};

class WithEchoExe : public WithParam {
public:
    WithEchoExe() : WithParam{"--echo_exe="} {}

    const std::string& get_echo_exe() { return get_value(); }
};

class WithWorkerExe : public WithParam {
public:
    WithWorkerExe() : WithParam{"--worker_exe="}, m_cmd{worker::Command::create()} {}

    const std::string& get_worker_exe() { return get_value(); }

    worker::Command::Shared m_cmd;
};