Skip to content

Commit 7d5f18a

Browse files
pks-tttaylorr
authored andcommitted
t/unit-tests: update clar to 206accb
Update clar from: - 1516124 (Merge pull request #97 from pks-t/pks-whitespace-fixes, 2024-08-15). To: - 206accb (Merge pull request #108 from pks-t/pks-uclibc-without-wchar, 2024-10-21) This update includes a bunch of fixes and improvements that we have discussed in Git when initial support for clar was merged: - There is a ".editorconfig" file now. - Compatibility with Windows has been improved so that the clar compiles on this platform without an issue. This has been tested with Cygwin, MinGW and Microsoft Visual Studio. - clar now uses CMake. This does not impact us at all as we wire up the clar into our own build infrastructure anyway. This conversion was done such that we can easily run CI jobs against Windows. - Allocation failures are now checked for consistently. - We now define feature test macros in "clar.c", which fixes compilation on some platforms that didn't previously pull in non-standard functions like lstat(3p) or strdup(3p). This was reported by a user of OpenSUSE Leap. - We stop using `struct timezone`, which is undefined behaviour nowadays and results in a compilation error on some platforms. - We now use the combination of mktemp(3) and mkdir(3) on SunOS, same as we do on NonStop. - We now support uClibc without support for <wchar.h>. The most important bits here are the improved platform compatibility with Windows, OpenSUSE, SunOS and uClibc. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Taylor Blau <[email protected]>
1 parent ef8ce8f commit 7d5f18a

File tree

11 files changed

+189
-124
lines changed

11 files changed

+189
-124
lines changed

t/unit-tests/clar/.editorconfig

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
insert_final_newline = true
6+
7+
[*.{c,h}]
8+
indent_style = tab
9+
tab_width = 8
10+
11+
[CMakeLists.txt]
12+
indent_style = tab
13+
tab_width = 8

t/unit-tests/clar/.github/workflows/ci.yml

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,26 @@ jobs:
1010
build:
1111
strategy:
1212
matrix:
13-
os: [ ubuntu-latest, macos-latest ]
13+
platform:
14+
- os: ubuntu-latest
15+
generator: Unix Makefiles
16+
- os: macos-latest
17+
generator: Unix Makefiles
18+
- os: windows-latest
19+
generator: Visual Studio 17 2022
20+
- os: windows-latest
21+
generator: MSYS Makefiles
22+
- os: windows-latest
23+
generator: MinGW Makefiles
1424

15-
runs-on: ${{ matrix.os }}
25+
runs-on: ${{ matrix.platform.os }}
1626

1727
steps:
1828
- name: Check out
1929
uses: actions/checkout@v2
2030
- name: Build
2131
run: |
22-
cd test
23-
make
32+
mkdir build
33+
cd build
34+
cmake .. -G "${{matrix.platform.generator}}"
35+
cmake --build .

t/unit-tests/clar/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build/

t/unit-tests/clar/CMakeLists.txt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
cmake_minimum_required(VERSION 3.16..3.29)
2+
3+
project(clar LANGUAGES C)
4+
5+
option(BUILD_TESTS "Build test executable" ON)
6+
7+
add_library(clar INTERFACE)
8+
target_sources(clar INTERFACE
9+
clar.c
10+
clar.h
11+
clar/fixtures.h
12+
clar/fs.h
13+
clar/print.h
14+
clar/sandbox.h
15+
clar/summary.h
16+
)
17+
set_target_properties(clar PROPERTIES
18+
C_STANDARD 90
19+
C_STANDARD_REQUIRED ON
20+
C_EXTENSIONS OFF
21+
)
22+
23+
if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
24+
include(CTest)
25+
if(BUILD_TESTING)
26+
add_subdirectory(test)
27+
endif()
28+
endif()

t/unit-tests/clar/clar.c

Lines changed: 71 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@
44
* This file is part of clar, distributed under the ISC license.
55
* For full terms see the included COPYING file.
66
*/
7-
#include <assert.h>
7+
8+
#define _BSD_SOURCE
9+
#define _DARWIN_C_SOURCE
10+
#define _DEFAULT_SOURCE
11+
12+
#include <errno.h>
813
#include <setjmp.h>
914
#include <stdlib.h>
1015
#include <stdio.h>
@@ -13,11 +18,22 @@
1318
#include <stdarg.h>
1419
#include <wchar.h>
1520
#include <time.h>
21+
#include <inttypes.h>
1622

1723
/* required for sandboxing */
1824
#include <sys/types.h>
1925
#include <sys/stat.h>
2026

27+
#if defined(__UCLIBC__) && ! defined(__UCLIBC_HAS_WCHAR__)
28+
/*
29+
* uClibc can optionally be built without wchar support, in which case
30+
* the installed <wchar.h> is a stub that only defines the `whar_t`
31+
* type but none of the functions typically declared by it.
32+
*/
33+
#else
34+
# define CLAR_HAVE_WCHAR
35+
#endif
36+
2137
#ifdef _WIN32
2238
# define WIN32_LEAN_AND_MEAN
2339
# include <windows.h>
@@ -28,6 +44,9 @@
2844

2945
# ifndef stat
3046
# define stat(path, st) _stat(path, st)
47+
typedef struct _stat STAT_T;
48+
# else
49+
typedef struct stat STAT_T;
3150
# endif
3251
# ifndef mkdir
3352
# define mkdir(path, mode) _mkdir(path)
@@ -60,30 +79,11 @@
6079
# else
6180
# define p_snprintf snprintf
6281
# endif
63-
64-
# ifndef PRIuZ
65-
# define PRIuZ "Iu"
66-
# endif
67-
# ifndef PRIxZ
68-
# define PRIxZ "Ix"
69-
# endif
70-
71-
# if defined(_MSC_VER) || (defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR))
72-
typedef struct stat STAT_T;
73-
# else
74-
typedef struct _stat STAT_T;
75-
# endif
7682
#else
7783
# include <sys/wait.h> /* waitpid(2) */
7884
# include <unistd.h>
7985
# define _MAIN_CC
8086
# define p_snprintf snprintf
81-
# ifndef PRIuZ
82-
# define PRIuZ "zu"
83-
# endif
84-
# ifndef PRIxZ
85-
# define PRIxZ "zx"
86-
# endif
8787
typedef struct stat STAT_T;
8888
#endif
8989

@@ -102,7 +102,7 @@ fixture_path(const char *base, const char *fixture_name);
102102
struct clar_error {
103103
const char *file;
104104
const char *function;
105-
size_t line_number;
105+
uintmax_t line_number;
106106
const char *error_msg;
107107
char *description;
108108

@@ -195,11 +195,12 @@ static void clar_print_shutdown(int test_count, int suite_count, int error_count
195195
static void clar_print_error(int num, const struct clar_report *report, const struct clar_error *error);
196196
static void clar_print_ontest(const char *suite_name, const char *test_name, int test_number, enum cl_test_status failed);
197197
static void clar_print_onsuite(const char *suite_name, int suite_index);
198+
static void clar_print_onabortv(const char *msg, va_list argp);
198199
static void clar_print_onabort(const char *msg, ...);
199200

200201
/* From clar_sandbox.c */
201202
static void clar_unsandbox(void);
202-
static int clar_sandbox(void);
203+
static void clar_sandbox(void);
203204

204205
/* From summary.h */
205206
static struct clar_summary *clar_summary_init(const char *filename);
@@ -218,6 +219,15 @@ static int clar_summary_shutdown(struct clar_summary *fp);
218219
_clar.trace_payload); \
219220
} while (0)
220221

222+
static void clar_abort(const char *msg, ...)
223+
{
224+
va_list argp;
225+
va_start(argp, msg);
226+
clar_print_onabortv(msg, argp);
227+
va_end(argp);
228+
exit(-1);
229+
}
230+
221231
void cl_trace_register(cl_trace_cb *cb, void *payload)
222232
{
223233
_clar.pfn_trace_cb = cb;
@@ -271,9 +281,7 @@ static double clar_time_diff(clar_time *start, clar_time *end)
271281

272282
static void clar_time_now(clar_time *out)
273283
{
274-
struct timezone tz;
275-
276-
gettimeofday(out, &tz);
284+
gettimeofday(out, NULL);
277285
}
278286

279287
static double clar_time_diff(clar_time *start, clar_time *end)
@@ -386,7 +394,8 @@ clar_run_suite(const struct clar_suite *suite, const char *filter)
386394

387395
_clar.active_test = test[i].name;
388396

389-
report = calloc(1, sizeof(struct clar_report));
397+
if ((report = calloc(1, sizeof(*report))) == NULL)
398+
clar_abort("Failed to allocate report.\n");
390399
report->suite = _clar.active_suite;
391400
report->test = _clar.active_test;
392401
report->test_number = _clar.tests_ran;
@@ -479,9 +488,10 @@ clar_parse_args(int argc, char **argv)
479488

480489
switch (action) {
481490
case 's': {
482-
struct clar_explicit *explicit =
483-
calloc(1, sizeof(struct clar_explicit));
484-
assert(explicit);
491+
struct clar_explicit *explicit;
492+
493+
if ((explicit = calloc(1, sizeof(*explicit))) == NULL)
494+
clar_abort("Failed to allocate explicit test.\n");
485495

486496
explicit->suite_idx = j;
487497
explicit->filter = argument;
@@ -505,10 +515,8 @@ clar_parse_args(int argc, char **argv)
505515
}
506516
}
507517

508-
if (!found) {
509-
clar_print_onabort("No suite matching '%s' found.\n", argument);
510-
exit(-1);
511-
}
518+
if (!found)
519+
clar_abort("No suite matching '%s' found.\n", argument);
512520
break;
513521
}
514522

@@ -540,11 +548,17 @@ clar_parse_args(int argc, char **argv)
540548
case 'r':
541549
_clar.write_summary = 1;
542550
free(_clar.summary_filename);
543-
_clar.summary_filename = *(argument + 2) ? strdup(argument + 2) : NULL;
551+
if (*(argument + 2)) {
552+
if ((_clar.summary_filename = strdup(argument + 2)) == NULL)
553+
clar_abort("Failed to allocate summary filename.\n");
554+
} else {
555+
_clar.summary_filename = NULL;
556+
}
544557
break;
545558

546559
default:
547-
assert(!"Unexpected commandline argument!");
560+
clar_abort("Unexpected commandline argument '%s'.\n",
561+
argument[1]);
548562
}
549563
}
550564
}
@@ -566,22 +580,18 @@ clar_test_init(int argc, char **argv)
566580
if (!_clar.summary_filename &&
567581
(summary_env = getenv("CLAR_SUMMARY")) != NULL) {
568582
_clar.write_summary = 1;
569-
_clar.summary_filename = strdup(summary_env);
583+
if ((_clar.summary_filename = strdup(summary_env)) == NULL)
584+
clar_abort("Failed to allocate summary filename.\n");
570585
}
571586

572587
if (_clar.write_summary && !_clar.summary_filename)
573-
_clar.summary_filename = strdup("summary.xml");
588+
if ((_clar.summary_filename = strdup("summary.xml")) == NULL)
589+
clar_abort("Failed to allocate summary filename.\n");
574590

575-
if (_clar.write_summary &&
576-
!(_clar.summary = clar_summary_init(_clar.summary_filename))) {
577-
clar_print_onabort("Failed to open the summary file\n");
578-
exit(-1);
579-
}
591+
if (_clar.write_summary)
592+
_clar.summary = clar_summary_init(_clar.summary_filename);
580593

581-
if (clar_sandbox() < 0) {
582-
clar_print_onabort("Failed to sandbox the test runner.\n");
583-
exit(-1);
584-
}
594+
clar_sandbox();
585595
}
586596

587597
int
@@ -615,10 +625,9 @@ clar_test_shutdown(void)
615625

616626
clar_unsandbox();
617627

618-
if (_clar.write_summary && clar_summary_shutdown(_clar.summary) < 0) {
619-
clar_print_onabort("Failed to write the summary file\n");
620-
exit(-1);
621-
}
628+
if (_clar.write_summary && clar_summary_shutdown(_clar.summary) < 0)
629+
clar_abort("Failed to write the summary file '%s: %s.\n",
630+
_clar.summary_filename, strerror(errno));
622631

623632
for (explicit = _clar.explicit; explicit; explicit = explicit_next) {
624633
explicit_next = explicit->next;
@@ -649,7 +658,7 @@ static void abort_test(void)
649658
{
650659
if (!_clar.trampoline_enabled) {
651660
clar_print_onabort(
652-
"Fatal error: a cleanup method raised an exception.");
661+
"Fatal error: a cleanup method raised an exception.\n");
653662
clar_report_errors(_clar.last_report);
654663
exit(-1);
655664
}
@@ -673,7 +682,10 @@ void clar__fail(
673682
const char *description,
674683
int should_abort)
675684
{
676-
struct clar_error *error = calloc(1, sizeof(struct clar_error));
685+
struct clar_error *error;
686+
687+
if ((error = calloc(1, sizeof(*error))) == NULL)
688+
clar_abort("Failed to allocate error.\n");
677689

678690
if (_clar.last_report->errors == NULL)
679691
_clar.last_report->errors = error;
@@ -688,8 +700,9 @@ void clar__fail(
688700
error->line_number = line;
689701
error->error_msg = error_msg;
690702

691-
if (description != NULL)
692-
error->description = strdup(description);
703+
if (description != NULL &&
704+
(error->description = strdup(description)) == NULL)
705+
clar_abort("Failed to allocate description.\n");
693706

694707
_clar.total_errors++;
695708
_clar.last_report->status = CL_TEST_FAILURE;
@@ -763,6 +776,7 @@ void clar__assert_equal(
763776
}
764777
}
765778
}
779+
#ifdef CLAR_HAVE_WCHAR
766780
else if (!strcmp("%ls", fmt)) {
767781
const wchar_t *wcs1 = va_arg(args, const wchar_t *);
768782
const wchar_t *wcs2 = va_arg(args, const wchar_t *);
@@ -798,8 +812,9 @@ void clar__assert_equal(
798812
}
799813
}
800814
}
801-
else if (!strcmp("%"PRIuZ, fmt) || !strcmp("%"PRIxZ, fmt)) {
802-
size_t sz1 = va_arg(args, size_t), sz2 = va_arg(args, size_t);
815+
#endif /* CLAR_HAVE_WCHAR */
816+
else if (!strcmp("%"PRIuMAX, fmt) || !strcmp("%"PRIxMAX, fmt)) {
817+
uintmax_t sz1 = va_arg(args, uintmax_t), sz2 = va_arg(args, uintmax_t);
803818
is_equal = (sz1 == sz2);
804819
if (!is_equal) {
805820
int offset = p_snprintf(buf, sizeof(buf), fmt, sz1);

t/unit-tests/clar/clar/print.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ static void clar_print_clap_error(int num, const struct clar_report *report, con
2121
{
2222
printf(" %d) Failure:\n", num);
2323

24-
printf("%s::%s [%s:%"PRIuZ"]\n",
24+
printf("%s::%s [%s:%"PRIuMAX"]\n",
2525
report->suite,
2626
report->test,
2727
error->file,
@@ -136,7 +136,7 @@ static void clar_print_tap_ontest(const char *suite_name, const char *test_name,
136136

137137
printf(" at:\n");
138138
printf(" file: '"); print_escaped(error->file); printf("'\n");
139-
printf(" line: %" PRIuZ "\n", error->line_number);
139+
printf(" line: %" PRIuMAX "\n", error->line_number);
140140
printf(" function: '%s'\n", error->function);
141141
printf(" ---\n");
142142

@@ -202,10 +202,15 @@ static void clar_print_onsuite(const char *suite_name, int suite_index)
202202
PRINT(onsuite, suite_name, suite_index);
203203
}
204204

205+
static void clar_print_onabortv(const char *msg, va_list argp)
206+
{
207+
PRINT(onabort, msg, argp);
208+
}
209+
205210
static void clar_print_onabort(const char *msg, ...)
206211
{
207212
va_list argp;
208213
va_start(argp, msg);
209-
PRINT(onabort, msg, argp);
214+
clar_print_onabortv(msg, argp);
210215
va_end(argp);
211216
}

0 commit comments

Comments
 (0)