summaryrefslogtreecommitdiff
path: root/test/execute_test.cc
diff options
context:
space:
mode:
authorBent Bisballe Nyeng <deva@aasimon.org>2023-01-20 08:37:29 +0100
committerBent Bisballe Nyeng <deva@aasimon.org>2023-01-20 09:02:45 +0100
commita5585150f0ff8d27ddd22792f521f1374a3eedd8 (patch)
tree3bb442e835502d452ebe5137ad7fca5f5156bb5f /test/execute_test.cc
parent3cbadb8f5c55020ece96fab0fc8f4f51da01888e (diff)
Add env to execute function.
Diffstat (limited to 'test/execute_test.cc')
-rw-r--r--test/execute_test.cc65
1 files changed, 60 insertions, 5 deletions
diff --git a/test/execute_test.cc b/test/execute_test.cc
index 9da18dc..03b3c2a 100644
--- a/test/execute_test.cc
+++ b/test/execute_test.cc
@@ -1,6 +1,14 @@
#include <uunit.h>
+#include <fstream>
+#include <map>
+#include <set>
+
#include <execute.h>
+#include <util.h>
+
+#include "paths.h"
+#include "tmpfile.h"
class ExecuteTest
: public uUnit
@@ -8,14 +16,61 @@ class ExecuteTest
public:
ExecuteTest()
{
- uTEST(ExecuteTest::runit);
+ 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 runit()
+ void env()
{
- uASSERT_EQUAL(0, execute("/bin/true", {}, false));
- uASSERT_EQUAL(1, execute("/bin/false", {}, false));
- uASSERT_EQUAL(1, execute("no-such-binary", {}, false));
+ 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::set<std::string> 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);
}
};