changeset 305:740377ad25eb

test=path parameter can now also be a file instead of a directory.
author Timo Sirainen <tss@iki.fi>
date Wed, 29 May 2013 03:15:06 +0300
parents 6a7b39d9b832
children ed0f37720946
files src/imaptest.c src/test-exec.c src/test-parser.c src/test-parser.h
diffstat 4 files changed, 55 insertions(+), 33 deletions(-) [+]
line wrap: on
line diff
--- a/src/imaptest.c	Wed May 29 03:01:18 2013 +0300
+++ b/src/imaptest.c	Wed May 29 03:15:06 2013 +0300
@@ -268,14 +268,14 @@
 	print_total();
 }
 
-static void imaptest_run_tests(const char *dir)
+static void imaptest_run_tests(const char *path)
 {
 	struct test_parser *test_parser;
 	const ARRAY_TYPE(test) *tests;
 	struct tests_execute_context *exec_ctx;
 
 	no_new_clients = TRUE;
-	test_parser = test_parser_init(dir);
+	test_parser = test_parser_init(path);
 	tests = test_parser_get_tests(test_parser);
 
 	exec_ctx = tests_execute(tests);
@@ -337,7 +337,7 @@
 {
 	struct timeout *to_stop;
 	struct state *state;
-	const char *key, *value, *testdir = NULL;
+	const char *key, *value, *testpath = NULL;
 	unsigned int i;
 	int ret;
 
@@ -477,7 +477,7 @@
 		}
 		/* test=dir */
 		if (strcmp(key, "test") == 0) {
-			testdir = value;
+			testpath = value;
 			continue;
 		}
 
@@ -515,11 +515,11 @@
 		return 1;
 	}
 	if (conf.mailbox == NULL)
-		conf.mailbox = testdir == NULL ? "INBOX" : "imaptest";
+		conf.mailbox = testpath == NULL ? "INBOX" : "imaptest";
 
 	if (conf.username_template == NULL)
 		i_fatal("Missing username");
-	if (testdir != NULL && strchr(conf.username_template, '%') != NULL) {
+	if (testpath != NULL && strchr(conf.username_template, '%') != NULL) {
 		printf("Don't use %% in username with tests\n");
 		return 1;
 	}
@@ -536,10 +536,10 @@
 	clients_init();
 
 	i_array_init(&clients, CLIENTS_COUNT);
-	if (testdir == NULL)
+	if (testpath == NULL)
 		imaptest_run();
 	else
-		imaptest_run_tests(testdir);
+		imaptest_run_tests(testpath);
 	clients_deinit();
 	mailboxes_deinit();
 
--- a/src/test-exec.c	Wed May 29 03:01:18 2013 +0300
+++ b/src/test-exec.c	Wed May 29 03:15:06 2013 +0300
@@ -1213,7 +1213,7 @@
 
 static void tests_execute_next(struct tests_execute_context *exec_ctx)
 {
-	const struct test *const *tests;
+	struct test *const *tests;
 	unsigned int count;
 
 	tests = array_get(exec_ctx->tests, &count);
--- a/src/test-parser.c	Wed May 29 03:01:18 2013 +0300
+++ b/src/test-parser.c	Wed May 29 03:15:06 2013 +0300
@@ -17,7 +17,7 @@
 
 struct test_parser {
 	pool_t pool;
-	const char *dir, *default_mbox_path;
+	const char *default_mbox_path;
 
 	struct imap_arg *reply_ok, *reply_no, *reply_bad, *reply_any;
 	struct test_command_group *cur_cmd_group;
@@ -708,8 +708,7 @@
 }
 
 static int
-test_parser_read_test(struct test_parser *parser, const char *fname,
-		      const struct test **test_r)
+test_parser_read_test(struct test_parser *parser, const char *path)
 {
 	struct test *test;
 	struct istream *input;
@@ -724,7 +723,7 @@
 	p_array_init(&test->cmd_groups, parser->pool, 32);
 	p_array_init(&test->connections, parser->pool, 4);
 
-	mbox_path = t_strdup_printf("%s/%s.mbox", parser->dir, fname);
+	mbox_path = t_strdup_printf("%s.mbox", path);
 	if (stat(mbox_path, &st) == 0) {
 		/* test-specific mbox */
 		test->mbox_source_path = p_strdup(parser->pool, mbox_path);
@@ -736,13 +735,17 @@
 		test->mbox_source_path = parser->default_mbox_path;
 	}
 
-	test->path = p_strdup_printf(parser->pool, "%s/%s", parser->dir, fname);
-	test->name = test->path + strlen(parser->dir) + 1;
+	test->path = p_strdup(parser->pool, path);
+	test->name = strrchr(test->path, '/');
+	if (test->name != NULL)
+		test->name++;
+	else
+		test->name = test->path;
 	if (stat(test->path, &st) < 0) {
 		i_error("stat(%s) failed: %m", test->path);
 		return -1;
 	}
-	if (!S_ISREG(st.st_mode))
+	if (S_ISDIR(st.st_mode))
 		return 0;
 
 	fd = open(test->path, O_RDONLY);
@@ -764,21 +767,23 @@
 	   OK for logout */
 	test_add_logout(parser, test, 0);
 
-	*test_r = test;
-	return ret < 0 ? -1 : 1;
+	if (ret < 0)
+		return -1;
+
+	array_append(&parser->tests, &test, 1);
+	return 1;
 }
 
-static int test_parser_scan_dir(struct test_parser *parser)
+static int test_parser_scan_dir(struct test_parser *parser, const char *path)
 {
-	const struct test *test;
 	DIR *dir;
 	struct dirent *d;
 	unsigned int len;
 	int ret = 0;
 
-	dir = opendir(parser->dir);
+	dir = opendir(path);
 	if (dir == NULL) {
-		i_error("opendir(%s) failed: %m", parser->dir);
+		i_error("opendir(%s) failed: %m", path);
 		return -1;
 	}
 
@@ -790,15 +795,15 @@
 			continue;
 
 		T_BEGIN {
-			ret = test_parser_read_test(parser, d->d_name, &test);
+			const char *filepath =
+				t_strdup_printf("%s/%s", path, d->d_name);
+			ret = test_parser_read_test(parser, filepath);
 		} T_END;
 		if (ret < 0)
 			break;
-		if (ret > 0)
-			array_append(&parser->tests, &test, 1);
 	}
 	if (closedir(dir) < 0) {
-		i_error("closedir(%s) failed: %m", parser->dir);
+		i_error("closedir(%s) failed: %m", path);
 		return -1;
 	}
 	return ret;
@@ -815,17 +820,17 @@
 	return args;
 }
 
-struct test_parser *test_parser_init(const char *dir)
+struct test_parser *test_parser_init(const char *path)
 {
 	struct test_parser *parser;
 	pool_t pool;
+	struct stat st;
+	const char *dir;
+	int ret;
 
 	pool = pool_alloconly_create("test parser", 1024*256);
 	parser = p_new(pool, struct test_parser, 1);
 	parser->pool = pool;
-	parser->dir = p_strdup(pool, dir);
-	parser->default_mbox_path =
-		p_strdup_printf(pool, "%s/"DEFAULT_MBOX_FNAME, dir);
 	i_array_init(&parser->tests, 128);
 
 	parser->reply_ok = test_parser_reply_init(pool, "ok");
@@ -833,8 +838,25 @@
 	parser->reply_bad = test_parser_reply_init(pool, "bad");
 	parser->reply_any = test_parser_reply_init(pool, "");
 
-	if (test_parser_scan_dir(parser) < 0)
-		i_fatal("Failed to read tests");
+	if (stat(path, &st) < 0)
+		i_fatal("stat(%s) failed: %m", path);
+	if (S_ISDIR(st.st_mode)) {
+		parser->default_mbox_path =
+			p_strdup_printf(pool, "%s/"DEFAULT_MBOX_FNAME, path);
+		if (test_parser_scan_dir(parser, path) < 0)
+			i_fatal("Failed to read tests");
+	} else {
+		dir = t_strcut(path, '/');
+		if (*dir == '\0')
+			parser->default_mbox_path = DEFAULT_MBOX_FNAME;
+		else {
+			parser->default_mbox_path =
+				p_strdup_printf(pool, "%s/"DEFAULT_MBOX_FNAME, dir);
+		}
+		if ((ret = test_parser_read_test(parser, path)) < 0)
+			i_fatal("Failed to read test");
+		i_assert(ret > 0);
+	}
 	return parser;
 }
 
--- a/src/test-parser.h	Wed May 29 03:01:18 2013 +0300
+++ b/src/test-parser.h	Wed May 29 03:15:06 2013 +0300
@@ -75,7 +75,7 @@
 
 	unsigned int require_user2:1;
 };
-ARRAY_DEFINE_TYPE(test, const struct test *);
+ARRAY_DEFINE_TYPE(test, struct test *);
 
 struct test_parser *test_parser_init(const char *dir);
 void test_parser_deinit(struct test_parser **parser);