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.
This commit is contained in:
Peter Hutterer 2021-06-08 13:13:09 +10:00
parent dd3f14d9d6
commit dc5751b569
2 changed files with 27 additions and 0 deletions

View file

@ -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, ...)
{

View file

@ -33,6 +33,7 @@
extern "C" {
#endif
#include <limits.h>
#include <stddef.h>
#include <stdbool.h>
#include <math.h>
@ -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]);
/**
* \}
*/