Browse Source

faux.testc_helpers: Function faux_testc_tmpfile_deploy()

Serj Kalichev 4 years ago
parent
commit
785309121f
3 changed files with 36 additions and 6 deletions
  1. 8 6
      faux/ini/testc_ini.c
  2. 1 0
      faux/testc_helpers.h
  3. 27 0
      faux/testc_helpers/testc_helpers.c

+ 8 - 6
faux/ini/testc_ini.c

@@ -70,18 +70,20 @@ int testc_faux_ini_parse(void) {
 	faux_ini_t *ini = NULL;
 	faux_ini_node_t *iter = NULL;
 	const faux_pair_t *pair = NULL;
-	const char *src_fn = "/tmp/src12";
+	const char *src_fn = NULL;
 	const char *dst_fn = "/tmp/dst12";
-	const char *etalon_fn = "/tmp/etalon12";
+	const char *etalon_fn = NULL;
 	unsigned num_entries = 0;
-	ssize_t r = 0;
 
 	// Prepare files
-	r = faux_testc_file_deploy(src_fn, src_file);
-	if (r < 0) {
+	src_fn = faux_testc_tmpfile_deploy(src_file);
+	if (!src_fn) {
 		fprintf(stderr, "Can't create test file %s\n", src_fn);
 	}
-	faux_testc_file_deploy(etalon_fn, etalon_file);
+	etalon_fn = faux_testc_tmpfile_deploy(etalon_file);
+	if (!src_fn) {
+		fprintf(stderr, "Can't create etalon file %s\n", etalon_fn);
+	}
 
 	ini = faux_ini_new();
 	faux_ini_parse_file(ini, src_fn);

+ 1 - 0
faux/testc_helpers.h

@@ -12,6 +12,7 @@
 C_DECL_BEGIN
 
 ssize_t faux_testc_file_deploy(const char *fn, const char *str);
+char *faux_testc_tmpfile_deploy(const char *str);
 
 C_DECL_END
 

+ 27 - 0
faux/testc_helpers/testc_helpers.c

@@ -36,3 +36,30 @@ 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";
+	int fd = -1;
+	faux_file_t *f = NULL;
+	ssize_t bytes_written = 0;
+
+	assert(str);
+	if (!str)
+		return NULL;
+
+	fd = mkstemp(template);
+	if (fd < 0)
+		return NULL;
+
+	f = faux_file_fdopen(fd);
+	if (!f)
+		return NULL;
+
+	bytes_written = faux_file_write_block(f, str, strlen(str));
+	faux_file_close(f);
+	if (bytes_written < 0)
+		return NULL;
+
+	return faux_str_dup(template);
+}