{"id":22248302,"url":"https://github.com/bw-hro/tempdir","last_synced_at":"2025-03-25T11:43:20.757Z","repository":{"id":263953493,"uuid":"891861453","full_name":"bw-hro/TempDir","owner":"bw-hro","description":"TempDir is a lightweight C++17 library designed to provide an easy-to-use solution for managing temporary directories and files.","archived":false,"fork":false,"pushed_at":"2025-02-21T07:38:03.000Z","size":67,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-21T08:28:31.707Z","etag":null,"topics":["cpp17","directory","filesystem","header-only","porable","temporary"],"latest_commit_sha":null,"homepage":"https://bw-hro.github.io/TempDir/","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bw-hro.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-11-21T04:46:59.000Z","updated_at":"2025-02-21T07:36:36.000Z","dependencies_parsed_at":"2024-11-21T07:31:51.520Z","dependency_job_id":"d69871b4-8bec-4490-abde-85a722ee433c","html_url":"https://github.com/bw-hro/TempDir","commit_stats":null,"previous_names":["bw-hro/tempdir"],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bw-hro%2FTempDir","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bw-hro%2FTempDir/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bw-hro%2FTempDir/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bw-hro%2FTempDir/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bw-hro","download_url":"https://codeload.github.com/bw-hro/TempDir/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245458124,"owners_count":20618693,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cpp17","directory","filesystem","header-only","porable","temporary"],"created_at":"2024-12-03T06:14:58.394Z","updated_at":"2025-03-25T11:43:20.746Z","avatar_url":"https://github.com/bw-hro.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# TempDir: Simplified Temporary Directory for C++\n\n[![CI Ubuntu](https://github.com/bw-hro/TempDir/actions/workflows/ubuntu.yml/badge.svg?branch=main)](https://github.com/bw-hro/TempDir/actions/workflows/ubuntu.yml)\n[![CI Windows](https://github.com/bw-hro/TempDir/actions/workflows/windows.yml/badge.svg?branch=main)](https://github.com/bw-hro/TempDir/actions/workflows/windows.yml)\n[![CI macOS](https://github.com/bw-hro/TempDir/actions/workflows/macos.yml/badge.svg?branch=main)](https://github.com/bw-hro/TempDir/actions/workflows/macos.yml)\n[![code coverage](https://bw-hro.github.io/TempDir/coverage-report/badge.svg)](https://bw-hro.github.io/TempDir/coverage-report)\n[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/bw-hro/TempDir/main/LICENSE)\n[![GitHub Releases](https://img.shields.io/github/release/bw-hro/TempDir.svg)](https://github.com/bw-hro/TempDir/releases)\n[![Vcpkg Version](https://img.shields.io/vcpkg/v/bw-tempdir)](https://vcpkg.link/ports/bw-tempdir)\n\n**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](https://github.com/catchorg/Catch2)). Inspired by JUnit's [`@TempDir`](https://junit.org/junit5/docs/current/api/org.junit.jupiter.api/org/junit/jupiter/api/io/TempDir.html) annoation in the Java ecosystem, **TempDir** simplifies the creation, cleanup, and management of temporary directories in your C++ projects.\n\nThe library is implemented as a **single-header file**, making integration easy and comes without additional dependencies. The project is licensed under the **MIT License**.\n\n## Features\n- **Automatic cleanup**: Configurable cleanup policies to manage temporary directories' lifecycle.\n- **Easy integration**: A single-header implementation that can be directly included in your project.\n- **Unit test focus**: Perfect for use in testing frameworks like Catch2\n- **Modern C++ support**: Written in C++17 for robust and efficient performance.\n\n## Installation\nSimply copy the [`tempdir.hpp`](https://github.com/bw-hro/TempDir/releases/latest/download/tempdir.hpp) file into your project and include it in your source files:\n\n```cpp\n#include \u003cbw/tempdir/tempdir.hpp\u003e\n```\nYou can also use the *vcpkg* port `bw-tempdir`\n\n```sh\nvcpkg install bw-tempdir\n```\n\nor add dependency to *vpkg.json* manifest file\n\n```json\n{\n  \"name\": \"your-project\",\n  \"version-string\": \"1.0.1\",\n  \"dependencies\": [\n    \"bw-tempdir\",\n    ...\n  ]\n}\n```\n\n## Example Usage\n\n### Creating and Managing a Temporary Directory\n```cpp\n#include \u003cbw/tempdir/tempdir.hpp\u003e\n#include \u003cfilesystem\u003e\n#include \u003cfstream\u003e\n#include \u003ciostream\u003e\n\nusing namespace bw::tempdir;\n\nint main()\n{\n    try\n    {\n        TempDir temp_dir; // Creates a temporary directory in the system's temp folder.\n\n        // Access the directory path\n        std::cout \u003c\u003c \"Temporary directory created at: \" \u003c\u003c temp_dir.path() \u003c\u003c std::endl;\n\n        // Perform operations in the temp directory\n        auto test_file = temp_dir.path() / \"example.txt\";\n        std::ofstream(test_file) \u003c\u003c \"Hello, TempDir!\";\n\n        std::cout \u003c\u003c \"Created file: \" \u003c\u003c test_file \u003c\u003c std::endl;\n\n        // Directory is automatically cleaned up based on the cleanup policy\n        // when temp_dir goes out of scope.\n    }\n    catch (const TempDirException\u0026 ex)\n    {\n        std::cerr \u003c\u003c \"An error occurred: \" \u003c\u003c ex.what() \u003c\u003c std::endl;\n    }\n\n    return 0;\n}\n```\nPlease do also check [examples directory](/examples/) for more examples.\n\n### Using TempDir in Catch2 Unit Tests\n```cpp\n#include \u003cbw/tempdir/tempdir.hpp\u003e\n#include \u003ccatch2/catch_all.hpp\u003e\n#include \u003cfilesystem\u003e\n#include \u003cfstream\u003e\n\nusing namespace bw::tempdir;\nnamespace fs = std::filesystem;\n\nTEST_CASE(\"TempDir usage in unit tests\")\n{\n    TempDir temp_dir;\n\n    REQUIRE(fs::exists(temp_dir.path())); // Temp directory should exist.\n\n    auto test_file = temp_dir.path() / \"test.txt\";\n    std::ofstream(test_file) \u003c\u003c \"Unit test data\";\n\n    REQUIRE(fs::exists(test_file)); // File should be created.\n}\n// temp_dir out of scope, created temp directory will be removed\n```\n\n\n## Cleanup Policies\nThe `TempDir` class offers configurable cleanup policies:\n- **`Cleanup::always`**: Always clean up the directory when `TempDir` goes out of scope. This is the default policy.\n- **`Cleanup::on_success`**: Clean up only if no exceptions were thrown.\n- **`Cleanup::never`**: Keep the directory and its contents.\n\nYou can set the cleanup policy in the constructor:\n```cpp\nTempDir temp_dir(Cleanup::on_success);\n```\n\n## Logging\nDisabled by default `TempDir` supports customizable logging by allowing you to provide a logging function in the `Config` object:\n```cpp\n// print to std::cout\nTempDir temp_dir(Config().enable_logging());\n\n// TempDir create '/tmp/temp_dir_1732162084442_37189'\n// TempDir remove '/tmp/temp_dir_1732162084442_37189'\n\n\n// forward to custom logger\nTempDir temp_dir(Config().enable_logging([](auto\u0026 msg) { \n    spdlog::info(msg);\n}));\n\n```\n\n## License\n**TempDir** is licensed under the MIT License. See [LICENSE](LICENSE) for details.\n\n## Contributing\nContributions are welcome! If you encounter a bug, have a feature request, or would like to contribute code, feel free to open an issue or submit a pull request.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbw-hro%2Ftempdir","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbw-hro%2Ftempdir","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbw-hro%2Ftempdir/lists"}