From: Michael Tremer Date: Thu, 21 Jul 2022 08:18:48 +0000 (+0000) Subject: tests: execute: Replace exit code tests with the stub command X-Git-Tag: 0.9.28~662 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=d02bd04a3106599c06c1f413731d04e45ed8a074;p=pakfire.git tests: execute: Replace exit code tests with the stub command Signed-off-by: Michael Tremer --- diff --git a/tests/python/execute.py b/tests/python/execute.py index d619e41e4..649ff06bc 100755 --- a/tests/python/execute.py +++ b/tests/python/execute.py @@ -16,10 +16,19 @@ class Test(unittest.TestCase): self.pakfire = pakfire.Pakfire(path) def test_execute(self): - r = self.pakfire.execute(["/usr/bin/sleep", "0"]) + r = self.pakfire.execute(["/command", "exit-with-code", "0"]) self.assertIsNone(r) + def test_return_value(self): + with self.assertRaises(pakfire.CommandExecutionError) as e: + self.pakfire.execute(["/command", "exit-with-code", "123"]) + + # Extract return code + code, = e.exception.args + + self.assertTrue(code == 123) + def test_environ(self): r = self.pakfire.execute(["/bin/sh", "-c", "echo ${VAR1}"], environ={"VAR1" : "VAL1"}) @@ -47,15 +56,6 @@ class Test(unittest.TestCase): with self.assertRaises(TypeError): self.pakfire.execute(["/usr/bin/sleep", "--help"], environ="VAR1=VAL1") - def test_return_value(self): - with self.assertRaises(pakfire.CommandExecutionError) as e: - self.pakfire.execute(["/bin/sh", "-c", "exit 123"]) - - # Extract return code - code, = e.exception.args - - self.assertTrue(code == 123) - def test_execute_non_existant_command(self): """ Executing non-existant commands should raise an error diff --git a/tests/stub/command.c b/tests/stub/command.c index dd1b781b6..164bc5278 100644 --- a/tests/stub/command.c +++ b/tests/stub/command.c @@ -18,6 +18,39 @@ # # #############################################################################*/ +#include +#include +#include + +static int exit_with_code(int argc, char* argv[]) { + if (argc < 1) { + fprintf(stderr, "exit-with-code requires an argument\n"); + return EXIT_FAILURE; + } + + // Return the given code + return strtoul(argv[0], NULL, 10); +} + int main(int argc, char* argv[]) { - return 0; + if (argc < 2) { + fprintf(stderr, "No command given\n"); + return EXIT_FAILURE; + } + + int (*callback)(int argc, char* argv[]) = NULL; + const char* command = argv[1]; + + // Exit with code + if (strcmp(command, "exit-with-code") == 0) { + callback = exit_with_code; + } + + // Exit if no callback has been set + if (!callback) { + fprintf(stderr, "Unknown command: %s\n", command); + return EXIT_FAILURE; + } + + return callback(argc - 2, argv + 2); }