dte test coverage


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 50.0% high: ≥ 85.0%
Coverage Exec / Excl / Total
Lines: 97.0% 159 / 0 / 164
Functions: 100.0% 15 / 0 / 15
Branches: 87.1% 61 / 18 / 88

src/config.c
Line Branch Exec Source
1 #include "build-defs.h"
2 #include <errno.h>
3 #include <stdbool.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <sys/types.h>
7 #include "config.h"
8 #include "command/cache.h"
9 #include "command/error.h"
10 #include "commands.h"
11 #include "compiler.h"
12 #include "editor.h"
13 #include "syntax/color.h"
14 #include "util/debug.h"
15 #include "util/hashmap.h"
16 #include "util/intmap.h"
17 #include "util/log.h"
18 #include "util/readfile.h"
19 #include "util/str-util.h"
20 #include "util/xsnprintf.h"
21
22 #if HAVE_EMBED
23 #include "builtin-config-embed.h"
24 #else
25 #include "builtin-config.h"
26 #endif
27
28 // Odd number of backslashes at end of line?
29 10011 static bool has_line_continuation(StringView line)
30 {
31 10011 ssize_t pos = line.length - 1;
32
4/4
✓ Branch 4 → 5 taken 9392 times.
✓ Branch 4 → 6 taken 1538 times.
✓ Branch 5 → 3 taken 919 times.
✓ Branch 5 → 6 taken 8473 times.
10930 while (pos >= 0 && line.data[pos] == '\\') {
33 919 pos--;
34 }
35 10011 return (line.length - 1 - pos) & 1;
36 }
37
38 24 UNITTEST {
39 // NOLINTBEGIN(bugprone-assert-side-effect)
40 24 BUG_ON(has_line_continuation(strview(NULL)));
41 24 BUG_ON(has_line_continuation(strview("0")));
42 24 BUG_ON(!has_line_continuation(strview("1 \\")));
43 24 BUG_ON(has_line_continuation(strview("2 \\\\")));
44 24 BUG_ON(!has_line_continuation(strview("3 \\\\\\")));
45 24 BUG_ON(has_line_continuation(strview("4 \\\\\\\\")));
46 // NOLINTEND(bugprone-assert-side-effect)
47 24 }
48
49 229 bool exec_config(CommandRunner *runner, StringView config)
50 {
51 229 EditorState *e = runner->e;
52 229 ErrorBuffer *ebuf = runner->ebuf;
53 229 BUG_ON(!e);
54 229 BUG_ON(!ebuf);
55
56
2/2
✓ Branch 6 → 7 taken 1 time.
✓ Branch 6 → 8 taken 228 times.
229 if (unlikely(e->include_recursion_count > 64)) {
57 1 return error_msg_for_cmd(ebuf, NULL, "config recursion limit reached");
58 }
59
60 228 bool stop_at_first_err = (runner->flags & CMDRUNNER_STOP_AT_FIRST_ERROR);
61 228 size_t nfailed = 0;
62 228 String buf = string_new(1024);
63 228 e->include_recursion_count++;
64
65
2/2
✓ Branch 28 → 10 taken 10140 times.
✓ Branch 28 → 29 taken 227 times.
10367 for (size_t i = 0, n = config.length; i < n; ebuf->sourcepos.line++) {
66 10140 StringView line = buf_slice_next_line(config.data, &i, n);
67 10140 strview_trim_left(&line);
68
4/4
✓ Branch 12 → 13 taken 9463 times.
✓ Branch 12 → 16 taken 677 times.
✓ Branch 14 → 15 taken 273 times.
✓ Branch 14 → 16 taken 9190 times.
10140 if (buf.len == 0 && strview_has_prefix(line, "#")) {
69 // Comment line
70 952 continue;
71 }
72
73
2/2
✓ Branch 16 → 17 taken 679 times.
✓ Branch 16 → 19 taken 9188 times.
9867 if (has_line_continuation(line)) {
74 // Line with backslash-escaped newline; append to buffer
75 // but don't execute yet
76 679 line.length--;
77 679 string_append_strview(&buf, line);
78 679 continue;
79 }
80
81 9188 string_append_strview(&buf, line);
82 9188 bool r = handle_command(runner, string_borrow_cstring(&buf));
83 9188 string_clear(&buf);
84 9188 nfailed += !r;
85
2/2
✓ Branch 23 → 24 taken 1 time.
✓ Branch 23 → 25 taken 9187 times.
9188 if (unlikely(!r && stop_at_first_err)) {
86 1 goto out;
87 }
88 }
89
90
2/2
✓ Branch 29 → 30 taken 225 times.
✓ Branch 29 → 31 taken 2 times.
227 if (unlikely(buf.len)) {
91 // This can only happen if the last line had a line continuation
92 2 bool r = handle_command(runner, string_borrow_cstring(&buf));
93 2 nfailed += !r;
94 }
95
96 225 out:
97 228 e->include_recursion_count--;
98 228 string_free(&buf);
99 228 return (nfailed == 0);
100 }
101
102 3 String dump_builtin_configs(void)
103 {
104 3 String str = string_new(1024);
105
2/2
✓ Branch 7 → 4 taken 240 times.
✓ Branch 7 → 8 taken 3 times.
246 for (size_t i = 0; i < ARRAYLEN(builtin_configs); i++) {
106 240 string_append_cstring(&str, builtin_configs[i].name);
107 240 string_append_byte(&str, '\n');
108 }
109 3 return str;
110 }
111
112 103 const BuiltinConfig *get_builtin_config(const char *name)
113 {
114
2/2
✓ Branch 6 → 3 taken 5385 times.
✓ Branch 6 → 7 taken 60 times.
5445 for (size_t i = 0; i < ARRAYLEN(builtin_configs); i++) {
115
2/2
✓ Branch 3 → 4 taken 43 times.
✓ Branch 3 → 5 taken 5342 times.
5385 if (streq(name, builtin_configs[i].name)) {
116 43 return &builtin_configs[i];
117 }
118 }
119 return NULL;
120 }
121
122 1 const BuiltinConfig *get_builtin_configs_array(size_t *nconfigs)
123 {
124 1 *nconfigs = ARRAYLEN(builtin_configs);
125 1 return &builtin_configs[0];
126 }
127
128 111 static SystemErrno do_read_config (
129 CommandRunner *runner,
130 const char *filename,
131 ConfigFlags flags
132 ) {
133 111 ErrorBuffer *ebuf = runner->ebuf;
134 111 bool must_exist = flags & CFG_MUST_EXIST;
135 111 bool stop_at_first_err = runner->flags & CMDRUNNER_STOP_AT_FIRST_ERROR;
136 111 BUG_ON(!ebuf);
137
138
2/2
✓ Branch 4 → 5 taken 42 times.
✓ Branch 4 → 11 taken 69 times.
111 if (flags & CFG_BUILTIN) {
139 42 const BuiltinConfig *cfg = get_builtin_config(filename);
140
2/2
✓ Branch 5 → 6 taken 41 times.
✓ Branch 5 → 9 taken 1 time.
42 if (cfg) {
141 41 ebuf->sourcepos.filename = filename;
142 41 ebuf->sourcepos.line = 1;
143 41 bool r = exec_config(runner, cfg->text);
144
1/2
✗ Branch 7 → 8 not taken.
✓ Branch 7 → 19 taken 41 times.
41 return (r || !stop_at_first_err) ? 0 : EINVAL;
145 }
146
1/2
✓ Branch 9 → 10 taken 1 time.
✗ Branch 9 → 19 not taken.
1 if (must_exist) {
147 1 error_msg(ebuf, "no built-in config with name '%s'", filename);
148 }
149 return ENOENT;
150 }
151
152 69 char *buf;
153 69 ssize_t size = read_file(filename, &buf, 0);
154
2/2
✓ Branch 12 → 13 taken 1 time.
✓ Branch 12 → 16 taken 68 times.
69 if (size < 0) {
155 1 int err = errno;
156
1/2
✓ Branch 13 → 14 taken 1 time.
✗ Branch 13 → 19 not taken.
1 if (err != ENOENT || must_exist) {
157 1 error_msg(ebuf, "Error reading %s: %s", filename, strerror(err));
158 }
159 return err;
160 }
161
162 68 ebuf->sourcepos.filename = filename;
163 68 ebuf->sourcepos.line = 1;
164 68 bool r = exec_config(runner, string_view(buf, size));
165 68 free(buf);
166
1/2
✗ Branch 17 → 18 not taken.
✓ Branch 17 → 19 taken 68 times.
68 return (r || !stop_at_first_err) ? 0 : EINVAL;
167 }
168
169 111 SystemErrno read_config(CommandRunner *runner, const char *filename, ConfigFlags flags)
170 {
171 111 ErrorBuffer *ebuf = runner->ebuf;
172 111 ConfigLocation save = ebuf->sourcepos;
173 111 SystemErrno ret = do_read_config(runner, filename, flags);
174 111 ebuf->sourcepos = save;
175 111 return ret;
176 }
177
178 27 static void exec_builtin_config(EditorState *e, StringView cfg, const char *name)
179 {
180 27 ErrorBuffer *ebuf = &e->err;
181 27 ConfigLocation save = ebuf->sourcepos;
182 27 ebuf->sourcepos.filename = name;
183 27 ebuf->sourcepos.line = 1;
184 27 exec_normal_config(e, cfg);
185 27 ebuf->sourcepos = save;
186 27 }
187
188 18 void exec_builtin_color_reset(EditorState *e)
189 {
190 18 StringView reset = string_view(builtin_color_reset, sizeof(builtin_color_reset) - 1);
191 18 clear_hl_styles(&e->styles);
192 18 exec_builtin_config(e, reset, "color/reset");
193 18 e->screen_update |= UPDATE_SYNTAX_STYLES | UPDATE_ALL_WINDOWS;
194 18 }
195
196 9 static void log_config_counts(const EditorState *e)
197 {
198
2/2
✓ Branch 3 → 4 taken 1 time.
✓ Branch 3 → 17 taken 8 times.
9 if (!log_level_enabled(LOG_LEVEL_INFO)) {
199 return;
200 }
201
202 1 size_t nbinds = 0;
203 1 size_t nbinds_cached = 0;
204
2/2
✓ Branch 11 → 5 taken 3 times.
✓ Branch 11 → 12 taken 1 time.
4 for (HashMapIter modeiter = hashmap_iter(&e->modes); hashmap_next(&modeiter); ) {
205 3 const ModeHandler *mode = modeiter.entry->value;
206 3 const IntMap *binds = &mode->key_bindings;
207 3 nbinds += binds->count;
208
2/2
✓ Branch 8 → 6 taken 217 times.
✓ Branch 8 → 9 taken 3 times.
220 for (IntMapIter binditer = intmap_iter(binds); intmap_next(&binditer); ) {
209 217 const CachedCommand *cc = binditer.entry->value;
210 217 nbinds_cached += !!cc->cmd;
211 }
212 }
213
214 1 size_t nerrorfmts = 0;
215
2/2
✓ Branch 15 → 13 taken 3 times.
✓ Branch 15 → 16 taken 1 time.
4 for (HashMapIter it = hashmap_iter(&e->compilers); hashmap_next(&it); ) {
216 3 const Compiler *compiler = it.entry->value;
217 3 nerrorfmts += compiler->error_formats.count;
218 }
219
220 1 LOG_INFO (
221 "bind=%zu(%zu) alias=%zu hi=%zu ft=%zu option=%zu errorfmt=%zu(%zu)",
222 nbinds,
223 nbinds_cached,
224 e->aliases.count,
225 e->styles.other.count + NR_BSE,
226 e->filetypes.count,
227 e->file_options.count,
228 e->compilers.count,
229 nerrorfmts
230 );
231 }
232
233 9 void exec_rc_files (
234 EditorState *e,
235 const char *filename,
236 bool read_user_rc,
237 bool no_color
238 ) {
239 9 StringView rc = string_view(builtin_rc, sizeof(builtin_rc) - 1);
240 9 exec_builtin_color_reset(e);
241 9 exec_builtin_config(e, rc, "rc");
242
243
1/2
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 7 taken 9 times.
9 if (no_color) {
244 size_t nc_len = sizeof(builtin_color_nocolor) - 1;
245 StringView nc = string_view(builtin_color_nocolor, nc_len);
246 exec_builtin_config(e, nc, "color/nocolor");
247 }
248
249
2/2
✓ Branch 7 → 8 taken 2 times.
✓ Branch 7 → 13 taken 7 times.
9 if (read_user_rc) {
250 2 ConfigFlags flags = CFG_NOFLAGS;
251 2 char buf[8192];
252
1/2
✗ Branch 8 → 9 not taken.
✓ Branch 8 → 10 taken 2 times.
2 if (filename) {
253 flags |= CFG_MUST_EXIST;
254 } else {
255 xsnprintf(buf, sizeof buf, "%s/%s", e->user_config_dir, "rc");
256 filename = buf;
257 }
258 2 LOG_INFO("loading configuration from %s", filename);
259 2 read_normal_config(e, filename, flags);
260 }
261
262 9 log_config_counts(e);
263 9 }
264
265 1 void collect_builtin_configs(PointerArray *a, const char *prefix)
266 {
267 1 size_t prefix_len = strlen(prefix);
268
2/2
✓ Branch 7 → 3 taken 80 times.
✓ Branch 7 → 8 taken 1 time.
81 for (size_t i = 0; i < ARRAYLEN(builtin_configs); i++) {
269 80 const char *name = builtin_configs[i].name;
270
2/2
✓ Branch 3 → 4 taken 1 time.
✓ Branch 3 → 6 taken 79 times.
80 if (str_has_strn_prefix(name, prefix, prefix_len)) {
271 1 ptr_array_append(a, xstrdup(name));
272 }
273 }
274 1 }
275
276 // Collect ${builtin:path/name} style variables (minus the leading "${")
277 // TODO: Support ${script:name} variables
278 2 void collect_builtin_config_variables(PointerArray *a, StringView prefix)
279 {
280 2 static const char builtin[] = "builtin:";
281 2 const size_t blen = sizeof(builtin) - 1;
282 2 const size_t cmplen = MIN(prefix.length, blen);
283
284
1/2
✓ Branch 3 → 9 taken 2 times.
✗ Branch 3 → 10 not taken.
2 if (!strview_remove_matching_sv_prefix(&prefix, string_view(builtin, cmplen))) {
285 return;
286 }
287
288
2/2
✓ Branch 9 → 4 taken 160 times.
✓ Branch 9 → 10 taken 2 times.
162 for (size_t i = 0; i < ARRAYLEN(builtin_configs); i++) {
289 160 StringView name = strview(builtin_configs[i].name);
290
2/2
✓ Branch 5 → 6 taken 86 times.
✓ Branch 5 → 8 taken 74 times.
160 if (strview_has_sv_prefix(name, prefix)) {
291 86 ptr_array_append(a, xmemjoin3(builtin, blen, name.data, name.length, "}", 2));
292 }
293 }
294 }
295
296 1 void collect_builtin_includes(PointerArray *a, const char *prefix)
297 {
298 1 size_t prefix_len = strlen(prefix);
299
2/2
✓ Branch 9 → 3 taken 80 times.
✓ Branch 9 → 10 taken 1 time.
81 for (size_t i = 0; i < ARRAYLEN(builtin_configs); i++) {
300 80 const char *name = builtin_configs[i].name;
301 80 if (
302
2/2
✓ Branch 3 → 4 taken 1 time.
✓ Branch 3 → 8 taken 79 times.
80 str_has_strn_prefix(name, prefix, prefix_len)
303
1/2
✓ Branch 4 → 5 taken 1 time.
✗ Branch 4 → 8 not taken.
1 && !str_has_prefix(name, "syntax/")
304
1/2
✓ Branch 5 → 6 taken 1 time.
✗ Branch 5 → 8 not taken.
1 && !str_has_prefix(name, "script/")
305 ) {
306 1 ptr_array_append(a, xstrdup(name));
307 }
308 }
309 1 }
310