diff options
-rw-r--r-- | samples/.gitignore | 1 | ||||
-rw-r--r-- | tests/.gitignore | 4 | ||||
-rw-r--r-- | tests/CMakeLists.txt | 10 | ||||
-rw-r--r-- | tests/file_open_test.c | 21 | ||||
-rw-r--r-- | tests/util.c | 28 | ||||
-rw-r--r-- | tests/util.h | 3 | ||||
-rw-r--r-- | tests/windows_unicode_test.c | 55 | ||||
-rw-r--r-- | tinydir.h | 5 |
8 files changed, 106 insertions, 21 deletions
diff --git a/samples/.gitignore b/samples/.gitignore index a948f88..bc406f8 100644 --- a/samples/.gitignore +++ b/samples/.gitignore @@ -32,6 +32,7 @@ Win32/ *.vcxproj.user *.vcxproj *.vcxproj.filters +*.VC.* *.sln # Linux diff --git a/tests/.gitignore b/tests/.gitignore index 75e9d66..cf0b910 100644 --- a/tests/.gitignore +++ b/tests/.gitignore @@ -33,6 +33,7 @@ Win32/ *.vcxproj.user *.vcxproj *.vcxproj.filters +*.VC.* *.sln # Linux @@ -41,3 +42,6 @@ Makefile # OS X CMakeScripts/ *.xcodeproj + +# Test files +*.tmp diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index ddf4a6f..487c59f 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -25,6 +25,14 @@ endif() # Add tests enable_testing() +add_library(util util.c util.h) + add_executable(file_open_test file_open_test.c) -target_link_libraries(file_open_test cbehave) +target_link_libraries(file_open_test util cbehave) add_test(NAME file_open_test COMMAND file_open_test) + +if(MSVC) +add_executable(windows_unicode_test windows_unicode_test.c) +target_link_libraries(windows_unicode_test cbehave) +add_test(NAME windows_unicode_test COMMAND windows_unicode_test) +endif() diff --git a/tests/file_open_test.c b/tests/file_open_test.c index dc238c3..3e659bc 100644 --- a/tests/file_open_test.c +++ b/tests/file_open_test.c @@ -2,27 +2,8 @@ #include <tinydir.h> #include "cbehave.h" +#include "util.h" -static void make_temp_file(const char *prefix, char *out) -{ -#ifdef _MSC_VER - if (GetTempFileName(".", prefix, 0, out) != 0) - { - // Strip the ".\\" prefix - if (strncmp(out, ".\\", 2) == 0) - { - memmove(out, out + 2, strlen(out)); - } - // Create file - fclose(fopen(out, "w")); - } -#else - #include <stdlib.h> - #include <unistd.h> - sprintf(out, "%sXXXXXX", prefix); - close(mkstemp(out)); -#endif -} FEATURE(file_open, "File open") SCENARIO("Open file in current directory") diff --git a/tests/util.c b/tests/util.c new file mode 100644 index 0000000..0304f17 --- /dev/null +++ b/tests/util.c @@ -0,0 +1,28 @@ +#include <stdio.h> + +#ifdef _MSC_VER +#define WIN32_LEAN_AND_MEAN +#include <windows.h> +#endif + + +void make_temp_file(const char *prefix, char *out) +{ +#ifdef _MSC_VER + if (GetTempFileName(".", prefix, 0, out) != 0) + { + // Strip the ".\\" prefix + if (strncmp(out, ".\\", 2) == 0) + { + memmove(out, out + 2, strlen(out)); + } + // Create file + fclose(fopen(out, "w")); + } +#else + #include <stdlib.h> + #include <unistd.h> + sprintf(out, "%sXXXXXX", prefix); + close(mkstemp(out)); +#endif +} diff --git a/tests/util.h b/tests/util.h new file mode 100644 index 0000000..0940720 --- /dev/null +++ b/tests/util.h @@ -0,0 +1,3 @@ +#pragma once + +void make_temp_file(const char *prefix, char *out); diff --git a/tests/windows_unicode_test.c b/tests/windows_unicode_test.c new file mode 100644 index 0000000..dc83f8c --- /dev/null +++ b/tests/windows_unicode_test.c @@ -0,0 +1,55 @@ +#include <stdio.h> + +#define UNICODE +#include <tinydir.h> +#include "cbehave.h" + + +#define TEST_PATH L"windows_unicode_test" +#define TEST_PATH_A "windows_unicode_test" + +void make_temp_file_utf16(const wchar_t *prefix, wchar_t *out) +{ + if (GetTempFileNameW(TEST_PATH, prefix, 0, out) != 0) + { + // Create file + fclose(_wfopen(out, L"w")); + // Strip the ".\\" prefix + if (wcsncmp(out, TEST_PATH L"\\", wcslen(TEST_PATH) + 1) == 0) + { + memmove(out, out + wcslen(TEST_PATH) + 1, wcslen(out)); + } + } +} + +FEATURE(windows_unicode, "Windows Unicode") + SCENARIO("Open directory with unicode file names") + GIVEN("a directory with unicode file names") + CreateDirectoryW(TEST_PATH, NULL); + wchar_t name[4096]; + make_temp_file_utf16(L"t📁", name); + WHEN("we open the directory") + tinydir_dir dir; + int r = tinydir_open(&dir, TEST_PATH); + THEN("the result should be successful") + SHOULD_INT_EQUAL(r, 0); + AND("iterating it, we should find the file") + bool found = false; + while (dir.has_next) + { + tinydir_file file; + tinydir_readfile(&dir, &file); + if (wcscmp((wchar_t *)file.name, name) == 0) + { + found = true; + } + tinydir_next(&dir); + } + SHOULD_BE_TRUE(found); + tinydir_close(&dir); + _wremove(name); + RemoveDirectoryW(TEST_PATH); + SCENARIO_END +FEATURE_END + +CBEHAVE_RUN("Windows unicode:", TEST_FEATURE(windows_unicode)) @@ -115,6 +115,9 @@ extern "C" { # define _TINYDIR_FUNC static inline #endif +/* readdir_r usage; define TINYDIR_USE_READDIR_R to use it (if supported) */ +#ifdef TINYDIR_USE_READDIR_R + /* readdir_r is a POSIX-only function, and may not be available under various * environments/settings, e.g. MinGW. Use readdir fallback */ #if _POSIX_C_SOURCE >= 1 || _XOPEN_SOURCE || _BSD_SOURCE || _SVID_SOURCE ||\ @@ -139,6 +142,8 @@ extern "C" { # define _TINYDIR_USE_READDIR #endif +#endif + /* MINGW32 has two versions of dirent, ASCII and UNICODE*/ #ifndef _MSC_VER #if (defined __MINGW32__) && (defined _UNICODE) |