dte test coverage


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 50.0% high: ≥ 85.0%
Coverage Exec / Excl / Total
Lines: 100.0% 34 / 0 / 34
Functions: 100.0% 1 / 0 / 1
Branches: -% 0 / 0 / 0

test/spawn.c
Line Branch Exec Source
1 #include <unistd.h>
2 #include "test.h"
3 #include "spawn.h"
4
5 1 static void test_spawn(TestContext *ctx)
6 {
7 1 static const char *args[] = {
8 "sh", "-c", "cat; echo OUT; echo ERR >&2",
9 NULL
10 };
11
12 1 SpawnContext sc = {
13 .argv = args,
14 .input = STRING_VIEW("IN-"),
15 .outputs = {STRING_INIT, STRING_INIT},
16 .lines = 32,
17 .columns = 120,
18 .quiet = true,
19 .actions = {
20 [STDIN_FILENO] = SPAWN_PIPE,
21 [STDOUT_FILENO] = SPAWN_PIPE,
22 [STDERR_FILENO] = SPAWN_PIPE,
23 },
24 };
25
26 1 static_assert(ARRAYLEN(sc.actions) == 3);
27 1 static_assert(ARRAYLEN(sc.outputs) == 2);
28
29 1 String *out = &sc.outputs[0];
30 1 String *err = &sc.outputs[1];
31 1 EXPECT_EQ(spawn(&sc), 0);
32 1 EXPECT_STRING_EQ_CSTRING(out, "IN-OUT\n");
33 1 EXPECT_STRING_EQ_CSTRING(err, "ERR\n");
34 1 EXPECT_EQ(string_clear(out), 7);
35 1 EXPECT_EQ(string_clear(err), 4);
36
37 1 sc.actions[STDIN_FILENO] = SPAWN_NULL;
38 1 sc.actions[STDERR_FILENO] = SPAWN_NULL;
39 1 EXPECT_EQ(spawn(&sc), 0);
40 1 EXPECT_STRING_EQ_CSTRING(out, "OUT\n");
41 1 EXPECT_STRING_EQ_CSTRING(err, "");
42 1 EXPECT_EQ(string_clear(out), 4);
43 1 EXPECT_EQ(string_clear(err), 0);
44
45 1 args[2] = "printf \"xyz $LINES $COLUMNS\"; exit 37";
46 1 EXPECT_EQ(spawn(&sc), 37);
47 1 EXPECT_STRING_EQ_CSTRING(out, "xyz 32 120");
48 1 EXPECT_STRING_EQ_CSTRING(err, "");
49 1 EXPECT_EQ(string_clear(out), 10);
50 1 EXPECT_EQ(string_clear(err), 0);
51
52 // Make sure zero-length input with one SPAWN_PIPE action
53 // doesn't deadlock
54 1 args[2] = "cat >/dev/null";
55 1 sc.input.length = 0;
56 1 sc.actions[STDIN_FILENO] = SPAWN_PIPE;
57 1 sc.actions[STDOUT_FILENO] = SPAWN_NULL;
58 1 sc.actions[STDERR_FILENO] = SPAWN_NULL;
59 1 EXPECT_EQ(spawn(&sc), 0);
60 1 string_free(out);
61 1 string_free(err);
62 1 }
63
64 static const TestEntry tests[] = {
65 TEST(test_spawn),
66 };
67
68 const TestGroup spawn_tests = TEST_GROUP(tests);
69