KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_project_template.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, you may find one here:
18 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
19 * or you may search the http://www.gnu.org website for the version 2 license,
20 * or you may write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 */
23
28
30#include <project_template.h>
31
32#include <wx/dir.h>
33#include <wx/filename.h>
34
35#include <filesystem>
36#include <fstream>
37
38namespace fs = std::filesystem;
39
40
42{
43public:
45 {
46 m_tempDir = fs::temp_directory_path() / "kicad_template_test";
47 fs::remove_all( m_tempDir );
48 fs::create_directories( m_tempDir );
49 }
50
52 {
53 fs::remove_all( m_tempDir );
54 }
55
56 void CreateTemplateStructure( const std::string& templateName,
57 const std::vector<std::string>& subdirs,
58 const std::vector<std::string>& files )
59 {
60 fs::path templatePath = m_tempDir / templateName;
61 fs::create_directories( templatePath );
62
63 fs::path metaPath = templatePath / "meta";
64 fs::create_directories( metaPath );
65
66 std::ofstream infoFile( ( metaPath / "info.html" ).string() );
67 infoFile << "<html><head><title>Test Template</title></head><body></body></html>";
68 infoFile.close();
69
70 for( const auto& subdir : subdirs )
71 {
72 fs::create_directories( templatePath / subdir );
73 }
74
75 for( const auto& file : files )
76 {
77 fs::path filePath = templatePath / file;
78 fs::create_directories( filePath.parent_path() );
79 std::ofstream f( filePath.string() );
80 f << "test content";
81 f.close();
82 }
83 }
84
85 fs::path m_tempDir;
86};
87
88
89BOOST_FIXTURE_TEST_SUITE( ProjectTemplate, PROJECT_TEMPLATE_TEST_FIXTURE )
90
91
92BOOST_AUTO_TEST_CASE( DirectoriesRenamedCorrectly )
93{
94 // Create a template with subdirectories that should be renamed
95 CreateTemplateStructure(
96 "issue22289",
97 { "issue22289-dir", "issue22289-backups", "other-dir" },
98 { "issue22289.kicad_pro", "issue22289.kicad_sch", "issue22289-dir/test.kicad_sym" } );
99
100 fs::path templatePath = m_tempDir / "issue22289";
101 fs::path destPath = m_tempDir / "myproject";
102 fs::create_directories( destPath );
103
104 PROJECT_TEMPLATE tmpl( wxString::FromUTF8( templatePath.string() ) );
105
106 // GetDestinationFiles expects a wxFileName with the project file path
107 wxFileName newProjectPath;
108 newProjectPath.SetPath( wxString::FromUTF8( destPath.string() ) );
109 newProjectPath.SetName( wxS( "myproject" ) );
110 newProjectPath.SetExt( wxS( "kicad_pro" ) );
111
112 std::vector<wxFileName> destFiles;
113 tmpl.GetDestinationFiles( newProjectPath, destFiles );
114
115 bool foundRenamedDir = false;
116 bool foundRenamedFile = false;
117 bool foundOtherDir = false;
118
119 for( const wxFileName& destFile : destFiles )
120 {
121 wxString fullPath = destFile.GetFullPath();
122
123 if( fullPath.Contains( wxS( "myproject-dir" ) ) )
124 foundRenamedDir = true;
125
126 if( fullPath.Contains( wxS( "issue22289-dir" ) ) )
127 BOOST_FAIL( "Directory should have been renamed from issue22289-dir to myproject-dir" );
128
129 if( fullPath.Contains( wxS( "other-dir" ) ) )
130 foundOtherDir = true;
131
132 if( destFile.GetName() == wxS( "myproject" ) && destFile.GetExt() == wxS( "kicad_pro" ) )
133 foundRenamedFile = true;
134 }
135
136 BOOST_CHECK_MESSAGE( foundRenamedDir, "Should find myproject-dir in destination files" );
137 BOOST_CHECK_MESSAGE( foundRenamedFile, "Should find myproject.kicad_pro in destination files" );
138 BOOST_CHECK_MESSAGE( foundOtherDir, "Should preserve other-dir (not matching template name)" );
139}
140
141
142BOOST_AUTO_TEST_CASE( CreateProjectRenamesDirectories )
143{
144 CreateTemplateStructure(
145 "testtemplate",
146 { "testtemplate-lib", "testtemplate" },
147 { "testtemplate.kicad_pro", "testtemplate-lib/component.kicad_sym",
148 "testtemplate/nested.txt" } );
149
150 fs::path templatePath = m_tempDir / "testtemplate";
151 fs::path destPath = m_tempDir / "newproject";
152 fs::create_directories( destPath );
153
154 PROJECT_TEMPLATE tmpl( wxString::FromUTF8( templatePath.string() ) );
155
156 // CreateProject expects a wxFileName with the project file path (including extension)
157 wxFileName newProjectPath;
158 newProjectPath.SetPath( wxString::FromUTF8( destPath.string() ) );
159 newProjectPath.SetName( wxS( "newproject" ) );
160 newProjectPath.SetExt( wxS( "kicad_pro" ) );
161
162 wxString errorMsg;
163 bool result = tmpl.CreateProject( newProjectPath, &errorMsg );
164
165 BOOST_CHECK_MESSAGE( result, "CreateProject should succeed: " + errorMsg.ToStdString() );
166
167 BOOST_CHECK( fs::exists( destPath / "newproject.kicad_pro" ) );
168 BOOST_CHECK( fs::exists( destPath / "newproject-lib" ) );
169 BOOST_CHECK( fs::exists( destPath / "newproject-lib" / "component.kicad_sym" ) );
170
171 BOOST_CHECK_MESSAGE( !fs::exists( destPath / "testtemplate-lib" ),
172 "Old directory name should not exist" );
173}
174
175
176BOOST_AUTO_TEST_CASE( ExactMatchDirectoryRenamed )
177{
178 // Test that a directory exactly matching the template name is renamed
179 CreateTemplateStructure( "mytemplate", { "mytemplate" },
180 { "mytemplate.kicad_pro", "mytemplate/subfile.txt" } );
181
182 fs::path templatePath = m_tempDir / "mytemplate";
183 fs::path destPath = m_tempDir / "finalproject";
184 fs::create_directories( destPath );
185
186 PROJECT_TEMPLATE tmpl( wxString::FromUTF8( templatePath.string() ) );
187
188 // GetDestinationFiles expects a wxFileName with the project file path
189 wxFileName newProjectPath;
190 newProjectPath.SetPath( wxString::FromUTF8( destPath.string() ) );
191 newProjectPath.SetName( wxS( "finalproject" ) );
192 newProjectPath.SetExt( wxS( "kicad_pro" ) );
193
194 std::vector<wxFileName> destFiles;
195 tmpl.GetDestinationFiles( newProjectPath, destFiles );
196
197 bool foundExactRenamedDir = false;
198
199 for( const wxFileName& destFile : destFiles )
200 {
201 wxString fullPath = destFile.GetFullPath();
202
203 if( fullPath.Contains( wxS( "/finalproject/finalproject/" ) )
204 || fullPath.Contains( wxS( "\\finalproject\\finalproject\\" ) ) )
205 {
206 foundExactRenamedDir = true;
207 }
208
209 if( fullPath.Contains( wxS( "/finalproject/mytemplate/" ) )
210 || fullPath.Contains( wxS( "\\finalproject\\mytemplate\\" ) ) )
211 {
212 BOOST_FAIL( "Exact match directory should be renamed from mytemplate to finalproject" );
213 }
214 }
215
216 BOOST_CHECK_MESSAGE( foundExactRenamedDir, "Should find renamed subdirectory finalproject" );
217}
218
219
void CreateTemplateStructure(const std::string &templateName, const std::vector< std::string > &subdirs, const std::vector< std::string > &files)
A class which provides project template functionality.
size_t GetDestinationFiles(const wxFileName &aNewProjectPath, std::vector< wxFileName > &aDestFiles)
Fetch the list of destination files to be copied when the new project is created.
bool CreateProject(wxFileName &aNewProjectPath, wxString *aErrorMsg=nullptr)
Copies and renames all template files to create a new project.
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_CASE(DirectoriesRenamedCorrectly)
wxString result
Test unit parsing edge cases and error handling.