KiCad PCB EDA Suite
Loading...
Searching...
No Matches
test_git_clone_handler.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
26
27#include <git2.h>
28
29#include <wx/ffile.h>
30#include <wx/filename.h>
31#include <wx/filesys.h>
32
33
34BOOST_AUTO_TEST_SUITE( GitCloneHandler )
35
36
37// Cloning through KiCad must not leave the team's work tree modified (WIN-45)
38BOOST_FIXTURE_TEST_CASE( CloneLeavesWorkTreeClean, KI_TEST::GIT_TEST_FIXTURE )
39{
40 const wxString sep = wxFileName::GetPathSeparator();
41 const wxString sourcePath = TempPath() + sep + wxS( "source" );
42 const wxString barePath = TempPath() + sep + wxS( "ctrl-board.git" );
43 const wxString clonePath = TempPath() + sep + wxS( "clone" );
44 const wxString dataDir = wxString::FromUTF8( KI_TEST::GetTestDataRootDir() ) + wxS( "common/git/mdw_ctrl_board/" );
45
46 // The campaign's ctrl-board seed keeps its .gitignore under a neutral name in the data directory
47 const std::pair<wxString, wxString> files[] = { { wxS( "ctrl-board.kicad_pro" ), wxS( "ctrl-board.kicad_pro" ) },
48 { wxS( "gitignore.txt" ), wxS( ".gitignore" ) } };
49
50 for( const auto& [src, dst] : files )
51 {
52 wxFFile in( dataDir + src, wxS( "rb" ) );
53 wxString contents;
54
55 BOOST_REQUIRE( in.IsOpened() && in.ReadAll( &contents, wxConvISO8859_1 ) );
56 KI_TEST::WriteGitFile( sourcePath, dst, std::string( contents.mb_str( wxConvISO8859_1 ) ) );
57 }
58
59 {
61 KI_TEST::CommitAllGitFiles( source.get(), wxS( "Import ctrl-board" ) );
62
63 git_repository* bare = nullptr;
64 git_clone_options options = GIT_CLONE_OPTIONS_INIT;
65 options.bare = 1;
66
67 BOOST_REQUIRE_EQUAL( git_clone( &bare, wxFileSystem::FileNameToURL( wxFileName( sourcePath ) ).utf8_str(),
68 barePath.utf8_str(), &options ),
69 GIT_OK );
70 git_repository_free( bare );
71 }
72
73 KIGIT_COMMON common( nullptr );
74 GIT_CLONE_HANDLER handler( &common );
75
76 handler.SetRemote( wxFileSystem::FileNameToURL( wxFileName( barePath ) ) );
77 handler.SetClonePath( clonePath );
78
79 BOOST_REQUIRE( handler.PerformClone() );
80 BOOST_REQUIRE( common.GetRepo() );
81
82 git_status_options statusOptions = GIT_STATUS_OPTIONS_INIT;
83 statusOptions.show = GIT_STATUS_SHOW_INDEX_AND_WORKDIR;
84 statusOptions.flags = GIT_STATUS_OPT_INCLUDE_UNTRACKED | GIT_STATUS_OPT_RECURSE_UNTRACKED_DIRS;
85
86 git_status_list* statusList = nullptr;
87 BOOST_REQUIRE_EQUAL( git_status_list_new( &statusList, common.GetRepo(), &statusOptions ), GIT_OK );
88 KIGIT::GitStatusListPtr statusPtr( statusList );
89
90 BOOST_CHECK_EQUAL( git_status_list_entrycount( statusList ), 0 );
91
92 wxFFile attributes( clonePath + sep + wxS( ".git/info/attributes" ), wxS( "rb" ) );
93 wxString attributesText;
94
95 BOOST_REQUIRE( attributes.IsOpened() && attributes.ReadAll( &attributesText ) );
96 BOOST_CHECK( attributesText.Contains( wxS( "*.kicad_pcb merge=kicad-pcb" ) ) );
97 BOOST_CHECK( attributesText.Contains( wxS( "*.kicad_sch merge=kicad-sch" ) ) );
98
99 git_repository_free( common.GetRepo() );
100 common.SetRepo( nullptr );
101}
102
103
void SetRemote(const wxString &aRemote)
void SetClonePath(const wxString &aPath)
The project's git session.
git_repository * GetRepo() const
void SetRepo(git_repository *aRepo)
std::string source
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_status_list, decltype([](git_status_list *aList) { git_status_list_free(aList); })> GitStatusListPtr
A unique pointer for git_status_list objects with automatic cleanup.
Test utilities for timestamps.
std::string GetTestDataRootDir()
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.
BOOST_FIXTURE_TEST_CASE(ServerStartsAndResponds, API_SERVER_E2E_FIXTURE)
BOOST_AUTO_TEST_SUITE(CadstarPartParser)
BOOST_CHECK(res.column_datatype(0)==SQL_NUMERIC||res.column_datatype(0)==SQL_DECIMAL)
BOOST_REQUIRE_EQUAL(rows.size(), 1)
BOOST_CHECK_EQUAL(std::any_cast< std::string >(rows[0].at("val")), "12345678901234567.89")
BOOST_REQUIRE(res.columns()==1)
BOOST_AUTO_TEST_SUITE_END()