From c098363e0d23bab1dc810b1a2c0f9d9a7eceaad5 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Fri, 1 May 2020 15:43:20 +0200 Subject: [PATCH] Add test for wxMemoryFSHandler bug fixed in the parent commit Executing this test before the changes of the previous commit would result in test failure/crash/Valgrind errors. Now it passes. See #18744. --- tests/filesys/filesystest.cpp | 50 +++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/tests/filesys/filesystest.cpp b/tests/filesys/filesystest.cpp index 06d07a0952..a46430ae11 100644 --- a/tests/filesys/filesystest.cpp +++ b/tests/filesys/filesystest.cpp @@ -24,6 +24,8 @@ #if wxUSE_FILESYSTEM +#include "wx/fs_mem.h" + // ---------------------------------------------------------------------------- // helpers // ---------------------------------------------------------------------------- @@ -176,4 +178,52 @@ TEST_CASE("wxFileSystem::UnicodeFileNameToUrlConversion", "[filesys][url][filena CHECK( filename.SameAs(wxFileName::URLToFileName(url)) ); } +// Test that using FindFirst() after removing a previously found URL works: +// this used to be broken, see https://trac.wxwidgets.org/ticket/18744 +TEST_CASE("wxFileSystem::MemoryFSHandler", "[filesys][memoryfshandler][find]") +{ + // Install wxMemoryFSHandler just for the duration of this test. + class AutoMemoryFSHandler + { + public: + AutoMemoryFSHandler() + : m_handler(new wxMemoryFSHandler()) + { + wxFileSystem::AddHandler(m_handler); + } + + ~AutoMemoryFSHandler() + { + wxFileSystem::RemoveHandler(m_handler); + } + + private: + wxMemoryFSHandler* const m_handler; + } autoMemoryFSHandler; + + wxMemoryFSHandler::AddFile("foo.txt", "foo contents"); + wxMemoryFSHandler::AddFile("bar.txt", "bar contents"); + wxFileSystem fs; + + wxString const url = fs.FindFirst("memory:*.txt"); + INFO("Found URL was: " << url); + + wxString filename; + REQUIRE( url.StartsWith("memory:", &filename) ); + + // We don't know which of the two files will be found, this depends on the + // details of the hash table implementation and varies across builds, so + // handle both cases and remove the other file, which should certainly + // invalidate the iterator pointing to it. + if ( filename == "foo.txt" ) + wxMemoryFSHandler::RemoveFile("bar.txt"); + else if ( filename == "bar.txt" ) + wxMemoryFSHandler::RemoveFile("foo.txt"); + else + FAIL("Unexpected filename: " << filename); + + CHECK( fs.FindFirst(url) == url ); + CHECK( fs.FindNext() == "" ); +} + #endif // wxUSE_FILESYSTEM