]> git.ipfire.org Git - pakfire.git/commitdiff
tests: execute: Replace exit code tests with the stub command
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 21 Jul 2022 08:18:48 +0000 (08:18 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 21 Jul 2022 08:20:08 +0000 (08:20 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
tests/python/execute.py
tests/stub/command.c

index d619e41e4b7cad4b0f8b73b76d69fa351c73232b..649ff06bcf97f9e19c4f574caf6d2d8abd61b989 100755 (executable)
@@ -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
index dd1b781b6341097a89a91fdb70a2f249d34aa017..164bc5278820b08ab043174134a50f2ef708d552 100644 (file)
 #                                                                             #
 #############################################################################*/
 
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+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);
 }