KiCad PCB EDA Suite
Loading...
Searching...
No Matches
git_test_fixture.h
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 modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * 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#ifndef QA_GIT_TEST_FIXTURE_H
21#define QA_GIT_TEST_FIXTURE_H
22
23#include <boost/test/unit_test.hpp>
25#include <qa_utils/file_utils.h>
27
28#include <wx/file.h>
29#include <wx/filefn.h>
30#include <wx/filename.h>
31
32#include <filesystem>
33#include <string>
34#include <utility>
35#include <vector>
36
37namespace KI_TEST
38{
39
42{
43public:
44 explicit SCOPED_GIT_CONFIG_SEARCH_PATH( const std::string& aPath )
45 {
46 for( git_config_level_t level :
47 { GIT_CONFIG_LEVEL_PROGRAMDATA, GIT_CONFIG_LEVEL_SYSTEM, GIT_CONFIG_LEVEL_XDG, GIT_CONFIG_LEVEL_GLOBAL } )
48 {
49 git_buf previous = GIT_BUF_INIT;
50 BOOST_REQUIRE_EQUAL( git_libgit2_opts( GIT_OPT_GET_SEARCH_PATH, level, &previous ), GIT_OK );
51 m_saved.emplace_back( level, previous.ptr ? previous.ptr : "" );
52 git_buf_dispose( &previous );
53 }
54
55 bool overridden = true;
56
57 for( const auto& [level, searchPath] : m_saved )
58 overridden &= git_libgit2_opts( GIT_OPT_SET_SEARCH_PATH, level, aPath.c_str() ) == GIT_OK;
59
60 // A throwing constructor skips the destructor, so undo a partial override here
61 if( !overridden )
62 restore();
63
64 BOOST_REQUIRE( overridden );
65 }
66
68
71
72private:
73 void restore()
74 {
75 for( const auto& [level, searchPath] : m_saved )
76 git_libgit2_opts( GIT_OPT_SET_SEARCH_PATH, level, searchPath.c_str() );
77 }
78
79 std::vector<std::pair<git_config_level_t, std::string>> m_saved;
80};
81
82
86{
87public:
89 m_tempDir( "kicad-qa-git" ),
90 m_configDir( "kicad-qa-gitconfig" ),
91 m_configSearchPath( toWxString( m_configDir.Path() ).utf8_string() ),
92 m_originalCwd( wxGetCwd() )
93 {
94 // Resolve temporary-directory symlinks to match libgit2's workdir path.
95 m_tempPath = toWxString( std::filesystem::canonical( m_tempDir.Path() ) );
96 }
97
99 ~GIT_TEST_FIXTURE() { wxSetWorkingDirectory( m_originalCwd ); }
100
101 const wxString& TempPath() const { return m_tempPath; }
102 LIBGIT_BACKEND* Backend() const { return m_backend.Backend(); }
103
104private:
105 static wxString toWxString( const std::filesystem::path& aPath )
106 {
107#ifdef __WINDOWS__
108 return aPath.wstring();
109#else
110 return wxString::FromUTF8( aPath.native() );
111#endif
112 }
113
114 // Declared after the backend so the search paths are set after libgit2 starts and restored before it stops
120 wxString m_tempPath;
121};
122
123
125inline KIGIT::GitRepositoryPtr InitGitRepository( const wxString& aPath, const char* aBranch = "main",
126 const char* aAuthorName = "QA Test",
127 const char* aAuthorEmail = "[email protected]" )
128{
129 git_repository_init_options options = GIT_REPOSITORY_INIT_OPTIONS_INIT;
130 options.flags = GIT_REPOSITORY_INIT_MKPATH;
131 options.initial_head = aBranch;
132
133 git_repository* repo = nullptr;
134 int result = git_repository_init_ext( &repo, aPath.utf8_str(), &options );
135 KIGIT::GitRepositoryPtr repoPtr( repo );
136 BOOST_REQUIRE_EQUAL( result, GIT_OK );
137
138 git_config* config = nullptr;
139 BOOST_REQUIRE_EQUAL( git_repository_config( &config, repo ), GIT_OK );
140 KIGIT::GitConfigPtr configPtr( config );
141 BOOST_REQUIRE_EQUAL( git_config_set_string( config, "user.name", aAuthorName ), GIT_OK );
142 BOOST_REQUIRE_EQUAL( git_config_set_string( config, "user.email", aAuthorEmail ), GIT_OK );
143
144 return repoPtr;
145}
146
147
149inline void WriteGitFile( const wxString& aRepoPath, const wxString& aRelativePath, const std::string& aContents )
150{
151 wxFileName path( aRepoPath + wxFileName::GetPathSeparator() + aRelativePath );
152
153 if( !wxFileName::DirExists( path.GetPath() ) )
154 BOOST_REQUIRE( wxFileName::Mkdir( path.GetPath(), wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL ) );
155
156 wxFile file( path.GetFullPath(), wxFile::write );
157 BOOST_REQUIRE( file.IsOpened() );
158 BOOST_REQUIRE_EQUAL( file.Write( aContents.data(), aContents.size() ), aContents.size() );
159 BOOST_REQUIRE( file.Close() );
160}
161
162
164inline git_oid CommitGitIndex( git_repository* aRepo, const wxString& aMessage )
165{
166 git_index* index = nullptr;
167 BOOST_REQUIRE_EQUAL( git_repository_index( &index, aRepo ), GIT_OK );
168 KIGIT::GitIndexPtr indexPtr( index );
169
170 git_oid treeOid;
171 BOOST_REQUIRE_EQUAL( git_index_write_tree( &treeOid, index ), GIT_OK );
172
173 git_tree* tree = nullptr;
174 BOOST_REQUIRE_EQUAL( git_tree_lookup( &tree, aRepo, &treeOid ), GIT_OK );
175 KIGIT::GitTreePtr treePtr( tree );
176
177 git_signature* signature = nullptr;
178 BOOST_REQUIRE_EQUAL( git_signature_default( &signature, aRepo ), GIT_OK );
179 KIGIT::GitSignaturePtr signaturePtr( signature );
180
181 git_oid headOid;
182 int result = git_reference_name_to_id( &headOid, aRepo, "HEAD" );
183 KIGIT::GitCommitPtr parent;
184
185 if( result == GIT_OK )
186 {
187 git_commit* commit = nullptr;
188 BOOST_REQUIRE_EQUAL( git_commit_lookup( &commit, aRepo, &headOid ), GIT_OK );
189 parent.reset( commit );
190 }
191 else
192 {
193 BOOST_REQUIRE( result == GIT_ENOTFOUND || result == GIT_EUNBORNBRANCH );
194 }
195
196 const git_commit* parents[] = { parent.get() };
197 git_oid commitOid;
198 BOOST_REQUIRE_EQUAL( git_commit_create( &commitOid, aRepo, "HEAD", signature, signature, nullptr,
199 aMessage.utf8_str(), tree, parent ? 1 : 0, parent ? parents : nullptr ),
200 GIT_OK );
201 return commitOid;
202}
203
204
206inline git_oid CommitGitFile( git_repository* aRepo, const wxString& aPath, const wxString& aMessage )
207{
208 {
209 git_index* index = nullptr;
210 BOOST_REQUIRE_EQUAL( git_repository_index( &index, aRepo ), GIT_OK );
211 KIGIT::GitIndexPtr indexPtr( index );
212 BOOST_REQUIRE_EQUAL( git_index_add_bypath( index, aPath.utf8_str() ), GIT_OK );
213 BOOST_REQUIRE_EQUAL( git_index_write( index ), GIT_OK );
214 }
215
216 return CommitGitIndex( aRepo, aMessage );
217}
218
219
221inline git_oid CommitAllGitFiles( git_repository* aRepo, const wxString& aMessage )
222{
223 {
224 git_index* index = nullptr;
225 BOOST_REQUIRE_EQUAL( git_repository_index( &index, aRepo ), GIT_OK );
226 KIGIT::GitIndexPtr indexPtr( index );
227 BOOST_REQUIRE_EQUAL( git_index_add_all( index, nullptr, GIT_INDEX_ADD_DEFAULT, nullptr, nullptr ), GIT_OK );
228 BOOST_REQUIRE_EQUAL( git_index_write( index ), GIT_OK );
229 }
230
231 return CommitGitIndex( aRepo, aMessage );
232}
233
234} // namespace KI_TEST
235
236#endif // QA_GIT_TEST_FIXTURE_H
int index
~GIT_TEST_FIXTURE()
Loading a project in a GUI app moves the working directory into it, so leave before the temp dir goes...
static wxString toWxString(const std::filesystem::path &aPath)
SCOPED_GIT_BACKEND m_backend
SCOPED_GIT_CONFIG_SEARCH_PATH m_configSearchPath
LIBGIT_BACKEND * Backend() const
const wxString & TempPath() const
Point every config level above the repository at one directory, restoring the previous paths afterwar...
std::vector< std::pair< git_config_level_t, std::string > > m_saved
SCOPED_GIT_CONFIG_SEARCH_PATH(const std::string &aPath)
SCOPED_GIT_CONFIG_SEARCH_PATH(const SCOPED_GIT_CONFIG_SEARCH_PATH &)=delete
SCOPED_GIT_CONFIG_SEARCH_PATH & operator=(const SCOPED_GIT_CONFIG_SEARCH_PATH &)=delete
Install a real backend for a test and restore the previous backend afterwards.
#define GIT_BUF_INIT
std::unique_ptr< git_tree, decltype([](git_tree *aTree) { git_tree_free(aTree); })> GitTreePtr
A unique pointer for git_tree objects with automatic cleanup.
std::unique_ptr< git_repository, decltype([](git_repository *aRepo) { git_repository_free(aRepo); })> GitRepositoryPtr
A unique pointer for git_repository objects with automatic cleanup.
std::unique_ptr< git_commit, decltype([](git_commit *aCommit) { git_commit_free(aCommit); })> GitCommitPtr
A unique pointer for git_commit objects with automatic cleanup.
std::unique_ptr< git_config, decltype([](git_config *aConfig) { git_config_free(aConfig); })> GitConfigPtr
A unique pointer for git_config objects with automatic cleanup.
std::unique_ptr< git_signature, decltype([](git_signature *aSignature) { git_signature_free(aSignature); })> GitSignaturePtr
A unique pointer for git_signature objects with automatic cleanup.
std::unique_ptr< git_index, decltype([](git_index *aIndex) { git_index_free(aIndex); })> GitIndexPtr
A unique pointer for git_index objects with automatic cleanup.
Test utilities for timestamps.
git_oid CommitGitIndex(git_repository *aRepo, const wxString &aMessage)
Commit the current index, using HEAD as the parent when it exists.
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.
git_oid CommitGitFile(git_repository *aRepo, const wxString &aPath, const wxString &aMessage)
Stage one file and commit the index. Other staged changes are kept.
BOOST_REQUIRE_EQUAL(rows.size(), 1)
BOOST_REQUIRE(res.columns()==1)
std::string path
wxString result
Test unit parsing edge cases and error handling.