30#include <wx/filename.h>
37namespace fs = std::filesystem;
46 m_tempDir = fs::temp_directory_path() /
"kicad_template_test";
58 const std::vector<std::string>& subdirs,
59 const std::vector<std::string>& files )
61 fs::path templatePath =
m_tempDir / templateName;
62 fs::create_directories( templatePath );
64 fs::path metaPath = templatePath /
"meta";
65 fs::create_directories( metaPath );
67 std::ofstream infoFile( ( metaPath /
"info.html" ).
string() );
68 infoFile <<
"<html><head><title>Test Template</title></head><body></body></html>";
71 for(
const auto& subdir : subdirs )
73 fs::create_directories( templatePath / subdir );
76 for(
const auto& file : files )
78 fs::path filePath = templatePath / file;
79 fs::create_directories( filePath.parent_path() );
80 std::ofstream f( filePath.string() );
97 CreateTemplateStructure(
99 {
"issue22289-dir",
"issue22289-backups",
"other-dir" },
100 {
"issue22289.kicad_pro",
"issue22289.kicad_sch",
"issue22289-dir/test.kicad_sym" } );
102 fs::path templatePath = m_tempDir /
"issue22289";
103 fs::path destPath = m_tempDir /
"myproject";
104 fs::create_directories( destPath );
109 wxFileName newProjectPath;
110 newProjectPath.SetPath( wxString::FromUTF8( destPath.string() ) );
111 newProjectPath.SetName( wxS(
"myproject" ) );
112 newProjectPath.SetExt( wxS(
"kicad_pro" ) );
114 std::vector<wxFileName> destFiles;
117 bool foundRenamedDir =
false;
118 bool foundRenamedFile =
false;
119 bool foundOtherDir =
false;
121 for(
const wxFileName& destFile : destFiles )
123 wxString fullPath = destFile.GetFullPath();
125 if( fullPath.Contains( wxS(
"myproject-dir" ) ) )
126 foundRenamedDir =
true;
128 if( fullPath.Contains( wxS(
"issue22289-dir" ) ) )
129 BOOST_FAIL(
"Directory should have been renamed from issue22289-dir to myproject-dir" );
131 if( fullPath.Contains( wxS(
"other-dir" ) ) )
132 foundOtherDir =
true;
134 if( destFile.GetName() == wxS(
"myproject" ) && destFile.GetExt() == wxS(
"kicad_pro" ) )
135 foundRenamedFile =
true;
138 BOOST_CHECK_MESSAGE( foundRenamedDir,
"Should find myproject-dir in destination files" );
139 BOOST_CHECK_MESSAGE( foundRenamedFile,
"Should find myproject.kicad_pro in destination files" );
140 BOOST_CHECK_MESSAGE( foundOtherDir,
"Should preserve other-dir (not matching template name)" );
146 CreateTemplateStructure(
148 {
"testtemplate-lib",
"testtemplate" },
149 {
"testtemplate.kicad_pro",
"testtemplate-lib/component.kicad_sym",
150 "testtemplate/nested.txt" } );
152 fs::path templatePath = m_tempDir /
"testtemplate";
153 fs::path destPath = m_tempDir /
"newproject";
154 fs::create_directories( destPath );
159 wxFileName newProjectPath;
160 newProjectPath.SetPath( wxString::FromUTF8( destPath.string() ) );
161 newProjectPath.SetName( wxS(
"newproject" ) );
162 newProjectPath.SetExt( wxS(
"kicad_pro" ) );
167 BOOST_CHECK_MESSAGE(
result,
"CreateProject should succeed: " + errorMsg.ToStdString() );
169 BOOST_CHECK( fs::exists( destPath /
"newproject.kicad_pro" ) );
170 BOOST_CHECK( fs::exists( destPath /
"newproject-lib" ) );
171 BOOST_CHECK( fs::exists( destPath /
"newproject-lib" /
"component.kicad_sym" ) );
173 BOOST_CHECK_MESSAGE( !fs::exists( destPath /
"testtemplate-lib" ),
174 "Old directory name should not exist" );
181 CreateTemplateStructure(
"mytemplate", {
"mytemplate" },
182 {
"mytemplate.kicad_pro",
"mytemplate/subfile.txt" } );
184 fs::path templatePath = m_tempDir /
"mytemplate";
185 fs::path destPath = m_tempDir /
"finalproject";
186 fs::create_directories( destPath );
191 wxFileName newProjectPath;
192 newProjectPath.SetPath( wxString::FromUTF8( destPath.string() ) );
193 newProjectPath.SetName( wxS(
"finalproject" ) );
194 newProjectPath.SetExt( wxS(
"kicad_pro" ) );
196 std::vector<wxFileName> destFiles;
199 bool foundExactRenamedDir =
false;
201 for(
const wxFileName& destFile : destFiles )
203 wxString fullPath = destFile.GetFullPath();
205 if( fullPath.Contains( wxS(
"/finalproject/finalproject/" ) )
206 || fullPath.Contains( wxS(
"\\finalproject\\finalproject\\" ) ) )
208 foundExactRenamedDir =
true;
211 if( fullPath.Contains( wxS(
"/finalproject/mytemplate/" ) )
212 || fullPath.Contains( wxS(
"\\finalproject\\mytemplate\\" ) ) )
214 BOOST_FAIL(
"Exact match directory should be renamed from mytemplate to finalproject" );
218 BOOST_CHECK_MESSAGE( foundExactRenamedDir,
"Should find renamed subdirectory finalproject" );
228 fs::path noMetaPath = m_tempDir /
"no_meta_template";
229 fs::create_directories( noMetaPath );
231 std::ofstream f( ( noMetaPath /
"no_meta_template.kicad_pro" ).
string() );
239 BOOST_CHECK_MESSAGE( !title->IsEmpty(),
"Template without meta dir should have an error title" );
251 fs::path tmplPath = m_tempDir /
"meta_no_html";
252 fs::create_directories( tmplPath /
"meta" );
254 std::ofstream f( ( tmplPath /
"meta_no_html.kicad_pro" ).
string() );
262 BOOST_CHECK_MESSAGE( !title->IsEmpty(),
263 "Template with meta dir but no info.html should have an error title" );
272 wxString baseDir = wxString::FromUTF8( ( m_tempDir /
"default_seed" ).
string() );
279 fs::path
expected = m_tempDir /
"default_seed" /
"default";
288 BOOST_CHECK_MESSAGE( tmpl.
GetError().IsEmpty(),
289 "Seeded default template should load without error: " + tmpl.
GetError() );
300 wxString baseDir = wxString::FromUTF8( ( m_tempDir /
"default_load" ).
string() );
305 fs::path destPath = m_tempDir /
"fromdefault";
306 fs::create_directories( destPath );
310 wxFileName newProjectPath;
311 newProjectPath.SetPath( wxString::FromUTF8( destPath.string() ) );
312 newProjectPath.SetName( wxS(
"fromdefault" ) );
313 newProjectPath.SetExt( wxS(
"kicad_pro" ) );
318 "CreateProject should succeed: " + errorMsg.ToStdString() );
322 BOOST_CHECK_MESSAGE( mgr.
LoadProject( newProjectPath.GetFullPath() ),
323 "A project made from the default template must load" );
339 wxString baseDir = wxString::FromUTF8( ( m_tempDir /
"default_idempotent" ).
string() );
345 fs::path proFile = m_tempDir /
"default_idempotent" /
"default" /
"default.kicad_pro";
347 std::ofstream f( proFile.string() );
348 f <<
"{\"custom\":true}";
355 std::ifstream in( proFile.string() );
356 std::stringstream buffer;
357 buffer << in.rdbuf();
365 CreateTemplateStructure(
"rotemplate", {
"rotemplate-lib" },
366 {
"rotemplate.kicad_pro",
"rotemplate.kicad_sch",
"rotemplate-lib/component.kicad_sym" } );
368 fs::path templatePath = m_tempDir /
"rotemplate";
369 fs::path destPath = m_tempDir /
"rwproject";
370 fs::create_directories( destPath );
372 const std::vector<fs::path> templateFiles = { templatePath /
"rotemplate.kicad_pro",
373 templatePath /
"rotemplate.kicad_sch",
374 templatePath /
"rotemplate-lib" /
"component.kicad_sym" };
376 for(
const fs::path& file : templateFiles )
378 fs::permissions( file, fs::perms::owner_write | fs::perms::group_write | fs::perms::others_write,
379 fs::perm_options::remove );
384 wxFileName newProjectPath;
385 newProjectPath.SetPath( wxString::FromUTF8( destPath.string() ) );
386 newProjectPath.SetName( wxS(
"rwproject" ) );
387 newProjectPath.SetExt( wxS(
"kicad_pro" ) );
390 bool created = tmpl.
CreateProject( newProjectPath, &errorMsg );
394 for(
const fs::path& file : templateFiles )
395 fs::permissions( file, fs::perms::owner_write, fs::perm_options::add );
399 const std::vector<fs::path> projectFiles = { destPath /
"rwproject.kicad_pro", destPath /
"rwproject.kicad_sch",
400 destPath /
"rwproject-lib" /
"component.kicad_sym" };
402 for(
const fs::path& file : projectFiles )
404 wxFileName fn( wxString::FromUTF8( file.string() ) );
407 BOOST_CHECK_MESSAGE( fn.IsFileWritable(), file.string() +
" is read only" );
420 std::vector<ENTRY> entries = { { wxS(
"STM32F407" ),
false },
421 { wxS(
"Default" ),
false },
422 { wxS(
"Motor_Driver" ),
true },
423 { wxS(
"Arduino_Mega" ),
false },
424 { wxS(
"amplifier" ),
true } };
426 std::sort( entries.begin(), entries.end(),
427 [](
const ENTRY& a,
const ENTRY& b )
429 return TemplateListLess( a.title, a.pinned, b.title, b.pinned );
432 std::vector<wxString> titles;
434 for(
const ENTRY& entry : entries )
435 titles.push_back( entry.title );
437 const std::vector<wxString>
expected = { wxS(
"amplifier" ), wxS(
"Motor_Driver" ), wxS(
"Default" ),
438 wxS(
"Arduino_Mega" ), wxS(
"STM32F407" ) };
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
bool LoadProject(const wxString &aFullPath, bool aSetActive=true, bool aOverrideLock=false)
Load a project or sets up a new project with a specified path.
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_CHECK(res.column_datatype(0)==SQL_NUMERIC||res.column_datatype(0)==SQL_DECIMAL)
BOOST_CHECK_EQUAL(std::any_cast< std::string >(rows[0].at("val")), "12345678901234567.89")
BOOST_REQUIRE_MESSAGE(dc.CacheTableInfo(table, { "part_id", "val" }), dc.GetLastError())
BOOST_REQUIRE(res.columns()==1)
BOOST_AUTO_TEST_SUITE_END()
VECTOR3I expected(15, 30, 45)
BOOST_AUTO_TEST_CASE(DirectoriesRenamedCorrectly)
wxString result
Test unit parsing edge cases and error handling.