From d29f904bec2858729a72654385d8377cc684359e Mon Sep 17 00:00:00 2001 From: Leandro Ribeiro Date: Wed, 7 Feb 2024 18:11:24 -0300 Subject: [PATCH] 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 --- tests/weston-test-client-helper.c | 93 +++++++++++++++++++++++++++++++ tests/weston-test-client-helper.h | 13 +++++ tests/weston-test-runner.c | 16 ++++++ tests/weston-test-runner.h | 15 +++-- 4 files changed, 131 insertions(+), 6 deletions(-) diff --git a/tests/weston-test-client-helper.c b/tests/weston-test-client-helper.c index 654e5088..3b279300 100644 --- a/tests/weston-test-client-helper.c +++ b/tests/weston-test-client-helper.c @@ -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. diff --git a/tests/weston-test-client-helper.h b/tests/weston-test-client-helper.h index 184f6a1b..0ed97459 100644 --- a/tests/weston-test-client-helper.h +++ b/tests/weston-test-client-helper.h @@ -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); diff --git a/tests/weston-test-runner.c b/tests/weston-test-runner.c index d1fb9a90..8b5085bc 100644 --- a/tests/weston-test-runner.c +++ b/tests/weston-test-runner.c @@ -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, diff --git a/tests/weston-test-runner.h b/tests/weston-test-runner.h index bd9c10a7..a9634834 100644 --- a/tests/weston-test-runner.h +++ b/tests/weston-test-runner.h @@ -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_);