[asterisk-commits] mnicholson: branch mnicholson/asttest r167266 - in /team/mnicholson/asttest/a...

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Tue Jan 6 15:03:12 CST 2009


Author: mnicholson
Date: Tue Jan  6 15:03:11 2009
New Revision: 167266

URL: http://svn.digium.com/view/asterisk?view=rev&rev=167266
Log:
Added command line parsing and added a few command line options.

 * asttest/include/asttest/asttest.h: added
 * asttest/include/asttest/testsuite.h, asttest/lib/testsuite.c (ts_init):
 updated for command line option support
 * asttest/asttest.c (print_test_name): tweaked output
 * asttest/asttest.c (process_test_dir): tweaked output and added support for
 command line options
 * asttest/asttest.c (usage): updated
 * asttest/asttest.c (main): do command line parsing and support multiple test
 directories
 * asttest/asttest.c (parse_cmdline): added
 * asttest/Makefile: updated


Modified:
    team/mnicholson/asttest/asttest/Makefile
    team/mnicholson/asttest/asttest/asttest.c
    team/mnicholson/asttest/asttest/include/asttest/testsuite.h
    team/mnicholson/asttest/asttest/lib/testsuite.c

Modified: team/mnicholson/asttest/asttest/Makefile
URL: http://svn.digium.com/view/asterisk/team/mnicholson/asttest/asttest/Makefile?view=diff&rev=167266&r1=167265&r2=167266
==============================================================================
--- team/mnicholson/asttest/asttest/Makefile (original)
+++ team/mnicholson/asttest/asttest/Makefile Tue Jan  6 15:03:11 2009
@@ -64,9 +64,9 @@
 	$(MAKE) -C ../ install
 
 lib/lua.o: lib/lua.c include/asttest/lua.h include/asttest/testsuite.h lib/asttest_lua.h
-lib/testsuite.o: lib/testsuite.c include/asttest/testsuite.h
+lib/testsuite.o: lib/testsuite.c include/asttest/testsuite.h include/asttest/asttest.h
 
-asttest: $(OBJS) $(T_OBJS)
+asttest: $(OBJS) $(T_OBJS) include/asttest/asttest.h
 	$(CC) -o $@ $^ $(T_LIBS)
 
 lib/asttest_lua.h: lib/asttest.lua tools/mkstring

Modified: team/mnicholson/asttest/asttest/asttest.c
URL: http://svn.digium.com/view/asterisk/team/mnicholson/asttest/asttest/asttest.c?view=diff&rev=167266&r1=167265&r2=167266
==============================================================================
--- team/mnicholson/asttest/asttest/asttest.c (original)
+++ team/mnicholson/asttest/asttest/asttest.c Tue Jan  6 15:03:11 2009
@@ -31,8 +31,11 @@
 #include <lauxlib.h>
 #include <lualib.h>
 
+#include "asttest/asttest.h"
 #include "asttest/lua.h"
 #include "asttest/testsuite.h"
+
+const char *default_log_filename = "asttest.log";
 
 
 /*
@@ -150,7 +153,7 @@
 	}
 
 	/* now print the test name */
-	len = printf(" %s: ", test_name);
+	len = printf(" %s ", test_name);
 
 	/* now pad the test name */
 	for (i = 31 - len; i > 0; i--) {
@@ -223,7 +226,7 @@
 	return res;
 }
 
-int process_test_dir(const char *path) {
+int process_test_dir(const char *path, struct asttest_opts *opts) {
 	DIR *main_dir = opendir(path);
 	char full_path[1024];
 	struct testsuite ts;
@@ -231,12 +234,14 @@
 	enum ts_result result;
 	int res = 0;
 
+	printf("Processing tests in '%s':\n", path);
+
 	if (!main_dir) {
 		fprintf(stderr, "Error opening path '%s': %s\n", path, strerror(errno));
 		return 1;
 	}
 
-	ts_init(&ts, "asttest.log");
+	ts_init(&ts, path, opts);
 
 	while ((ent = readdir(main_dir))) {
 		snprintf(full_path, sizeof(full_path), "%s/%s", path, ent->d_name);
@@ -255,27 +260,102 @@
 	if (ts.fail)
 		res = 1;
 
-	/* if any tests were skipped print a warning */
-	if (ts.skip)
+	if (opts->warn_on_skip && ts.skip) {
 		printf("\n***WARNING: some tests were skipped, see log for details\n");
+	} else if (ts.skip) {
+		/* signal a failure */
+		res = 1;
+	}
 
 	ts_cleanup(&ts);
 	return res;
 }
 
 void usage(const char *prog_name) {
-	fprintf(stderr, "Usage:\n");
-	fprintf(stderr, "  %s <test_dir>\n", prog_name);
+	fprintf(stderr,
+		"Usage:\n"
+		"  %s [-wh] [-l <filename>] <test_dir> [test_dir...]\n"
+		"\n"
+		"Options:\n"
+		"  -l <filename>  Specify the name of the log file.  One log file will be\n"
+		"                 created for each test directory in that test directory\n"
+		"                 using the given name.  The default filename is\n"
+		"                 asttest.log.\n"
+		"  -w             warn if tests were skipped.  This option will cause a\n"
+		"                 warning to print instead of an error being generated if\n"
+		"                 any tests are skipped.\n" 
+		"  -h             print this help message\n"
+		"\n"
+		, prog_name);
+}
+
+/*
+ * \brief Parse command line options.
+ * @param argc the argument count
+ * @param argv an array of strings
+ * @param opts the struct where our options will be stored
+ * 
+ * @note If this function returns 0 the remaining options should be test 
+ * directories.
+ *
+ * @retval 1 -h option
+ * @retval -1 error
+ * @return 0 success
+ */
+int parse_cmdline(int argc, char *argv[], struct asttest_opts *opts) {
+	char c;
+	memset(opts, 0, sizeof(struct asttest_opts));
+
+	/* set some default options */
+	opts->warn_on_skip = 0;
+	opts->log_filename = default_log_filename;
+
+	/* parse options */	
+	while ((c = getopt(argc, argv, "l:wh")) != -1) {
+		switch (c) {
+		case 'l':
+			opts->log_filename = optarg;
+			break;
+		case 'w':
+			opts->warn_on_skip = 1;
+			break;
+		case 'h':
+			return 1;
+		case '?':
+			return -1;
+			break;
+		}
+	}
+
+	return 0;
 }
 
 int main(int argc, char *argv[]) {
-	if (argc != 2) {
-		fprintf(stderr, "Error: invalid arguments\n");
+	int res = 0;
+	int i;
+	struct asttest_opts opts;
+
+	if ((res = parse_cmdline(argc, argv, &opts))) {
+		usage(argv[0]);
+
+		if (res == 1)
+			return 0;
+		else
+			return 1;
+	}
+
+	if (optind == argc) {
+		fprintf(stderr, "%s: missing arguments -- specify at least one test directory\n", argv[0]);
 		usage(argv[0]);
 		return 1;
 	}
 
-	// TODO process args and except --log (-l) as an argument for where to log
-
-	return process_test_dir(argv[1]);
-}
+	for (i = optind; i < argc; i++) {
+		if (process_test_dir(argv[i], &opts)) {
+			fprintf(stderr, "test suite failed, exiting...\n");
+			return 1;
+		}
+	}
+
+	return 0;
+}

Modified: team/mnicholson/asttest/asttest/include/asttest/testsuite.h
URL: http://svn.digium.com/view/asterisk/team/mnicholson/asttest/asttest/include/asttest/testsuite.h?view=diff&rev=167266&r1=167265&r2=167266
==============================================================================
--- team/mnicholson/asttest/asttest/include/asttest/testsuite.h (original)
+++ team/mnicholson/asttest/asttest/include/asttest/testsuite.h Tue Jan  6 15:03:11 2009
@@ -19,6 +19,8 @@
 #ifndef ASTTEST_TESTSUITE_H
 #define ASTTEST_TESTSUITE_H
 
+#include "asttest/asttest.h"
+
 struct testsuite {
 	unsigned int pass;
 	unsigned int fail;
@@ -35,7 +37,7 @@
 	TS_SKIP,
 };
 
-int ts_init(struct testsuite *ts, const char *log_file_path);
+int ts_init(struct testsuite *ts, const char *path, struct asttest_opts *opts);
 void ts_cleanup(struct testsuite *ts);
 
 void ts_print(struct testsuite *ts);

Modified: team/mnicholson/asttest/asttest/lib/testsuite.c
URL: http://svn.digium.com/view/asterisk/team/mnicholson/asttest/asttest/lib/testsuite.c?view=diff&rev=167266&r1=167265&r2=167266
==============================================================================
--- team/mnicholson/asttest/asttest/lib/testsuite.c (original)
+++ team/mnicholson/asttest/asttest/lib/testsuite.c Tue Jan  6 15:03:11 2009
@@ -22,14 +22,19 @@
 #include <string.h>
 #include <stdarg.h>
 
+#include "asttest/asttest.h"
 #include "asttest/testsuite.h"
 
-int ts_init(struct testsuite *ts, const char *log_file_path) {
+int ts_init(struct testsuite *ts, const char *path, struct asttest_opts *opts) {
+	char log_path[1024];
+	
 	memset(ts, 0, sizeof(struct testsuite));
+	
+	snprintf(log_path, sizeof(log_path), "%s/%s", path, opts->log_filename);
 
-	ts->log = fopen(log_file_path, "w");
+	ts->log = fopen(log_path, "w");
 	if (!ts->log) {
-		fprintf(stderr, "Error log file (%s): %s\n", log_file_path, strerror(errno));
+		fprintf(stderr, "Error opening log file for writing (%s): %s\n", log_path, strerror(errno));
 		return 1;
 	}
 




More information about the asterisk-commits mailing list