From a6d20e5c73a449ef9afdb09c6ee48c88013729fc Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Fri, 17 Jan 2025 00:56:37 +0100 Subject: [PATCH] Test using paths longer than MAX_PATH under Windows This works now, unlike before, at least for the operations tested here. --- tests/filename/filenametest.cpp | 43 +++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/tests/filename/filenametest.cpp b/tests/filename/filenametest.cpp index 328e3c5658..86dcf6fa3a 100644 --- a/tests/filename/filenametest.cpp +++ b/tests/filename/filenametest.cpp @@ -1030,6 +1030,49 @@ TEST_CASE("wxFileName::Shortcuts", "[filename]") CHECK( fnLinkRel.GetFullName() == fnLink.GetFullName() ); } +TEST_CASE("wxFileName::LongPath", "[filename]") +{ + constexpr const char* dir = "test_directory_name"; + constexpr const char* file = "test_file_name"; + + // Use many nested directories to ensure that the total path length for + // a file inside the innermost one is longer than MAX_PATH. + wxString path(dir); + path += wxFileName::GetPathSeparator(); + path += path; + path += path; + path += path; + path += path; + + CHECK( path.length() > MAX_PATH ); + + REQUIRE( wxFileName::Mkdir(path, wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL) ); + + // If we managed to create it, ensure that we delete it. + class CleanupTestDir + { + public: + explicit CleanupTestDir(const wxString& dir) : m_dir(dir) { } + ~CleanupTestDir() { wxFileName::Rmdir(m_dir, wxPATH_RMDIR_RECURSIVE); } + + private: + const wxString m_dir; + } cleanupTestDir(dir); + + const wxFileName fn{path, file}; + + // Create the file in this directory by constructing a temporary wxFile. + { + wxFile f(fn.GetFullPath(), wxFile::write); + REQUIRE( f.IsOpened() ); + REQUIRE( f.Write("Hello wx") ); + } + + // Try accessing it too. + CHECK( fn.FileExists() ); + CHECK( fn.GetSize() == 8 ); +} + #endif // __WINDOWS__ #ifdef __LINUX__