check_check_log.c 8.7 KB

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