From: Karel Zak Date: Thu, 9 Oct 2025 20:02:24 +0000 (+0200) Subject: lib/configs: add test program for ul_configs_file_list() X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3d57b9059a94c9e0ab6529fad3d24778086ee8ab;p=thirdparty%2Futil-linux.git lib/configs: add test program for ul_configs_file_list() 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 --- diff --git a/lib/Makemodule.am b/lib/Makemodule.am index 81ec4fe2f..a9da57734 100644 --- a/lib/Makemodule.am +++ b/lib/Makemodule.am @@ -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 diff --git a/lib/configs.c b/lib/configs.c index 105d97f37..e720917e4 100644 --- a/lib/configs.c +++ b/lib/configs.c @@ -394,3 +394,86 @@ int ul_configs_next_filename(struct list_head *file_list, return 0; } + +#ifdef TEST_PROGRAM_CONFIGS +# include + +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 to /etc directory\n" + " -r, --run path to /run directory\n" + " -u, --usr path to /usr directory\n" + " -p, --project project name subdirectory\n" + " -n, --name config file base name\n" + " -s, --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, ¤t, &name) == 0) + printf(" %s\n", name); + + ul_configs_free_list(&file_list); + return EXIT_SUCCESS; +} +#endif /* TEST_PROGRAM_CONFIGS */ diff --git a/meson.build b/meson.build index aff99dab9..66fcbef54 100644 --- a/meson.build +++ b/meson.build @@ -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',