Skip to content

Commit

Permalink
Added cmdline arguments + output file flag (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
iWas-Coder committed Sep 30, 2024
1 parent a1c4c00 commit d642fc3
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 6 deletions.
2 changes: 1 addition & 1 deletion include/carbon_junit.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ typedef struct {
unsigned char has_failed;
} carbon_junit_testcase;

void carbon_junit_output(carbon_junit_testsuite *junit_ts, carbon_junit_testcase *junit_tcs);
void carbon_junit_output(carbon_junit_testsuite *junit_ts, carbon_junit_testcase *junit_tcs, const char *out_filename);

#endif // CARBON_JUNIT_H_

Expand Down
5 changes: 5 additions & 0 deletions include/carbon_test_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ typedef struct {
size_t n;
} Suite;

typedef struct {
char *output;
} CmdArgs;

void carbon_test_manager_argparse(int argc, char **argv);
Suite carbon_test_manager_spawn(void);
Test *carbon_test_manager_alloc(Suite *s);
void carbon_test_manager_register_s(Suite *s, TestFunc test_func, char *name);
Expand Down
7 changes: 4 additions & 3 deletions src/carbon_junit.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@
#define XML_OUT_FILENAME "carbon_results.xml"
#define ISO_8601_FMT "%Y-%m-%dT%H:%M:%S%z"

void carbon_junit_output(carbon_junit_testsuite *junit_ts, carbon_junit_testcase *junit_tcs) {
FILE *fd = fopen(XML_OUT_FILENAME, "w");
void carbon_junit_output(carbon_junit_testsuite *junit_ts, carbon_junit_testcase *junit_tcs, const char *out_filename) {
if (!out_filename) out_filename = XML_OUT_FILENAME;
FILE *fd = fopen(out_filename, "w");
if (!fd) {
CARBON_ERROR("[ERROR]: carbon_junit_output :: unable to open file (`carbon_results.xml`)\n");
CARBON_ERROR("[ERROR]: carbon_junit_output :: unable to open file (`%s`)\n", out_filename);
return;
}
time_t t = time(0);
Expand Down
38 changes: 37 additions & 1 deletion src/carbon_test_manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,42 @@
#include <stdlib.h>

static Suite test_suite = {0};
static CmdArgs cmd_args = {0};

static const char * const help_msg = "usage: %s [OPTION]\n"
"Options:\n"
" -o, --output output JUnit XML test results to specific file (default: `carbon_results.xml`)\n"
" -h, --help display this help and exit\n"
" -v, --version output version information and exit\n\n"
"Report bugs to: <https://github.com/sparky-game/carbon/issues>\n"
"BSD Carbon home page: <https://github.com/sparky-game/carbon>\n";

static const char * const version_msg = "BSD Carbon %s\n"
"Copyright (C) 2024 Wasym A. Alonso\n"
"License MIT: <https://opensource.org/license/MIT>.\n"
"This is free software: you are free to change and redistribute it.\n"
"There is NO WARRANTY, to the extent permitted by law.\n\n"
"Written by Wasym A. Alonso\n";

void carbon_test_manager_argparse(int argc, char **argv) {
if (argc == 1) return;
if (argc == 3 && (!strcmp(argv[1], "-o") || !strcmp(argv[1], "--output"))) {
cmd_args.output = argv[2];
return;
}
if (argc == 2 && (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help"))) {
CARBON_INFO(help_msg, argv[0]);
exit(0);
}
if (argc == 2 && (!strcmp(argv[1], "-v") || !strcmp(argv[1], "--version"))) {
CARBON_INFO(version_msg, CARBON_VERSION);
exit(0);
}
else {
CARBON_ERROR("[ERROR]: unrecognized option\nTry '%s --help' for more information.\n", argv[0]);
exit(1);
}
}

Suite carbon_test_manager_spawn(void) {
return (Suite) {0};
Expand Down Expand Up @@ -128,7 +164,7 @@ unsigned char carbon_test_manager_run(void) {
total_time_micro);
else CARBON_INFO(CARBON_COLOR_GREEN "=========== %zu passed in %.2fs ===========" CARBON_COLOR_RESET "\n", passed, total_time);
}
carbon_junit_output(&junit_testsuite_info, junit_testcase_infos);
carbon_junit_output(&junit_testsuite_info, junit_testcase_infos, cmd_args.output);
carbon_test_manager_cleanup();
return status;
}
3 changes: 2 additions & 1 deletion test/src/carbon.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
#include <carbon_should_test.h>
#include <carbon_test_manager_test.h>

int main(void) {
int main(int argc, char **argv) {
carbon_test_manager_argparse(argc, argv);
carbon_should_test_register();
carbon_test_manager_test_register();
return carbon_test_manager_run();
Expand Down

0 comments on commit d642fc3

Please sign in to comment.