TempDir is a lightweight C++17 library designed to provide an easy-to-use solution for managing temporary directories and files, particularly in unit testing scenarios (e.g., with Catch2). Inspired by JUnit's @TempDir annoation in the Java ecosystem, TempDir simplifies the creation, cleanup, and management of temporary directories in your C++ projects.
The library is implemented as a single-header file, making integration easy and comes without additional dependencies. The project is licensed under the MIT License.
Features
- Automatic cleanup
- Configurable cleanup policies to manage temporary directories' lifecycle.
- Easy integration
- A single-header implementation that can be directly included in your project.
- Unit test focus
- Perfect for use in testing frameworks like Catch2.
- Modern C++ support
- Written in C++17 for robust and efficient performance.
Example
#include <bw/tempdir/tempdir.hpp>
#include <catch2/catch_all.hpp>
#include <filesystem>
#include <fstream>
using namespace bw::tempdir;
namespace fs = std::filesystem;
TEST_CASE("TempDir usage in unit tests")
{
TempDir temp_dir;
REQUIRE(fs::exists(temp_dir.path())); // Temp directory should exist.
auto test_file = temp_dir.path() / "test.txt";
std::ofstream(test_file) << "Unit test data";
REQUIRE(fs::exists(test_file)); // File should be created.
}
// temp_dir out of scope, created temp directory will be removed