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

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Mon Dec 22 19:20:34 CST 2008


Author: mnicholson
Date: Mon Dec 22 19:20:34 2008
New Revision: 166505

URL: http://svn.digium.com/view/asterisk?view=rev&rev=166505
Log:
 * asttest/asttest.c: implemented test directory scanning and basic test
 loading
 * asttest/Makefile: added lua include dir
 * asttest/tests: added, will contain tests

Added:
    team/mnicholson/asttest/asttest/tests/
Modified:
    team/mnicholson/asttest/asttest/Makefile
    team/mnicholson/asttest/asttest/asttest.c

Modified: team/mnicholson/asttest/asttest/Makefile
URL: http://svn.digium.com/view/asterisk/team/mnicholson/asttest/asttest/Makefile?view=diff&rev=166505&r1=166504&r2=166505
==============================================================================
--- team/mnicholson/asttest/asttest/Makefile (original)
+++ team/mnicholson/asttest/asttest/Makefile Mon Dec 22 19:20:34 2008
@@ -22,7 +22,7 @@
 
 # Basic set of sources and flags/libraries/includes
 OBJS:=asttest.o
-CFLAGS:=-g -c -D_GNU_SOURCE -Wall
+CFLAGS:=-g -c -D_GNU_SOURCE -Wall -I/usr/include/lua5.1
 T_LIBS:=-llua5.1
 
 AST_INSTALL_DIR = $(PWD)/asterisk

Modified: team/mnicholson/asttest/asttest/asttest.c
URL: http://svn.digium.com/view/asterisk/team/mnicholson/asttest/asttest/asttest.c?view=diff&rev=166505&r1=166504&r2=166505
==============================================================================
--- team/mnicholson/asttest/asttest/asttest.c (original)
+++ team/mnicholson/asttest/asttest/asttest.c Mon Dec 22 19:20:34 2008
@@ -16,9 +16,144 @@
  * at the top of the source tree.
  */
 
+#include <errno.h>
 #include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
 
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <dirent.h>
+#include <libgen.h>
 
-int main(int argv, char *argc[]) {
+#include <lua.h>
+#include <lauxlib.h>
+#include <lualib.h>
+
+struct test_suite {
+	unsigned int pass;
+	unsigned int fail;
+	unsigned int xfail;
+	unsigned int skip;
+	unsigned int total;
+};
+
+void test_suite_init(struct test_suite *ts) {
+	memset(ts, 0, sizeof(struct test_suite));
+}
+
+void test_suite_print_results(struct test_suite *ts) {
+	printf("Test results:\n");
+	printf("  tests passed:      %d\n", ts->pass);
+	printf("  test failures:     %d\n", ts->fail);
+	printf("  expected failures: %d\n", ts->xfail);
+	printf("  tests skipped:     %d\n", ts->skip);
+	printf("\n");
+	printf("Total test run:      %d\n", ts->total);
+}
+
+void test_suite_pass(struct test_suite *ts) {
+	ts->pass++;
+	ts->total++;
+}
+
+void test_suite_fail(struct test_suite *ts) {
+	ts->fail++;
+	ts->total++;
+}
+
+void test_suite_xfail(struct test_suite *ts) {
+	ts->xfail++;
+	ts->total++;
+}
+
+void test_suite_skip(struct test_suite *ts) {
+	ts->skip++;
+	ts->total++;
+}
+
+int run_test(struct test_suite *ts, const char *test_dir_path) {
+	char test_file_path[1024];
+	lua_State *L = luaL_newstate();
+	if (!L) {
+		fprintf(stderr, "Error creating lua_State\n");
+		return 1;
+	}
+
+	luaL_openlibs(L);
+
+	// TODO load the standard test libraries
+
+	snprintf(test_file_path, sizeof(test_file_path), "%s/test.lua", test_file_path);
+	if (luaL_dofile(L, test_file_path)) {
+		// TODO print error message
+		test_suite_skip(ts);
+		lua_close(L);
+		return 1;
+	}
+
+	lua_close(L);
 	return 0;
 }
+
+int is_directory(const char *dir) {
+	struct stat st;
+	if (lstat(dir, &st)) {
+		return 0;
+	}
+
+	return S_ISDIR(st.st_mode);
+}
+
+int ignored_dir(const char *dir) {
+	char *dir_dup = strdup(dir);  /* dup the string as basename may modify it */
+	char *base_dir = basename(dir_dup);
+
+	if (base_dir[0] == '.') {
+		free(dir_dup);
+		return 1;
+	}
+	free(dir_dup);
+	return 0;
+}
+
+int process_tests_in_dir(const char *path) {
+	DIR *main_dir = opendir(path);
+	char full_path[1024];
+	struct test_suite ts;
+	struct dirent *ent;
+
+	if (!main_dir) {
+		fprintf(stderr, "Error opening path '%s': %s\n", path, strerror(errno));
+		return 1;
+	}
+
+	test_suite_init(&ts);
+
+	while ((ent = readdir(main_dir))) {
+		snprintf(full_path, sizeof(full_path), "%s/%s", path, ent->d_name);
+		if (is_directory(full_path) && !ignored_dir(full_path)) {
+			run_test(&ts, full_path);
+		}
+	}
+	closedir(main_dir);
+
+	test_suite_print_results(&ts);
+	return 0;
+}
+
+void usage(const char *prog_name) {
+	fprintf(stderr, "Usage:\n");
+	fprintf(stderr, "  %s <test_dir>\n", prog_name);
+}
+
+int main(int argc, char *argv[]) {
+	if (argc != 2) {
+		fprintf(stderr, "Error: invalid arguments\n");
+		usage(argv[0]);
+		return 1;
+	}
+
+	return process_tests_in_dir(argv[1]);
+}




More information about the asterisk-commits mailing list