Reset watch directory in wxFileSystemWatcher unit test

Share the same variable between GetWatchDir() and RemoveWatchDir() to
make things more clear.

Also check for GetWatchDir() postcondition (i.e. that the directory
exists) in the function itself instead of doing it in the caller.
This commit is contained in:
Vadim Zeitlin 2022-02-10 13:38:00 +00:00
parent 93c86fe5cd
commit 046f228bb2

View file

@ -128,32 +128,32 @@ public:
// static helpers
static const wxFileName& GetWatchDir()
{
static wxFileName dir;
if (dir.DirExists())
return dir;
if (ms_watchDir.DirExists())
return ms_watchDir;
wxString tmp = wxStandardPaths::Get().GetTempDir();
dir.AssignDir(tmp);
ms_watchDir.AssignDir(tmp);
// XXX look for more unique name? there is no function to generate
// unique filename, the file always get created...
dir.AppendDir("fswatcher_test");
REQUIRE(!dir.DirExists());
REQUIRE(dir.Mkdir());
ms_watchDir.AppendDir("fswatcher_test");
REQUIRE(!ms_watchDir.DirExists());
REQUIRE(ms_watchDir.Mkdir());
REQUIRE(ms_watchDir.DirExists());
return dir;
return ms_watchDir;
}
static void RemoveWatchDir()
{
wxFileName dir = GetWatchDir();
REQUIRE(dir.DirExists());
REQUIRE(ms_watchDir.DirExists());
// just to be really sure we know what we remove
REQUIRE( dir.GetDirs().Last() == "fswatcher_test" );
REQUIRE( ms_watchDir.GetDirs().Last() == "fswatcher_test" );
CHECK( dir.Rmdir(wxPATH_RMDIR_RECURSIVE) );
CHECK( ms_watchDir.Rmdir(wxPATH_RMDIR_RECURSIVE) );
ms_watchDir = wxFileName();
}
static wxFileName RandomName(const wxFileName& base, int length = 10)
@ -178,9 +178,13 @@ public:
protected:
static EventGenerator* ms_instance;
private:
static wxFileName ms_watchDir;
};
EventGenerator* EventGenerator::ms_instance = 0;
wxFileName EventGenerator::ms_watchDir;
// Abstract base class from which concrete event tests inherit.
@ -799,7 +803,6 @@ TEST_CASE_METHOD(FileSystemWatcherTestCase,
// We don't use this function for events. Just run the tests
wxFileName watchdir = EventGenerator::GetWatchDir();
REQUIRE(watchdir.DirExists());
wxFileName treedir(watchdir);
treedir.AppendDir("treetrunk");