20#ifndef QA_GIT_TEST_FIXTURE_H
21#define QA_GIT_TEST_FIXTURE_H
23#include <boost/test/unit_test.hpp>
29#include <wx/filename.h>
45 for( git_config_level_t level :
46 { GIT_CONFIG_LEVEL_PROGRAMDATA, GIT_CONFIG_LEVEL_SYSTEM, GIT_CONFIG_LEVEL_XDG, GIT_CONFIG_LEVEL_GLOBAL } )
49 BOOST_REQUIRE_EQUAL( git_libgit2_opts( GIT_OPT_GET_SEARCH_PATH, level, &previous ), GIT_OK );
50 m_saved.emplace_back( level, previous.ptr ? previous.ptr :
"" );
51 git_buf_dispose( &previous );
54 bool overridden =
true;
56 for(
const auto& [level, searchPath] :
m_saved )
57 overridden &= git_libgit2_opts( GIT_OPT_SET_SEARCH_PATH, level, aPath.c_str() ) == GIT_OK;
74 for(
const auto& [level, searchPath] :
m_saved )
75 git_libgit2_opts( GIT_OPT_SET_SEARCH_PATH, level, searchPath.c_str() );
78 std::vector<std::pair<git_config_level_t, std::string>>
m_saved;
100 static wxString
toWxString(
const std::filesystem::path& aPath )
103 return aPath.wstring();
105 return wxString::FromUTF8( aPath.native() );
120 const char* aAuthorName =
"QA Test",
123 git_repository_init_options options = GIT_REPOSITORY_INIT_OPTIONS_INIT;
124 options.flags = GIT_REPOSITORY_INIT_MKPATH;
125 options.initial_head = aBranch;
128 int result = git_repository_init_ext( &repo, aPath.utf8_str(), &options );
130 BOOST_REQUIRE_EQUAL(
result, GIT_OK );
132 git_config*
config =
nullptr;
133 BOOST_REQUIRE_EQUAL( git_repository_config( &
config, repo ), GIT_OK );
135 BOOST_REQUIRE_EQUAL( git_config_set_string(
config,
"user.name", aAuthorName ), GIT_OK );
136 BOOST_REQUIRE_EQUAL( git_config_set_string(
config,
"user.email", aAuthorEmail ), GIT_OK );
143inline void WriteGitFile(
const wxString& aRepoPath,
const wxString& aRelativePath,
const std::string& aContents )
145 wxFileName
path( aRepoPath + wxFileName::GetPathSeparator() + aRelativePath );
147 if( !wxFileName::DirExists(
path.GetPath() ) )
148 BOOST_REQUIRE( wxFileName::Mkdir(
path.GetPath(), wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL ) );
150 wxFile file(
path.GetFullPath(), wxFile::write );
152 BOOST_REQUIRE_EQUAL( file.Write( aContents.data(), aContents.size() ), aContents.size() );
160 git_index*
index =
nullptr;
161 BOOST_REQUIRE_EQUAL( git_repository_index( &
index, aRepo ), GIT_OK );
165 BOOST_REQUIRE_EQUAL( git_index_write_tree( &treeOid,
index ), GIT_OK );
167 git_tree* tree =
nullptr;
168 BOOST_REQUIRE_EQUAL( git_tree_lookup( &tree, aRepo, &treeOid ), GIT_OK );
171 git_signature* signature =
nullptr;
172 BOOST_REQUIRE_EQUAL( git_signature_default( &signature, aRepo ), GIT_OK );
176 int result = git_reference_name_to_id( &headOid, aRepo,
"HEAD" );
181 git_commit* commit =
nullptr;
182 BOOST_REQUIRE_EQUAL( git_commit_lookup( &commit, aRepo, &headOid ), GIT_OK );
183 parent.reset( commit );
190 const git_commit* parents[] = { parent.get() };
192 BOOST_REQUIRE_EQUAL( git_commit_create( &commitOid, aRepo,
"HEAD", signature, signature,
nullptr,
193 aMessage.utf8_str(), tree, parent ? 1 : 0, parent ? parents :
nullptr ),
203 git_index*
index =
nullptr;
204 BOOST_REQUIRE_EQUAL( git_repository_index( &
index, aRepo ), GIT_OK );
206 BOOST_REQUIRE_EQUAL( git_index_add_bypath(
index, aPath.utf8_str() ), GIT_OK );
207 BOOST_REQUIRE_EQUAL( git_index_write(
index ), GIT_OK );
218 git_index*
index =
nullptr;
219 BOOST_REQUIRE_EQUAL( git_repository_index( &
index, aRepo ), GIT_OK );
221 BOOST_REQUIRE_EQUAL( git_index_add_all(
index,
nullptr, GIT_INDEX_ADD_DEFAULT,
nullptr,
nullptr ), GIT_OK );
222 BOOST_REQUIRE_EQUAL( git_index_write(
index ), GIT_OK );
static wxString toWxString(const std::filesystem::path &aPath)
SCOPED_GIT_BACKEND m_backend
SCOPED_TEMP_DIR m_configDir
SCOPED_GIT_CONFIG_SEARCH_PATH m_configSearchPath
LIBGIT_BACKEND * Backend() const
SCOPED_TEMP_DIR m_tempDir
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()
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.
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(intersection.has_value()==c.ExpectedIntersection.has_value())
wxString result
Test unit parsing edge cases and error handling.