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
|
#include <uunit.h>
#include <fstream>
#include <map>
#include <vector>
#include <execute.h>
#include <util.h>
#include <algorithm>
#include "paths.h"
#include "tmpfile.h"
class ExecuteTest
: public uUnit
{
public:
ExecuteTest()
{
uTEST(ExecuteTest::return_value);
uTEST(ExecuteTest::env);
}
void return_value()
{
auto cur_path = std::filesystem::path(paths::argv_0).parent_path();
std::vector<std::string> paths{{cur_path.string()}};
auto cmd = locate("testprog", paths);
uASSERT(!cmd.empty());
uASSERT_EQUAL(0, execute(cmd, {"retval", "0"}, {}, false));
uASSERT_EQUAL(1, execute(cmd, {"retval", "1"}, {}, false));
uASSERT_EQUAL(1, execute("no-such-binary", {}, {}, false));
}
void env()
{
using namespace std::string_literals;
auto cur_path = std::filesystem::path(paths::argv_0).parent_path();
std::vector<std::string> paths{{cur_path.string()}};
auto cmd = locate("testprog", paths);
uASSERT(!cmd.empty());
tmp_file tmp;
std::map<std::string, std::string> env;
// New env vars
env["foo"] = "bar";
env["bar"] = "42";
// Overwrite the exiting LANG var
env["LANG"] = "foo";
uASSERT_EQUAL(0, execute(cmd, {"envdump", tmp.get()}, env, false));
std::vector<std::string> vars;
{
std::ifstream infile(tmp.get());
std::string line;
while (std::getline(infile, line))
{
vars.push_back(line);
}
}
// Check the two explicitly set vars
auto chk = std::find(vars.begin(), vars.end(), "foo=bar"s);
uASSERT(chk != vars.end());
chk = std::find(vars.begin(), vars.end(), "bar=42"s);
uASSERT(chk != vars.end());
// Check the one that should have overwritten the existing one (probably LANG=en_US.UTF-8 or something)
chk = std::find(vars.begin(), vars.end(), "LANG=foo"s);
uASSERT(chk != vars.end());
// Check that other vars are also there (ie. the env wasn't cleared on entry)
uASSERT(vars.size() > 3);
}
};
// Registers the fixture into the 'registry'
static ExecuteTest test;
|