KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_project_file.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
33
34#include <filesystem>
35#include <fstream>
36
37namespace fs = std::filesystem;
38
39
41{
42public:
44 {
45 m_tempDir = fs::temp_directory_path() / "kicad_project_file_test";
46 fs::remove_all( m_tempDir );
47 fs::create_directories( m_tempDir );
48 }
49
51 {
52 fs::remove_all( m_tempDir );
53 }
54
55 fs::path m_tempDir;
56};
57
58
59BOOST_FIXTURE_TEST_SUITE( ProjectFile, PROJECT_FILE_TEST_FIXTURE )
60
61
62
67BOOST_AUTO_TEST_CASE( SaveAsUpdatesTopLevelSheetNames )
68{
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 );
73
74 wxString oldProjectPath = wxString( oldProjectDir.string() ) + wxFileName::GetPathSeparator()
75 + wxS( "old_project." ) + FILEEXT::ProjectFileExtension;
76
77 PROJECT_FILE projectFile( oldProjectPath );
78
79 // Add a top-level sheet with name matching the project name
80 TOP_LEVEL_SHEET_INFO sheetInfo;
81 sheetInfo.uuid = KIID();
82 sheetInfo.name = wxS( "old_project" );
83 sheetInfo.filename = wxS( "old_project.kicad_sch" );
84
85 projectFile.GetTopLevelSheets().push_back( sheetInfo );
86
87 // Add another sheet with a custom name that should NOT be changed
88 TOP_LEVEL_SHEET_INFO customSheet;
89 customSheet.uuid = KIID();
90 customSheet.name = wxS( "CustomSheet" );
91 customSheet.filename = wxS( "custom_sheet.kicad_sch" );
92
93 projectFile.GetTopLevelSheets().push_back( customSheet );
94
95 // Perform SaveAs to new project
96 projectFile.SaveAs( wxString( newProjectDir.string() ), wxS( "new_project" ) );
97
98 // Verify the sheet name was updated
99 const std::vector<TOP_LEVEL_SHEET_INFO>& sheets = projectFile.GetTopLevelSheets();
100
101 BOOST_REQUIRE_EQUAL( sheets.size(), 2 );
102
103 // First sheet's name should be updated to match the new project name
104 BOOST_CHECK_EQUAL( sheets[0].name, wxS( "new_project" ) );
105
106 // First sheet's filename should also be updated
107 BOOST_CHECK_EQUAL( sheets[0].filename, wxS( "new_project.kicad_sch" ) );
108
109 // Second sheet's custom name should remain unchanged
110 BOOST_CHECK_EQUAL( sheets[1].name, wxS( "CustomSheet" ) );
111
112 // Second sheet's filename should remain unchanged
113 BOOST_CHECK_EQUAL( sheets[1].filename, wxS( "custom_sheet.kicad_sch" ) );
114}
115
116
126BOOST_AUTO_TEST_CASE( LoadFixesStaleTopLevelSheetReferences )
127{
128 fs::path projectDir = m_tempDir / "my_project";
129 fs::create_directories( projectDir );
130
131 // Write a .kicad_pro that references "default.kicad_sch" (as if copied from a template)
132 std::string proContent = R"({
133 "meta": {
134 "filename": "my_project.kicad_pro",
135 "version": 3
136 },
137 "schematic": {
138 "top_level_sheets": [
139 {
140 "uuid": "00000000-0000-0000-0000-000000000000",
141 "name": "default",
142 "filename": "default.kicad_sch"
143 }
144 ]
145 }
146 })";
147
148 fs::path proPath = projectDir / "my_project.kicad_pro";
149 std::ofstream proFile( proPath );
150 proFile << proContent;
151 proFile.close();
152
153 // Create the renamed schematic file (as if the template copy renamed it)
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\"))";
159 schFile.close();
160
161 // Load the project using SETTINGS_MANAGER
162 SETTINGS_MANAGER settingsManager;
163 settingsManager.LoadProject( wxString( proPath.string() ) );
164
165 PROJECT& project = settingsManager.Prj();
166 PROJECT_FILE& projectFile = project.GetProjectFile();
167
168 const std::vector<TOP_LEVEL_SHEET_INFO>& sheets = projectFile.GetTopLevelSheets();
169
170 BOOST_REQUIRE_EQUAL( sheets.size(), 1 );
171
172 // The filename should have been corrected from "default.kicad_sch" to "my_project.kicad_sch"
173 BOOST_CHECK_EQUAL( sheets[0].filename, wxS( "my_project.kicad_sch" ) );
174
175 // The name should also be updated
176 BOOST_CHECK_EQUAL( sheets[0].name, wxS( "my_project" ) );
177}
178
179
183BOOST_AUTO_TEST_CASE( LoadPreservesValidTopLevelSheetReferences )
184{
185 fs::path projectDir = m_tempDir / "valid_project";
186 fs::create_directories( projectDir );
187
188 // Write a .kicad_pro with valid references
189 std::string proContent = R"({
190 "meta": {
191 "filename": "valid_project.kicad_pro",
192 "version": 3
193 },
194 "schematic": {
195 "top_level_sheets": [
196 {
197 "uuid": "00000000-0000-0000-0000-000000000000",
198 "name": "valid_project",
199 "filename": "valid_project.kicad_sch"
200 }
201 ]
202 }
203 })";
204
205 fs::path proPath = projectDir / "valid_project.kicad_pro";
206 std::ofstream proFile( proPath );
207 proFile << proContent;
208 proFile.close();
209
210 // Create the schematic file that matches the reference
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\"))";
216 schFile.close();
217
218 SETTINGS_MANAGER settingsManager;
219 settingsManager.LoadProject( wxString( proPath.string() ) );
220
221 PROJECT& project = settingsManager.Prj();
222 PROJECT_FILE& projectFile = project.GetProjectFile();
223
224 const std::vector<TOP_LEVEL_SHEET_INFO>& sheets = projectFile.GetTopLevelSheets();
225
226 BOOST_REQUIRE_EQUAL( sheets.size(), 1 );
227
228 // References should be unchanged
229 BOOST_CHECK_EQUAL( sheets[0].filename, wxS( "valid_project.kicad_sch" ) );
230 BOOST_CHECK_EQUAL( sheets[0].name, wxS( "valid_project" ) );
231}
232
233
const char * name
Definition kiid.h:49
The backing store for a PROJECT, in JSON format.
bool SaveAs(const wxString &aDirectory, const wxString &aFile)
std::vector< TOP_LEVEL_SHEET_INFO > & GetTopLevelSheets()
Container for project specific data.
Definition project.h:65
bool LoadProject(const wxString &aFullPath, bool aSetActive=true)
Load a project or sets up a new project with a specified path.
PROJECT & Prj() const
A helper while we are not MDI-capable – return the one and only project.
static const std::string ProjectFileExtension
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_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.