Skip to content

Commit

Permalink
#5825: Add unit test checking support of relative paths as snapshot p…
Browse files Browse the repository at this point in the history
…aths.
  • Loading branch information
codereader committed Feb 27, 2022
1 parent b4d0de2 commit 3b1ae25
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
13 changes: 9 additions & 4 deletions radiantcore/map/autosaver/AutoSaver.cpp
Expand Up @@ -43,7 +43,7 @@ namespace
std::string mapExt = game::current::getValue<std::string>(GKEY_MAP_EXTENSION);

// Construct the base name without numbered extension
std::string filename = (snapshotPath / mapName).string();
std::string filename = (snapshotPath / mapName).replace_extension().string();

// Now append the number and the map extension to the map name
filename += ".";
Expand Down Expand Up @@ -81,6 +81,11 @@ void AutoMapSaver::saveSnapshot()
// Construct the fs::path class out of the full map path (throws on fail)
fs::path fullPath = GlobalMapModule().getMapName();

if (!fullPath.is_absolute())
{
fullPath = GlobalFileSystem().findFile(fullPath.string()) + fullPath.string();
}

// Append the the snapshot folder to the path
fs::path snapshotPath = fullPath;
snapshotPath.remove_filename();
Expand All @@ -89,12 +94,12 @@ void AutoMapSaver::saveSnapshot()
// Retrieve the mapname
std::string mapName = fullPath.filename().string();

// Map existing snapshots (snapshot num => path)
std::map<int, std::string> existingSnapshots;

// Check if the folder exists and create it if necessary
if (os::fileOrDirExists(snapshotPath.string()) || os::makeDirectory(snapshotPath.string()))
{
// Map existing snapshots (snapshot num => path)
std::map<int, std::string> existingSnapshots;

collectExistingSnapshots(existingSnapshots, snapshotPath, mapName);

int highestNum = existingSnapshots.empty() ? 0 : existingSnapshots.rbegin()->first + 1;
Expand Down
35 changes: 35 additions & 0 deletions test/MapSavingLoading.cpp
Expand Up @@ -22,6 +22,7 @@
#include "os/file.h"
#include <sigc++/connection.h>
#include "testutil/FileSelectionHelper.h"
#include "registry/registry.h"

using namespace std::chrono_literals;

Expand Down Expand Up @@ -1277,6 +1278,40 @@ TEST_F(MapSavingTest, AutoSaverDoesntChangeSaveCopyAsFilename)
GlobalRadiantCore().getMessageBus().removeListener(msgSubscription);
}

TEST_F(MapSavingTest, AutoSaveSnapshotsSupportRelativePaths)
{
std::string modRelativePath = "maps/altar.map";
GlobalCommandSystem().executeCommand("OpenMap", modRelativePath);
checkAltarScene();

// Set this to a relative path
registry::setValue(map::RKEY_AUTOSAVE_SNAPSHOTS_ENABLED, true);
registry::setValue(map::RKEY_AUTOSAVE_SNAPSHOTS_FOLDER, "customsnapshots/");

// We expect the file to end up here
std::string expectedSnapshotPath = "maps/customsnapshots/altar.0.map";

EXPECT_FALSE(GlobalFileSystem().openTextFile(expectedSnapshotPath)) << "Snapshot already exists in " << expectedSnapshotPath;

// Trigger an auto save now
GlobalAutoSaver().performAutosave();

EXPECT_TRUE(GlobalFileSystem().openTextFile(expectedSnapshotPath)) << "Snapshot should now exist in " << expectedSnapshotPath;

// Load and confirm the saved scene
GlobalCommandSystem().executeCommand("OpenMap", expectedSnapshotPath);
checkAltarScene();

auto fullPath = GlobalFileSystem().findFile(expectedSnapshotPath) + expectedSnapshotPath;
EXPECT_NE(fullPath, "") << "Failed to resolve absolute file path of " << expectedSnapshotPath;

if (!fullPath.empty())
{
fs::remove(os::replaceExtension(fullPath, "darkradiant"));
fs::remove(fullPath);
}
}

namespace
{

Expand Down

0 comments on commit 3b1ae25

Please sign in to comment.