• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: LGPL-2.1
2 /*
3  * Copyright (C) 2020, VMware, Tzvetomir Stoyanov <tz.stoyanov@gmail.com>
4  *
5  */
6 #include <stdio.h>
7 #include <unistd.h>
8 #include <getopt.h>
9 #include <stdlib.h>
10 #include <libgen.h>
11 
12 #include <CUnit/CUnit.h>
13 #include <CUnit/Basic.h>
14 
15 #include "trace-utest.h"
16 
17 const char *argv0;
18 bool show_output;
19 
20 enum unit_tests {
21 	RUN_NONE	= 0,
22 	RUN_TRACECMD	= (1 << 0),
23 	RUN_ALL		= 0xFFFF
24 };
25 
print_help(char ** argv)26 static void print_help(char **argv)
27 {
28 	printf("Usage: %s [OPTIONS]\n", basename(argv[0]));
29 	printf("\t-s, --silent\tPrint test summary\n");
30 	printf("\t-r, --run test\tRun specific test:\n");
31 	printf("\t\t  trace-cmd   run trace-cmd tests\n");
32 	printf("\t-h, --help\tPrint usage information\n");
33 	exit(0);
34 }
35 
main(int argc,char ** argv)36 int main(int argc, char **argv)
37 {
38 	CU_BasicRunMode verbose = CU_BRM_VERBOSE;
39 	enum unit_tests tests = RUN_NONE;
40 
41 	argv0 = argv[0];
42 
43 	for (;;) {
44 		int c;
45 		int index = 0;
46 		const char *opts = "+hsr:v";
47 		static struct option long_options[] = {
48 			{"silent", no_argument, NULL, 's'},
49 			{"run", required_argument, NULL, 'r'},
50 			{"verbose", no_argument, NULL, 'v'},
51 			{"help", no_argument, NULL, 'h'},
52 			{NULL, 0, NULL, 0}
53 		};
54 
55 		c = getopt_long (argc, argv, opts, long_options, &index);
56 		if (c == -1)
57 			break;
58 		switch (c) {
59 		case 'r':
60 			if (strcmp(optarg, "trace-cmd") == 0)
61 				tests |= RUN_TRACECMD;
62 			else
63 				print_help(argv);
64 			break;
65 		case 's':
66 			verbose = CU_BRM_SILENT;
67 			break;
68 		case 'v':
69 			show_output = true;
70 			break;
71 		case 'h':
72 		default:
73 			print_help(argv);
74 			break;
75 		}
76 	}
77 
78 	if (tests == RUN_NONE)
79 		tests = RUN_ALL;
80 
81 	if (CU_initialize_registry() != CUE_SUCCESS) {
82 		printf("Test registry cannot be initialized\n");
83 		return -1;
84 	}
85 
86 	if (tests & RUN_TRACECMD)
87 		test_tracecmd_lib();
88 
89 	CU_basic_set_mode(verbose);
90 	CU_basic_run_tests();
91 	CU_cleanup_registry();
92 	return 0;
93 }
94