20#ifndef QA_GIT_TEST_FIXTURE_H
21#define QA_GIT_TEST_FIXTURE_H
23#include <boost/test/unit_test.hpp>
30#include <wx/filename.h>
46 for( git_config_level_t level :
47 { GIT_CONFIG_LEVEL_PROGRAMDATA, GIT_CONFIG_LEVEL_SYSTEM, GIT_CONFIG_LEVEL_XDG, GIT_CONFIG_LEVEL_GLOBAL } )
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 );
55 bool overridden =
true;
57 for(
const auto& [level, searchPath] :
m_saved )
58 overridden &= git_libgit2_opts( GIT_OPT_SET_SEARCH_PATH, level, aPath.c_str() ) == GIT_OK;
75 for(
const auto& [level, searchPath] :
m_saved )
76 git_libgit2_opts( GIT_OPT_SET_SEARCH_PATH, level, searchPath.c_str() );
79 std::vector<std::pair<git_config_level_t, std::string>>
m_saved;
105 static wxString
toWxString(
const std::filesystem::path& aPath )
108 return aPath.wstring();
110 return wxString::FromUTF8( aPath.native() );
126 const char* aAuthorName =
"QA Test",
129 git_repository_init_options options = GIT_REPOSITORY_INIT_OPTIONS_INIT;
130 options.flags = GIT_REPOSITORY_INIT_MKPATH;
131 options.initial_head = aBranch;
134 int result = git_repository_init_ext( &repo, aPath.utf8_str(), &options );
138 git_config*
config =
nullptr;
149inline void WriteGitFile(
const wxString& aRepoPath,
const wxString& aRelativePath,
const std::string& aContents )
151 wxFileName
path( aRepoPath + wxFileName::GetPathSeparator() + aRelativePath );
153 if( !wxFileName::DirExists(
path.GetPath() ) )
154 BOOST_REQUIRE( wxFileName::Mkdir(
path.GetPath(), wxS_DIR_DEFAULT, wxPATH_MKDIR_FULL ) );
156 wxFile file(
path.GetFullPath(), wxFile::write );
166 git_index*
index =
nullptr;
173 git_tree* tree =
nullptr;
177 git_signature* signature =
nullptr;
182 int result = git_reference_name_to_id( &headOid, aRepo,
"HEAD" );
187 git_commit* commit =
nullptr;
189 parent.reset( commit );
196 const git_commit* parents[] = { parent.get() };
198 BOOST_REQUIRE_EQUAL( git_commit_create( &commitOid, aRepo,
"HEAD", signature, signature,
nullptr,
199 aMessage.utf8_str(), tree, parent ? 1 : 0, parent ? parents :
nullptr ),
209 git_index*
index =
nullptr;
224 git_index*
index =
nullptr;
~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_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_EQUAL(rows.size(), 1)
BOOST_REQUIRE(res.columns()==1)
wxString result
Test unit parsing edge cases and error handling.