dte test coverage


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 50.0% high: ≥ 85.0%
Coverage Exec / Excl / Total
Lines: 98.8% 160 / 0 / 162
Functions: 100.0% 15 / 0 / 15
Branches: 88.2% 60 / 18 / 86

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 237 times.
✓ Branch 7 → 8 taken 3 times.
240 for (size_t i = 0; i < ARRAYLEN(builtin_configs); i++) {
106 237 string_append_cstring(&str, builtin_configs[i].name);
107 237 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 5319 times.
✓ Branch 6 → 7 taken 60 times.
5379 for (size_t i = 0; i < ARRAYLEN(builtin_configs); i++) {
115
2/2
✓ Branch 3 → 4 taken 43 times.
✓ Branch 3 → 5 taken 5276 times.
5319 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 → 13 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 → 10 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 → 9 taken 41 times.
41 return (r || !stop_at_first_err) ? 0 : EINVAL;
145 }
146
1/2
✓ Branch 10 → 11 taken 1 time.
✗ Branch 10 → 12 not taken.
1 if (must_exist) {
147 1 error_msg(ebuf, "no built-in config with name '%s'", filename);
148 }
149 1 return ENOENT;
150 }
151
152 69 char *buf;
153 69 ssize_t size = read_file(filename, &buf, 0);
154
2/2
✓ Branch 14 → 15 taken 1 time.
✓ Branch 14 → 19 taken 68 times.
69 if (size < 0) {
155 1 int err = errno;
156
1/2
✓ Branch 15 → 16 taken 1 time.
✗ Branch 15 → 18 not taken.
1 if (err != ENOENT || must_exist) {
157 1 error_msg(ebuf, "Error reading %s: %s", filename, strerror(err));
158 }
159 1 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 20 → 21 not taken.
✓ Branch 20 → 22 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(EditorState *e, const char *filename, bool read_user_rc)
234 {
235 9 StringView rc = string_view(builtin_rc, sizeof(builtin_rc) - 1);
236 9 exec_builtin_color_reset(e);
237 9 exec_builtin_config(e, rc, "rc");
238
239
2/2
✓ Branch 4 → 5 taken 2 times.
✓ Branch 4 → 10 taken 7 times.
9 if (read_user_rc) {
240 2 ConfigFlags flags = CFG_NOFLAGS;
241 2 char buf[8192];
242
1/2
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 7 taken 2 times.
2 if (filename) {
243 flags |= CFG_MUST_EXIST;
244 } else {
245 xsnprintf(buf, sizeof buf, "%s/%s", e->user_config_dir, "rc");
246 filename = buf;
247 }
248 2 LOG_INFO("loading configuration from %s", filename);
249 2 read_normal_config(e, filename, flags);
250 }
251
252 9 log_config_counts(e);
253 9 }
254
255 1 void collect_builtin_configs(PointerArray *a, const char *prefix)
256 {
257 1 size_t prefix_len = strlen(prefix);
258
2/2
✓ Branch 7 → 3 taken 79 times.
✓ Branch 7 → 8 taken 1 time.
80 for (size_t i = 0; i < ARRAYLEN(builtin_configs); i++) {
259 79 const char *name = builtin_configs[i].name;
260
2/2
✓ Branch 3 → 4 taken 1 time.
✓ Branch 3 → 6 taken 78 times.
79 if (str_has_strn_prefix(name, prefix, prefix_len)) {
261 1 ptr_array_append(a, xstrdup(name));
262 }
263 }
264 1 }
265
266 // Collect ${builtin:path/name} style variables (minus the leading "${")
267 // TODO: Support ${script:name} variables
268 2 void collect_builtin_config_variables(PointerArray *a, StringView prefix)
269 {
270 2 static const char builtin[] = "builtin:";
271 2 const size_t blen = sizeof(builtin) - 1;
272 2 const size_t cmplen = MIN(prefix.length, blen);
273
274
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))) {
275 return;
276 }
277
278
2/2
✓ Branch 9 → 4 taken 158 times.
✓ Branch 9 → 10 taken 2 times.
160 for (size_t i = 0; i < ARRAYLEN(builtin_configs); i++) {
279 158 StringView name = strview(builtin_configs[i].name);
280
2/2
✓ Branch 5 → 6 taken 84 times.
✓ Branch 5 → 8 taken 74 times.
158 if (strview_has_sv_prefix(name, prefix)) {
281 84 ptr_array_append(a, xmemjoin3(builtin, blen, name.data, name.length, "}", 2));
282 }
283 }
284 }
285
286 1 void collect_builtin_includes(PointerArray *a, const char *prefix)
287 {
288 1 size_t prefix_len = strlen(prefix);
289
2/2
✓ Branch 9 → 3 taken 79 times.
✓ Branch 9 → 10 taken 1 time.
80 for (size_t i = 0; i < ARRAYLEN(builtin_configs); i++) {
290 79 const char *name = builtin_configs[i].name;
291 79 if (
292
2/2
✓ Branch 3 → 4 taken 1 time.
✓ Branch 3 → 8 taken 78 times.
79 str_has_strn_prefix(name, prefix, prefix_len)
293
1/2
✓ Branch 4 → 5 taken 1 time.
✗ Branch 4 → 8 not taken.
1 && !str_has_prefix(name, "syntax/")
294
1/2
✓ Branch 5 → 6 taken 1 time.
✗ Branch 5 → 8 not taken.
1 && !str_has_prefix(name, "script/")
295 ) {
296 1 ptr_array_append(a, xstrdup(name));
297 }
298 }
299 1 }
300