diff options
author | Maxim Pimenov <m@maps.me> | 2017-11-17 16:01:54 +0300 |
---|---|---|
committer | Arsentiy Milchakov <milcars@mapswithme.com> | 2017-11-17 17:34:25 +0300 |
commit | 50653171ff545219eae083cffa5106baeeea325d (patch) | |
tree | c382b7c754a88e54d6243752412c76d5d9a59e5a /platform | |
parent | e03e986c71969db0d3d5b625224368715252c6e8 (diff) |
[platform] [tests] Added an option to ScopedFile.
The option allows one to push responsibility on the user
of the ScopedFile, that is, enables a scenario where
a ScopedFile does not try to create the file on disk
but only assures the cleanup.
Diffstat (limited to 'platform')
-rw-r--r-- | platform/platform_tests/local_country_file_tests.cpp | 37 | ||||
-rw-r--r-- | platform/platform_tests_support/scoped_file.cpp | 33 | ||||
-rw-r--r-- | platform/platform_tests_support/scoped_file.hpp | 21 | ||||
-rw-r--r-- | platform/platform_tests_support/scoped_mwm.cpp | 2 |
4 files changed, 59 insertions, 34 deletions
diff --git a/platform/platform_tests/local_country_file_tests.cpp b/platform/platform_tests/local_country_file_tests.cpp index 2d63de0777..77eb658236 100644 --- a/platform/platform_tests/local_country_file_tests.cpp +++ b/platform/platform_tests/local_country_file_tests.cpp @@ -112,7 +112,7 @@ UNIT_TEST(LocalCountryFile_DiskFiles) string const mapFileName = GetFileName(countryFile.GetName(), MapOptions::Map, version::FOR_TESTING_TWO_COMPONENT_MWM1); - ScopedFile testMapFile(mapFileName, "map"); + ScopedFile testMapFile(mapFileName, ScopedFile::Mode::Create); localFile.SyncWithDisk(); TEST(localFile.OnDisk(MapOptions::Map), ()); @@ -122,7 +122,7 @@ UNIT_TEST(LocalCountryFile_DiskFiles) string const routingFileName = GetFileName(countryFile.GetName(), MapOptions::CarRouting, version::FOR_TESTING_TWO_COMPONENT_MWM1); - ScopedFile testRoutingFile(routingFileName, "routing"); + ScopedFile testRoutingFile(routingFileName, ScopedFile::Mode::Create); localFile.SyncWithDisk(); TEST(localFile.OnDisk(MapOptions::Map), ()); @@ -158,13 +158,13 @@ UNIT_TEST(LocalCountryFile_CleanupMapFiles) CountryFile irelandFile("Ireland"); LocalCountryFile japanLocalFile(mapsDir, japanFile, 0 /* version */); - ScopedFile japanMapFile("Japan.mwm", "Japan"); + ScopedFile japanMapFile("Japan.mwm", ScopedFile::Mode::Create); LocalCountryFile brazilLocalFile(mapsDir, brazilFile, 0 /* version */); - ScopedFile brazilMapFile("Brazil.mwm", "Brazil"); + ScopedFile brazilMapFile("Brazil.mwm", ScopedFile::Mode::Create); LocalCountryFile irelandLocalFile(dir4.GetFullPath(), irelandFile, 4 /* version */); - ScopedFile irelandMapFile(dir4, irelandFile, MapOptions::Map, "Ireland"); + ScopedFile irelandMapFile(dir4, irelandFile, MapOptions::Map); // Check FindAllLocalMaps() vector<LocalCountryFile> localFiles; @@ -202,18 +202,18 @@ UNIT_TEST(LocalCountryFile_CleanupPartiallyDownloadedFiles) ScopedDir latestDir("101010"); ScopedFile toBeDeleted[] = { - {"Ireland.mwm.ready", "Ireland"}, - {"Netherlands.mwm.routing.downloading2", "Netherlands"}, - {"Germany.mwm.ready3", "Germany"}, - {"UK_England.mwm.resume4", "UK"}, + {"Ireland.mwm.ready", ScopedFile::Mode::Create}, + {"Netherlands.mwm.routing.downloading2", ScopedFile::Mode::Create}, + {"Germany.mwm.ready3", ScopedFile::Mode::Create}, + {"UK_England.mwm.resume4", ScopedFile::Mode::Create}, {my::JoinFoldersToPath(oldDir.GetRelativePath(), "Russia_Central.mwm.downloading"), - "Central Russia map"}}; + ScopedFile::Mode::Create}}; ScopedFile toBeKept[] = { - {"Italy.mwm", "Italy"}, - {"Spain.mwm", "Spain map"}, - {"Spain.mwm.routing", "Spain routing"}, + {"Italy.mwm", ScopedFile::Mode::Create}, + {"Spain.mwm", ScopedFile::Mode::Create}, + {"Spain.mwm.routing", ScopedFile::Mode::Create}, {my::JoinFoldersToPath(latestDir.GetRelativePath(), "Russia_Southern.mwm.downloading"), - "Southern Russia map"}}; + ScopedFile::Mode::Create}}; CleanupMapsDirectory(101010 /* latestVersion */); @@ -244,10 +244,9 @@ UNIT_TEST(LocalCountryFile_DirectoryLookup) ScopedDir testDir("test-dir"); - ScopedFile testIrelandMapFile(testDir, irelandFile, MapOptions::Map, "Ireland-map"); - ScopedFile testNetherlandsMapFile(testDir, netherlandsFile, MapOptions::Map, "Netherlands-map"); - ScopedFile testNetherlandsRoutingFile(testDir, netherlandsFile, MapOptions::CarRouting, - "Netherlands-routing"); + ScopedFile testIrelandMapFile(testDir, irelandFile, MapOptions::Map); + ScopedFile testNetherlandsMapFile(testDir, netherlandsFile, MapOptions::Map); + ScopedFile testNetherlandsRoutingFile(testDir, netherlandsFile, MapOptions::CarRouting); vector<LocalCountryFile> localFiles; FindAllLocalMapsInDirectoryAndCleanup(testDir.GetFullPath(), 150309 /* version */, @@ -279,7 +278,7 @@ UNIT_TEST(LocalCountryFile_AllLocalFilesLookup) settings::Delete("LastMigration"); - ScopedFile testItalyMapFile(testDir, italyFile, MapOptions::Map, "Italy-map"); + ScopedFile testItalyMapFile(testDir, italyFile, MapOptions::Map); vector<LocalCountryFile> localFiles; FindAllLocalMapsAndCleanup(10101 /* latestVersion */, localFiles); diff --git a/platform/platform_tests_support/scoped_file.cpp b/platform/platform_tests_support/scoped_file.cpp index 44baf13ea2..d083dfc446 100644 --- a/platform/platform_tests_support/scoped_file.cpp +++ b/platform/platform_tests_support/scoped_file.cpp @@ -20,11 +20,31 @@ namespace platform { namespace tests_support { -ScopedFile::ScopedFile(string const & relativePath) : ScopedFile(relativePath, {} /* contents */) {} +ScopedFile::ScopedFile(string const & relativePath, Mode mode) + : ScopedFile(relativePath, {} /* contents */, mode) +{ +} ScopedFile::ScopedFile(string const & relativePath, string const & contents) + : ScopedFile(relativePath, contents, Mode::Create) +{ +} + +ScopedFile::ScopedFile(ScopedDir const & dir, CountryFile const & countryFile, + MapOptions mapOptions) + : ScopedFile( + my::JoinPath(dir.GetRelativePath(), GetFileName(countryFile.GetName(), mapOptions, + version::FOR_TESTING_TWO_COMPONENT_MWM1)), + Mode::Create) +{ +} + +ScopedFile::ScopedFile(string const & relativePath, string const & contents, Mode mode) : m_fullPath(my::JoinFoldersToPath(GetPlatform().WritableDir(), relativePath)) { + if (mode == Mode::DoNotCreate) + return; + try { FileWriter writer(GetFullPath()); @@ -38,22 +58,13 @@ ScopedFile::ScopedFile(string const & relativePath, string const & contents) CHECK(Exists(), ("Can't create test file", GetFullPath())); } -ScopedFile::ScopedFile(ScopedDir const & dir, CountryFile const & countryFile, MapOptions file, - string const & contents) - : ScopedFile(my::JoinFoldersToPath(dir.GetRelativePath(), - GetFileName(countryFile.GetName(), file, version::FOR_TESTING_TWO_COMPONENT_MWM1)), - contents) -{ - CHECK(Exists(), ("Can't create test file", GetFullPath())); -} - ScopedFile::~ScopedFile() { if (m_reset) return; if (!Exists()) { - LOG(LERROR, ("File", GetFullPath(), "was deleted before dtor of ScopedFile.")); + LOG(LERROR, ("File", GetFullPath(), "did not exist or was deleted before dtor of ScopedFile.")); return; } if (!my::DeleteFileX(GetFullPath())) diff --git a/platform/platform_tests_support/scoped_file.hpp b/platform/platform_tests_support/scoped_file.hpp index e59d47e1e5..a7acf571d7 100644 --- a/platform/platform_tests_support/scoped_file.hpp +++ b/platform/platform_tests_support/scoped_file.hpp @@ -18,12 +18,25 @@ class ScopedDir; class ScopedFile { public: - explicit ScopedFile(string const & relativePath); + enum class Mode : uint32_t + { + // Create or overwrite the file and remove it at scope exit. + Create, + // Remove the file at scope exit. The caller must + // ensure that the file has been created by that time. + DoNotCreate + }; + + // Creates a scoped file in the specified mode. + ScopedFile(string const & relativePath, Mode mode); + + // Creates a scoped file in Mode::Create and writes |contents| to it. ScopedFile(string const & relativePath, string const & contents); - ScopedFile(ScopedDir const & dir, CountryFile const & countryFile, MapOptions file, - string const & contents); + // Creates a scoped file in Mode::Create using the path inferred from |countryFile| + // and |mapOptions|. + ScopedFile(ScopedDir const & dir, CountryFile const & countryFile, MapOptions mapOptions); ~ScopedFile(); @@ -34,6 +47,8 @@ public: inline bool Exists() const { return GetPlatform().IsFileExistsByFullPath(GetFullPath()); } private: + ScopedFile(string const & relativePath, string const & contents, Mode mode); + string const m_fullPath; bool m_reset = false; diff --git a/platform/platform_tests_support/scoped_mwm.cpp b/platform/platform_tests_support/scoped_mwm.cpp index c7bc9e413f..01fc5b6dad 100644 --- a/platform/platform_tests_support/scoped_mwm.cpp +++ b/platform/platform_tests_support/scoped_mwm.cpp @@ -17,7 +17,7 @@ namespace platform { namespace tests_support { -ScopedMwm::ScopedMwm(string const & relativePath) : m_file(relativePath, "") +ScopedMwm::ScopedMwm(string const & relativePath) : m_file(relativePath, ScopedFile::Mode::Create) { DataHeader header; { |