KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_step_export_modified_board.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
24/*
25 * Regression test for https://gitlab.com/kicad/code/kicad/-/issues/24061 (and its duplicate
26 * https://gitlab.com/kicad/code/kicad/-/issues/23704).
27 *
28 * The 3D/STEP export runs kicad-cli as a child process that reads the board from disk. A board
29 * with unsaved edits (the user deletes panel sections without saving) therefore exported its stale
30 * on-disk contents instead of what the user sees.
31 *
32 * DIALOG_EXPORT_STEP::StageBoardForExport bridges the gap by serializing the live board to a
33 * temporary file when it has unsaved modifications. This test deletes footprints in memory, runs
34 * that helper, and verifies the staged file reflects the deletions while the original on-disk file
35 * is left untouched.
36 */
37
38#include <filesystem>
39#include <fstream>
40#include <vector>
41
43#include <boost/test/unit_test.hpp>
44
47#include <board.h>
48#include <footprint.h>
49
50#include <wx/filefn.h>
51#include <wx/filename.h>
52#include <wx/string.h>
53
54
55namespace
56{
59class SCOPED_TEMP_DIR
60{
61public:
62 SCOPED_TEMP_DIR( const wxString& aPrefix )
63 {
64 wxString reserved = wxFileName::CreateTempFileName( aPrefix );
65 BOOST_REQUIRE( !reserved.IsEmpty() );
66 BOOST_REQUIRE( wxRemoveFile( reserved ) );
67
68 m_path = std::filesystem::path( std::string( reserved.utf8_str() ) );
69 std::filesystem::create_directory( m_path );
70 }
71
72 ~SCOPED_TEMP_DIR()
73 {
74 std::error_code ec;
75 std::filesystem::remove_all( m_path, ec );
76 }
77
78 const std::filesystem::path& Path() const { return m_path; }
79
80private:
81 std::filesystem::path m_path;
82};
83} // namespace
84
85
86BOOST_AUTO_TEST_SUITE( StepExportModifiedBoard )
87
88
89BOOST_AUTO_TEST_CASE( ModifiedBoardStagesCurrentState )
90{
91 const std::string sourceBoard = KI_TEST::GetPcbnewTestDataDir() + "issue23704/issue23704.kicad_pcb";
92
93 BOOST_REQUIRE_MESSAGE( std::filesystem::exists( sourceBoard ), "Missing test board " << sourceBoard );
94
95 std::unique_ptr<BOARD> board = KI_TEST::ReadBoardFromFileOrStream( sourceBoard );
96 BOOST_REQUIRE( board );
97
98 const size_t originalCount = board->Footprints().size();
99 BOOST_REQUIRE_MESSAGE( originalCount > 1, "Panel board needs multiple footprints to delete" );
100
101 // Stage the board's "saved" copy in an isolated temp directory so the helper writes its
102 // temporary export file alongside it rather than into the shared test data tree.
103 const SCOPED_TEMP_DIR workDir( wxS( "kicad_step_export_23704_" ) );
104 const std::filesystem::path onDisk = workDir.Path() / "panel.kicad_pcb";
105 KI_TEST::DumpBoardToFile( *board, onDisk.string() );
106 const wxString onDiskPath = wxString::FromUTF8( onDisk.string() );
107
108 // A sibling project file must be staged too, so project-relative model and library paths
109 // resolve identically during export.
110 std::filesystem::path projectFile = onDisk;
111 projectFile.replace_extension( ".kicad_pro" );
112
113 {
114 std::ofstream project( projectFile );
116 project << "{ \"meta\": { \"version\": 1 } }\n";
117 }
118
119 // The user deletes every panel section but one, never saving the file.
120 while( board->Footprints().size() > 1 )
121 board->Remove( board->Footprints().back() );
122
123 const size_t remainingCount = board->Footprints().size();
124 BOOST_REQUIRE_EQUAL( remainingCount, 1u );
125
126 // Drive the production helper exactly as the export dialog does for a modified board.
127 wxString inputPath;
128 std::vector<wxString> tempFiles;
129 wxString errorDetail;
130 const wxString error =
131 DIALOG_EXPORT_STEP::StageBoardForExport( onDiskPath, true, board.get(), inputPath, tempFiles, errorDetail );
132
133 BOOST_REQUIRE_MESSAGE( error.IsEmpty(), "StageBoardForExport failed: " << error.ToStdString() );
134 BOOST_REQUIRE( wxFileExists( inputPath ) );
135 BOOST_CHECK_MESSAGE( inputPath != onDiskPath, "Modified board must stage a temporary copy, not the on-disk file" );
136
137 std::unique_ptr<BOARD> staged = KI_TEST::ReadBoardFromFileOrStream( std::string( inputPath.utf8_str() ) );
138 BOOST_REQUIRE( staged );
139
140 // The staged file reflects the in-memory deletions, not the full on-disk panel.
141 BOOST_CHECK_EQUAL( staged->Footprints().size(), remainingCount );
142
143 // The sibling project file is staged next to the temporary board.
144 std::filesystem::path stagedProject = std::filesystem::path( std::string( inputPath.utf8_str() ) );
145 stagedProject.replace_extension( ".kicad_pro" );
146 BOOST_CHECK( std::filesystem::exists( stagedProject ) );
147 BOOST_CHECK_EQUAL( std::filesystem::file_size( stagedProject ), std::filesystem::file_size( projectFile ) );
148
149 // The original on-disk file is never mutated by staging.
150 std::unique_ptr<BOARD> onDiskReloaded = KI_TEST::ReadBoardFromFileOrStream( onDisk.string() );
151 BOOST_REQUIRE( onDiskReloaded );
152 BOOST_CHECK_EQUAL( onDiskReloaded->Footprints().size(), originalCount );
153
154 for( const wxString& f : tempFiles )
155 {
156 if( wxFileExists( f ) )
157 wxRemoveFile( f );
158 }
159}
160
161
165BOOST_AUTO_TEST_CASE( UnmodifiedBoardStagesOnDiskFile )
166{
167 const std::string sourceBoard = KI_TEST::GetPcbnewTestDataDir() + "issue23704/issue23704.kicad_pcb";
168
169 std::unique_ptr<BOARD> board = KI_TEST::ReadBoardFromFileOrStream( sourceBoard );
170 BOOST_REQUIRE( board );
171
172 const wxString onDiskPath = wxString::FromUTF8( sourceBoard );
173
174 wxString inputPath;
175 std::vector<wxString> tempFiles;
176 wxString errorDetail;
177 const wxString error = DIALOG_EXPORT_STEP::StageBoardForExport( onDiskPath, false, board.get(), inputPath,
178 tempFiles, errorDetail );
179
180 BOOST_CHECK( error.IsEmpty() );
181 BOOST_CHECK_EQUAL( inputPath, onDiskPath );
182 BOOST_CHECK( tempFiles.empty() );
183}
184
185
189BOOST_AUTO_TEST_CASE( UnsavedBoardIsRejected )
190{
191 const std::string sourceBoard = KI_TEST::GetPcbnewTestDataDir() + "issue23704/issue23704.kicad_pcb";
192
193 std::unique_ptr<BOARD> board = KI_TEST::ReadBoardFromFileOrStream( sourceBoard );
194 BOOST_REQUIRE( board );
195
196 wxString inputPath;
197 std::vector<wxString> tempFiles;
198 wxString errorDetail;
199 const wxString error = DIALOG_EXPORT_STEP::StageBoardForExport( wxEmptyString, true, board.get(), inputPath,
200 tempFiles, errorDetail );
201
202 BOOST_CHECK( !error.IsEmpty() );
203 BOOST_CHECK( inputPath.IsEmpty() );
204 BOOST_CHECK( tempFiles.empty() );
205}
206
207
General utilities for PCB file IO for QA programs.
static wxString StageBoardForExport(const wxString &aBoardPath, bool aContentModified, BOARD *aBoard, wxString &aInputPath, std::vector< wxString > &aTempFiles, wxString &aErrorDetail)
Resolve the board file to hand to the external 3D/STEP exporter.
std::string GetPcbnewTestDataDir()
Utility which returns a path to the data directory where the test board files are stored.
std::unique_ptr< BOARD > ReadBoardFromFileOrStream(const std::string &aFilename, std::istream &aFallback)
Read a board from a file, or another stream, as appropriate.
void DumpBoardToFile(BOARD &board, const std::string &aFilename)
Utility function to simply write a Board out to a file.
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_REQUIRE(intersection.has_value()==c.ExpectedIntersection.has_value())
BOOST_AUTO_TEST_SUITE_END()
BOOST_CHECK_MESSAGE(totalMismatches==0, std::to_string(totalMismatches)+" board(s) with strategy disagreements")
BOOST_AUTO_TEST_CASE(ModifiedBoardStagesCurrentState)
BOOST_CHECK_EQUAL(result, "25.4")