From c966bfed30e064149cf8f9c6aa06d9281a5a5c21 Mon Sep 17 00:00:00 2001 From: vng Date: Fri, 3 Jul 2015 20:23:05 +0300 Subject: [PATCH] [platform] Replaced Platform::AddOptionalPath with Platform::SetResourceDir. Added Platform::TestsDataPathForFile to read test files (usually placed in resource directory) in unit tests. --- coding/coding_tests/file_utils_test.cpp | 15 +++++++++ coding/file_name_utils.cpp | 36 +++++++++++++-------- coding/file_name_utils.hpp | 3 ++ generator/generator_tool/generator_tool.cpp | 29 ++++++----------- integration_tests/osrm_test_tools.cpp | 16 +++++---- map/map_tests/bookmarks_test.cpp | 29 ++++++----------- map/map_tests/kmz_unarchive_test.cpp | 20 +++++++----- platform/platform.cpp | 9 +++--- platform/platform.hpp | 15 +++++---- 9 files changed, 93 insertions(+), 79 deletions(-) diff --git a/coding/coding_tests/file_utils_test.cpp b/coding/coding_tests/file_utils_test.cpp index bba7856b39..c742fc2284 100644 --- a/coding/coding_tests/file_utils_test.cpp +++ b/coding/coding_tests/file_utils_test.cpp @@ -59,3 +59,18 @@ UNIT_TEST(FileName_GetDirectory) TEST_EQUAL(".", my::GetDirectory("somefile"), ()); #endif // !defined(OMIM_OS_WINDOWS) } + +UNIT_TEST(FilePath_Slash) +{ +#ifndef OMIM_OS_WINDOWS + TEST_EQUAL("/", my::AddSlashIfNeeded(""), ()); + TEST_EQUAL("/", my::AddSlashIfNeeded("/"), ()); + TEST_EQUAL("./", my::AddSlashIfNeeded("."), ()); + TEST_EQUAL("data/", my::AddSlashIfNeeded("data"), ()); + TEST_EQUAL("data/", my::AddSlashIfNeeded("data/"), ()); + TEST_EQUAL("/data/", my::AddSlashIfNeeded("/data"), ()); + TEST_EQUAL("/data/", my::AddSlashIfNeeded("/data/"), ()); + TEST_EQUAL("../../data/", my::AddSlashIfNeeded("../../data"), ()); + TEST_EQUAL("../../data/", my::AddSlashIfNeeded("../../data/"), ()); +#endif +} diff --git a/coding/file_name_utils.cpp b/coding/file_name_utils.cpp index 063e1c67d7..b2e44146f0 100644 --- a/coding/file_name_utils.cpp +++ b/coding/file_name_utils.cpp @@ -49,32 +49,42 @@ string GetNativeSeparator() string JoinFoldersToPath(const string & folder, const string & file) { - return folder + GetNativeSeparator() + file; + return folder + GetNativeSeparator() + file; } string JoinFoldersToPath(const string & folder1, const string & folder2, const string & file) { - string nativeSeparator = GetNativeSeparator(); - return folder1 + nativeSeparator + folder2 + nativeSeparator + file; + string nativeSeparator = GetNativeSeparator(); + return folder1 + nativeSeparator + folder2 + nativeSeparator + file; } string JoinFoldersToPath(const string & folder1, const string & folder2, const string & folder3, const string & file) { - string nativeSeparator = GetNativeSeparator(); - return folder1 + nativeSeparator + folder2 + nativeSeparator + folder3 + nativeSeparator + file; + string nativeSeparator = GetNativeSeparator(); + return folder1 + nativeSeparator + folder2 + nativeSeparator + folder3 + nativeSeparator + file; } string JoinFoldersToPath(const vector & folders, const string & file) { - if (folders.empty()) - return file; + if (folders.empty()) + return file; - string nativeSeparator = GetNativeSeparator(); - string result; - for (size_t i = 0; i < folders.size(); ++i) - result = result + folders[i] + nativeSeparator; + string nativeSeparator = GetNativeSeparator(); + string result; + for (size_t i = 0; i < folders.size(); ++i) + result = result + folders[i] + nativeSeparator; - result += file; - return result; + result += file; + return result; } + +string AddSlashIfNeeded(string const & path) +{ + string const sep = GetNativeSeparator(); + string::size_type const pos = path.rfind(sep); + if ((pos != string::npos) && (pos == path.size() - sep.size())) + return path; + return path + sep; +} + } // namespace my diff --git a/coding/file_name_utils.hpp b/coding/file_name_utils.hpp index ddef2af4a9..f738588acd 100644 --- a/coding/file_name_utils.hpp +++ b/coding/file_name_utils.hpp @@ -26,4 +26,7 @@ namespace my string JoinFoldersToPath(const string & folder1, const string & folder2, const string & file); string JoinFoldersToPath(const string & folder1, const string & folder2, const string & folder3, const string & file); string JoinFoldersToPath(const vector & folders, const string & file); + + /// Add terminating slash, if it's not exist to the folder path string. + string AddSlashIfNeeded(string const & path); } diff --git a/generator/generator_tool/generator_tool.cpp b/generator/generator_tool/generator_tool.cpp index 060ac0e275..3f4f04116d 100644 --- a/generator/generator_tool/generator_tool.cpp +++ b/generator/generator_tool/generator_tool.cpp @@ -19,6 +19,8 @@ #include "indexer/index_builder.hpp" #include "indexer/search_index_builder.hpp" +#include "coding/file_name_utils.hpp" + #include "defines.hpp" #include "platform/platform.hpp" @@ -30,6 +32,7 @@ #include "std/iomanip.hpp" #include "std/numeric.hpp" + DEFINE_bool(generate_update, false, "If specified, update.maps file will be generated from cells in the data path"); @@ -67,21 +70,6 @@ DEFINE_string(osm_file_type, "xml", "Input osm area file type [xml, o5m]"); DEFINE_string(user_resource_path, "", "User defined resource path for classificator.txt and etc."); -string AddSlashIfNeeded(string const & str) -{ - string result(str); - size_t const size = result.size(); - if (size) - { - if (result.find_last_of('\\') == size - 1) - result[size - 1] = '/'; - else - if (result.find_last_of('/') != size - 1) - result.push_back('/'); - } - return result; -} - int main(int argc, char ** argv) { google::SetUsageMessage( @@ -92,19 +80,20 @@ int main(int argc, char ** argv) Platform & pl = GetPlatform(); if (!FLAGS_user_resource_path.empty()) - { - pl.AddOptionalPath(FLAGS_user_resource_path); - } + pl.SetResourceDir(my::AddSlashIfNeeded(FLAGS_user_resource_path)); string const path = - FLAGS_data_path.empty() ? pl.WritableDir() : AddSlashIfNeeded(FLAGS_data_path); + FLAGS_data_path.empty() ? pl.WritableDir() : my::AddSlashIfNeeded(FLAGS_data_path); // Generating intermediate files if (FLAGS_preprocess) { LOG(LINFO, ("Generating intermediate data ....")); - if (!GenerateIntermediateData(FLAGS_intermediate_data_path, FLAGS_node_storage, FLAGS_osm_file_type, FLAGS_osm_file_name)) + if (!GenerateIntermediateData(FLAGS_intermediate_data_path, FLAGS_node_storage, + FLAGS_osm_file_type, FLAGS_osm_file_name)) + { return -1; + } } feature::GenerateInfo genInfo; diff --git a/integration_tests/osrm_test_tools.cpp b/integration_tests/osrm_test_tools.cpp index 07c968fb6d..64d15f4d78 100644 --- a/integration_tests/osrm_test_tools.cpp +++ b/integration_tests/osrm_test_tools.cpp @@ -2,21 +2,23 @@ #include "testing/testing.hpp" -#include "indexer/index.hpp" - -#include "geometry/distance_on_sphere.hpp" +#include "map/feature_vec_model.hpp" #include "routing/online_cross_fetcher.hpp" #include "routing/route.hpp" -#include "map/feature_vec_model.hpp" +#include "search/search_engine.hpp" + +#include "indexer/index.hpp" #include "platform/local_country_file.hpp" #include "platform/local_country_file_utils.hpp" #include "platform/platform.hpp" #include "platform/preferred_languages.hpp" -#include "search/search_engine.hpp" +#include "geometry/distance_on_sphere.hpp" + +#include "coding/file_name_utils.hpp" #include @@ -126,9 +128,9 @@ namespace integration Platform & pl = GetPlatform(); CommandLineOptions const & options = GetTestingOptions(); if (options.m_dataPath) - pl.SetWritableDirForTests(options.m_dataPath); + pl.SetWritableDirForTests(my::AddSlashIfNeeded(options.m_dataPath)); if (options.m_resourcePath) - pl.AddOptionalPath(options.m_resourcePath); + pl.SetResourceDir(my::AddSlashIfNeeded(options.m_resourcePath)); vector localFiles; platform::FindAllLocalMaps(localFiles); diff --git a/map/map_tests/bookmarks_test.cpp b/map/map_tests/bookmarks_test.cpp index 51cc6e854b..f41bd679a9 100644 --- a/map/map_tests/bookmarks_test.cpp +++ b/map/map_tests/bookmarks_test.cpp @@ -637,24 +637,15 @@ UNIT_TEST(Bookmarks_SpecialXMLNames) TEST(my::DeleteFileX(cat1.GetFileName()), ()); } -namespace -{ -bool AlmostEqualULPs(double const & a, double const & b) -{ - if (fabs(a - b) <= 1e-6) - return true; - return false; -} -} - UNIT_TEST(TrackParsingTest_1) { Framework framework; - string const KML = GetPlatform().SettingsPathForFile("kml-with-track-kml.test"); - BookmarkCategory * cat = BookmarkCategory::CreateFromKMLFile(KML, framework); - if (!cat) - TEST(false, ("Category can't be created")); + string const kmlFile = GetPlatform().TestsDataPathForFile("kml-with-track-kml.test"); + BookmarkCategory * cat = BookmarkCategory::CreateFromKMLFile(kmlFile, framework); + TEST(cat, ("Category can't be created")); + TEST_EQUAL(cat->GetTracksCount(), 4, ()); + string names[4] = { "Option1", "Pakkred1", "Pakkred2", "Pakkred3"}; graphics::Color col[4] = {graphics::Color(230, 0, 0, 255), graphics::Color(171, 230, 0, 255), @@ -666,7 +657,7 @@ UNIT_TEST(TrackParsingTest_1) { Track const * track = cat->GetTrack(i); TEST_EQUAL(names[i], track->GetName(), ()); - TEST(AlmostEqualULPs(track->GetLengthMeters(), length[i]), (track->GetLengthMeters(), length[i])); + TEST(fabs(track->GetLengthMeters() - length[i]) < 1.0E-6, (track->GetLengthMeters(), length[i])); TEST_EQUAL(col[i], track->GetMainColor(), ()); } } @@ -674,10 +665,10 @@ UNIT_TEST(TrackParsingTest_1) UNIT_TEST(TrackParsingTest_2) { Framework framework; - string const KML = GetPlatform().SettingsPathForFile("kml-with-track-from-google-earth.test"); - BookmarkCategory * cat = BookmarkCategory::CreateFromKMLFile(KML, framework); - if (!cat) - TEST(false, ("Category can't be created")); + string const kmlFile = GetPlatform().TestsDataPathForFile("kml-with-track-from-google-earth.test"); + BookmarkCategory * cat = BookmarkCategory::CreateFromKMLFile(kmlFile, framework); + TEST(cat, ("Category can't be created")); + TEST_EQUAL(cat->GetTracksCount(), 1, ()); Track const * track = cat->GetTrack(0); TEST_EQUAL(track->GetName(), "XY", ()); diff --git a/map/map_tests/kmz_unarchive_test.cpp b/map/map_tests/kmz_unarchive_test.cpp index 0de0a2503a..07cdf70be0 100644 --- a/map/map_tests/kmz_unarchive_test.cpp +++ b/map/map_tests/kmz_unarchive_test.cpp @@ -1,19 +1,23 @@ #include "testing/testing.hpp" -#include "coding/zip_reader.hpp" #include "map/framework.hpp" + #include "platform/platform.hpp" + +#include "coding/zip_reader.hpp" + #include "base/scope_guard.hpp" #include "std/string.hpp" #include "std/vector.hpp" #include "std/iostream.hpp" -UNIT_TEST(Open_KMZ_Test) + +UNIT_TEST(KMZ_UnzipTest) { - string const KMZFILE = GetPlatform().SettingsPathForFile("test.kmz"); + string const kmzFile = GetPlatform().TestsDataPathForFile("test.kmz"); ZipFileReader::FileListT files; - ZipFileReader::FilesList(KMZFILE, files); + ZipFileReader::FilesList(kmzFile, files); bool isKMLinZip = false; @@ -27,13 +31,13 @@ UNIT_TEST(Open_KMZ_Test) } TEST(isKMLinZip, ("No KML file in KMZ")); - string const KMLFILE = GetPlatform().SettingsPathForFile("newKml.kml"); - MY_SCOPE_GUARD(fileGuard, bind(&FileWriter::DeleteFileX, KMLFILE)); - ZipFileReader::UnzipFile(KMZFILE, "doc.kml", KMLFILE); + string const kmlFile = GetPlatform().WritablePathForFile("newKml.kml"); + MY_SCOPE_GUARD(fileGuard, bind(&FileWriter::DeleteFileX, kmlFile)); + ZipFileReader::UnzipFile(kmzFile, "doc.kml", kmlFile); Framework framework; BookmarkCategory cat("Default", framework); - TEST(cat.LoadFromKML(new FileReader(KMLFILE)), ()); + TEST(cat.LoadFromKML(new FileReader(kmlFile)), ()); TEST_EQUAL(files.size(), 6, ("KMZ file wrong number of files")); diff --git a/platform/platform.cpp b/platform/platform.cpp index bfffb65a7c..78dcdbde9a 100644 --- a/platform/platform.cpp +++ b/platform/platform.cpp @@ -32,7 +32,7 @@ Platform::EError Platform::ErrnoToError() string Platform::ReadPathForFile(string const & file, string searchScope) const { if (searchScope.empty()) - searchScope = "wrfo"; + searchScope = "wrf"; string fullPath; for (size_t i = 0; i < searchScope.size(); ++i) @@ -42,7 +42,6 @@ string Platform::ReadPathForFile(string const & file, string searchScope) const case 'w': fullPath = m_writableDir + file; break; case 'r': fullPath = m_resourcesDir + file; break; case 's': fullPath = m_settingsDir + file; break; - case 'o': fullPath = m_optionalDir + file; break; case 'f': fullPath = file; break; default : CHECK(false, ("Unsupported searchScope:", searchScope)); break; } @@ -50,9 +49,9 @@ string Platform::ReadPathForFile(string const & file, string searchScope) const return fullPath; } - string possiblePaths = m_writableDir + "\n" + m_resourcesDir + "\n" + m_settingsDir + "\n" + m_optionalDir; - - MYTHROW(FileAbsentException, ("File", file, "doesn't exist in the scope", searchScope, "Have been looking in:\n", possiblePaths)); + string const possiblePaths = m_writableDir + "\n" + m_resourcesDir + "\n" + m_settingsDir; + MYTHROW(FileAbsentException, ("File", file, "doesn't exist in the scope", searchScope, + "Have been looking in:\n", possiblePaths)); } string Platform::HashUniqueID(string const & s) diff --git a/platform/platform.hpp b/platform/platform.hpp index 98de3e437f..930f488bba 100644 --- a/platform/platform.hpp +++ b/platform/platform.hpp @@ -52,9 +52,6 @@ public: protected: /// Usually read-only directory for application resources string m_resourcesDir; - /// Optional resource search path - string m_optionalDir; - /// Writable directory to store downloaded map data /// @note on some systems it can point to external ejectable storage string m_writableDir; @@ -88,11 +85,9 @@ public: static bool IsFileExistsByFullPath(string const & filePath); - /// @return void - void AddOptionalPath(string const & path) { m_optionalDir = path; } /// @return always the same writable dir for current user with slash at the end string WritableDir() const { return m_writableDir; } - /// @return set writable dir — use for testing and linux stuff only + /// Set writable dir — use for testing and linux stuff only void SetWritableDirForTests(string const & path) { m_writableDir = path; } /// @return full path to file in user's writable directory string WritablePathForFile(string const & file) const { return WritableDir() + file; } @@ -104,6 +99,9 @@ public: /// @return resource dir (on some platforms it's differ from Writable dir) string ResourcesDir() const { return m_resourcesDir; } + /// @note! This function is used in generator_tool and unit tests. + /// Client app should not replace default resource dir. + void SetResourceDir(string const & path) { m_resourcesDir = path; } /// Creates directory at filesystem EError MkDir(string const & dirName) const; @@ -118,6 +116,9 @@ public: /// @return full path to file in the temporary directory string TmpPathForFile(string const & file) const { return TmpDir() + file; } + /// @return full path to file where stored data for unit tests. + string TestsDataPathForFile(string const & file) const { return ReadPathForFile(file); } + /// @return path for directory in the persistent memory, can be the same /// as WritableDir, but on some platforms it's different string SettingsDir() const { return m_settingsDir; } @@ -209,7 +210,7 @@ public: string GetMemoryInfo() const; static EConnectionType ConnectionStatus(); - static bool IsConnected() { return ConnectionStatus() != EConnectionType::CONNECTION_NONE; }; + static bool IsConnected() { return ConnectionStatus() != EConnectionType::CONNECTION_NONE; } private: void GetSystemFontNames(FilesList & res) const;