29#include <wx/filename.h>
35namespace fs = std::filesystem;
43 m_tempDir = fs::temp_directory_path() /
"kicad_template_test";
54 const std::vector<std::string>& subdirs,
55 const std::vector<std::string>& files )
57 fs::path templatePath =
m_tempDir / templateName;
58 fs::create_directories( templatePath );
60 fs::path metaPath = templatePath /
"meta";
61 fs::create_directories( metaPath );
63 std::ofstream infoFile( ( metaPath /
"info.html" ).
string() );
64 infoFile <<
"<html><head><title>Test Template</title></head><body></body></html>";
67 for(
const auto& subdir : subdirs )
69 fs::create_directories( templatePath / subdir );
72 for(
const auto& file : files )
74 fs::path filePath = templatePath / file;
75 fs::create_directories( filePath.parent_path() );
76 std::ofstream f( filePath.string() );
92 CreateTemplateStructure(
94 {
"issue22289-dir",
"issue22289-backups",
"other-dir" },
95 {
"issue22289.kicad_pro",
"issue22289.kicad_sch",
"issue22289-dir/test.kicad_sym" } );
97 fs::path templatePath = m_tempDir /
"issue22289";
98 fs::path destPath = m_tempDir /
"myproject";
99 fs::create_directories( destPath );
104 wxFileName newProjectPath;
105 newProjectPath.SetPath( wxString::FromUTF8( destPath.string() ) );
106 newProjectPath.SetName( wxS(
"myproject" ) );
107 newProjectPath.SetExt( wxS(
"kicad_pro" ) );
109 std::vector<wxFileName> destFiles;
112 bool foundRenamedDir =
false;
113 bool foundRenamedFile =
false;
114 bool foundOtherDir =
false;
116 for(
const wxFileName& destFile : destFiles )
118 wxString fullPath = destFile.GetFullPath();
120 if( fullPath.Contains( wxS(
"myproject-dir" ) ) )
121 foundRenamedDir =
true;
123 if( fullPath.Contains( wxS(
"issue22289-dir" ) ) )
124 BOOST_FAIL(
"Directory should have been renamed from issue22289-dir to myproject-dir" );
126 if( fullPath.Contains( wxS(
"other-dir" ) ) )
127 foundOtherDir =
true;
129 if( destFile.GetName() == wxS(
"myproject" ) && destFile.GetExt() == wxS(
"kicad_pro" ) )
130 foundRenamedFile =
true;
134 BOOST_CHECK_MESSAGE( foundRenamedFile,
"Should find myproject.kicad_pro in destination files" );
135 BOOST_CHECK_MESSAGE( foundOtherDir,
"Should preserve other-dir (not matching template name)" );
141 CreateTemplateStructure(
143 {
"testtemplate-lib",
"testtemplate" },
144 {
"testtemplate.kicad_pro",
"testtemplate-lib/component.kicad_sym",
145 "testtemplate/nested.txt" } );
147 fs::path templatePath = m_tempDir /
"testtemplate";
148 fs::path destPath = m_tempDir /
"newproject";
149 fs::create_directories( destPath );
154 wxFileName newProjectPath;
155 newProjectPath.SetPath( wxString::FromUTF8( destPath.string() ) );
156 newProjectPath.SetName( wxS(
"newproject" ) );
157 newProjectPath.SetExt( wxS(
"kicad_pro" ) );
164 BOOST_CHECK( fs::exists( destPath /
"newproject.kicad_pro" ) );
165 BOOST_CHECK( fs::exists( destPath /
"newproject-lib" ) );
166 BOOST_CHECK( fs::exists( destPath /
"newproject-lib" /
"component.kicad_sym" ) );
169 "Old directory name should not exist" );
176 CreateTemplateStructure(
"mytemplate", {
"mytemplate" },
177 {
"mytemplate.kicad_pro",
"mytemplate/subfile.txt" } );
179 fs::path templatePath = m_tempDir /
"mytemplate";
180 fs::path destPath = m_tempDir /
"finalproject";
181 fs::create_directories( destPath );
186 wxFileName newProjectPath;
187 newProjectPath.SetPath( wxString::FromUTF8( destPath.string() ) );
188 newProjectPath.SetName( wxS(
"finalproject" ) );
189 newProjectPath.SetExt( wxS(
"kicad_pro" ) );
191 std::vector<wxFileName> destFiles;
194 bool foundExactRenamedDir =
false;
196 for(
const wxFileName& destFile : destFiles )
198 wxString fullPath = destFile.GetFullPath();
200 if( fullPath.Contains( wxS(
"/finalproject/finalproject/" ) )
201 || fullPath.Contains( wxS(
"\\finalproject\\finalproject\\" ) ) )
203 foundExactRenamedDir =
true;
206 if( fullPath.Contains( wxS(
"/finalproject/mytemplate/" ) )
207 || fullPath.Contains( wxS(
"\\finalproject\\mytemplate\\" ) ) )
209 BOOST_FAIL(
"Exact match directory should be renamed from mytemplate to finalproject" );
213 BOOST_CHECK_MESSAGE( foundExactRenamedDir,
"Should find renamed subdirectory finalproject" );
223 fs::path noMetaPath = m_tempDir /
"no_meta_template";
224 fs::create_directories( noMetaPath );
226 std::ofstream f( ( noMetaPath /
"no_meta_template.kicad_pro" ).
string() );
234 BOOST_CHECK_MESSAGE( !title->IsEmpty(),
"Template without meta dir should have an error title" );
239 BOOST_CHECK( *title == *title2 );
246 fs::path tmplPath = m_tempDir /
"meta_no_html";
247 fs::create_directories( tmplPath /
"meta" );
249 std::ofstream f( ( tmplPath /
"meta_no_html.kicad_pro" ).
string() );
258 "Template with meta dir but no info.html should have an error title" );
267 wxString baseDir = wxString::FromUTF8( ( m_tempDir /
"default_seed" ).
string() );
270 BOOST_REQUIRE_MESSAGE( seeded.IsOk(),
"Seeding the default template should succeed" );
274 fs::path
expected = m_tempDir /
"default_seed" /
"default";
275 BOOST_CHECK( fs::exists(
expected ) );
276 BOOST_CHECK( fs::exists(
expected /
"meta" /
"info.html" ) );
277 BOOST_CHECK( fs::exists(
expected /
"default.kicad_pro" ) );
284 "Seeded default template should load without error: " + tmpl.
GetError() );
289 BOOST_CHECK( !title->IsEmpty() );
298 BOOST_CHECK( !seeded.IsOk() );
305 wxString baseDir = wxString::FromUTF8( ( m_tempDir /
"default_idempotent" ).
string() );
311 fs::path proFile = m_tempDir /
"default_idempotent" /
"default" /
"default.kicad_pro";
313 std::ofstream f( proFile.string() );
314 f <<
"{\"custom\":true}";
321 std::ifstream in( proFile.string() );
322 std::stringstream buffer;
323 buffer << in.rdbuf();
PROJECT_TEMPLATE_TEST_FIXTURE()
void CreateTemplateStructure(const std::string &templateName, const std::vector< std::string > &subdirs, const std::vector< std::string > &files)
~PROJECT_TEMPLATE_TEST_FIXTURE()
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.
wxString * GetTitle()
Get the title of the project (extracted from the html title tag)
bool CreateProject(wxFileName &aNewProjectPath, wxString *aErrorMsg=nullptr)
Copies and renames all template files to create a new project.
const wxString & GetError() const
wxFileName EnsureDefaultProjectTemplate(const wxString &aBaseDir)
Seed the built-in "default" project template under aBaseDir, creating the directory tree and minimal ...
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
BOOST_AUTO_TEST_SUITE_END()
VECTOR3I expected(15, 30, 45)
BOOST_CHECK_MESSAGE(totalMismatches==0, std::to_string(totalMismatches)+" board(s) with strategy disagreements")
BOOST_AUTO_TEST_CASE(DirectoriesRenamedCorrectly)
wxString result
Test unit parsing edge cases and error handling.
BOOST_CHECK_EQUAL(result, "25.4")