File: SubprocessTest.cpp

package info (click to toggle)
cryfs 1.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 28,412 kB
  • sloc: cpp: 150,187; asm: 10,493; python: 1,455; javascript: 65; sh: 50; makefile: 17; xml: 7
file content (173 lines) | stat: -rw-r--r-- 5,419 bytes parent folder | download | duplicates (3)
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include <cpp-utils/process/subprocess.h>
#include <gtest/gtest.h>
#include <boost/filesystem.hpp>

#include <cpp-utils/lock/ConditionBarrier.h>
#include "my-gtest-main.h"

using cpputils::Subprocess;
using cpputils::SubprocessError;
using std::string;
namespace bf = boost::filesystem;

// TODO Test passing input to stdin of processes
// TODO Test stderr

#if defined(_MSC_VER)
constexpr const char* NEWLINE = "\r\n";
#else
constexpr const char* NEWLINE = "\n";
#endif

namespace
{
    bf::path exit_with_message_and_status()
    {
#if defined(_MSC_VER)
        auto executable = bf::canonical(get_executable().parent_path()) / "cpp-utils-test_exit_status.exe";
#else
        auto executable = bf::canonical(get_executable().parent_path()) / "cpp-utils-test_exit_status";
#endif
        if (!bf::exists(executable))
        {
            throw std::runtime_error(executable.string() + " not found.");
        }
        return executable;
    }
}

TEST(SubprocessTest, CheckCall_success_output)
{
    EXPECT_EQ(std::string("hello") + NEWLINE, Subprocess::check_call(exit_with_message_and_status(), {"0", "hello"}, "").output_stdout);
}

TEST(SubprocessTest, CheckCall_successwithemptyoutput_output)
{
    EXPECT_EQ("", Subprocess::check_call(exit_with_message_and_status(), {"0"}, "").output_stdout);
}

TEST(SubprocessTest, CheckCall_success_exitcode)
{
    EXPECT_EQ(0, Subprocess::check_call(exit_with_message_and_status(), {"0", "hello"}, "").exitcode);
}

TEST(SubprocessTest, CheckCall_successwithemptyoutput_exitcode)
{
    EXPECT_EQ(0, Subprocess::check_call(exit_with_message_and_status(), {"0"}, "").exitcode);
}

TEST(SubprocessTest, CheckCall_error)
{
    EXPECT_THROW(
        Subprocess::check_call(exit_with_message_and_status(), {"1"}, ""),
        SubprocessError);
}

TEST(SubprocessTest, CheckCall_error5)
{
    EXPECT_THROW(
        Subprocess::check_call(exit_with_message_and_status(), {"5"}, ""),
        SubprocessError);
}

TEST(SubprocessTest, CheckCall_errorwithoutput)
{
    EXPECT_THROW(
        Subprocess::check_call(exit_with_message_and_status(), {"1", "hello"}, ""),
        SubprocessError);
}

TEST(SubprocessTest, CheckCall_error5withoutput)
{
    EXPECT_THROW(
        Subprocess::check_call(exit_with_message_and_status(), {"5", "hello"}, ""),
        SubprocessError);
}

TEST(SubprocessTest, Call_success_exitcode)
{
    EXPECT_EQ(0, Subprocess::call(exit_with_message_and_status(), {"0", "hello"}, "").exitcode);
}

TEST(SubprocessTest, Call_success_output)
{
    EXPECT_EQ(std::string("hello") + NEWLINE, Subprocess::call(exit_with_message_and_status(), {"0", "hello"}, "").output_stdout);
}

TEST(SubprocessTest, Call_error_exitcode)
{
    EXPECT_EQ(1, Subprocess::call(exit_with_message_and_status(), {"1"}, "").exitcode);
}

TEST(SubprocessTest, Call_error_output)
{
    EXPECT_EQ("", Subprocess::call(exit_with_message_and_status(), {"1"}, "").output_stdout);
}

TEST(SubprocessTest, Call_error5_exitcode)
{
    EXPECT_EQ(5, Subprocess::call(exit_with_message_and_status(), {"5"}, "").exitcode);
}

TEST(SubprocessTest, Call_error5_output)
{
    EXPECT_EQ("", Subprocess::call(exit_with_message_and_status(), {"1"}, "").output_stdout);
}

TEST(SubprocessTest, Call_errorwithoutput_output)
{
    EXPECT_EQ(std::string("hello") + NEWLINE, Subprocess::call(exit_with_message_and_status(), {"1", "hello"}, "").output_stdout);
}

TEST(SubprocessTest, Call_errorwithoutput_exitcode)
{
    EXPECT_EQ(1, Subprocess::call(exit_with_message_and_status(), {"1", "hello"}, "").exitcode);
}

TEST(SubprocessTest, Call_error5withoutput_output)
{
    EXPECT_EQ(std::string("hello") + NEWLINE, Subprocess::call(exit_with_message_and_status(), {"5", "hello"}, "").output_stdout);
}

TEST(SubprocessTest, Call_error5withoutput_exitcode)
{
    EXPECT_EQ(5, Subprocess::call(exit_with_message_and_status(), {"5", "hello"}, "").exitcode);
}

// TODO Move this test to a test suite for ThreadSystem/LoopThread
#include <cpp-utils/thread/LoopThread.h>
TEST(SubprocessTest, CallFromThreadSystemThread)
{
    cpputils::ConditionBarrier barrier;

    cpputils::LoopThread thread(
        [&barrier]()
        {
            auto result = Subprocess::check_call(exit_with_message_and_status(), {"0", "hello"}, "");
            EXPECT_EQ(0, result.exitcode);
            EXPECT_EQ(std::string("hello") + NEWLINE, result.output_stdout);

            barrier.release();

            return false; // don't run loop again
        },
        "child_thread");
    thread.start();
    barrier.wait();
    thread.stop(); // just to make sure it's stopped before the test exits. Returning false above should already stop it, but we don't know when exactly. thread.stop() will block until it's actually stopped.
}

TEST(SubprocessTest, Call_argumentwithspaces)
{
    // Test that arguments can have spaces and are still treated as one argument
    EXPECT_EQ(std::string("hello world") + NEWLINE, Subprocess::check_call(exit_with_message_and_status(), {"0", "hello world"}, "").output_stdout);
    EXPECT_EQ(std::string("hello") + NEWLINE + "world" + NEWLINE, Subprocess::check_call(exit_with_message_and_status(), {"0", "hello", "world"}, "").output_stdout);
}

#if !defined(_MSC_VER)
TEST(SubprocessTest, Call_withcommandfrompath)
{
    // Test that we can call a system command without specifying the full path
    EXPECT_EQ("hello\n", Subprocess::check_call("echo", {"hello"}, "").output_stdout);
}
#endif