Skip to content

Commit

Permalink
chore: remove dependency on libutil
Browse files Browse the repository at this point in the history
clib doesn't handle differing transitive dependencies very well. it's
impossible to actually update libutil and test with libtap because the
build has two copies of the libutil header file
  • Loading branch information
exbotanical committed Apr 23, 2024
1 parent 86f01f1 commit 222de12
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 60 deletions.
5 changes: 1 addition & 4 deletions clib.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
{
"name": "libtap",
"version": "0.0.4",
"version": "0.0.5",
"author": "Matthew Zito",
"repo": "exbotanical/libtap",
"license": "MIT",
"description": "An implementation of TAP testing",
"keywords": ["test anything protocol", "testing", "unit test"],
"src": ["include/libtap.h", "src/tap.c"],
"dependencies": {
"exbotanical/libutil": "*"
},
"development": {
"exbotanical/print-assert": "0.0.1"
}
Expand Down
43 changes: 32 additions & 11 deletions include/libtap.h
Original file line number Diff line number Diff line change
@@ -1,20 +1,41 @@
#ifndef LIBTAP_H
#define LIBTAP_H

#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/param.h>
#include <sys/wait.h>
#include <unistd.h>

#include "libutil/libutil.h"

#ifdef __cplusplus
extern "C" {
#endif

// TODO: parser for gh actions, etc

char*
__s_fmt__ (char* fmt, ...) {
va_list args, args_cp;
va_start(args, fmt);
va_copy(args_cp, args);

// Pass length of zero first to determine number of bytes needed
unsigned int n = vsnprintf(NULL, 0, fmt, args) + 1;
char* buf = (char*)malloc(n);
if (!buf) {
return NULL;
}

vsnprintf(buf, n, fmt, args_cp);

va_end(args);
va_end(args_cp);

return buf;
}

unsigned int __ok(
unsigned int ok,
const char* fn_name,
Expand All @@ -40,7 +61,7 @@ unsigned int exit_status(void);
unsigned int bail_out(const char* fmt, ...);

#define ok(test, ...) \
__ok(test ? 1 : 0, __func__, __FILE__, __LINE__, s_fmt(__VA_ARGS__))
__ok(test ? 1 : 0, __func__, __FILE__, __LINE__, __s_fmt__(__VA_ARGS__))

#define is(actual, expected, ...) \
__ok( \
Expand All @@ -51,21 +72,21 @@ unsigned int bail_out(const char* fmt, ...);
__func__, \
__FILE__, \
__LINE__, \
s_fmt(__VA_ARGS__) \
__s_fmt__(__VA_ARGS__) \
);

#define skip_start(cond, num_skips, ...) \
do { \
if (cond) { \
__skip(num_skips, s_fmt(__VA_ARGS__)); \
break; \
#define skip_start(cond, num_skips, ...) \
do { \
if (cond) { \
__skip(num_skips, __s_fmt__(__VA_ARGS__)); \
break; \
}

#define skip_end() \
} \
while (0)

#define skip(test, ...) __skip(1, s_fmt(__VA_ARGS__));
#define skip(test, ...) __skip(1, __s_fmt__(__VA_ARGS__));

#define done_testing() return exit_status()

Expand Down Expand Up @@ -106,7 +127,7 @@ unsigned int bail_out(const char* fmt, ...);
__func__, \
__FILE__, \
__LINE__, \
s_fmt(__VA_ARGS__) \
__s_fmt__(__VA_ARGS__) \
); \
} while (0)

Expand Down
36 changes: 12 additions & 24 deletions src/tap.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ static unsigned int is_todo_block = 0;
static char* todo_msg = NULL;

noreturn void
panic (const unsigned int errcode, const char* fmt, ...)
{
panic (const unsigned int errcode, const char* fmt, ...) {
va_list args;
va_start(args, fmt);
vfprintf(stderr, fmt, args);
Expand All @@ -51,16 +50,14 @@ panic (const unsigned int errcode, const char* fmt, ...)
}

static void
diagv (const char* fmt, va_list ap)
{
diagv (const char* fmt, va_list ap) {
fprintf(stdout, "# ");
vfprintf(stdout, fmt, ap);
fprintf(stdout, "\n");
}

static void
cleanup (void)
{
cleanup (void) {
// Fast exit if we've forked
if (getpid() != test_runner_pid) {
return;
Expand Down Expand Up @@ -109,8 +106,7 @@ __ok (
const char* file,
const unsigned int line,
char* msg
)
{
) {
lock();

num_ran_tests++;
Expand Down Expand Up @@ -155,8 +151,7 @@ __ok (
}

void
__skip (unsigned int num_skips, char* msg)
{
__skip (unsigned int num_skips, char* msg) {
with_lock({
while (num_skips--) {
fprintf(stdout, "ok %d # SKIP %s", ++num_ran_tests, msg);
Expand All @@ -167,8 +162,7 @@ __skip (unsigned int num_skips, char* msg)
}

int
__write_shared_mem (int status)
{
__write_shared_mem (int status) {
static int* test_died = NULL;
int prev;

Expand All @@ -190,8 +184,7 @@ __write_shared_mem (int status)
}

void
todo_start (const char* fmt, ...)
{
todo_start (const char* fmt, ...) {
va_list ap;

with_lock({
Expand All @@ -206,8 +199,7 @@ todo_start (const char* fmt, ...)
}

void
todo_end (void)
{
todo_end (void) {
with_lock({
is_todo_block = 0;
free(todo_msg);
Expand All @@ -216,17 +208,15 @@ todo_end (void)
}

void
diag (const char* fmt, ...)
{
diag (const char* fmt, ...) {
va_list ap;
va_start(ap, fmt);
diagv(fmt, ap);
va_end(ap);
}

void
plan (unsigned int num_ran_tests)
{
plan (unsigned int num_ran_tests) {
lock();

static unsigned int singleton = 0;
Expand Down Expand Up @@ -262,8 +252,7 @@ plan (unsigned int num_ran_tests)
}

unsigned int
exit_status (void)
{
exit_status (void) {
unsigned int retval;

with_lock({
Expand All @@ -282,8 +271,7 @@ exit_status (void)
}

unsigned int
bail_out (const char* fmt, ...)
{
bail_out (const char* fmt, ...) {
va_list args;

va_start(args, fmt);
Expand Down
30 changes: 9 additions & 21 deletions t/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,41 +74,29 @@ main () {

todo_end();

lives(
{
will_succeed();
printf("test\n");
},
"doesn't die"
);
lives({ will_succeed(); }, "doesn't die");

assert(pa_match_stdout("ok 9 - doesn't die\n") == 1);

dies(
{
will_fail();
printf("test\n");
},
"dies"
);
dies({ will_fail(); }, "dies");

assert(
pa_match_stdout("not ok 10 - dies\n"
"# \tFailed test (t/main.c:main at line 87)\n")
"# \tFailed test (t/main.c:main at line 81)\n")
== 1
);

char *x = s_fmt("x");
char *y = s_fmt("y");
char *a = s_fmt("a");
char *a2 = s_fmt("a");
char *b = s_fmt("b");
char *x = strdup("x");
char *y = strdup("y");
char *a = strdup("a");
char *a2 = strdup("a");
char *b = strdup("b");
char *n = NULL;

is(a, b, "not equal");
assert(
pa_match_stdout("not ok 11 - not equal\n"
"# \tFailed test (t/main.c:main at line 108)\n")
"# \tFailed test (t/main.c:main at line 96)\n")
== 1
);

Expand Down

0 comments on commit 222de12

Please sign in to comment.