#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() { 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()); 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() { 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"; uASSERT_EQUAL(0, execute(cmd, {"envdump", tmp.get()}, env, false)); std::set vars; { std::ifstream infile(tmp.get()); std::string line; while (std::getline(infile, line)) { vars.insert(line); } } // Check the two explicitly set vars uASSERT(vars.find("foo=bar") != vars.end()); uASSERT(vars.find("bar=42") != vars.end()); // Check the one that should have overwritten the existing one (probably LANG=en_US.UTF-8 or something) uASSERT(vars.find("LANG=foo") != 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;