Skip to content
Merged
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
7 changes: 4 additions & 3 deletions components/filesystem/src/legacy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,11 @@ void create_directory(const std::string &path)

void create_path(const std::string &root, const std::string &path)
{
for (auto it = path.begin(); it != path.end(); ++it)
std::filesystem::path full_path = std::filesystem::path(root) / path;

if (!vkb::filesystem::get()->create_directory(full_path))
{
it = std::find(it, path.end(), '/');
create_directory(root + '/' + std::string(path.begin(), it));
ERRORF("Failed to create directory: {}", full_path.string());
}
}

Expand Down
6 changes: 3 additions & 3 deletions components/filesystem/src/std_filesystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ bool StdFileSystem::create_directory(const Path &path)
{
std::error_code ec;

std::filesystem::create_directory(path, ec);
std::filesystem::create_directories(path, ec);

if (ec)
{
Expand Down Expand Up @@ -132,7 +132,7 @@ void StdFileSystem::remove(const Path &path)
{
std::error_code ec;

std::filesystem::remove(path, ec);
std::filesystem::remove_all(path, ec);

if (ec)
{
Expand All @@ -156,4 +156,4 @@ const Path &StdFileSystem::temp_directory() const
}

} // namespace filesystem
} // namespace vkb
} // namespace vkb
78 changes: 72 additions & 6 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,4 +150,43 @@ 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]")
{
vkb::filesystem::init();

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

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_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_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_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(test_sub_dir);

REQUIRE_FALSE(fs->exists(test_sub_dir));

delete_test_directory(fs, test_dir);
}
Loading