29#include <wx/filename.h>
37namespace fs = std::filesystem;
45 m_tempDir = fs::temp_directory_path() /
"kicad_project_file_test";
69 fs::path oldProjectDir = m_tempDir /
"old_project";
70 fs::path newProjectDir = m_tempDir /
"new_project";
71 fs::create_directories( oldProjectDir );
72 fs::create_directories( newProjectDir );
74 wxString oldProjectPath = wxString( oldProjectDir.string() ) + wxFileName::GetPathSeparator()
82 sheetInfo.
name = wxS(
"old_project" );
83 sheetInfo.
filename = wxS(
"old_project.kicad_sch" );
90 customSheet.
name = wxS(
"CustomSheet" );
91 customSheet.
filename = wxS(
"custom_sheet.kicad_sch" );
96 projectFile.
SaveAs( wxString( newProjectDir.string() ), wxS(
"new_project" ) );
99 const std::vector<TOP_LEVEL_SHEET_INFO>& sheets = projectFile.
GetTopLevelSheets();
101 BOOST_REQUIRE_EQUAL( sheets.size(), 2 );
128 fs::path projectDir = m_tempDir /
"my_project";
129 fs::create_directories( projectDir );
132 std::string proContent = R
"({
134 "filename": "my_project.kicad_pro",
138 "top_level_sheets": [
140 "uuid": "00000000-0000-0000-0000-000000000000",
142 "filename": "default.kicad_sch"
148 fs::path proPath = projectDir / "my_project.kicad_pro";
149 std::ofstream proFile( proPath );
150 proFile << proContent;
154 fs::path schPath = projectDir /
"my_project.kicad_sch";
155 std::ofstream schFile( schPath );
156 schFile <<
"(kicad_sch (version 20231120) (generator \"eeschema\") (generator_version \"9.99\")";
157 schFile <<
" (uuid \"12345678-1234-1234-1234-123456789abc\")";
158 schFile <<
" (paper \"A4\"))";
163 settingsManager.
LoadProject( wxString( proPath.string() ) );
168 const std::vector<TOP_LEVEL_SHEET_INFO>& sheets = projectFile.
GetTopLevelSheets();
170 BOOST_REQUIRE_EQUAL( sheets.size(), 1 );
185 fs::path projectDir = m_tempDir /
"valid_project";
186 fs::create_directories( projectDir );
189 std::string proContent = R
"({
191 "filename": "valid_project.kicad_pro",
195 "top_level_sheets": [
197 "uuid": "00000000-0000-0000-0000-000000000000",
198 "name": "valid_project",
199 "filename": "valid_project.kicad_sch"
205 fs::path proPath = projectDir / "valid_project.kicad_pro";
206 std::ofstream proFile( proPath );
207 proFile << proContent;
211 fs::path schPath = projectDir /
"valid_project.kicad_sch";
212 std::ofstream schFile( schPath );
213 schFile <<
"(kicad_sch (version 20231120) (generator \"eeschema\") (generator_version \"9.99\")";
214 schFile <<
" (uuid \"12345678-1234-1234-1234-123456789abc\")";
215 schFile <<
" (paper \"A4\"))";
219 settingsManager.
LoadProject( wxString( proPath.string() ) );
224 const std::vector<TOP_LEVEL_SHEET_INFO>& sheets = projectFile.
GetTopLevelSheets();
226 BOOST_REQUIRE_EQUAL( sheets.size(), 1 );
249 fs::path projectDir = m_tempDir /
"jobset_project";
250 fs::create_directories( projectDir );
252 std::string proContent = R
"({
254 "filename": "jobset_project.kicad_pro",
259 fs::path proPath = projectDir / "jobset_project.kicad_pro";
260 std::ofstream proFile( proPath );
261 proFile << proContent;
264 wxFileName absFn( wxString( proPath.string() ) );
265 absFn.MakeAbsolute();
266 wxString absPath = absFn.GetFullPath();
281 BOOST_CHECK( afterReload == heldProject );
299 fs::path projADir = m_tempDir /
"proj_a";
300 fs::path projBDir = m_tempDir /
"proj_b";
301 fs::create_directories( projADir );
302 fs::create_directories( projBDir );
307 const std::string projectName =
"shared_name";
309 auto writeProject = [&](
const fs::path& aDir )
311 std::string content =
"{\n"
313 " \"filename\": \"" + projectName +
".kicad_pro\",\n"
316 " \"schematic\": {\n"
317 " \"top_level_sheets\": [\n"
319 " \"uuid\": \"00000000-0000-0000-0000-000000000000\",\n"
320 " \"name\": \"" + projectName +
"\",\n"
321 " \"filename\": \"" + projectName +
".kicad_sch\"\n"
326 std::ofstream out( aDir / ( projectName +
".kicad_pro" ) );
330 std::ofstream sch( aDir / ( projectName +
".kicad_sch" ) );
331 sch <<
"(kicad_sch (version 20231120) (generator \"eeschema\") (generator_version \"9.99\")";
332 sch <<
" (uuid \"12345678-1234-1234-1234-123456789abc\") (paper \"A4\"))";
336 fs::path proAPath = projADir / ( projectName +
".kicad_pro" );
337 fs::path proBPath = projBDir / ( projectName +
".kicad_pro" );
338 writeProject( projADir );
339 writeProject( projBDir );
358 auto readFile = [](
const fs::path& aPath )
360 std::ifstream in( aPath );
361 std::stringstream buffer;
362 buffer << in.rdbuf();
367 std::string savedB =
readFile( proBPath );
368 BOOST_CHECK_MESSAGE( savedB.find(
"OWNER" ) != std::string::npos,
369 "unloaded project must be saved to its own directory" );
372 std::string savedA =
readFile( proAPath );
373 BOOST_CHECK_MESSAGE( savedA.find(
"OWNER" ) == std::string::npos,
374 "active project's file must not receive the unloaded project's data" );
390 fs::path projectDir = m_tempDir /
"unchanged_project";
391 fs::create_directories( projectDir );
393 fs::path proPath = projectDir /
"unchanged_project.kicad_pro";
398 std::ofstream seed( proPath );
399 seed << R
"({"meta":{"version":3}})";
408 auto readFile = [](
const fs::path& aPath )
410 std::ifstream in( aPath );
411 std::stringstream buffer;
412 buffer << in.rdbuf();
421 nlohmann::json js = nlohmann::json::parse(
readFile( proPath ) );
422 js[
"board"].erase(
"ipc2581" );
423 js[
"schematic"].erase(
"bus_aliases" );
425 std::ofstream out( proPath );
426 out << std::setw( 2 ) << js << std::endl;
430 std::string before =
readFile( proPath );
431 BOOST_REQUIRE( before.find(
"ipc2581" ) == std::string::npos );
432 BOOST_REQUIRE( before.find(
"bus_aliases" ) == std::string::npos );
440 BOOST_CHECK( !projectFile.
SaveToFile( wxString( projectDir.string() ) ) );
~PROJECT_FILE_TEST_FIXTURE()
PROJECT_FILE_TEST_FIXTURE()
The backing store for a PROJECT, in JSON format.
std::map< wxString, wxString > m_TextVars
bool SaveAs(const wxString &aDirectory, const wxString &aFile)
std::vector< TOP_LEVEL_SHEET_INFO > & GetTopLevelSheets()
bool SaveToFile(const wxString &aDirectory="", bool aForce=false) override
Calls Store() and then writes the contents of the JSON document to a file.
Container for project specific data.
virtual const wxString GetProjectFullName() const
Return the full path and name of the project.
virtual PROJECT_FILE & GetProjectFile() const
bool SaveProject(const wxString &aFullPath=wxEmptyString, PROJECT *aProject=nullptr)
Save a loaded project.
bool LoadProject(const wxString &aFullPath, bool aSetActive=true)
Load a project or sets up a new project with a specified path.
PROJECT * GetProject(const wxString &aFullPath) const
Retrieve a loaded project by name.
bool UnloadProject(PROJECT *aProject, bool aSave=true)
Save, unload and unregister the given PROJECT.
PROJECT & Prj() const
A helper while we are not MDI-capable – return the one and only project.
static const std::string ProjectFileExtension
static bool readFile(const wxString &aFileName, wxString &aOut, size_t aLimit=0)
Read a file into aOut.
Information about a top-level schematic sheet.
KIID uuid
Unique identifier for the sheet.
wxString name
Display name for the sheet.
wxString filename
Relative path to the sheet file.
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_CASE(SaveAsUpdatesTopLevelSheetNames)
Test that SaveAs updates top-level sheet names when they match the old project name.
BOOST_CHECK_EQUAL(result, "25.4")
Definition of file extensions used in Kicad.