Debian Codegraph @

tests/integration.c

raw symbols SCIP
1 /**
2 * SPDX-FileCopyrightText: 2016-2022 Dr. Tobias Quathamer <[email protected]>
3 *
4 * SPDX-License-Identifier: GPL-3.0-or-later
5 */
6
7 #include <glib.h>
8 #include <string.h>
9 #include <unistd.h>
10
11 /**
12 * Test all codes for ISO standard.
13 */
14 void test_integration_add_test_from_files(gconstpointer data)
15 {
16 gchar *path_prefix = (gchar *) data;
17 gchar *filename;
18 GError *error = NULL;
19 gchar *tmp = NULL;
20 gchar *test_binary = NULL;
21 gchar *commandline = NULL;
22 gchar *expected_stdout = NULL;
23 gchar *expected_stderr = NULL;
24 gchar **commandline_args = NULL;
25
26 if (g_test_subprocess()) {
27 // Get the commandline
28 filename = g_strdup_printf("%s_test_commandline.txt", path_prefix);
29 g_file_get_contents(filename, &commandline, NULL, &error);
30 g_assert_null(error);
31 g_assert_nonnull(commandline);
32 g_free(filename);
33
34 // Set up the commandline prefix
35 tmp = g_strdup_printf("isoquery -p %s/data %s", TESTDIR, commandline);
36 g_free(commandline);
37 commandline = tmp;
38
39 // Set up arguments
40 commandline_args = g_strsplit(g_strchomp(commandline), " ", -1);
41
42 // Set up a defined environment to execute test reproducibly
43 gchar *env[] = { "LC_ALL=en_GB.UTF-8", NULL };
44
45 // Construct path to testing binary
46 test_binary = g_strdup_printf("%s/src/isoquery", BUILDDIR);
47
48 // Execute program
49 execvpe(test_binary, commandline_args, env);
50 g_free(test_binary);
51
52 g_strfreev(commandline_args);
53 g_free(commandline);
54
55 return;
56 }
57 g_test_trap_subprocess(NULL, 0, 0);
58 g_test_trap_assert_passed();
59
60 // Get the expected output on stdout
61 filename = g_strdup_printf("%s_test_stdout.txt", path_prefix);
62 g_file_get_contents(filename, &expected_stdout, NULL, &error);
63 g_assert_null(error);
64 g_assert_nonnull(expected_stdout);
65 g_free(filename);
66
67 // Get the expected output on stderr, if available
68 filename = g_strdup_printf("%s_test_stderr.txt", path_prefix);
69 if (g_file_get_contents(filename, &expected_stderr, NULL, &error)) {
70 g_assert_null(error);
71 g_assert_nonnull(expected_stderr);
72 } else {
73 // Make sure that the only acceptable error is "file not found".
74 g_assert_cmpint(error->code, ==, G_FILE_ERROR_NOENT);
75 expected_stderr = g_strdup("");
76 }
77 g_free(filename);
78
79 g_test_trap_assert_stdout(expected_stdout);
80 g_test_trap_assert_stderr(expected_stderr);
81
82 g_free(expected_stdout);
83 g_free(expected_stderr);
84 }
85
86 /**
87 * Initializing all test functions
88 */
89 int main(int argc, gchar *argv[])
90 {
91 gchar *test_directories[] = {
92 "iso_639-2", "iso_639-3", "iso_639-5",
93 "iso_3166-1", "iso_3166-2", "iso_3166-3",
94 "iso_4217", "iso_15924",
95 "common",
96 NULL
97 };
98 gchar *pathname, *testpath, *testname;
99 const gchar *filename;
100 GDir *testdir;
101 GError *error = NULL;
102 GList *testfiles = NULL;
103 int i = 0;
104
105 g_test_init(&argc, &argv, NULL);
106
107 // Add common tests for various standards
108 while (test_directories[i]) {
109 // Open the directory for the current standard
110 testdir = g_dir_open(g_strdup_printf("%s/expected/%s", TESTDIR, test_directories[i]), 0, &error);
111 g_assert_null(error);
112 g_assert_nonnull(testdir);
113
114 // Read in all files
115 while ((filename = g_dir_read_name(testdir))) {
116 // Only add files for command lines, the stdout files
117 // will be read by the test; the stderr files are optional.
118 if (g_str_has_suffix(filename, "_test_commandline.txt")) {
119 testfiles = g_list_prepend(testfiles, g_strdup(filename));
120 }
121 }
122 testfiles = g_list_sort(testfiles, (GCompareFunc) * g_strcmp0);
123 g_dir_close(testdir);
124
125 // Add all test files to the queue
126 for (GList * l = testfiles; l != NULL; l = l->next) {
127 // Remove the file extension and test description to get a name
128 testname = g_strndup(l->data, strlen(l->data) - strlen("_test_commandline.txt"));
129 testpath = g_strdup_printf("/integration/%s/%s", test_directories[i], testname);
130
131 // Construct the pathname to the file
132 pathname = g_strdup_printf("%s/expected/%s/%s", TESTDIR, test_directories[i], testname);
133 g_test_add_data_func(testpath, pathname, test_integration_add_test_from_files);
134 g_free(testname);
135 g_free(testpath);
136 }
137 g_list_free_full(testfiles, *g_free);
138 testfiles = NULL;
139 i++;
140 }
141
142 return g_test_run();
143 }