KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_kigit_ignore.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, you may find one here:
18 * http://www.gnu.org/licenses/gpl-3.0.html
19 */
20
26
28
32#include <git/kigit_ignore.h>
33
34#include <git2.h>
35
36#include <map>
37
39
40#include <wx/ffile.h>
41#include <wx/filename.h>
42#include <wx/textfile.h>
43
44
45static const char* TEST_AUTHOR_NAME = "Test Author";
47
48
51{
54 {
56
57 writeFile( wxT( "board.kicad_pcb" ), wxT( "(kicad_pcb)\n" ) );
58 KI_TEST::CommitAllGitFiles( m_repo.get(), wxT( "First commit" ) );
59 }
60
61
62 void writeFile( const wxString& aFile, const wxString& aContent )
63 {
64 KI_TEST::WriteGitFile( m_repoPath, aFile, aContent.utf8_string() );
65 }
66
67
69 std::map<wxString, FileStatus> reported( KIGIT_COMMON& aCommon, bool aChangedOnly )
70 {
71 GIT_STATUS_HANDLER handler( &aCommon );
72 const wxString workDir = handler.GetWorkingDirectory();
73 std::map<wxString, FileStatus> status = handler.GetFileStatus( wxEmptyString, aChangedOnly );
74 std::map<wxString, FileStatus> paths;
75
76 for( const auto& [path, fileStatus] : status )
77 paths.emplace( path.StartsWith( workDir ) ? path.Mid( workDir.length() ) : path, fileStatus );
78
79 return paths;
80 }
81
82
84 std::map<wxString, FileStatus> changes( KIGIT_COMMON& aCommon ) { return reported( aCommon, true ); }
85
86
88 std::map<wxString, FileStatus> treeStatus( KIGIT_COMMON& aCommon ) { return reported( aCommon, false ); }
89
90
91 wxFileName ignoreFile() const { return KIGIT::ProjectIgnoreFile( projectPath() ); }
92
94 virtual wxString projectPath() const { return m_repoPath + wxFileName::GetPathSeparator(); }
95
96 git_repository* repo() const { return m_repo.get(); }
97
98 virtual ~GIT_IGNORE_FIXTURE() = default;
99
100 wxString rules() const
101 {
102 wxTextFile file( ignoreFile().GetFullPath() );
103 wxString contents;
104
105 BOOST_REQUIRE( file.Open() );
106
107 for( size_t ii = 0; ii < file.GetLineCount(); ++ii )
108 contents += file[ii] + wxT( "\n" );
109
110 return contents;
111 }
112
113protected:
115 wxString m_repoPath;
116};
117
118
119BOOST_FIXTURE_TEST_SUITE( KiGitIgnore, GIT_IGNORE_FIXTURE )
120
121
122BOOST_AUTO_TEST_CASE( RuleNamesOnlyThePathPicked )
123{
124 BOOST_CHECK_EQUAL( KIGIT::PathIgnoreRule( wxT( "gerbers" ), true ), wxT( "/gerbers/" ) );
125 BOOST_CHECK_EQUAL( KIGIT::PathIgnoreRule( wxT( "out/top.gbr" ), false ), wxT( "/out/top.gbr" ) );
126 BOOST_CHECK_EQUAL( KIGIT::PathIgnoreRule( wxT( "board[1].gbr" ), false ), wxT( "/board\\[1].gbr" ) );
127 BOOST_CHECK_EQUAL( KIGIT::PathIgnoreRule( wxEmptyString, false ), wxEmptyString );
128
129 BOOST_CHECK_EQUAL( KIGIT::ExtensionIgnoreRule( wxT( "out/top.gbr" ) ), wxT( "*.gbr" ) );
130 BOOST_CHECK_EQUAL( KIGIT::ExtensionIgnoreRule( wxT( "fp-info-cache" ) ), wxEmptyString );
131}
132
133
134// The tree still hears about an ignored file, so it can dim the name instead of dropping it.
135BOOST_AUTO_TEST_CASE( IgnoredFileLeavesTheCommitList )
136{
137 writeFile( wxT( "out/top.gbr" ), wxT( "gerber\n" ) );
138
139 KIGIT_COMMON common( repo(), projectPath() );
140
141 BOOST_REQUIRE( changes( common ).count( wxT( "out/top.gbr" ) ) == 1 );
142 BOOST_CHECK( !KIGIT::IgnoreState( common, wxT( "out/top.gbr" ), false ).ignored );
143
144 wxString error;
145 BOOST_REQUIRE_MESSAGE( KIGIT::AddIgnoreRules( ignoreFile(), { wxT( "/out/top.gbr" ) }, wxEmptyString, error ),
146 error );
147
148 BOOST_CHECK( changes( common ).count( wxT( "out/top.gbr" ) ) == 0 );
149 BOOST_CHECK( KIGIT::IgnoreState( common, wxT( "out/top.gbr" ), false ).ignored );
150
151 const std::map<wxString, FileStatus> tree = treeStatus( common );
152 const auto entry = tree.find( wxT( "out/top.gbr" ) );
153
154 BOOST_REQUIRE( entry != tree.end() );
156 BOOST_CHECK( !entry->second.isDirectory );
157}
158
159
160// Git names an ignored folder alone, which is why the tree dims its children itself.
161BOOST_AUTO_TEST_CASE( IgnoredFolderLeavesTheCommitList )
162{
163 writeFile( wxT( "gerbers/top.gbr" ), wxT( "gerber\n" ) );
164 writeFile( wxT( "gerbers/bottom.gbr" ), wxT( "gerber\n" ) );
165
166 KIGIT_COMMON common( repo(), projectPath() );
167
168 BOOST_REQUIRE( changes( common ).count( wxT( "gerbers/top.gbr" ) ) == 1 );
169
170 wxString error;
171 BOOST_REQUIRE_MESSAGE( KIGIT::AddIgnoreRules( ignoreFile(), { wxT( "/gerbers/" ) }, wxEmptyString, error ), error );
172
173 const std::map<wxString, FileStatus> changed = changes( common );
174
175 BOOST_CHECK( changed.count( wxT( "gerbers/top.gbr" ) ) == 0 );
176 BOOST_CHECK( changed.count( wxT( "gerbers/bottom.gbr" ) ) == 0 );
177 BOOST_CHECK( KIGIT::IgnoreState( common, wxT( "gerbers" ), true ).ignored );
178
179 const std::map<wxString, FileStatus> tree = treeStatus( common );
180 const auto folder = tree.find( wxT( "gerbers" ) );
181
182 BOOST_REQUIRE_MESSAGE( folder != tree.end(), "the tree needs the folder itself to dim it" );
184 BOOST_CHECK( folder->second.isDirectory );
185 BOOST_CHECK( tree.count( wxT( "gerbers/top.gbr" ) ) == 0 );
186}
187
188
189BOOST_AUTO_TEST_CASE( TrackedPathReportsThatARuleWouldNotApply )
190{
191 writeFile( wxT( "docs/notes.txt" ), wxT( "notes\n" ) );
192 KI_TEST::CommitAllGitFiles( repo(), wxT( "Add notes" ) );
193
194 KIGIT_COMMON common( repo(), projectPath() );
195
196 BOOST_CHECK( KIGIT::IgnoreState( common, wxT( "board.kicad_pcb" ), false ).tracked );
197 BOOST_CHECK( KIGIT::IgnoreState( common, wxT( "docs/notes.txt" ), false ).tracked );
198
199 // A folder holding a tracked file cannot be ignored into untracking it either.
200 BOOST_CHECK( KIGIT::IgnoreState( common, wxT( "docs" ), true ).tracked );
201
202 writeFile( wxT( "out/top.gbr" ), wxT( "gerber\n" ) );
203 BOOST_CHECK( !KIGIT::IgnoreState( common, wxT( "out/top.gbr" ), false ).tracked );
204 BOOST_CHECK( !KIGIT::IgnoreState( common, wxT( "out" ), true ).tracked );
205}
206
207
208BOOST_AUTO_TEST_CASE( AddingKeepsTheFileTheUserHad )
209{
210 writeFile( wxT( ".gitignore" ), wxT( "# my own rules\n*.bak\n" ) );
211
212 wxString error;
213 BOOST_REQUIRE_MESSAGE( KIGIT::AddIgnoreRules( ignoreFile(), { wxT( "/out/top.gbr" ) }, wxEmptyString, error ),
214 error );
216 KIGIT::AddIgnoreRules( ignoreFile(), { wxT( "/out/top.gbr" ), wxT( "*.bak" ) }, wxEmptyString, error ),
217 error );
218
219 BOOST_CHECK_EQUAL( rules(), wxT( "# my own rules\n*.bak\n/out/top.gbr\n" ) );
220}
221
222
223// Without this the first new rule would land on the end of the user's last one.
224BOOST_AUTO_TEST_CASE( AddingAfterAnUnterminatedLastLine )
225{
226 wxFFile out( ignoreFile().GetFullPath(), wxT( "w" ) );
227 BOOST_REQUIRE( out.IsOpened() );
228 BOOST_REQUIRE( out.Write( wxT( "*.bak" ) ) );
229 BOOST_REQUIRE( out.Close() );
230
231 wxString error;
232 BOOST_REQUIRE_MESSAGE( KIGIT::AddIgnoreRules( ignoreFile(), { wxT( "/out/top.gbr" ) }, wxEmptyString, error ),
233 error );
234
235 BOOST_CHECK_EQUAL( rules(), wxT( "*.bak\n/out/top.gbr\n" ) );
236}
237
238
239BOOST_AUTO_TEST_CASE( RemovingTakesOnlyTheRuleNamed )
240{
241 writeFile( wxT( ".gitignore" ), wxT( "# my own rules\n*.gbr\n\n/out/top.gbr\n/gerbers/\n" ) );
242
243 wxString error;
244 BOOST_REQUIRE_MESSAGE( KIGIT::RemoveIgnoreRules( ignoreFile(), { wxT( "/out/top.gbr" ) }, error ), error );
245
246 BOOST_CHECK_EQUAL( rules(), wxT( "# my own rules\n*.gbr\n\n/gerbers/\n" ) );
247
248 // A rule the file does not hold is not an error, and changes nothing.
249 BOOST_REQUIRE_MESSAGE( KIGIT::RemoveIgnoreRules( ignoreFile(), { wxT( "/nowhere.gbr" ) }, error ), error );
250 BOOST_CHECK_EQUAL( rules(), wxT( "# my own rules\n*.gbr\n\n/gerbers/\n" ) );
251
252 // The broader rule is still there, so the file it covers is still ignored.
253 KIGIT_COMMON common( repo(), projectPath() );
254 BOOST_CHECK( KIGIT::IgnoreState( common, wxT( "out/top.gbr" ), false ).ignored );
255}
256
257
259
260
261
264{
265 wxString projectPath() const override
266 {
267 return m_repoPath + wxFileName::GetPathSeparator() + wxT( "proj" ) + wxFileName::GetPathSeparator();
268 }
269};
270
271
272BOOST_FIXTURE_TEST_SUITE( KiGitIgnoreSubdirectory, GIT_IGNORE_SUBDIR_FIXTURE )
273
274
275BOOST_AUTO_TEST_CASE( RulesApplyToTheProjectOnly )
276{
277 writeFile( wxT( "proj/out/top.gbr" ), wxT( "gerber\n" ) );
278 writeFile( wxT( "other/out/top.gbr" ), wxT( "gerber\n" ) );
279
280 KIGIT_COMMON common( repo(), projectPath() );
281
282 wxString error;
283 BOOST_REQUIRE_MESSAGE( KIGIT::AddIgnoreRules( ignoreFile(), { wxT( "/out/" ) }, wxEmptyString, error ), error );
284
285 BOOST_CHECK_EQUAL( rules(), wxT( "/out/\n" ) );
286 BOOST_CHECK( KIGIT::IgnoreState( common, wxT( "out" ), true ).ignored );
287 BOOST_CHECK( KIGIT::IgnoreState( common, wxT( "out/top.gbr" ), false ).ignored );
288
289 const std::map<wxString, FileStatus> changed = changes( common );
290
291 BOOST_CHECK( changed.count( wxT( "proj/out/top.gbr" ) ) == 0 );
292 BOOST_CHECK( changed.count( wxT( "other/out/top.gbr" ) ) == 1 );
293}
294
295
std::map< wxString, FileStatus > GetFileStatus(const wxString &aPathspec=wxEmptyString, bool aChangedOnly=false)
Get detailed file status for all files in the specified path.
wxString GetWorkingDirectory()
Get the repository working directory path.
The project's git session.
Give each test a backend and a temporary directory.
const wxString & TempPath() const
std::unique_ptr< git_repository, decltype([](git_repository *aRepo) { git_repository_free(aRepo); })> GitRepositoryPtr
A unique pointer for git_repository objects with automatic cleanup.
APIEXPORT bool AddIgnoreRules(const wxFileName &aIgnoreFile, const std::vector< wxString > &aRules, const wxString &aHeader, wxString &aError)
Append rules to an ignore file, creating it with aHeader as a comment when it is not there.
APIEXPORT wxFileName ProjectIgnoreFile(const wxString &aProjectPath)
The ignore file that covers a project.
APIEXPORT bool RemoveIgnoreRules(const wxFileName &aIgnoreFile, const std::vector< wxString > &aRules, wxString &aError)
Remove rules from an ignore file.
APIEXPORT wxString ExtensionIgnoreRule(const wxString &aProjectRelPath)
The rule that ignores every file sharing a path's extension, or nothing when it has none.
APIEXPORT IGNORE_STATE IgnoreState(KIGIT_COMMON &aCommon, const wxString &aProjectRelPath, bool aIsDirectory)
What git's rules and its index say about a project path, so the user is offered only what would take ...
APIEXPORT wxString PathIgnoreRule(const wxString &aProjectRelPath, bool aIsDirectory)
The rule that ignores one path and nothing else.
KIGIT::GitRepositoryPtr InitGitRepository(const wxString &aPath, const char *aBranch="main", const char *aAuthorName="QA Test", const char *aAuthorEmail="[email protected]")
Pin the branch and author so repository setup does not depend on the user's Git config.
void WriteGitFile(const wxString &aRepoPath, const wxString &aRelativePath, const std::string &aContents)
Write test data without staging it. Create parent directories when needed.
git_oid CommitAllGitFiles(git_repository *aRepo, const wxString &aMessage)
Stage all changes, including deletions, and commit them.
A repository holding one committed file, with the project at its root.
virtual wxString projectPath() const
The project directory, which is where its ignore file belongs.
std::map< wxString, FileStatus > changes(KIGIT_COMMON &aCommon)
What the commit list offers, which is changes only.
wxFileName ignoreFile() const
std::map< wxString, FileStatus > reported(KIGIT_COMMON &aCommon, bool aChangedOnly)
What the status reports, keyed by repository relative path.
std::map< wxString, FileStatus > treeStatus(KIGIT_COMMON &aCommon)
What the project tree is built from, ignored paths included.
virtual ~GIT_IGNORE_FIXTURE()=default
git_repository * repo() const
KIGIT::GitRepositoryPtr m_repo
void writeFile(const wxString &aFile, const wxString &aContent)
The project is a folder inside a larger repository, where rules have to be anchored to the project ra...
wxString projectPath() const override
The project directory, which is where its ignore file belongs.
BOOST_AUTO_TEST_CASE(HorizontalAlignment)
BOOST_CHECK(res.column_datatype(0)==SQL_NUMERIC||res.column_datatype(0)==SQL_DECIMAL)
BOOST_CHECK_EQUAL(std::any_cast< std::string >(rows[0].at("val")), "12345678901234567.89")
BOOST_REQUIRE_MESSAGE(dc.CacheTableInfo(table, { "part_id", "val" }), dc.GetLastError())
BOOST_REQUIRE(res.columns()==1)
static const char * TEST_AUTHOR_NAME
static const char * TEST_AUTHOR_EMAIL
BOOST_AUTO_TEST_SUITE_END()
std::string path
static const char * TEST_AUTHOR_NAME
static const char * TEST_AUTHOR_EMAIL
BOOST_AUTO_TEST_CASE(RuleNamesOnlyThePathPicked)