Skip to content

Commit

Permalink
#5108: Add unit tests asserting that we can get the file size of both…
Browse files Browse the repository at this point in the history
… physical files and those in a PK4.
  • Loading branch information
codereader committed Nov 20, 2020
1 parent 6148236 commit 3df1215
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
5 changes: 5 additions & 0 deletions include/ifilesystem.h
Expand Up @@ -104,6 +104,11 @@ class FileInfo
return _infoProvider ? _infoProvider->getFileSize(fullPath()) : 0;
}

std::size_t getIsPhysicalFile() const
{
return _infoProvider ? _infoProvider->getIsPhysical(fullPath()) : false;
}

/// Equality comparison with another FileInfo
bool operator== (const FileInfo& rhs) const
{
Expand Down
33 changes: 31 additions & 2 deletions test/VFS.cpp
Expand Up @@ -2,6 +2,7 @@

#include "ifilesystem.h"
#include "os/path.h"
#include "os/file.h"

namespace test
{
Expand Down Expand Up @@ -29,17 +30,45 @@ TEST_F(VfsTest, FilePrerequisites)
TEST_F(VfsTest, VisitEntireTree)
{
// Use a visitor to walk the tree
std::set<std::string> foundFiles;
std::map<std::string, vfs::FileInfo> foundFiles;
GlobalFileSystem().forEachFile(
"", "*",
[&](const vfs::FileInfo& fi) { foundFiles.insert(fi.name); },
[&](const vfs::FileInfo& fi) { foundFiles.emplace(fi.name, fi); },
0
);
EXPECT_EQ(foundFiles.count("dummy"), 0);
EXPECT_EQ(foundFiles.count("materials/example.mtr"), 1);
EXPECT_EQ(foundFiles.count("models/darkmod/test/unit_cube.ase"), 1);
}

TEST_F(VfsTest, GetArchiveFileInfo)
{
// Use a visitor to walk the tree
std::map<std::string, vfs::FileInfo> foundFiles;
GlobalFileSystem().forEachFile(
"", "*",
[&](const vfs::FileInfo& fi) { foundFiles.emplace(fi.name, fi); },
0
);

// Inspect a physical file that is in the test resources
std::string physicalFile = "materials/example.mtr";
std::string fileInPak = "materials/tdm_bloom_afx.mtr"; // this is in tdm_example_mtrs.pk4

EXPECT_EQ(foundFiles.count(physicalFile), 1); // physical file
EXPECT_EQ(foundFiles.count(fileInPak), 1); // file in pk4

// Get the file size of example.mtr
fs::path physicalFilePath = _context.getTestResourcePath();
physicalFilePath /= physicalFile;

EXPECT_EQ(foundFiles.find(physicalFile)->second.getSize(), os::getFileSize(physicalFilePath.string()));
EXPECT_EQ(foundFiles.find(physicalFile)->second.getIsPhysicalFile(), true);

EXPECT_EQ(foundFiles.find(fileInPak)->second.getSize(), 1096); // that file should have 1096 bytes
EXPECT_EQ(foundFiles.find(fileInPak)->second.getIsPhysicalFile(), false);
}

TEST_F(VfsTest, VisitMaterialsFolderOnly)
{
// Visit files only under materials/
Expand Down

0 comments on commit 3df1215

Please sign in to comment.