Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP #846 Initial idea for unit tests for ModLibrary #919

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 64 additions & 17 deletions src/data/mod_library.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,32 +27,70 @@
#include <utility>


static void nativeFilesystemIterate(
const std::filesystem::path& p,
std::filesystem::directory_options options,
std::error_code& ec,
Filesystem::DirectoryIterateCallbackFuncT callback,
void* userData) noexcept
{
namespace fs = std::filesystem;
auto dir_it = fs::directory_iterator(p, options, ec);
if (ec)
{
return;
}

for (const auto& dir_entry : dir_it)
{
if (!callback(dir_entry.path(), userData))
{
return;
}
}
}

const Filesystem gNativeFilesystemHandle{
&std::filesystem::is_directory,
&std::filesystem::exists,
&nativeFilesystemIterate};

namespace rigel::data
{

namespace
{

bool isNonEmptyDirectory(const std::filesystem::directory_entry& entry)
bool isNonEmptyDirectory(
const std::filesystem::path& entry,
const Filesystem& fsHandle)
{
namespace fs = std::filesystem;

std::error_code err;
if (!entry.is_directory(err) || err)
if (!fsHandle.f_is_directory(entry, err) || err)
{
return false;
}

if (!entry.exists(err) || err)
if (!fsHandle.f_exists(entry, err) || err)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note to self: adapt to this code base's style guide (sorry! was in a rush for a Proof of Concept)

{
return false;
}

using std::begin;
using std::end;
bool foundOne = false;
fsHandle.f_directory_iterate(
entry,
fs::directory_options::none,
err,
[](const std::filesystem::path& p, void* userData) noexcept -> bool
{
*static_cast<bool*>(userData) = true;
// once we've found one, we can just return, it's surely non empty
return false;
},
&foundOne);

const auto iContents = fs::directory_iterator{entry.path(), err};
return !err && begin(iContents) != end(iContents);
return !err && !foundOne;
}

} // namespace
Expand Down Expand Up @@ -82,7 +120,7 @@ void ModLibrary::updateGamePath(std::filesystem::path gamePath)
}


void ModLibrary::rescan()
void ModLibrary::rescan(const Filesystem& fsHandle)
{
namespace fs = std::filesystem;

Expand All @@ -94,18 +132,27 @@ void ModLibrary::rescan()
auto newAvailableMods = std::vector<std::string>{};

std::error_code err;
auto iModsDir = fs::directory_iterator{
mGamePath / MODS_PATH, fs::directory_options::skip_permission_denied, err};
if (!err)
struct Context
{
for (const auto& entry : iModsDir)
std::vector<std::string>* pNewAvailableMods = nullptr;
const Filesystem* pFsHandle = nullptr;
} ctx{&newAvailableMods, &fsHandle};

fsHandle.f_directory_iterate(
mGamePath / MODS_PATH,
fs::directory_options::skip_permission_denied,
err,
[](const std::filesystem::path& p, void* userData) noexcept -> bool
{
if (isNonEmptyDirectory(entry))
auto ctx =
static_cast<Context*>(userData);
if (isNonEmptyDirectory(p, *(ctx->pFsHandle)))
{
newAvailableMods.push_back(entry.path().filename().u8string());
ctx->pNewAvailableMods->push_back(p.filename().u8string());
}
}
}
return true;
},
&ctx);

LOG_F(INFO, "Found %d mods", int(newAvailableMods.size()));

Expand Down
23 changes: 22 additions & 1 deletion src/data/mod_library.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,27 @@
#include <tuple>
#include <vector>

struct Filesystem
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note to self, move/rename when done, discuss with Nikolai

{
using IsDirectoryFuncT =
bool (*)(const std::filesystem::path& p, std::error_code& ec);
using ExistsFuncT =
bool (*)(const std::filesystem::path& p, std::error_code& ec) noexcept;
using DirectoryIterateCallbackFuncT =
bool (*)(const std::filesystem::path& p, void* user_data) noexcept;
using DirectoryIterateFuncT = void (*)(
const std::filesystem::path& p,
std::filesystem::directory_options options,
std::error_code& ec,
DirectoryIterateCallbackFuncT callback,
void* user_data) noexcept;

IsDirectoryFuncT f_is_directory;
ExistsFuncT f_exists;
DirectoryIterateFuncT f_directory_iterate;
};

extern const Filesystem gNativeFilesystemHandle;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we could extend this for other purposes


namespace rigel::data
{
Expand Down Expand Up @@ -57,7 +78,7 @@ class ModLibrary
std::vector<ModStatus> initialSelection);

void updateGamePath(std::filesystem::path gamePath);
void rescan();
void rescan(const Filesystem& fsHandle = gNativeFilesystemHandle);

[[nodiscard]] std::vector<std::filesystem::path> enabledModPaths() const;
[[nodiscard]] const std::string& modDirName(int index) const;
Expand Down
26 changes: 14 additions & 12 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
add_executable(tests
test_main.cpp
test_array_view.cpp
test_duke_script_loader.cpp
test_elevator.cpp
test_high_score_list.cpp
test_json_utils.cpp
test_letter_collection.cpp
test_physics_system.cpp
test_player.cpp
test_rng.cpp
test_spike_ball.cpp
test_string_utils.cpp
test_timing.cpp
# test_array_view.cpp
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note to self: so that I can compile faster, remove these when ready

# test_duke_script_loader.cpp
# test_elevator.cpp
# test_high_score_list.cpp
# test_json_utils.cpp
# test_letter_collection.cpp
# test_physics_system.cpp
# test_player.cpp
# test_rng.cpp
# test_spike_ball.cpp
# test_string_utils.cpp
# test_timing.cpp

test_mod_library.cpp
)

target_link_libraries(tests
Expand Down
30 changes: 30 additions & 0 deletions test/test_mod_library.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include <base/warnings.hpp>
#include <data/mod_library.hpp>

RIGEL_DISABLE_WARNINGS
#include <catch.hpp>
RIGEL_RESTORE_WARNINGS

#include <cstdio>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why did I leave this here?


TEST_CASE("Mod library rescan")
{
namespace fs = std::filesystem;

rigel::data::ModLibrary ml{"/tmp", {}, {}};

Filesystem mockFsHandle{
[](const std::filesystem::path& p, std::error_code& ec) noexcept -> bool
{ return true; },
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

foolish mocks, but I just wanted a proof of concept

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

even without Gmock we can mock these nicely, we can use some static arrays of booleans and iterate through them with a counter (similar to WillOnce/WillRepeteadely)

I think we can also hack the directory iteration function for all the calls we need (similarly with an array and counter) since we expect that one to be called twice - doable

[](const std::filesystem::path& p, std::error_code& ec) noexcept -> bool
{ return true; },
[](
const std::filesystem::path& p,
std::filesystem::directory_options options,
std::error_code& ec,
Filesystem::DirectoryIterateCallbackFuncT callback,
void* user_data) noexcept {
}};

ml.rescan(mockFsHandle);
}
Loading