KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_scratch_doc.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 3
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
21
22#include <jobs/scratch_doc.h>
23#include <pgm_base.h>
25
26#include <wx/filename.h>
27#include <wx/utils.h>
28
29#include <filesystem>
30#include <fstream>
31#include <memory>
32#include <string>
33
34
35BOOST_AUTO_TEST_SUITE( ScratchDoc )
36
37
38// A diff of a file the editor has open names the live project's own path. That path must get
39// a scratch project of its own, and the live project must still be there afterwards.
40BOOST_AUTO_TEST_CASE( LivePathGetsItsOwnScratchProject )
41{
42 std::error_code ec;
43 std::filesystem::path dir = std::filesystem::temp_directory_path( ec )
44 / ( "kicad_qa_scratch_doc_live_" + std::to_string( wxGetProcessId() ) );
45
46 std::filesystem::remove_all( dir, ec );
47 std::filesystem::create_directories( dir, ec );
48
49 std::filesystem::path proPath = dir / "live.kicad_pro";
50 std::filesystem::path schPath = dir / "live.kicad_sch";
51
52 {
53 std::ofstream proFile( proPath );
54 proFile << R"({ "meta": { "filename": "live.kicad_pro", "version": 3 } })";
55 std::ofstream schFile( schPath );
56 schFile << "(kicad_sch)";
57 }
58
60
61 // However the test ends, later tests must not inherit this project as the active one.
62 struct ACTIVE_PROJECT_RESET
63 {
65 std::filesystem::path dir;
66
67 ~ACTIVE_PROJECT_RESET()
68 {
69 std::error_code ignored;
70 mgr.LoadProject( "" );
71 std::filesystem::remove_all( dir, ignored );
72 }
73 } reset{ mgr, dir };
74
75 BOOST_REQUIRE( mgr.LoadProject( wxString( proPath.string() ) ) );
76
77 PROJECT* live = &mgr.Prj();
78 BOOST_REQUIRE_EQUAL( live, mgr.GetProject( wxString( proPath.string() ) ) );
79
80 PROJECT* handed = nullptr;
81 wxString scratchPath;
82
83 {
85 mgr, wxString( schPath.string() ),
86 [&handed, &scratchPath]( PROJECT* aProject )
87 {
88 handed = aProject;
89 scratchPath = aProject->GetProjectFullName();
90 return std::make_unique<int>( 1 );
91 },
92 []( int* ) {} );
93
94 BOOST_REQUIRE( doc.doc );
95 BOOST_REQUIRE( handed );
96 BOOST_CHECK_MESSAGE( handed != live, "the live project was handed to the loader" );
97 BOOST_CHECK( doc.ownsProject );
98 BOOST_CHECK( scratchPath.StartsWith( wxFileName::GetTempDir() ) );
99 }
100
101 // The scratch project is gone, the live one is untouched.
102 BOOST_CHECK( mgr.GetProject( scratchPath ) == nullptr );
103 BOOST_CHECK_EQUAL( &mgr.Prj(), live );
104 BOOST_CHECK_EQUAL( mgr.GetProject( wxString( proPath.string() ) ), live );
105}
106
107
108// A path outside the live project keeps the existing behaviour: its own project, loaded and
109// unloaded by the wrapper.
110BOOST_AUTO_TEST_CASE( OtherPathLoadsAndUnloadsItsProject )
111{
112 std::error_code ec;
113 std::filesystem::path dir = std::filesystem::temp_directory_path( ec )
114 / ( "kicad_qa_scratch_doc_other_" + std::to_string( wxGetProcessId() ) );
115
116 std::filesystem::remove_all( dir, ec );
117 std::filesystem::create_directories( dir, ec );
118
119 std::filesystem::path schPath = dir / "other.kicad_sch";
120
121 {
122 std::ofstream schFile( schPath );
123 schFile << "(kicad_sch)";
124 }
125
126 // With no project open the active one is the empty project, which no path can match.
128 mgr.LoadProject( "" );
129
130 wxString proPath = wxString( ( dir / "other.kicad_pro" ).string() );
131 PROJECT* handed = nullptr;
132
133 {
135 mgr, wxString( schPath.string() ),
136 [&handed]( PROJECT* aProject )
137 {
138 handed = aProject;
139 return std::make_unique<int>( 1 );
140 },
141 []( int* ) {} );
142
143 BOOST_REQUIRE( handed );
144 BOOST_CHECK_EQUAL( handed, mgr.GetProject( proPath ) );
145 BOOST_CHECK( handed != &mgr.Prj() );
146 }
147
148 BOOST_CHECK( mgr.GetProject( proPath ) == nullptr );
149 std::filesystem::remove_all( dir, ec );
150}
151
152
virtual SETTINGS_MANAGER & GetSettingsManager() const
Definition pgm_base.h:123
Container for project specific data.
Definition project.h:63
bool LoadProject(const wxString &aFullPath, bool aSetActive=true)
Load a project or sets up a new project with a specified path.
PROJECT * GetProject(const wxString &aFullPath) const
Retrieve a loaded project by name.
PROJECT & Prj() const
A helper while we are not MDI-capable – return the one and only project.
PGM_BASE & Pgm()
The global program "get" accessor.
see class PGM_BASE
SCRATCH_DOC< DOC > LoadScratchDoc(SETTINGS_MANAGER &aMgr, const wxString &aDocPath, Loader aLoader, ClearFn aClearFn)
Construct a SCRATCH_DOC by loading a project non-active and then handing it to the caller's document ...
Move-only RAII wrapper for "load a KiCad document into a non-active scratch PROJECT and clean up afte...
bool ownsProject
True when this load created the project (LoadScratchDoc found the path not yet in the SettingsManager...
std::unique_ptr< DOC > doc
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(LivePathGetsItsOwnScratchProject)
BOOST_CHECK_EQUAL(result, "25.4")