From 91294611afe5d80197aed777d26dabd8df70fda7 Mon Sep 17 00:00:00 2001 From: "leonard.kosta" Date: Fri, 12 Jan 2024 09:33:58 -0500 Subject: [PATCH] Added output directory Signed-off-by: leonard.kosta --- CMakeLists.txt | 2 ++ tests/file_paths.c | 19 ++++++++++++++++++- tests/file_paths.h | 14 ++++++++++++++ tests/main.c | 10 +++++++--- 4 files changed, 41 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e7c3a2c..65232a4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -27,6 +27,8 @@ target_link_libraries(main endian) include_directories(${CMOCKA_INCLUDE_DIR}) find_package(cmocka REQUIRED) add_executable(tests tests/main.c) +add_library(test_file_paths tests/file_paths.c) +target_link_libraries(tests test_file_paths) add_library(test_linked_list tests/test_linked_list.c) target_link_libraries(test_linked_list linked_list) target_link_libraries(tests test_linked_list) diff --git a/tests/file_paths.c b/tests/file_paths.c index 874d516..2c071be 100644 --- a/tests/file_paths.c +++ b/tests/file_paths.c @@ -49,7 +49,8 @@ int get_fixture_directory(char *dirname) { if (0 != return_code) { goto end; } - return_code = snprintf( + printf("Executable directory: %s\n", executable_directory); //TODO remove + snprintf( dirname, MAX_PATH, "%s/../tests/fixtures/", @@ -57,3 +58,19 @@ int get_fixture_directory(char *dirname) { end: return return_code; } + +int get_output_directory(char *dirname) { + // TODO MAX_PATH is only on Windows? + char executable_directory[MAX_PATH] = {0}; + int return_code = get_executable_directory(executable_directory); + if (0 != return_code) { + goto end; + } + snprintf( + dirname, + MAX_PATH, + "%s/../tests/output/", + executable_directory); +end: + return return_code; +} diff --git a/tests/file_paths.h b/tests/file_paths.h index ef1901b..c1a6f1e 100644 --- a/tests/file_paths.h +++ b/tests/file_paths.h @@ -8,9 +8,23 @@ /** * @brief Fills dirname with the path to the fixture directory. * + * The fixture directory is located at tests/fixtures. This function assumes + * that the executable is located in the build directory. + * * @param dirname A character array of length MAX_PATH to fill with the path. * @return 0 on success, 1 on failure. */ int get_fixture_directory(char *dirname); +/** + * @brief Fills dirname with the path to the output directory. + * + * The output directory is located at tests/output. This function assumes + * that the executable is located in the build directory. + * + * @param dirname A character array of length MAX_PATH to fill with the path. + * @return 0 on success, 1 on failure. + */ +int get_output_directory(char *dirname); + #endif // TESTS_FILE_PATHS_H_ diff --git a/tests/main.c b/tests/main.c index 33b8da3..6b1dea2 100644 --- a/tests/main.c +++ b/tests/main.c @@ -8,6 +8,7 @@ #include #include #include +#include // TODO remove #include "tests/file_paths.h" #include "tests/test_linked_list.h" #include "tests/test_block.h" @@ -45,11 +46,14 @@ int create_empty_output_directory(char *dirname) { } int main(int argc, char **argv) { - // TODO use path to executable? - int return_value = create_empty_output_directory(TEST_OUTPUT_DIR); + // TODO Unix does not have MAX_PATH + char output_directory[MAX_PATH] = {0}; + int return_value = get_output_directory(output_directory); if (0 != return_value) { goto end; } + printf("Output directory: %s\n", output_directory); + create_empty_output_directory(output_directory); const struct CMUnitTest tests[] = { // test_linked_list.h cmocka_unit_test(test_linked_list_create_gives_linked_list), @@ -146,7 +150,7 @@ int main(int argc, char **argv) { cmocka_unit_test(test_htobe64_correctly_encodes_data), cmocka_unit_test(test_betoh64_correctly_decodes_data), }; - //return_value = cmocka_run_group_tests(tests, NULL, NULL); + return_value = cmocka_run_group_tests(tests, NULL, NULL); end: return return_value; }