#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); assert_equal(0, value, __FILE__, __LINE__); value = execute(s, cmd, {"retval", "1"}, {}, false); assert_equal(1, value, __FILE__, __LINE__); value = execute(s, "no-such-binary", {}, {}, false); assert_equal(1, value, __FILE__, __LINE__); } 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); assert_equal(0, value, __FILE__, __LINE__); 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;