]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
lib/configs: add test program for ul_configs_file_list()
authorKarel Zak <kzak@redhat.com>
Thu, 9 Oct 2025 20:02:24 +0000 (22:02 +0200)
committerKarel Zak <kzak@redhat.com>
Tue, 14 Oct 2025 11:22:05 +0000 (13:22 +0200)
Add a test program with command line options to test the configuration
file list functionality. The test program allows specifying custom paths
for /etc, /run, and /usr directories, project name, config name, and suffix.

This enables testing the priority ordering and file discovery logic
of ul_configs_file_list() from the command line.

Signed-off-by: Karel Zak <kzak@redhat.com>
lib/Makemodule.am
lib/configs.c
meson.build

index 81ec4fe2fbb84fa8a4eaae7704f673b3a05aeb7c..a9da577348baf8bb31bb81444cdf07b63123bd9e 100644 (file)
@@ -91,6 +91,7 @@ check_PROGRAMS += \
        test_buffer \
        test_canonicalize \
        test_colors \
+       test_configs \
        test_fileeq \
        test_fileutils \
        test_ismounted \
@@ -209,6 +210,10 @@ test_loopdev_CFLAGS = $(AM_CFLAGS) -DTEST_PROGRAM_LOOPDEV
 test_loopdev_LDADD = $(LDADD) libcommon.la
 endif
 
+test_configs_SOURCES = lib/configs.c
+test_configs_CFLAGS = $(AM_CFLAGS) -DTEST_PROGRAM_CONFIGS
+test_configs_LDADD = $(LDADD) libcommon.la
+
 test_fileeq_SOURCES = lib/fileeq.c
 test_fileeq_CFLAGS = $(AM_CFLAGS) -DTEST_PROGRAM_FILEEQ
 
index 105d97f37a905abf51f2414abf8c9efc63b43f68..e720917e4e2bb5b6f1d1879c1320046c59d1fbf8 100644 (file)
@@ -394,3 +394,86 @@ int ul_configs_next_filename(struct list_head *file_list,
 
        return 0;
 }
+
+#ifdef TEST_PROGRAM_CONFIGS
+# include <getopt.h>
+
+int main(int argc, char *argv[])
+{
+       struct list_head file_list;
+       struct list_head *current = NULL;
+       char *name = NULL;
+       const char *etc_path = NULL;
+       const char *run_path = NULL;
+       const char *usr_path = NULL;
+       const char *project = NULL;
+       const char *config_name = NULL;
+       const char *config_suffix = NULL;
+       static const struct option longopts[] = {
+               { "etc", required_argument, NULL, 'e' },
+               { "run", required_argument, NULL, 'r' },
+               { "usr", required_argument, NULL, 'u' },
+               { "project", required_argument, NULL, 'p' },
+               { "name", required_argument, NULL, 'n' },
+               { "suffix", required_argument, NULL, 's' },
+               { "help", no_argument, NULL, 'h' },
+               { NULL, 0, NULL, 0 }
+       };
+       int ch, count;
+
+       while ((ch = getopt_long(argc, argv, "e:r:u:p:n:s:h", longopts, NULL)) != -1) {
+               switch (ch) {
+               case 'e':
+                       etc_path = optarg;
+                       break;
+               case 'r':
+                       run_path = optarg;
+                       break;
+               case 'u':
+                       usr_path = optarg;
+                       break;
+               case 'p':
+                       project = optarg;
+                       break;
+               case 'n':
+                       config_name = optarg;
+                       break;
+               case 's':
+                       config_suffix = optarg;
+                       break;
+               case 'h':
+                       printf("usage: %s [options]\n"
+                               " -e, --etc <path>      path to /etc directory\n"
+                               " -r, --run <path>      path to /run directory\n"
+                               " -u, --usr <path>      path to /usr directory\n"
+                               " -p, --project <name>  project name subdirectory\n"
+                               " -n, --name <name>     config file base name\n"
+                               " -s, --suffix <suffix> config file suffix\n"
+                               " -h, --help            display this help\n",
+                               program_invocation_short_name);
+                       return EXIT_SUCCESS;
+               default:
+                       return EXIT_FAILURE;
+               }
+       }
+
+       if (!config_name) {
+               fprintf(stderr, "config name is required (use --name)\n");
+               return EXIT_FAILURE;
+       }
+
+       count = ul_configs_file_list(&file_list, project, etc_path, run_path, usr_path,
+                                    config_name, config_suffix);
+       if (count < 0) {
+               fprintf(stderr, "failed to get config file list: %d\n", count);
+               return EXIT_FAILURE;
+       }
+
+       printf("Found %d configuration file(s):\n", count);
+       while (ul_configs_next_filename(&file_list, &current, &name) == 0)
+               printf("  %s\n", name);
+
+       ul_configs_free_list(&file_list);
+       return EXIT_SUCCESS;
+}
+#endif /* TEST_PROGRAM_CONFIGS */
index aff99dab9efe75be8c422520bbba7dca4c4a599e..66fcbef543ddcb4f7e2da8c804df0e5a436aadb3 100644 (file)
@@ -3638,6 +3638,17 @@ if not is_disabler(exe)
   exes += exe
 endif
 
+exe = executable(
+  'test_configs',
+  'lib/configs.c',
+  c_args : ['-DTEST_PROGRAM_CONFIGS'],
+  include_directories : dir_include,
+  link_with : lib_common,
+  build_by_default: program_tests)
+if not is_disabler(exe)
+  exes += exe
+endif
+
 exe = executable(
   'test_pwdutils',
   'lib/pwdutils.c',