tests: add helpers to create unique filenames

With such helpers we are able to create unique filenames for a test
program, a fixture or specific test cases.

This help us to avoid accidents related to using files from other
tests or overriding them.

Signed-off-by: Leandro Ribeiro <leandro.ribeiro@collabora.com>
This commit is contained in:
Leandro Ribeiro 2024-02-07 18:11:24 -03:00 committed by Pekka Paalanen
parent ac3e416402
commit d29f904bec
4 changed files with 131 additions and 6 deletions

View File

@ -1184,6 +1184,99 @@ image_filename(const char *basename)
return filename;
}
/** Helper to create filenames for test programs.
*
* \param test_program The test program name.
* \param suffix Arbitrary suffix to append after the test program name.
* Optional, NULL is valid as well.
* \param file_ext The file extension (without '.').
* \return The ICC filename.
*/
char *
output_filename_for_test_program(const char *test_program, const char *suffix,
const char *file_ext)
{
char *filename;
assert(test_program);
assert(file_ext);
if (suffix)
str_printf(&filename, "%s/%s-%s.%s", output_path(), test_program,
suffix, file_ext);
else
str_printf(&filename, "%s/%s.%s", output_path(), test_program,
file_ext);
assert(filename);
return filename;
}
/** Helper to create filenames for fixtures.
*
* \param test_program The test program name.
* \param harness The test harness, from which we get the fixture number.
* \param suffix Arbitrary suffix to append after the fixture number. Optional,
* NULL is valid as well.
* \param file_ext The file extension (without '.').
* \return The ICC filename.
*/
char *
output_filename_for_fixture(const char *test_program,
struct weston_test_harness *harness,
const char *suffix, const char *file_ext)
{
int fixture_number;
char *filename;
assert(test_program);
assert(harness);
assert(file_ext);
fixture_number = get_test_fixture_number_from_harness(harness);
if (suffix)
str_printf(&filename, "%s/%s-f%02d-%s.%s", output_path(), test_program,
fixture_number, suffix, file_ext);
else
str_printf(&filename, "%s/%s-f%02d.%s", output_path(), test_program,
fixture_number, file_ext);
assert(filename);
return filename;
}
/** Helper to create filenames for test cases.
*
* \param suffix Arbitrary suffix to append after the test case name. Optional,
* NULL is valid as well.
* \param seq_number To differentiate filenames created from a loop. Simply use
* 0 if not in a loop.
* \param file_ext The file extension (without '.').
* \return The ICC filename.
*
* This is only usable from code paths inside TEST(), TEST_P(), PLUGIN_TEST()
* etc. defined functions.
*/
char *
output_filename_for_test_case(const char *suffix, uint32_t seq_number,
const char *file_ext)
{
char *filename;
assert(file_ext);
if (suffix)
str_printf(&filename, "%s/%s-%s-%02d.%s", output_path(), get_test_name(),
suffix, seq_number, file_ext);
else
str_printf(&filename, "%s/%s-%02d.%s", output_path(), get_test_name(),
seq_number, file_ext);
assert(filename);
return filename;
}
/** Open a writable file
*
* \param suffix Custom file name suffix.

View File

@ -258,6 +258,19 @@ screenshot_reference_filename(const char *basename, uint32_t seq);
char *
image_filename(const char *basename);
char *
output_filename_for_test_program(const char *test_program, const char *suffix,
const char *file_ext);
char *
output_filename_for_fixture(const char *test_program,
struct weston_test_harness *harness,
const char *suffix, const char *file_ext);
char *
output_filename_for_test_case(const char *suffix, uint32_t seq_number,
const char *file_ext);
FILE *
fopen_dump_file(const char *suffix);

View File

@ -212,6 +212,22 @@ struct weston_test_harness {
struct wet_testsuite_data data;
};
/** Get the current fixture number from test harness
*
* \param harness The test harness.
*
* Similar to get_test_fixture_index(), but get the fixture number (index + 1)
* directly from the test harness. Can be called from fixture_setup() functions.
*
* \sa DECLARE_FIXTURE_SETUP(), DECLARE_FIXTURE_SETUP_WITH_ARG()
* \ingroup testharness
*/
int
get_test_fixture_number_from_harness(struct weston_test_harness *harness)
{
return harness->data.fixture_iteration + 1;
}
typedef void (*weston_test_cb)(struct wet_testsuite_data *suite_data,
const struct weston_test_entry *t,
const void *test_data,

View File

@ -41,6 +41,12 @@
#error "Tests must not be built with NDEBUG defined, they rely on assert()."
#endif
/** Test harness context
*
* \ingroup testharness
*/
struct weston_test_harness;
/** Test program entry
*
* Each invocation of TEST(), TEST_P(), or PLUGIN_TEST() will create one
@ -163,6 +169,9 @@ get_test_name(void);
int
get_test_fixture_index(void);
int
get_test_fixture_number_from_harness(struct weston_test_harness *harness);
/** Metadata for fixture setup array elements
*
* Every type used as a fixture setup array's elements needs one member of
@ -194,12 +203,6 @@ struct fixture_setup_array {
const struct fixture_setup_array *
fixture_setup_array_get_(void);
/** Test harness context
*
* \ingroup testharness
*/
struct weston_test_harness;
enum test_result_code
fixture_setup_run_(struct weston_test_harness *harness, const void *arg_);