Browse Source

testc: setenv TESTC_TMPDIR for each test

Serj Kalichev 4 years ago
parent
commit
3f4630c0e2
5 changed files with 100 additions and 27 deletions
  1. 24 16
      faux/ini/testc_ini.c
  2. 2 0
      faux/testc_helpers.h
  3. 14 2
      faux/testc_helpers/testc_helpers.c
  4. 1 1
      faux/testc_module/testc_module.c
  5. 59 8
      testc/testc.c

+ 24 - 16
faux/ini/testc_ini.c

@@ -1,6 +1,7 @@
 #include <stdlib.h>
 #include <stdio.h>
 
+#include "faux/str.h"
 #include "faux/ini.h"
 #include "faux/testc_helpers.h"
 
@@ -8,7 +9,7 @@ int testc_faux_ini_good(void) {
 
 	char *path = NULL;
 
-	path = getenv("FAUX_INI_PATH");
+	path = getenv("TESTC_TMPDIR");
 	if (path)
 		printf("Env var is [%s]\n", path);
 	return 0;
@@ -67,26 +68,26 @@ int testc_faux_ini_parse(void) {
 		"\"test space\"=\"lk lk lk \"\n"
 	;
 
+	int ret = -1; // Pessimistic return value
 	faux_ini_t *ini = NULL;
 	faux_ini_node_t *iter = NULL;
 	const faux_pair_t *pair = NULL;
-	const char *src_fn = NULL;
-	const char *dst_fn = "/tmp/dst12";
-	const char *etalon_fn = NULL;
+	char *src_fn = NULL;
+	char *dst_fn = NULL;
+	char *etalon_fn = NULL;
 	unsigned num_entries = 0;
 
 	// Prepare files
 	src_fn = faux_testc_tmpfile_deploy(src_file);
-	if (!src_fn) {
-		fprintf(stderr, "Can't create test file %s\n", src_fn);
-	}
 	etalon_fn = faux_testc_tmpfile_deploy(etalon_file);
-	if (!src_fn) {
-		fprintf(stderr, "Can't create etalon file %s\n", etalon_fn);
-	}
+	dst_fn = faux_str_sprintf("%s/dst", getenv(FAUX_TESTC_TMPDIR_ENV));
 
 	ini = faux_ini_new();
-	faux_ini_parse_file(ini, src_fn);
+	if (faux_ini_parse_file(ini, src_fn) < 0) {
+		fprintf(stderr, "Can't parse INI file %s\n", src_fn);
+		goto parse_error;
+	}
+
 	iter = faux_ini_iter(ini);
 	while ((pair = faux_ini_each(&iter))) {
 		num_entries++;
@@ -94,15 +95,22 @@ int testc_faux_ini_parse(void) {
 	}
 	if (10 != num_entries) {
 		fprintf(stderr, "Wrong number of entries %u\n", num_entries);
-		faux_ini_free(ini);
-		return -1;
+		goto parse_error;
 	}
 
 	faux_ini_set(ini, "test space", "lk lk lk ");
-	faux_ini_write_file(ini, dst_fn);
+	if (faux_ini_write_file(ini, dst_fn) < 0) {
+		fprintf(stderr, "Can't write INI file %s\n", dst_fn);
+		goto parse_error;
+	}
 
-	faux_ini_free(ini);
+	ret = 0; // success
 
-	return 0;
+parse_error:
+	faux_ini_free(ini);
+	faux_str_free(dst_fn);
+	faux_str_free(src_fn);
+	faux_str_free(etalon_fn);
 
+	return ret;
 }

+ 2 - 0
faux/testc_helpers.h

@@ -9,6 +9,8 @@
 
 #include "faux/faux.h"
 
+#define FAUX_TESTC_TMPDIR_ENV "TESTC_TMPDIR"
+
 C_DECL_BEGIN
 
 ssize_t faux_testc_file_deploy(const char *fn, const char *str);

+ 14 - 2
faux/testc_helpers/testc_helpers.c

@@ -14,6 +14,7 @@
 #include "faux/ctype.h"
 #include "faux/str.h"
 #include "faux/file.h"
+#include "faux/testc_helpers.h"
 
 
 ssize_t faux_testc_file_deploy(const char *fn, const char *str) {
@@ -37,17 +38,28 @@ ssize_t faux_testc_file_deploy(const char *fn, const char *str) {
 	return bytes_written;
 }
 
+
 char *faux_testc_tmpfile_deploy(const char *str) {
 
-	char template[] = "/tmp/testc_tmpfile_XXXXXX";
+	char *template = NULL;
 	int fd = -1;
 	faux_file_t *f = NULL;
 	ssize_t bytes_written = 0;
+	char *env_tmpdir = NULL;
 
 	assert(str);
 	if (!str)
 		return NULL;
 
+	env_tmpdir = getenv(FAUX_TESTC_TMPDIR_ENV);
+	if (env_tmpdir)
+		template = faux_str_sprintf("%s/tmpfile-XXXXXX", env_tmpdir);
+	else
+		template = faux_str_sprintf("/tmp/testc-tmpfile-XXXXXX");
+	assert(template);
+	if (!template)
+		return NULL;
+
 	fd = mkstemp(template);
 	if (fd < 0)
 		return NULL;
@@ -61,5 +73,5 @@ char *faux_testc_tmpfile_deploy(const char *str) {
 	if (bytes_written < 0)
 		return NULL;
 
-	return faux_str_dup(template);
+	return template;
 }

+ 1 - 1
faux/testc_module/testc_module.c

@@ -4,9 +4,9 @@ const unsigned char testc_version_major = 1;
 const unsigned char testc_version_minor = 0;
 
 const char *testc_module[][2] = {
-	{"testc_faux_ini_good", "INI subsystem good"},
 	{"testc_faux_ini_bad", "INI bad"},
 	{"testc_faux_ini_signal", "Interrupted by signal"},
+	{"testc_faux_ini_good", "INI subsystem good"},
 	{"testc_faux_ini_parse", "Complex test of INI file parsing"},
 	{NULL, NULL}
 	};

+ 59 - 8
testc/testc.c

@@ -13,6 +13,8 @@
 #include <sys/wait.h>
 #include <sys/uio.h>
 #include <errno.h>
+#include <sys/stat.h>
+#include <errno.h>
 
 #if WITH_INTERNAL_GETOPT
 #include "libc/getopt.h"
@@ -32,6 +34,7 @@
 #include "faux/faux.h"
 #include "faux/str.h"
 #include "faux/list.h"
+#include "faux/testc_helpers.h"
 
 #ifndef VERSION
 #define VERSION 1.0.0
@@ -51,7 +54,8 @@
 
 // Command line options */
 struct opts_s {
-	int debug;
+	bool_t debug;
+	bool_t preserve_tmp;
 	faux_list_t *so_list;
 };
 
@@ -98,6 +102,7 @@ int main(int argc, char *argv[]) {
 	while ((so = faux_list_each(&iter))) {
 
 		void *so_handle = NULL;
+		char testc_tmpdir[] = "/tmp/testc-XXXXXX";
 
 		// Module symbols
 		unsigned char testc_version_major = TESTC_VERSION_MAJOR_DEFAULT;
@@ -169,6 +174,18 @@ int main(int argc, char *argv[]) {
 		printf("Processing module \"%s\" v%u.%u ...\n", so,
 			testc_version_major, testc_version_minor);
 
+		// Create tmpdir for current shared object
+		if (!mkdtemp(testc_tmpdir)) {
+			fprintf(stderr, "Warning: "
+				"Can't create tmp dir for module \"%s\"... "
+				"Ignored\n", so);
+		}
+		if (opts->preserve_tmp) {
+			fprintf(stderr, "Warning: "
+				"Temp dir \"%s\" will be preserved\n",
+				testc_tmpdir);
+		}
+
 		// Iterate through testing functions list
 		while ((*testc_module)[0]) {
 
@@ -178,8 +195,8 @@ int main(int argc, char *argv[]) {
 			int wstatus = 0; // Test's retval
 			char *result_str = NULL;
 			char *attention_str = NULL;
-
 			faux_list_t *buf_list = NULL;
+			char *tmpdir = NULL; // tmp dir for current test
 
 			// Get name and description of testing function
 			test_name = (*testc_module)[0];
@@ -200,6 +217,21 @@ int main(int argc, char *argv[]) {
 				continue;
 			}
 
+			// Create tmp dir for current test and set TESTC_TMPDIR
+			tmpdir = faux_str_sprintf("%s/test%03u",
+				testc_tmpdir, module_tests);
+			if (tmpdir) {
+				if (mkdir(tmpdir, 0755) < 0) {
+					fprintf(stderr, "Warning: "
+						"Can't create temp dir \"%s\": %s\n",
+						tmpdir, strerror(errno));
+				}
+				setenv(FAUX_TESTC_TMPDIR_ENV, tmpdir, 1);
+			} else {
+				fprintf(stderr, "Warning: "
+					"Can't generate name for temp dir\n");
+			}
+
 			// Execute testing function
 			wstatus = exec_test(test_sym, &buf_list);
 
@@ -247,19 +279,32 @@ int main(int argc, char *argv[]) {
 			if (!WIFEXITED(wstatus) ||
 				WEXITSTATUS(wstatus) != 0 ||
 				opts->debug) {
+				if (opts->preserve_tmp) {
+					fprintf(stderr, "Info: "
+						"Test's temp dir is \"%s\"\n",
+						tmpdir);
+				}
 				if (faux_list_len(buf_list) > 0)
 					printf("~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ \n");
 				print_test_output(buf_list);
 				if (faux_list_len(buf_list) > 0)
 					printf("~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ \n");
 			}
-
 			faux_list_free(buf_list);
+
+			// Remove test's tmp dir
+			if (!opts->preserve_tmp)
+				faux_rm(tmpdir);
+			faux_str_free(tmpdir);
 		}
 
 		dlclose(so_handle);
 		so_handle = NULL;
 
+		// Remove module's tmp dir
+		if (!opts->preserve_tmp)
+			faux_rm(testc_tmpdir);
+
 		// Report module statistics
 		printf("Module tests: %u\n", module_tests);
 		printf("Module broken tests: %u\n", module_broken_tests);
@@ -438,6 +483,7 @@ static opts_t *opts_new(void) {
 		return NULL;
 
 	opts->debug = BOOL_FALSE;
+	opts->preserve_tmp = BOOL_FALSE;
 
 	// Members of list are static strings from argv so don't free() it
 	opts->so_list = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_UNIQUE,
@@ -465,13 +511,14 @@ static opts_t *opts_parse(int argc, char *argv[]) {
 
 	opts_t *opts = NULL;
 
-	static const char *shortopts = "hvd";
+	static const char *shortopts = "hvdt";
 #ifdef HAVE_GETOPT_LONG
 	static const struct option longopts[] = {
-		{"help",	0, NULL, 'h'},
-		{"version",	0, NULL, 'v'},
-		{"debug",	0, NULL, 'd'},
-		{NULL,		0, NULL, 0}
+		{"help",		0, NULL, 'h'},
+		{"version",		0, NULL, 'v'},
+		{"debug",		0, NULL, 'd'},
+		{"preserve-tmp",	0, NULL, 't'},
+		{NULL,			0, NULL, 0}
 	};
 #endif
 
@@ -493,6 +540,9 @@ static opts_t *opts_parse(int argc, char *argv[]) {
 		case 'd':
 			opts->debug = BOOL_TRUE;
 			break;
+		case 't':
+			opts->preserve_tmp = BOOL_TRUE;
+			break;
 		case 'h':
 			help(0, argv[0]);
 			exit(0);
@@ -552,5 +602,6 @@ static void help(int status, const char *argv0) {
 		printf("\t-v, --version\tPrint version.\n");
 		printf("\t-h, --help\tPrint this help.\n");
 		printf("\t-d, --debug\tDebug mode. Show output for all tests.\n");
+		printf("\t-t, --preserve-tmp\tPreserve test's tmp files.\n");
 	}
 }