From dc5751b569f9eecb6db872fafca1e3ea3ed39e84 Mon Sep 17 00:00:00 2001 From: Peter Hutterer Date: Tue, 8 Jun 2021 13:13:09 +1000 Subject: [PATCH] test: add a helper function for making tempfiles Having a helper aids with the file being in the right directory and cleaned up automatically on exit. Plus, failing the test with the sytem error status code signals that it's not the actual test failing here. --- test/pwtest.c | 15 +++++++++++++++ test/pwtest.h | 12 ++++++++++++ 2 files changed, 27 insertions(+) diff --git a/test/pwtest.c b/test/pwtest.c index 066774560..d9da36091 100644 --- a/test/pwtest.c +++ b/test/pwtest.c @@ -381,6 +381,21 @@ pwtest_spa_plugin_load_interface(struct pwtest_spa_plugin *plugin, return iface; } +void +pwtest_mkstemp(char path[PATH_MAX]) +{ + const char *tmpdir = getenv("TMPDIR"); + int r; + + if (tmpdir == NULL) + pwtest_error_with_msg("tmpdir is unset"); + + spa_scnprintf(path, PATH_MAX, "%s/%s", tmpdir, "tmp.XXXXXX"); + r = mkstemp(path); + if (r == -1) + pwtest_error_with_msg("Unable to create temporary file: %s", strerror(errno)); +} + void _pwtest_add(struct pwtest_context *ctx, struct pwtest_suite *suite, const char *funcname, const void *func, ...) { diff --git a/test/pwtest.h b/test/pwtest.h index 768482df8..01817bdc0 100644 --- a/test/pwtest.h +++ b/test/pwtest.h @@ -33,6 +33,7 @@ extern "C" { #endif +#include #include #include #include @@ -535,6 +536,17 @@ pwtest_spa_plugin_try_load_interface(struct pwtest_spa_plugin *plugin, const struct spa_dict *info); + +/** + * Create a temporary file and copy its full path to \a path. Fails the test + * with \ref PWTEST_SYSTEM_ERROR on error. + * + * This file does not need to be removed by the test, the pwtest framework + * will take care of it on exit. + */ +void pwtest_mkstemp(char path[PATH_MAX]); + + /** * \} */