KiCad PCB EDA Suite
Loading...
Searching...
No Matches
temporary_directory.h
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
5 * SPDX-License-Identifier: GPL-3.0-or-later
6 */
7
8#pragma once
9
10#include <filesystem>
11#include <string>
12#include <system_error>
13
14namespace KI_TEST
15{
16
24{
25public:
26 TEMPORARY_DIRECTORY( const std::string& aNamePrefix, const std::string& aSuffix = "" )
27 {
28 for( size_t i = 0;; ++i )
29 {
30 m_path = std::filesystem::temp_directory_path() / ( aNamePrefix + std::to_string( i ) + aSuffix );
31
32 std::error_code error;
33
34 if( std::filesystem::create_directory( m_path, error ) )
35 break;
36
37 if( error && error != std::errc::file_exists )
38 throw std::filesystem::filesystem_error( "Cannot create temporary test directory", m_path, error );
39 }
40 }
41
44
46 {
47 std::error_code ignored;
48 std::filesystem::remove_all( m_path, ignored );
49 }
50
51 const std::filesystem::path& GetPath() const { return m_path; }
52
53private:
54 std::filesystem::path m_path;
55};
56
57} // namespace KI_TEST
TEMPORARY_DIRECTORY(const std::string &aNamePrefix, const std::string &aSuffix="")
TEMPORARY_DIRECTORY(const TEMPORARY_DIRECTORY &)=delete
const std::filesystem::path & GetPath() const
TEMPORARY_DIRECTORY & operator=(const TEMPORARY_DIRECTORY &)=delete
Test utilities for timestamps.