dte test coverage


Directory: ./
File: test/init.c
Date: 2025-05-08 15:05:54
Exec Total Coverage
Lines: 108 114 94.7%
Functions: 5 5 100.0%
Branches: 2 4 50.0%

Line Branch Exec Source
1 #include <errno.h>
2 #include <fcntl.h>
3 #include <limits.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <sys/stat.h>
7 #include <unistd.h>
8 #include "test.h"
9 #include "editor.h"
10 #include "terminal/color.h"
11 #include "terminal/terminal.h"
12 #include "trace.h"
13 #include "util/fd.h"
14 #include "util/log.h"
15 #include "util/path.h"
16 #include "util/xreadwrite.h"
17 #include "version.h"
18
19 1 static void test_process_sanity(TestContext *ctx)
20 {
21 // Note that if this fails, there'll only be (at best) an "aborted"
22 // message, since there's no stderr for test_fail() to use
23 1 ASSERT_TRUE(fd_is_valid(STDERR_FILENO));
24
25 1 ASSERT_NONNULL(freopen("/dev/null", "r", stdin));
26 1 ASSERT_NONNULL(freopen("/dev/null", "w", stdout));
27 1 }
28
29 1 static void test_posix_sanity(TestContext *ctx)
30 {
31 // This is not guaranteed by ISO C99, but it is required by POSIX
32 // and is relied upon by this codebase.
33 // See: https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/limits.h.html#tag_14_26_03_06
34 1 ASSERT_EQ(CHAR_BIT, 8);
35 1 ASSERT_TRUE(sizeof(int) >= 4);
36 1 ASSERT_TRUE(BITSIZE(int) >= 32);
37 1 ASSERT_EQ(SCHAR_MIN, -128);
38 1 ASSERT_EQ(SCHAR_MAX, 127);
39 1 ASSERT_EQ(UCHAR_MAX, 255);
40 1 ASSERT_TRUE(UINT_MAX >= 0xFFFFFFFFu);
41 1 ASSERT_TRUE(INT_MAX >= 0x7FFFFFFF);
42 1 ASSERT_TRUE(INT_MIN <= -0x80000000);
43
44 IGNORE_WARNING("-Wformat-truncation")
45
46 // Some snprintf(3) implementations historically returned -1 in case of
47 // truncation. C99 and POSIX 2001 both require that it return the full
48 // size of the formatted string, as if there had been enough space.
49 1 char buf[8] = "........";
50 1 ASSERT_EQ(snprintf(buf, 8, "0123456789"), 10);
51 1 ASSERT_EQ(buf[7], '\0');
52 1 EXPECT_STREQ(buf, "0123456");
53
54 // C99 and POSIX 2001 also require the same behavior as above when the
55 // size argument is 0 (and allow the buffer argument to be NULL).
56 1 ASSERT_EQ(snprintf(NULL, 0, "987654321"), 9);
57
58 UNIGNORE_WARNINGS
59 1 }
60
61 // Note: this must be ordered before a successful call to log_open()
62 // (i.e. the one in test_init()), so as not to trigger the BUG_ON()
63 // assertions at the top of log_open()
64 1 static void test_log_open_errors(TestContext *ctx)
65 {
66 1 const LogLevel none = LOG_LEVEL_NONE;
67 1 const LogLevel info = LOG_LEVEL_INFO;
68 1 errno = 0;
69 1 ASSERT_EQ(log_open("/dev/null", none, false), none);
70 1 EXPECT_EQ(errno, 0);
71 1 ASSERT_EQ(log_open("build/test/non-existent-dir/x", info, false), none);
72 1 EXPECT_EQ(errno, ENOENT);
73
74
1/2
✓ Branch 0 (9→10) taken 1 times.
✗ Branch 1 (9→13) not taken.
1 if (access("/dev/full", W_OK) == 0) {
75 1 errno = 0;
76 1 ASSERT_EQ(log_open("/dev/full", info, false), none);
77 1 EXPECT_EQ(errno, ENOSPC);
78 }
79
80 1 int tty = open("/dev/tty", O_WRONLY | O_CLOEXEC | O_NOCTTY);
81
1/2
✗ Branch 0 (14→15) not taken.
✓ Branch 1 (14→23) taken 1 times.
1 if (tty >= 0) {
82 ASSERT_TRUE(tty > STDERR_FILENO);
83 ASSERT_TRUE(is_controlling_tty(tty));
84 EXPECT_EQ(xclose(tty), 0);
85 errno = 0;
86 ASSERT_EQ(log_open("/dev/tty", info, false), none);
87 EXPECT_EQ(errno, EINVAL);
88 }
89 1 }
90
91 1 static void test_init(TestContext *ctx)
92 {
93 1 char *home = path_absolute("build/test/HOME");
94 1 char *dte_home = path_absolute("build/test/DTE_HOME");
95 1 ASSERT_NONNULL(home);
96 1 ASSERT_NONNULL(dte_home);
97
98 1 ASSERT_TRUE(mkdir(home, 0755) == 0 || errno == EEXIST);
99 1 ASSERT_TRUE(mkdir(dte_home, 0755) == 0 || errno == EEXIST);
100
101 1 ASSERT_EQ(setenv("HOME", home, 1), 0);
102 1 ASSERT_EQ(setenv("DTE_HOME", dte_home, 1), 0);
103 1 ASSERT_EQ(setenv("XDG_RUNTIME_DIR", dte_home, 1), 0);
104 1 ASSERT_EQ(setenv("TZ", "UTC", 1), 0);
105
106 1 free(home);
107 1 free(dte_home);
108
109 1 ASSERT_EQ(unsetenv("TERM"), 0);
110 1 ASSERT_EQ(unsetenv("COLORTERM"), 0);
111
112 1 LogLevel lvl = LOG_LEVEL_WARNING;
113 1 ASSERT_EQ(log_open("build/test/log.txt", lvl, false), lvl);
114 1 LOG_CRITICAL("%s: testing LOG_CRITICAL()", __func__);
115 1 LOG_ERROR("%s: testing LOG_ERROR()", __func__);
116 1 LOG_WARNING("%s: testing LOG_WARNING()", __func__);
117 1 LOG_NOTICE("%s: testing LOG_NOTICE()", __func__);
118 1 LOG_INFO("%s: testing LOG_INFO()", __func__);
119 1 LOG_DEBUG("%s: testing LOG_DEBUG()", __func__);
120 1 LOG_TRACE(TRACEFLAGS_ALL, "%s: testing LOG_TRACE()", __func__);
121 1 log_write(LOG_LEVEL_WARNING, STRN("testing log_write()"));
122 1 log_write(LOG_LEVEL_INFO, STRN("testing log_write()"));
123
124 1 EditorState *e = init_editor_state(EFLAG_HEADLESS);
125 1 ctx->userdata = e;
126 1 ASSERT_NONNULL(e);
127 1 ASSERT_NONNULL(e->user_config_dir);
128 1 ASSERT_NONNULL(e->home_dir.data);
129 1 EXPECT_NONNULL(e->terminal.obuf.buf);
130 1 EXPECT_NONNULL(e->terminal.ibuf.buf);
131
132 1 const char *ver = getenv("DTE_VERSION");
133 1 EXPECT_NONNULL(ver);
134 1 EXPECT_STREQ(ver, VERSION);
135 1 }
136
137 1 static void test_deinit(TestContext *ctx)
138 {
139 // Make sure default Terminal state wasn't changed
140 1 EditorState *e = ctx->userdata;
141 1 const Terminal *term = &e->terminal;
142 1 EXPECT_EQ(term->width, 80);
143 1 EXPECT_EQ(term->height, 24);
144 1 EXPECT_EQ(term->ncv_attributes, 0);
145 1 EXPECT_EQ(term->features, TFLAG_8_COLOR);
146 1 EXPECT_EQ(term->ibuf.len, 0);
147 1 EXPECT_NONNULL(term->ibuf.buf);
148
149 // Make sure no terminal output was buffered
150 1 const TermOutputBuffer *obuf = &term->obuf;
151 1 EXPECT_NONNULL(obuf->buf);
152 1 EXPECT_EQ(obuf->count, 0);
153 1 EXPECT_EQ(obuf->scroll_x, 0);
154 1 EXPECT_EQ(obuf->x, 0);
155 1 EXPECT_EQ(obuf->width, 0);
156 1 EXPECT_EQ(obuf->style.fg, COLOR_DEFAULT);
157 1 EXPECT_EQ(obuf->style.bg, COLOR_DEFAULT);
158 1 EXPECT_EQ(obuf->style.attr, 0);
159 1 EXPECT_EQ(obuf->cursor_style.type, CURSOR_DEFAULT);
160 1 EXPECT_EQ(obuf->cursor_style.color, COLOR_DEFAULT);
161
162 1 ASSERT_NONNULL(e->view);
163 1 ASSERT_NONNULL(e->buffer);
164 1 EXPECT_EQ(e->buffers.count, 1);
165 1 EXPECT_NULL(e->buffer->abs_filename);
166 1 EXPECT_PTREQ(e->buffer, e->buffers.ptrs[0]);
167 1 EXPECT_FALSE(e->child_controls_terminal);
168 1 EXPECT_EQ(e->flags, EFLAG_HEADLESS);
169 1 EXPECT_EQ(e->include_recursion_count, 0);
170
171 1 frame_remove(e, e->root_frame);
172 1 EXPECT_NULL(e->view);
173 1 EXPECT_NULL(e->buffer);
174 1 EXPECT_EQ(e->buffers.count, 0);
175
176 1 free_editor_state(e);
177 1 EXPECT_TRUE(log_close());
178 1 }
179
180 static const TestEntry itests[] = {
181 TEST(test_process_sanity),
182 TEST(test_posix_sanity),
183 TEST(test_log_open_errors),
184 TEST(test_init),
185 };
186
187 static const TestEntry dtests[] = {
188 TEST(test_deinit),
189 };
190
191 const TestGroup init_tests = TEST_GROUP(itests);
192 const TestGroup deinit_tests = TEST_GROUP(dtests);
193