[generator][tests] Added ScopedDirCleanup helper.

Signed-off-by: Viktor Govako <viktor.govako@gmail.com>
This commit is contained in:
Viktor Govako
2025-09-08 00:19:39 -03:00
committed by x7z4w
parent bf6f33e5ed
commit 22cc10d127
6 changed files with 38 additions and 31 deletions

View File

@@ -165,18 +165,16 @@ void TestAltitudesBuilding(std::vector<TPoint3DList> const & roads, bool hasAlti
AltitudeGetter & altitudeGetter)
{
classificator::Load();
Platform & platform = GetPlatform();
std::string const testDirFullPath = base::JoinPath(platform.WritableDir(), kTestDir);
std::string const testDirFullPath = base::JoinPath(GetPlatform().WritableDir(), kTestDir);
ScopedDirCleanup testScopedDir(testDirFullPath);
// Building mwm without altitude section.
LocalCountryFile country(testDirFullPath, CountryFile(kTestMwm), 1);
ScopedDir testScopedDir(kTestDir);
ScopedFile testScopedMwm(base::JoinPath(kTestDir, kTestMwm + DATA_FILE_EXTENSION), ScopedFile::Mode::Create);
BuildMwmWithoutAltitudes(roads, country);
// Adding altitude section to mwm.
auto const mwmPath = testScopedMwm.GetFullPath();
auto const mwmPath = base::JoinPath(testDirFullPath, kTestMwm + DATA_FILE_EXTENSION);
BuildRoadAltitudes(mwmPath, altitudeGetter);
// Reading from mwm and testing altitude information.

View File

@@ -64,18 +64,15 @@ std::unique_ptr<CityRoads> LoadCityRoads(LocalCountryFile const & country)
/// section and then read from it.
void TestCityRoadsBuilding(vector<uint32_t> && cityRoadFeatureIds)
{
string const writableDir = GetPlatform().WritableDir();
string const testDir = base::JoinPath(GetPlatform().WritableDir(), kTestDir);
ScopedDirCleanup scopedDir(testDir);
// Building empty mwm.
LocalCountryFile country(base::JoinPath(writableDir, kTestDir), CountryFile(kTestMwm), 0 /* version */);
ScopedDir const scopedDir(kTestDir);
string const mwmRelativePath = base::JoinPath(kTestDir, kTestMwm + DATA_FILE_EXTENSION);
ScopedFile const scopedMwm(mwmRelativePath, ScopedFile::Mode::Create);
LocalCountryFile country(testDir, CountryFile(kTestMwm), 0 /* version */);
BuildEmptyMwm(country);
// Adding city_roads section to mwm.
string const mwmFullPath = base::JoinPath(writableDir, mwmRelativePath);
string const mwmFullPath = base::JoinPath(testDir, kTestMwm + DATA_FILE_EXTENSION);
vector<uint32_t> originalCityRoadFeatureIds = cityRoadFeatureIds;
routing_builder::SerializeCityRoads(mwmFullPath, std::move(cityRoadFeatureIds));

View File

@@ -73,19 +73,18 @@ void TestMaxspeedsSection(Features const & roads, string const & maxspeedsCsvCon
FeatureIdToOsmId const & featureIdToOsmId)
{
classificator::Load();
string const testDirFullPath = base::JoinPath(GetPlatform().WritableDir(), kTestDir);
ScopedDir testScopedDir(kTestDir);
ScopedDirCleanup testScopedDir(testDirFullPath);
// Writing |maxspeedsCsvContent| to a file in |kTestDir|.
ScopedFile testScopedMaxspeedsCsv(base::JoinPath(kTestDir, kCsv), maxspeedsCsvContent);
// Writing |roads| to test mwm.
LocalCountryFile country(testDirFullPath, CountryFile(kTestMwm), 1 /* version */);
string const testMwm = kTestMwm + DATA_FILE_EXTENSION;
ScopedFile testScopedMwm(base::JoinPath(kTestDir, testMwm), ScopedFile::Mode::Create);
BuildGeometry(roads, country);
string const testMwmFullPath = base::JoinPath(testDirFullPath, testMwm);
string const testMwmFullPath = base::JoinPath(testDirFullPath, kTestMwm + DATA_FILE_EXTENSION);
// Create routing graph for test mwm.
auto const countryParentGetter = [](std::string const &) { return string(); };

View File

@@ -134,33 +134,26 @@ void LoadRestrictions(string const & mwmFilePath, vector<Restriction> & restrict
/// loads the restriction section and test loaded restrictions.
/// \param |restrictionPath| comma separated text with restrictions in osm id terms.
/// \param |osmIdsToFeatureIdContent| comma separated text with mapping from osm ids to feature ids.
void TestRestrictionBuilding(string const & restrictionPath, string const & osmIdsToFeatureIdContent,
void TestRestrictionBuilding(string const & restrictionContent, string const & osmIdsToFeatureIdContent,
unique_ptr<IndexGraph> graph, vector<Restriction> & expectedNotUTurn,
vector<RestrictionUTurnForTests> & expectedUTurn)
{
Platform & platform = GetPlatform();
string const writableDir = platform.WritableDir();
string const targetDir = base::JoinPath(GetPlatform().WritableDir(), kTestDir);
ScopedDirCleanup scopedDir(targetDir);
string const targetDir = base::JoinPath(writableDir, kTestDir);
// Building empty mwm.
LocalCountryFile country(targetDir, CountryFile(kTestMwm), 0 /* version */);
ScopedDir const scopedDir(kTestDir);
string const mwmRelativePath = base::JoinPath(kTestDir, kTestMwm + DATA_FILE_EXTENSION);
ScopedFile const scopedMwm(mwmRelativePath, ScopedFile::Mode::Create);
BuildEmptyMwm(country);
// Creating a file with restrictions.
string const restrictionRelativePath = base::JoinPath(kTestDir, kRestrictionFileName);
ScopedFile const restrictionScopedFile(restrictionRelativePath, restrictionPath);
ScopedFile const restrictionScopedFile(base::JoinPath(kTestDir, kRestrictionFileName), restrictionContent);
// Creating osm ids to feature ids mapping.
string const mappingRelativePath = base::JoinPath(kTestDir, kOsmIdsToFeatureIdsName);
ScopedFile const mappingFile(mappingRelativePath, ScopedFile::Mode::Create);
string const & osmIdsToFeatureIdFullPath = mappingFile.GetFullPath();
string const osmIdsToFeatureIdFullPath = base::JoinPath(targetDir, kOsmIdsToFeatureIdsName);
ReEncodeOsmIdsToFeatureIdsMapping(osmIdsToFeatureIdContent, osmIdsToFeatureIdFullPath);
string const restrictionFullPath = base::JoinPath(writableDir, restrictionRelativePath);
string const & mwmFullPath = scopedMwm.GetFullPath();
string const restrictionFullPath = base::JoinPath(targetDir, kRestrictionFileName);
string const mwmFullPath = base::JoinPath(targetDir, kTestMwm + DATA_FILE_EXTENSION);
// Prepare data to collector.
auto restrictionCollector =

View File

@@ -57,5 +57,16 @@ std::string DebugPrint(ScopedDir const & dir)
os << "ScopedDir [" << dir.GetFullPath() << "]";
return os.str();
}
ScopedDirCleanup::ScopedDirCleanup(std::string const & path) : m_fullPath(path)
{
UNUSED_VALUE(Platform::MkDir(m_fullPath));
}
ScopedDirCleanup::~ScopedDirCleanup()
{
UNUSED_VALUE(Platform::RmDirRecursively(m_fullPath));
}
} // namespace tests_support
} // namespace platform

View File

@@ -38,6 +38,15 @@ private:
DISALLOW_COPY_AND_MOVE(ScopedDir);
};
class ScopedDirCleanup
{
std::string const m_fullPath;
public:
explicit ScopedDirCleanup(std::string const & path);
~ScopedDirCleanup();
};
std::string DebugPrint(ScopedDir const & dir);
} // namespace tests_support
} // namespace platform