Debian Codegraph @

src/test/test-pretty-print.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <stdio.h>
4 #include <unistd.h>
5
6 #include "alloc-util.h"
7 #include "path-util.h"
8 #include "pretty-print.h"
9 #include "rm-rf.h"
10 #include "tests.h"
11 #include "tmpfile-util.h"
12
13 #define CYLON_WIDTH 6
14
15 static void test_draw_cylon_one(unsigned pos) {
16 char buf[CYLON_WIDTH + CYLON_BUFFER_EXTRA + 1];
17
18 log_debug("/* %s(%u) */", __func__, pos);
19
20 assert(pos <= CYLON_WIDTH + 1);
21
22 memset(buf, 0xff, sizeof(buf));
23 draw_cylon(buf, sizeof(buf), CYLON_WIDTH, pos);
24 ASSERT_LE(strlen(buf), sizeof(buf));
25 }
26
27 TEST(draw_cylon) {
28 bool saved = log_get_show_color();
29
30 log_show_color(false);
31 for (unsigned i = 0; i <= CYLON_WIDTH + 1; i++)
32 test_draw_cylon_one(i);
33
34 log_show_color(true);
35 for (unsigned i = 0; i <= CYLON_WIDTH + 1; i++)
36 test_draw_cylon_one(i);
37
38 log_show_color(saved);
39 }
40
41 TEST(terminal_urlify) {
42 _cleanup_free_ char *formatted = NULL;
43
44 assert_se(terminal_urlify("https://www.freedesktop.org/wiki/Software/systemd", "systemd homepage", &formatted) >= 0);
45 printf("Hey, consider visiting the %s right now! It is very good!\n", formatted);
46
47 formatted = mfree(formatted);
48
49 assert_se(terminal_urlify_path("/etc/fstab", "this link to your /etc/fstab", &formatted) >= 0);
50 printf("Or click on %s to have a look at it!\n", formatted);
51 }
52
53 TEST(cat_files) {
54 assert_se(cat_files("/no/such/file", NULL, 0) == -ENOENT);
55 assert_se(cat_files(NULL, NULL, 0) == 0);
56
57 if (access("/etc/fstab", R_OK) >= 0)
58 assert_se(cat_files("/etc/fstab", STRV_MAKE("/etc/fstab", "/etc/fstab"), 0) == 0);
59
60 /* Test masked file (symlink to /dev/null) - should succeed with exit code 0 */
61 _cleanup_(rm_rf_physical_and_freep) char *tmp = NULL;
62 ASSERT_OK(mkdtemp_malloc("/tmp/test-cat-files-XXXXXX", &tmp));
63 _cleanup_free_ char *masked_file = ASSERT_NOT_NULL(path_join(tmp, "masked.conf"));
64 ASSERT_OK_ERRNO(symlink("/dev/null", masked_file));
65 ASSERT_OK(cat_files(masked_file, /* dropins= */ NULL, /* flags= */ 0));
66 }
67
68 TEST(red_green_cross_check_mark) {
69 bool b = false;
70
71 printf("yea: <%s>\n", GREEN_CHECK_MARK());
72 printf("nay: <%s>\n", RED_CROSS_MARK());
73
74 printf("%s → %s → %s → %s\n",
75 COLOR_MARK_BOOL(b),
76 COLOR_MARK_BOOL(!b),
77 COLOR_MARK_BOOL(!!b),
78 COLOR_MARK_BOOL(!!!b));
79 }
80
81 TEST(print_separator) {
82 print_separator();
83 }
84
85 DEFINE_TEST_MAIN(LOG_INFO);