Skip to content

Commit

Permalink
Test that pointfiles are not returned for an unrelated map
Browse files Browse the repository at this point in the history
This test actually provoked a bug in the forEachPointfile() method, which was
not checking if the map directory actually existed before constructing a
directory_iterator, possibly resulting in an exception.
  • Loading branch information
Matthew Mott committed May 18, 2021
1 parent 9fc9017 commit e1072c8
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
5 changes: 5 additions & 0 deletions radiantcore/map/Map.cpp
Expand Up @@ -218,6 +218,11 @@ void Map::forEachPointfile(PointfileFunctor func) const
const fs::path mapDir = map.parent_path();
const fs::path mapStem = map.stem();

// Don't bother trying to iterate over a missing map directory, this will
// just throw an exception.
if (!fs::is_directory(mapDir))
return;

// Iterate over files in the map directory
for (const auto& entry: fs::directory_iterator(mapDir))
{
Expand Down
31 changes: 27 additions & 4 deletions test/PointTrace.cpp
Expand Up @@ -36,15 +36,38 @@ TEST_F(PointTraceTest, ConstructPointTraceWithData)
EXPECT_EQ(trace.size(), 5);
}

namespace
{

using StringList = std::list<std::string>;

// Get pointfile names in a list
StringList pointfiles()
{
StringList result;
GlobalMapModule().forEachPointfile([&](const std::string& pf)
{ result.push_back(pf); });
return result;
}

}

TEST_F(PointTraceTest, IdentifyMapPointfiles)
{
GlobalCommandSystem().executeCommand("OpenMap", std::string("altar.map"));

// Check the number of pointfiles for this map
int pointfiles = 0;
GlobalMapModule().forEachPointfile([&](const std::string&)
{ ++pointfiles; });
EXPECT_EQ(pointfiles, 2);
EXPECT_EQ(pointfiles().size(), 2);
}

TEST_F(PointTraceTest, PointFilesAssociatedWithCorrectMap)
{
std::string modRelativePath = "maps/altar_in_pk4.map";
GlobalCommandSystem().executeCommand("OpenMap", modRelativePath);

// No pointfiles should be associated with this map, even though it also
// starts with "altar_"
EXPECT_EQ(pointfiles().size(), 0);
}

}

0 comments on commit e1072c8

Please sign in to comment.