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