KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_scoped_temp_path.cpp
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 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20#include <boost/test/unit_test.hpp>
21
22#include <gestfich.h>
23
24#include <wx/ffile.h>
25#include <wx/filename.h>
26#include <wx/log.h>
27
28#ifndef __WINDOWS__
29#include <wx/scopeguard.h>
30#include <sys/stat.h>
31#include <unistd.h>
32#endif
33
34
35BOOST_AUTO_TEST_SUITE( ScopedTempPath )
36
37
38BOOST_AUTO_TEST_CASE( RemovesWhatItCreated )
39{
40 wxString directory;
41 wxString file;
42
43 {
44 SCOPED_TEMP_PATH tempDirectory;
45 SCOPED_TEMP_PATH tempFile;
46 BOOST_REQUIRE( tempDirectory.MakeDirectory( wxS( "kicad_qa_scoped" ) ) );
47 BOOST_REQUIRE( tempFile.MakeFile( wxS( "kicad_qa_scoped" ) ) );
48 directory = tempDirectory.Path();
49 file = tempFile.Path();
50 BOOST_CHECK( wxFileName::DirExists( directory ) );
51 BOOST_CHECK( wxFileName::FileExists( file ) );
52
53 const wxString nested = directory + wxS( "/nested/child" );
54 BOOST_REQUIRE( wxFileName::Mkdir( nested, wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL ) );
55 wxFFile content( wxFileName( nested, wxS( "content" ) ).GetFullPath(), wxS( "w" ) );
56 BOOST_REQUIRE( content.IsOpened() );
57 }
58
59 BOOST_CHECK( !wxFileName::DirExists( directory ) );
60 BOOST_CHECK( !wxFileName::FileExists( file ) );
61}
62
63
64BOOST_AUTO_TEST_CASE( DirectoriesAreUniqueAndCanBePrivate )
65{
66#ifndef __WINDOWS__
67 const mode_t previousMask = umask( 0 );
68 wxON_BLOCK_EXIT1( umask, previousMask );
69#endif
70
71 SCOPED_TEMP_PATH first;
72 SCOPED_TEMP_PATH second;
73 BOOST_REQUIRE( first.MakeDirectory( wxS( "kicad_qa_scoped" ), 0700 ) );
74 BOOST_REQUIRE( second.MakeDirectory( wxS( "kicad_qa_scoped" ), 0700 ) );
75 BOOST_CHECK( first.Path() != second.Path() );
76 BOOST_CHECK( wxFileName::DirExists( first.Path() ) );
77 BOOST_CHECK( wxFileName::DirExists( second.Path() ) );
78
79#ifndef __WINDOWS__
80 for( const wxString& path : { first.Path(), second.Path() } )
81 {
82 struct stat status;
83 BOOST_REQUIRE_EQUAL( stat( path.fn_str(), &status ), 0 );
84 BOOST_CHECK_EQUAL( status.st_mode & 0777, 0700 );
85 }
86#endif
87}
88
89
90BOOST_AUTO_TEST_CASE( FailedDirectoryCreationLeavesExistingFileAlone )
91{
92 SCOPED_TEMP_PATH existing;
93 BOOST_REQUIRE( existing.MakeFile( wxS( "kicad_qa_scoped" ) ) );
94
95 {
96 wxFFile content( existing.Path(), wxS( "w" ) );
97 BOOST_REQUIRE( content.IsOpened() );
98 BOOST_REQUIRE( content.Write( wxS( "keep this file" ) ) );
99 }
100
101 {
102 wxLogNull noLog;
103 SCOPED_TEMP_PATH failed;
104 BOOST_CHECK( !failed.MakeDirectory( existing.Path() + wxS( "/scratch_" ), 0700 ) );
105 BOOST_CHECK( failed.Path().IsEmpty() );
106 }
107
108 wxFFile content( existing.Path(), wxS( "r" ) );
109 wxString text;
110 BOOST_REQUIRE( content.IsOpened() );
111 BOOST_REQUIRE( content.ReadAll( &text ) );
112 BOOST_CHECK_EQUAL( text.ToStdString(), "keep this file" );
113}
114
115
116#ifndef __WINDOWS__
117BOOST_AUTO_TEST_CASE( DirectoryCleanupLeavesSymlinkTargetAlone )
118{
119 SCOPED_TEMP_PATH outside;
120 BOOST_REQUIRE( outside.MakeDirectory( wxS( "kicad_qa_scoped" ), 0700 ) );
121 const wxString sentinel = wxFileName( outside.Path(), wxS( "sentinel" ) ).GetFullPath();
122
123 {
124 wxFFile content( sentinel, wxS( "w" ) );
125 BOOST_REQUIRE( content.IsOpened() );
126 BOOST_REQUIRE( content.Write( wxS( "keep this directory" ) ) );
127 }
128
129 wxString scratchPath;
130
131 {
132 SCOPED_TEMP_PATH scratch;
133 BOOST_REQUIRE( scratch.MakeDirectory( wxS( "kicad_qa_scoped" ), 0700 ) );
134 scratchPath = scratch.Path();
135 const wxString link = wxFileName( scratchPath, wxS( "outside" ) ).GetFullPath();
136 BOOST_REQUIRE_EQUAL( symlink( outside.Path().fn_str(), link.fn_str() ), 0 );
137 }
138
139 BOOST_CHECK( !wxFileName::DirExists( scratchPath ) );
140 BOOST_CHECK( wxFileName::DirExists( outside.Path() ) );
141 wxFFile content( sentinel, wxS( "r" ) );
142 wxString text;
143 BOOST_REQUIRE( content.IsOpened() );
144 BOOST_REQUIRE( content.ReadAll( &text ) );
145 BOOST_CHECK_EQUAL( text.ToStdString(), "keep this directory" );
146}
147#endif
148
149
A temporary file or directory that is removed when the object goes out of scope.
Definition gestfich.h:62
bool MakeDirectory(const wxString &aPrefix, int aPermissions=wxS_DIR_DEFAULT)
Create an empty directory with a unique name after aPrefix.
Definition gestfich.cpp:959
bool MakeFile(const wxString &aPrefix)
Create an empty file with a unique name after aPrefix.
Definition gestfich.cpp:952
const wxString & Path() const
Definition gestfich.h:76
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
BOOST_AUTO_TEST_SUITE_END()
std::string path
BOOST_AUTO_TEST_CASE(RemovesWhatItCreated)
BOOST_CHECK_EQUAL(result, "25.4")