dte test coverage


Directory: ./
File: test/spawn.c
Date: 2025-07-01 18:22:46
Exec Total Coverage
Lines: 39 39 100.0%
Functions: 1 1 100.0%
Branches: 0 0 -%

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 ErrorBuffer ebuf = {.print_to_stderr = false};
13 1 SpawnContext sc = {
14 .argv = args,
15 .prog_fd = -1,
16 .input = STRING_VIEW("IN-"),
17 .outputs = {STRING_INIT, STRING_INIT},
18 .ebuf = &ebuf,
19 .lines = 32,
20 .columns = 120,
21 .quiet = true,
22 .actions = {
23 [STDIN_FILENO] = SPAWN_PIPE,
24 [STDOUT_FILENO] = SPAWN_PIPE,
25 [STDERR_FILENO] = SPAWN_PIPE,
26 },
27 };
28
29 1 static_assert(ARRAYLEN(sc.actions) == 3);
30 1 static_assert(ARRAYLEN(sc.outputs) == 2);
31
32 1 String *out = &sc.outputs[0];
33 1 String *err = &sc.outputs[1];
34 1 EXPECT_EQ(spawn(&sc), 0);
35 1 EXPECT_EQ(out->len, 7);
36 1 EXPECT_EQ(err->len, 4);
37 1 EXPECT_STREQ(string_borrow_cstring(out), "IN-OUT\n");
38 1 EXPECT_STREQ(string_borrow_cstring(err), "ERR\n");
39 1 EXPECT_EQ(string_clear(out), 7);
40 1 EXPECT_EQ(string_clear(err), 4);
41
42 1 sc.actions[STDIN_FILENO] = SPAWN_NULL;
43 1 sc.actions[STDERR_FILENO] = SPAWN_NULL;
44 1 EXPECT_EQ(spawn(&sc), 0);
45 1 EXPECT_EQ(out->len, 4);
46 1 EXPECT_EQ(err->len, 0);
47 1 EXPECT_STREQ(string_borrow_cstring(out), "OUT\n");
48 1 EXPECT_EQ(string_clear(out), 4);
49 1 EXPECT_EQ(string_clear(err), 0);
50
51 1 args[2] = "printf \"xyz $LINES $COLUMNS\"; exit 37";
52 1 EXPECT_EQ(spawn(&sc), 37);
53 1 EXPECT_EQ(out->len, 10);
54 1 EXPECT_EQ(err->len, 0);
55 1 EXPECT_STREQ(string_borrow_cstring(out), "xyz 32 120");
56 1 EXPECT_EQ(string_clear(out), 10);
57 1 EXPECT_EQ(string_clear(err), 0);
58
59 // Make sure zero-length input with one SPAWN_PIPE action
60 // doesn't deadlock
61 1 args[2] = "cat >/dev/null";
62 1 sc.input.length = 0;
63 1 sc.actions[STDIN_FILENO] = SPAWN_PIPE;
64 1 sc.actions[STDOUT_FILENO] = SPAWN_NULL;
65 1 sc.actions[STDERR_FILENO] = SPAWN_NULL;
66 1 EXPECT_EQ(spawn(&sc), 0);
67 1 string_free(out);
68 1 string_free(err);
69 1 }
70
71 static const TestEntry tests[] = {
72 TEST(test_spawn),
73 };
74
75 const TestGroup spawn_tests = TEST_GROUP(tests);
76