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