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