forked from libcheck/check
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_check_log.c
315 lines (248 loc) · 8.68 KB
/
check_check_log.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*
* Check: a unit test framework for C
* Copyright (C) 2001, 2002 Arien Malec
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
#include "../lib/libcompat.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <check.h>
#include "check_check.h"
#if HAVE_DECL_SETENV
/* save environment variable's value and set new value */
static int save_set_env(const char *name, const char *value,
const char **old_value)
{
*old_value = getenv(name);
return setenv(name, value, 1);
}
/* restore environment variable's old value, handle cases where
variable must be unset (old value is NULL) */
static int restore_env(const char *name, const char *old_value)
{
int res;
if (old_value == NULL) {
res = unsetenv(name);
} else {
res = setenv(name, old_value, 1);
}
return res;
}
#endif /* HAVE_DECL_SETENV */
START_TEST(test_set_log)
{
Suite *s = suite_create("Suite");
SRunner *sr = srunner_create(s);
srunner_set_log (sr, "test_log");
ck_assert_msg (srunner_has_log (sr), "SRunner not logging");
ck_assert_msg (strcmp(srunner_log_fname(sr), "test_log") == 0,
"Bad file name returned");
srunner_free(sr);
}
END_TEST
#if HAVE_DECL_SETENV
/* Test enabling logging via environment variable */
START_TEST(test_set_log_env)
{
const char *old_val;
Suite *s = suite_create("Suite");
SRunner *sr = srunner_create(s);
/* check that setting log file via environment variable works */
ck_assert_msg(save_set_env("CK_LOG_FILE_NAME", "test_log", &old_val) == 0,
"Failed to set environment variable");
ck_assert_msg (srunner_has_log (sr), "SRunner not logging");
ck_assert_msg (strcmp(srunner_log_fname(sr), "test_log") == 0,
"Bad file name returned");
/* check that explicit call to srunner_set_log()
overrides environment variable */
srunner_set_log (sr, "test2_log");
ck_assert_msg (srunner_has_log (sr), "SRunner not logging");
ck_assert_msg (strcmp(srunner_log_fname(sr), "test2_log") == 0,
"Bad file name returned");
/* restore old environment */
ck_assert_msg(restore_env("CK_LOG_FILE_NAME", old_val) == 0,
"Failed to restore environment variable");
srunner_free(sr);
}
END_TEST
#endif /* HAVE_DECL_SETENV */
START_TEST(test_no_set_log)
{
Suite *s = suite_create("Suite");
SRunner *sr = srunner_create(s);
ck_assert_msg (!srunner_has_log (sr), "SRunner not logging");
ck_assert_msg (srunner_log_fname(sr) == NULL, "Bad file name returned");
srunner_free(sr);
}
END_TEST
START_TEST(test_double_set_log)
{
Suite *s = suite_create("Suite");
SRunner *sr = srunner_create(s);
srunner_set_log (sr, "test_log");
srunner_set_log (sr, "test2_log");
ck_assert_msg(strcmp(srunner_log_fname(sr), "test_log") == 0,
"Log file is initialize only and shouldn't be changeable once set");
srunner_free(sr);
}
END_TEST
START_TEST(test_set_xml)
{
Suite *s = suite_create("Suite");
SRunner *sr = srunner_create(s);
srunner_set_xml (sr, "test_log.xml");
ck_assert_msg (srunner_has_xml (sr), "SRunner not logging XML");
ck_assert_msg (strcmp(srunner_xml_fname(sr), "test_log.xml") == 0,
"Bad file name returned");
srunner_free(sr);
}
END_TEST
#if HAVE_DECL_SETENV
/* Test enabling XML logging via environment variable */
START_TEST(test_set_xml_env)
{
const char *old_val;
Suite *s = suite_create("Suite");
SRunner *sr = srunner_create(s);
/* check that setting XML log file via environment variable works */
ck_assert_msg(save_set_env("CK_XML_LOG_FILE_NAME", "test_log.xml", &old_val) == 0,
"Failed to set environment variable");
ck_assert_msg (srunner_has_xml (sr), "SRunner not logging XML");
ck_assert_msg (strcmp(srunner_xml_fname(sr), "test_log.xml") == 0,
"Bad file name returned");
/* check that explicit call to srunner_set_xml()
overrides environment variable */
srunner_set_xml (sr, "test2_log.xml");
ck_assert_msg (srunner_has_xml (sr), "SRunner not logging XML");
ck_assert_msg (strcmp(srunner_xml_fname(sr), "test2_log.xml") == 0,
"Bad file name returned");
/* restore old environment */
ck_assert_msg(restore_env("CK_XML_LOG_FILE_NAME", old_val) == 0,
"Failed to restore environment variable");
srunner_free(sr);
}
END_TEST
#endif /* HAVE_DECL_SETENV */
START_TEST(test_no_set_xml)
{
Suite *s = suite_create("Suite");
SRunner *sr = srunner_create(s);
ck_assert_msg (!srunner_has_xml (sr), "SRunner not logging XML");
ck_assert_msg (srunner_xml_fname(sr) == NULL, "Bad file name returned");
srunner_free(sr);
}
END_TEST
START_TEST(test_double_set_xml)
{
Suite *s = suite_create("Suite");
SRunner *sr = srunner_create(s);
srunner_set_xml (sr, "test_log.xml");
srunner_set_xml (sr, "test2_log.xml");
ck_assert_msg(strcmp(srunner_xml_fname(sr), "test_log.xml") == 0,
"XML Log file is initialize only and shouldn't be changeable once set");
srunner_free(sr);
}
END_TEST
START_TEST(test_set_tap)
{
Suite *s = suite_create("Suite");
SRunner *sr = srunner_create(s);
srunner_set_tap (sr, "test_log.tap");
ck_assert_msg (srunner_has_tap (sr), "SRunner not logging TAP");
ck_assert_msg (strcmp(srunner_tap_fname(sr), "test_log.tap") == 0,
"Bad file name returned");
srunner_free(sr);
}
END_TEST
#if HAVE_DECL_SETENV
/* Test enabling TAP logging via environment variable */
START_TEST(test_set_tap_env)
{
const char *old_val;
Suite *s = suite_create("Suite");
SRunner *sr = srunner_create(s);
/* check that setting XML log file via environment variable works */
ck_assert_msg(save_set_env("CK_TAP_LOG_FILE_NAME", "test_log.tap", &old_val) == 0,
"Failed to set environment variable");
ck_assert_msg (srunner_has_tap (sr), "SRunner not logging TAP");
ck_assert_msg (strcmp(srunner_tap_fname(sr), "test_log.tap") == 0,
"Bad file name returned");
/* check that explicit call to srunner_set_tap()
overrides environment variable */
srunner_set_tap (sr, "test2_log.tap");
ck_assert_msg (srunner_has_tap (sr), "SRunner not logging TAP");
ck_assert_msg (strcmp(srunner_tap_fname(sr), "test2_log.tap") == 0,
"Bad file name returned");
/* restore old environment */
ck_assert_msg(restore_env("CK_TAP_LOG_FILE_NAME", old_val) == 0,
"Failed to restore environment variable");
srunner_free(sr);
}
END_TEST
#endif /* HAVE_DECL_SETENV */
START_TEST(test_no_set_tap)
{
Suite *s = suite_create("Suite");
SRunner *sr = srunner_create(s);
ck_assert_msg (!srunner_has_tap (sr), "SRunner not logging TAP");
ck_assert_msg (srunner_tap_fname(sr) == NULL, "Bad file name returned");
srunner_free(sr);
}
END_TEST
START_TEST(test_double_set_tap)
{
Suite *s = suite_create("Suite");
SRunner *sr = srunner_create(s);
srunner_set_tap (sr, "test_log.tap");
srunner_set_tap (sr, "test2_log.tap");
ck_assert_msg(strcmp(srunner_tap_fname(sr), "test_log.tap") == 0,
"TAP Log file is initialize only and shouldn't be changeable once set");
srunner_free(sr);
}
END_TEST
Suite *make_log_suite(void)
{
Suite *s;
TCase *tc_core, *tc_core_xml, *tc_core_tap;
s = suite_create("Log");
tc_core = tcase_create("Core");
tc_core_xml = tcase_create("Core XML");
tc_core_tap = tcase_create("Core TAP");
suite_add_tcase(s, tc_core);
tcase_add_test(tc_core, test_set_log);
#if HAVE_DECL_SETENV
tcase_add_test(tc_core, test_set_log_env);
#endif /* HAVE_DECL_SETENV */
tcase_add_test(tc_core, test_no_set_log);
tcase_add_test(tc_core, test_double_set_log);
suite_add_tcase(s, tc_core_xml);
tcase_add_test(tc_core_xml, test_set_xml);
#if HAVE_DECL_SETENV
tcase_add_test(tc_core_xml, test_set_xml_env);
#endif /* HAVE_DECL_SETENV */
tcase_add_test(tc_core_xml, test_no_set_xml);
tcase_add_test(tc_core_xml, test_double_set_xml);
suite_add_tcase(s, tc_core_tap);
tcase_add_test(tc_core_tap, test_set_tap);
#if HAVE_DECL_SETENV
tcase_add_test(tc_core_tap, test_set_tap_env);
#endif /* HAVE_DECL_SETENV */
tcase_add_test(tc_core_tap, test_no_set_tap);
tcase_add_test(tc_core_tap, test_double_set_tap);
return s;
}