KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_jobs_output_archive.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
23#include <project.h>
24
25#include <wx/file.h>
26#include <wx/filename.h>
27
28
29BOOST_AUTO_TEST_SUITE( JobsOutputArchive )
30
31
32class TEST_PROJECT : public PROJECT
33{
34public:
35 explicit TEST_PROJECT( const wxString& aPath ) : m_path( aPath ) {}
36
37 const wxString GetProjectFullName() const override { return m_path + wxS( "test.kicad_pro" ); }
38
39private:
40 wxString m_path;
41};
42
43
48BOOST_AUTO_TEST_CASE( RelativeDestinationResolvesAgainstProject )
49{
50 wxFileName projectDir;
51 projectDir.AssignDir( wxFileName::GetTempDir() );
52 projectDir.AppendDir( wxS( "kicad_qa_resolve_project" ) );
53
54 TEST_PROJECT project( projectDir.GetPath() + wxFileName::GetPathSeparator() );
55
56 wxString resolved = JOBS_OUTPUT_HANDLER::ResolveOutputPath( wxS( "out\\sub/../final.zip" ), &project );
57
58 wxFileName expected( projectDir.GetPath(), wxS( "final.zip" ) );
59 expected.AppendDir( wxS( "out" ) );
60
61 BOOST_CHECK_EQUAL( resolved, expected.GetFullPath() );
62}
63
64
70{
71 explicit ScopedTempDir( const wxString& aTag )
72 {
73 wxFileName fn;
74 fn.AssignDir( wxFileName::GetTempDir() );
75 fn.AppendDir( wxString::Format( wxS( "kicad_qa_%s_%p" ), aTag, this ) );
76
77 BOOST_REQUIRE( wxFileName::Mkdir( fn.GetPath(), wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL ) );
78 path = fn.GetPath();
79 }
80
82 {
83 if( !path.IsEmpty() && wxFileName::DirExists( path ) )
84 wxFileName::Rmdir( path, wxPATH_RMDIR_RECURSIVE );
85 }
86
87 wxString path;
88};
89
90
99BOOST_AUTO_TEST_CASE( NestedDestinationPathCreatesParents )
100{
101 ScopedTempDir base( wxS( "archive_nested" ) );
102
103 // Populate a temp source directory with a single file to be archived.
104 wxString srcDir = base.path + wxFileName::GetPathSeparator() + wxS( "src" );
105 BOOST_REQUIRE( wxFileName::Mkdir( srcDir, wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL ) );
106
107 wxString srcFile = srcDir + wxFileName::GetPathSeparator() + wxS( "payload.txt" );
108 {
109 wxFile f;
110 BOOST_REQUIRE( f.Create( srcFile, true ) );
111 f.Write( wxS( "hello" ) );
112 }
113
114 // Destination uses two layers of nesting that do not yet exist.
115 wxString nestedDir = base.path + wxFileName::GetPathSeparator() + wxS( "level1" )
116 + wxFileName::GetPathSeparator() + wxS( "level2" );
117 wxString outPath = nestedDir + wxFileName::GetPathSeparator() + wxS( "archive.zip" );
118
119 BOOST_REQUIRE( !wxFileName::DirExists( nestedDir ) );
120
121 JOBS_OUTPUT_ARCHIVE archive;
122 archive.SetOutputPath( outPath );
123
124 std::vector<wxString> noOverwriteList;
125 std::vector<JOB_OUTPUT> outputs;
126 std::optional<wxString> resolved;
127
128 bool ok = archive.HandleOutputs( srcDir, nullptr, noOverwriteList, outputs, resolved );
129
130 BOOST_CHECK_MESSAGE( ok, "Archive job should succeed with a nested destination path." );
131 BOOST_CHECK_MESSAGE( wxFileName::DirExists( nestedDir ),
132 "Nested parent directory should have been created." );
133 BOOST_REQUIRE( resolved.has_value() );
134 BOOST_CHECK( wxFileName::FileExists( resolved.value() ) );
135}
136
137
142BOOST_AUTO_TEST_CASE( ExistingDestinationStillWorks )
143{
144 ScopedTempDir base( wxS( "archive_existing" ) );
145
146 wxString srcDir = base.path + wxFileName::GetPathSeparator() + wxS( "src" );
147 BOOST_REQUIRE( wxFileName::Mkdir( srcDir, wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL ) );
148
149 wxString srcFile = srcDir + wxFileName::GetPathSeparator() + wxS( "payload.txt" );
150 {
151 wxFile f;
152 BOOST_REQUIRE( f.Create( srcFile, true ) );
153 f.Write( wxS( "hello" ) );
154 }
155
156 wxString outPath = base.path + wxFileName::GetPathSeparator() + wxS( "archive.zip" );
157
158 JOBS_OUTPUT_ARCHIVE archive;
159 archive.SetOutputPath( outPath );
160
161 std::vector<wxString> noOverwriteList;
162 std::vector<JOB_OUTPUT> outputs;
163 std::optional<wxString> resolved;
164
165 bool ok = archive.HandleOutputs( srcDir, nullptr, noOverwriteList, outputs, resolved );
166
167 BOOST_CHECK( ok );
168 BOOST_REQUIRE( resolved.has_value() );
169 BOOST_CHECK( wxFileName::FileExists( resolved.value() ) );
170}
171
172
bool HandleOutputs(const wxString &aBaseTempPath, PROJECT *aProject, const std::vector< wxString > &aPathsWithOverwriteDisallowed, const std::vector< JOB_OUTPUT > &aOutputsToHandle, std::optional< wxString > &aResolvedOutputPath) override
void SetOutputPath(const wxString &aPath)
Definition jobs_output.h:56
static wxString ResolveOutputPath(const wxString &aPath, const PROJECT *aProject)
Expand variables and a leading '~' in a destination path, then anchor a relative result at the projec...
PROJECT()
Definition project.cpp:52
const wxString GetProjectFullName() const override
Return the full path and name of the project.
TEST_PROJECT(const wxString &aPath)
Scoped temporary directory used by the tests below.
ScopedTempDir(const wxString &aTag)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_CASE(RelativeDestinationResolvesAgainstProject)
A relative destination must land in the project directory, not the working directory,...
VECTOR3I expected(15, 30, 45)
BOOST_CHECK_EQUAL(result, "25.4")