Skip to content

Commit

Permalink
uniform test directories
Browse files Browse the repository at this point in the history
  • Loading branch information
tomadamatkinson committed Nov 19, 2024
1 parent 23c2028 commit 270923b
Showing 1 changed file with 43 additions and 34 deletions.
77 changes: 43 additions & 34 deletions components/filesystem/tests/filesystem.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,24 @@

using namespace vkb::filesystem;

Path create_test_directory(FileSystemPtr fs, const std::string &test_name)
{
const auto test_dir = fs->temp_directory() / "vulkan_samples_tests" / test_name;

REQUIRE(fs->create_directory(test_dir));
REQUIRE(fs->exists(test_dir));
REQUIRE(fs->is_directory(test_dir));

return test_dir;
}

void delete_test_directory(FileSystemPtr fs, const Path &test_dir)
{
REQUIRE(fs->exists(test_dir));
fs->remove(test_dir);
REQUIRE_FALSE(fs->exists(test_dir));
}

void create_test_file(FileSystemPtr fs, const Path &path, const std::string &data)
{
REQUIRE(fs);
Expand All @@ -47,11 +65,13 @@ TEST_CASE("File read and write", "[filesystem]")

auto fs = vkb::filesystem::get();

const auto test_file = fs->temp_directory() / "vulkan_samples" / "test.txt";
const auto test_dir = create_test_directory(fs, "file_test");
const auto test_file = test_dir / "test.txt";
const std::string test_data = "Hello, World!";

create_test_file(fs, test_file, test_data);
delete_test_file(fs, test_file);
delete_test_directory(fs, test_dir);
}

TEST_CASE("Read file chunk", "[filesystem]")
Expand All @@ -60,7 +80,8 @@ TEST_CASE("Read file chunk", "[filesystem]")

auto fs = vkb::filesystem::get();

const auto test_file = fs->temp_directory() / "vulkan_samples" / "chunk_test.txt";
const auto test_dir = create_test_directory(fs, "chunk_test");
const auto test_file = test_dir / "chunk_test.txt";
const std::string test_data = "Hello, World!";

create_test_file(fs, test_file, test_data);
Expand All @@ -70,6 +91,7 @@ TEST_CASE("Read file chunk", "[filesystem]")
REQUIRE(chunk_str == "Hello");

delete_test_file(fs, test_file);
delete_test_directory(fs, test_dir);
}

TEST_CASE("Read file chunk out of bounds", "[filesystem]")
Expand All @@ -78,7 +100,8 @@ TEST_CASE("Read file chunk out of bounds", "[filesystem]")

auto fs = vkb::filesystem::get();

const auto test_file = fs->temp_directory() / "vulkan_samples" / "chunk_oob_test.txt";
const auto test_dir = create_test_directory(fs, "chunk_oob");
const auto test_file = test_dir / "chunk_oob_test.txt";
const std::string test_data = "Hello, World!";

create_test_file(fs, test_file, test_data);
Expand All @@ -87,6 +110,7 @@ TEST_CASE("Read file chunk out of bounds", "[filesystem]")
REQUIRE(chunk.empty());

delete_test_file(fs, test_file);
delete_test_directory(fs, test_dir);
}

TEST_CASE("Read file chunk with offset", "[filesystem]")
Expand All @@ -95,7 +119,8 @@ TEST_CASE("Read file chunk with offset", "[filesystem]")

auto fs = vkb::filesystem::get();

const auto test_file = fs->temp_directory() / "vulkan_samples" / "chunk_offset_test.txt";
const auto test_dir = create_test_directory(fs, "chunk_offset");
const auto test_file = test_dir / "chunk_offset_test.txt";
const std::string test_data = "Hello, World!";

create_test_file(fs, test_file, test_data);
Expand All @@ -105,6 +130,7 @@ TEST_CASE("Read file chunk with offset", "[filesystem]")
REQUIRE(chunk_str == "World");

delete_test_file(fs, test_file);
delete_test_directory(fs, test_dir);
}

TEST_CASE("Read binary file", "[filesystem]")
Expand All @@ -113,7 +139,8 @@ TEST_CASE("Read binary file", "[filesystem]")

auto fs = vkb::filesystem::get();

const auto test_file = fs->temp_directory() / "vulkan_samples" / "binary_test.txt";
const auto test_dir = create_test_directory(fs, "binary_test");
const auto test_file = test_dir / "binary_test.txt";
const std::string test_data = "Hello, World!";

create_test_file(fs, test_file, test_data);
Expand All @@ -123,6 +150,7 @@ TEST_CASE("Read binary file", "[filesystem]")
REQUIRE(binary_str == test_data);

delete_test_file(fs, test_file);
delete_test_directory(fs, test_dir);
}

TEST_CASE("Create Directory", "[filesystem]")
Expand All @@ -131,53 +159,34 @@ TEST_CASE("Create Directory", "[filesystem]")

auto fs = vkb::filesystem::get();

const auto test_dir = fs->temp_directory() / "vulkan_samples_directory_test" / "test_dir";
const auto test_dir = create_test_directory(fs, "create_directory_test");
const auto test_sub_dir = test_dir / "test_dir";

REQUIRE(fs->create_directory(test_dir));
REQUIRE(fs->exists(test_dir));
REQUIRE(fs->is_directory(test_dir));
REQUIRE(fs->create_directory(test_sub_dir));
REQUIRE(fs->exists(test_sub_dir));
REQUIRE(fs->is_directory(test_sub_dir));

std::vector<uint8_t> data = {0, 1, 2, 3, 4, 5};
for (uint8_t i : data)
{
// Create sub directories
const auto sub_dir = test_dir / fmt::format("sub_dir_{}", i);
const auto sub_dir = test_sub_dir / fmt::format("sub_dir_{}", i);
REQUIRE(fs->create_directory(sub_dir));
}

// Check in a separate pass to ensure create_directory called multiple times doesn't fail
for (uint8_t i : data)
{
// Check sub directories
const auto sub_dir = test_dir / fmt::format("sub_dir_{}", i);
const auto sub_dir = test_sub_dir / fmt::format("sub_dir_{}", i);
REQUIRE(fs->exists(sub_dir));
REQUIRE(fs->is_directory(sub_dir));
}

// Remove both the directory and its parent
fs->remove(fs->temp_directory() / "vulkan_samples_directory_test");

REQUIRE_FALSE(fs->exists(test_dir));
}

TEST_CASE("Remove File Test", "[filesystem]")
{
vkb::filesystem::init();

auto fs = vkb::filesystem::get();

const auto test_file = fs->temp_directory() / "vulkan_samples_remove_file_test" / "remove_test.txt";

REQUIRE(fs->create_directory(test_file.parent_path()));

fs->write_file(test_file, "Hello, World!");

REQUIRE(fs->exists(test_file));
REQUIRE(fs->is_file(test_file));

REQUIRE_NOTHROW(fs->remove(test_file));
fs->remove(test_sub_dir);

REQUIRE_FALSE(fs->exists(test_file));
REQUIRE_FALSE(fs->exists(test_sub_dir));

fs->remove(test_file.parent_path());
delete_test_directory(fs, test_dir);
}

0 comments on commit 270923b

Please sign in to comment.