KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_saveas_copy_subsheets.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 "eeschema_test_utils.h"
31
33#include <schematic.h>
34#include <sch_sheet.h>
35#include <sch_sheet_path.h>
37#include <wx/filename.h>
38#include <wx/ffile.h>
39#include <wx/filefn.h>
40
42{
43public:
45
46 wxFileName GetSchematicPath( const wxString& aRelativePath ) override;
47
48 wxFileName m_baseDir;
49 wxFileName m_srcDir;
50 wxFileName m_externalDir;
51};
52
54{
55 wxFileName tmp( wxFileName::CreateTempFileName( wxS( "saveas" ) ) );
56 wxString dirPath = tmp.GetPath(); // /tmp
57 wxString dirName = tmp.GetName(); // saveasXXXX
58 wxRemoveFile( tmp.GetFullPath() );
59
60 wxFileName base( dirPath, wxEmptyString ); // represents /tmp
61 base.AppendDir( dirName ); // now /tmp/saveasXXXX
62 base.Mkdir(); // create the directory
63 m_baseDir = base;
64
66 m_srcDir.AppendDir( wxS( "src" ) );
67 m_srcDir.Mkdir();
68
70 m_externalDir.AppendDir( wxS( "external" ) );
71 m_externalDir.Mkdir();
72
73 wxFileName dataDir( KI_TEST::GetEeschemaTestDataDir() );
74
75 wxCopyFile( dataDir.GetFullPath() + wxS( "/issue13212.kicad_sch" ),
76 m_srcDir.GetFullPath() + wxS( "/issue13212.kicad_sch" ) );
77 wxCopyFile( dataDir.GetFullPath() + wxS( "/issue13212_subsheet_1.kicad_sch" ),
78 m_srcDir.GetFullPath() + wxS( "/issue13212_subsheet_1.kicad_sch" ) );
79 wxCopyFile( dataDir.GetFullPath() + wxS( "/issue13212_subsheet_2.kicad_sch" ),
80 m_externalDir.GetFullPath() + wxS( "/issue13212_subsheet_2.kicad_sch" ) );
81
82 wxFileName rootFile( m_srcDir );
83 rootFile.SetFullName( wxS( "issue13212.kicad_sch" ) );
84
85 wxFFile file( rootFile.GetFullPath(), wxS( "rb" ) );
86 wxString content;
87 file.ReadAll( &content );
88 file.Close();
89 content.Replace( wxS( "issue13212_subsheet_2.kicad_sch" ),
90 wxS( "../external/issue13212_subsheet_2.kicad_sch" ) );
91 wxFFile outFile( rootFile.GetFullPath(), wxS( "wb" ) );
92 outFile.Write( content );
93 outFile.Close();
94}
95
96wxFileName SAVEAS_SUBSHEET_FIXTURE::GetSchematicPath( const wxString& aRelativePath )
97{
98 wxFileName fn( m_srcDir );
99 fn.SetName( aRelativePath );
101 return fn;
102}
103
104BOOST_FIXTURE_TEST_SUITE( SaveAsSubsheetCopy, SAVEAS_SUBSHEET_FIXTURE )
105
106BOOST_AUTO_TEST_CASE( CopyInternalReferenceExternal )
107{
108 LoadSchematic( wxS( "issue13212" ) );
109
110 SCH_SCREENS screens( m_schematic->Root() );
111 wxFileName srcRoot = GetSchematicPath( wxS( "issue13212" ) );
112
113 wxFileName destRoot( m_baseDir );
114 destRoot.AppendDir( wxS( "new" ) );
115 destRoot.AppendDir( wxS( "location" ) );
116 destRoot.SetName( wxS( "issue13212" ) );
117 destRoot.SetExt( FILEEXT::KiCadSchematicFileExtension );
118 { wxFileName destDir( destRoot ); destDir.SetFullName( wxEmptyString ); if( !destDir.DirExists() ) destDir.Mkdir( 0777, wxPATH_MKDIR_FULL ); }
119
120 std::unordered_map<SCH_SCREEN*, wxString> filenameMap;
121 wxString msg;
122
123 m_schematic->Root().SetFileName( destRoot.GetFullName() );
124 m_schematic->RootScreen()->SetFileName( destRoot.GetFullPath() );
125
126 bool ok = PrepareSaveAsFiles( *m_schematic, screens, srcRoot, destRoot, false, true,
127 false, filenameMap, msg );
128 BOOST_CHECK( ok );
129
130 SCH_SCREEN* internal = nullptr;
131 SCH_SCREEN* external = nullptr;
132
133 for( size_t i = 0; i < screens.GetCount(); i++ )
134 {
135 SCH_SCREEN* s = screens.GetScreen( i );
136
137 if( wxString( s->GetFileName() ).EndsWith( wxS( "issue13212_subsheet_1.kicad_sch" ) ) )
138 internal = s;
139 else if( wxString( s->GetFileName() ).EndsWith( wxS( "issue13212_subsheet_2.kicad_sch" ) ) )
140 external = s;
141 }
142
143 wxFileName internalExpected( destRoot.GetPath(), wxS( "issue13212_subsheet_1.kicad_sch" ) );
144 BOOST_CHECK_EQUAL( internal->GetFileName(), internalExpected.GetFullPath() );
145
146 wxFileName externalExpected( m_externalDir.GetFullPath(), wxS( "issue13212_subsheet_2.kicad_sch" ) );
147 BOOST_CHECK_EQUAL( external->GetFileName(), externalExpected.GetFullPath() );
148
149 SCH_SHEET_LIST sheetList = m_schematic->BuildSheetListSortedByPageNumbers();
150
151 wxString externalSheetPath;
152
153 for( const SCH_SHEET_PATH& path : sheetList )
154 {
155 if( path.Last()->GetFileName().Contains( wxS( "issue13212_subsheet_2" ) ) )
156 externalSheetPath = path.Last()->GetFileName();
157 }
158
159 BOOST_CHECK_EQUAL( externalSheetPath, wxS( "../../external/issue13212_subsheet_2.kicad_sch" ) );
160}
161
162BOOST_AUTO_TEST_CASE( CopyIncludingExternal )
163{
164 LoadSchematic( wxS( "issue13212" ) );
165
166 SCH_SCREENS screens( m_schematic->Root() );
167 wxFileName srcRoot = GetSchematicPath( wxS( "issue13212" ) );
168
169 wxFileName destRoot( m_baseDir );
170 destRoot.AppendDir( wxS( "destall" ) );
171 destRoot.SetName( wxS( "issue13212" ) );
172 destRoot.SetExt( FILEEXT::KiCadSchematicFileExtension );
173 { wxFileName destDir( destRoot ); destDir.SetFullName( wxEmptyString ); if( !destDir.DirExists() ) destDir.Mkdir( 0777, wxPATH_MKDIR_FULL ); }
174
175 std::unordered_map<SCH_SCREEN*, wxString> filenameMap;
176 wxString msg;
177
178 m_schematic->Root().SetFileName( destRoot.GetFullName() );
179 m_schematic->RootScreen()->SetFileName( destRoot.GetFullPath() );
180
181 bool ok = PrepareSaveAsFiles( *m_schematic, screens, srcRoot, destRoot, false, true,
182 true, filenameMap, msg );
183 BOOST_CHECK( ok );
184
185 SCH_SCREEN* external = nullptr;
186
187 for( size_t i = 0; i < screens.GetCount(); i++ )
188 {
189 SCH_SCREEN* s = screens.GetScreen( i );
190
191 if( wxString( s->GetFileName() ).EndsWith( wxS( "issue13212_subsheet_2.kicad_sch" ) ) )
192 external = s;
193 }
194
195 wxFileName externalExpected( destRoot.GetPath(), wxS( "issue13212_subsheet_2.kicad_sch" ) );
196 BOOST_CHECK_EQUAL( external->GetFileName(), externalExpected.GetFullPath() );
197}
198
199BOOST_AUTO_TEST_CASE( NoCopyKeepsOriginalPaths )
200{
201 LoadSchematic( wxS( "issue13212" ) );
202
203 SCH_SCREENS screens( m_schematic->Root() );
204 wxFileName srcRoot = GetSchematicPath( wxS( "issue13212" ) );
205
206 wxFileName destRoot( m_baseDir );
207 destRoot.AppendDir( wxS( "nocopy" ) );
208 destRoot.SetName( wxS( "issue13212" ) );
209 destRoot.SetExt( FILEEXT::KiCadSchematicFileExtension );
210 { wxFileName destDir( destRoot ); destDir.SetFullName( wxEmptyString ); if( !destDir.DirExists() ) destDir.Mkdir( 0777, wxPATH_MKDIR_FULL ); }
211
212 std::unordered_map<SCH_SCREEN*, wxString> filenameMap;
213 wxString msg;
214
215 m_schematic->Root().SetFileName( destRoot.GetFullName() );
216 m_schematic->RootScreen()->SetFileName( destRoot.GetFullPath() );
217
218 bool ok = PrepareSaveAsFiles( *m_schematic, screens, srcRoot, destRoot, false, false,
219 false, filenameMap, msg );
220 BOOST_CHECK( ok );
221
222 SCH_SCREEN* internal = nullptr;
223
224 for( size_t i = 0; i < screens.GetCount(); i++ )
225 {
226 SCH_SCREEN* s = screens.GetScreen( i );
227
228 if( wxString( s->GetFileName() ).EndsWith( wxS( "issue13212_subsheet_1.kicad_sch" ) ) )
229 internal = s;
230 }
231
232 wxFileName internalExpected( m_srcDir.GetFullPath(), wxS( "issue13212_subsheet_1.kicad_sch" ) );
233 BOOST_CHECK_EQUAL( internal->GetFileName(), internalExpected.GetFullPath() );
234}
235
A generic fixture for loading schematics and associated settings for qa tests.
wxFileName GetSchematicPath(const wxString &aRelativePath) override
Container class that holds multiple SCH_SCREEN objects in a hierarchy.
Definition sch_screen.h:758
SCH_SCREEN * GetScreen(unsigned int aIndex) const
size_t GetCount() const
Definition sch_screen.h:763
const wxString & GetFileName() const
Definition sch_screen.h:152
A container for handling SCH_SHEET_PATH objects in a flattened hierarchy.
Handle access to a stack of flattened SCH_SHEET objects by way of a path for creating a flattened sch...
bool PrepareSaveAsFiles(SCHEMATIC &aSchematic, SCH_SCREENS &aScreens, const wxFileName &aOldRoot, const wxFileName &aNewRoot, bool aSaveCopy, bool aCopySubsheets, bool aIncludeExternSheets, std::unordered_map< SCH_SCREEN *, wxString > &aFilenameMap, wxString &aErrorMsg)
static const std::string KiCadSchematicFileExtension
std::string GetEeschemaTestDataDir()
Get the configured location of Eeschema test data.
static void LoadSchematic(SCHEMATIC *aSchematic, SCH_SHEET *aRootSheet, const wxString &aFileName)
Definition of the SCH_SHEET_PATH and SCH_SHEET_LIST classes for Eeschema.
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_SUITE_END()
BOOST_AUTO_TEST_CASE(CopyInternalReferenceExternal)
BOOST_CHECK_EQUAL(result, "25.4")
Definition of file extensions used in Kicad.