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 <qa_utils/file_utils.h>
40#include <boost/test/unit_test.hpp>
41
44#include <board.h>
45#include <footprint.h>
46
47#include <wx/filefn.h>
48#include <wx/filename.h>
49#include <wx/string.h>
50
51
52BOOST_AUTO_TEST_SUITE( StepExportModifiedBoard )
53
54
55BOOST_AUTO_TEST_CASE( ModifiedBoardStagesCurrentState )
56{
57 const std::string sourceBoard = KI_TEST::GetPcbnewTestDataDir() + "issue23704/issue23704.kicad_pcb";
58
59 BOOST_REQUIRE_MESSAGE( std::filesystem::exists( sourceBoard ), "Missing test board " << sourceBoard );
60
61 std::unique_ptr<BOARD> board = KI_TEST::ReadBoardFromFileOrStream( sourceBoard );
62 BOOST_REQUIRE( board );
63
64 const size_t originalCount = board->Footprints().size();
65 BOOST_REQUIRE_MESSAGE( originalCount > 1, "Panel board needs multiple footprints to delete" );
66
67 // Stage the board's "saved" copy in an isolated temp directory so the helper writes its
68 // temporary export file alongside it rather than into the shared test data tree.
69 const KI_TEST::SCOPED_TEMP_DIR workDir( wxS( "kicad_step_export_23704_" ) );
70 const std::filesystem::path& onDisk = workDir.Path() / "panel.kicad_pcb";
71 KI_TEST::DumpBoardToFile( *board, onDisk );
72 const wxString onDiskPath = wxString::FromUTF8( onDisk.string() );
73
74 // A sibling project file must be staged too, so project-relative model and library paths
75 // resolve identically during export.
76 std::filesystem::path projectFile = onDisk;
77 projectFile.replace_extension( ".kicad_pro" );
78
79 {
80 std::ofstream project( projectFile );
82 project << "{ \"meta\": { \"version\": 1 } }\n";
83 }
84
85 // The user deletes every panel section but one, never saving the file.
86 while( board->Footprints().size() > 1 )
87 board->Remove( board->Footprints().back() );
88
89 const size_t remainingCount = board->Footprints().size();
90 BOOST_REQUIRE_EQUAL( remainingCount, 1u );
91
92 // Drive the production helper exactly as the export dialog does for a modified board.
93 wxString inputPath;
94 std::vector<wxString> tempFiles;
95 wxString errorDetail;
96 const wxString error =
97 DIALOG_EXPORT_STEP::StageBoardForExport( onDiskPath, true, board.get(), inputPath, tempFiles, errorDetail );
98
99 BOOST_REQUIRE_MESSAGE( error.IsEmpty(), "StageBoardForExport failed: " << error.ToStdString() );
100 BOOST_REQUIRE( wxFileExists( inputPath ) );
101 BOOST_CHECK_MESSAGE( inputPath != onDiskPath, "Modified board must stage a temporary copy, not the on-disk file" );
102
103 std::unique_ptr<BOARD> staged = KI_TEST::ReadBoardFromFileOrStream( std::string( inputPath.utf8_str() ) );
104 BOOST_REQUIRE( staged );
105
106 // The staged file reflects the in-memory deletions, not the full on-disk panel.
107 BOOST_CHECK_EQUAL( staged->Footprints().size(), remainingCount );
108
109 // The sibling project file is staged next to the temporary board.
110 std::filesystem::path stagedProject = std::filesystem::path( std::string( inputPath.utf8_str() ) );
111 stagedProject.replace_extension( ".kicad_pro" );
112 BOOST_CHECK( std::filesystem::exists( stagedProject ) );
113 BOOST_CHECK_EQUAL( std::filesystem::file_size( stagedProject ), std::filesystem::file_size( projectFile ) );
114
115 // The original on-disk file is never mutated by staging.
116 std::unique_ptr<BOARD> onDiskReloaded = KI_TEST::ReadBoardFromFileOrStream( onDisk.string() );
117 BOOST_REQUIRE( onDiskReloaded );
118 BOOST_CHECK_EQUAL( onDiskReloaded->Footprints().size(), originalCount );
119
120 for( const wxString& f : tempFiles )
121 {
122 if( wxFileExists( f ) )
123 wxRemoveFile( f );
124 }
125}
126
127
131BOOST_AUTO_TEST_CASE( UnmodifiedBoardStagesOnDiskFile )
132{
133 const std::string sourceBoard = KI_TEST::GetPcbnewTestDataDir() + "issue23704/issue23704.kicad_pcb";
134
135 std::unique_ptr<BOARD> board = KI_TEST::ReadBoardFromFileOrStream( sourceBoard );
136 BOOST_REQUIRE( board );
137
138 const wxString onDiskPath = wxString::FromUTF8( sourceBoard );
139
140 wxString inputPath;
141 std::vector<wxString> tempFiles;
142 wxString errorDetail;
143 const wxString error = DIALOG_EXPORT_STEP::StageBoardForExport( onDiskPath, false, board.get(), inputPath,
144 tempFiles, errorDetail );
145
146 BOOST_CHECK( error.IsEmpty() );
147 BOOST_CHECK_EQUAL( inputPath, onDiskPath );
148 BOOST_CHECK( tempFiles.empty() );
149}
150
151
155BOOST_AUTO_TEST_CASE( UnsavedBoardIsRejected )
156{
157 const std::string sourceBoard = KI_TEST::GetPcbnewTestDataDir() + "issue23704/issue23704.kicad_pcb";
158
159 std::unique_ptr<BOARD> board = KI_TEST::ReadBoardFromFileOrStream( sourceBoard );
160 BOOST_REQUIRE( board );
161
162 wxString inputPath;
163 std::vector<wxString> tempFiles;
164 wxString errorDetail;
165 const wxString error = DIALOG_EXPORT_STEP::StageBoardForExport( wxEmptyString, true, board.get(), inputPath,
166 tempFiles, errorDetail );
167
168 BOOST_CHECK( !error.IsEmpty() );
169 BOOST_CHECK( inputPath.IsEmpty() );
170 BOOST_CHECK( tempFiles.empty() );
171}
172
173
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.
const std::filesystem::path & Path() const
Get the path to the temporary directory as a std::filesystem::path.
Definition file_utils.h:59
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::filesystem::path &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_AUTO_TEST_CASE(ModifiedBoardStagesCurrentState)
BOOST_CHECK_EQUAL(result, "25.4")