#include #include #include #include #include #include #include #include "paths.h" #include "tmpfile.h" class ExecuteTest : public uUnit { public: ExecuteTest() { uTEST(ExecuteTest::return_value); uTEST(ExecuteTest::env); } void return_value() { ctor::settings s; auto cur_path = std::filesystem::path(paths::argv_0).parent_path(); std::vector paths{{cur_path.string()}}; auto cmd = locate("testprog", paths); uASSERT(!cmd.empty()); auto value = execute(s, cmd, {"retval", "0"}, {}, false); uASSERT_EQUAL(0, value); value = execute(s, cmd, {"retval", "1"}, {}, false); uASSERT_EQUAL(1, value); value = execute(s, "no-such-binary", {}, {}, false); uASSERT_EQUAL(1, value); } void env() { using namespace std::string_literals; ctor::settings s; auto cur_path = std::filesystem::path(paths::argv_0).parent_path(); std::vector paths{{cur_path.string()}}; auto cmd = locate("testprog", paths); uASSERT(!cmd.empty()); tmp_file tmp; std::map env; // New env vars env["foo"] = "bar"; env["bar"] = "42"; // Overwrite the exiting LANG var env["LANG"] = "foo"; auto value = execute(s, cmd, {"envdump", tmp.get()}, env, false); uASSERT_EQUAL(0, value); std::vector 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;