Skip to content

Commit f1895d8

Browse files
committed
git-annex: add tests for push-to-create
This adds tests for creating a repository using the push-to-create feature. The tests are the equivalent of doing ``` git remote add origin <url> git annex sync --content ``` in a local git-annex repository. The steps it does are: 1. create a local git-annex repository 2. add a non-existing repository as a remote 3. sync using `git annex sync --content` It then checks that: - the repository was indeed created - the default branch matches the HEAD of the local repository - all annexed files exist on the remote and have the correct content This is done both for a repository in a user's namespace and for a repository in an organization's namespace.
1 parent 81f56b1 commit f1895d8

File tree

5 files changed

+104
-0
lines changed

5 files changed

+104
-0
lines changed

tests/integration/git_annex_test.go

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ import (
88
"errors"
99
"fmt"
1010
"io"
11+
"io/fs"
1112
"math/rand"
1213
"net/http"
1314
"net/url"
1415
"os"
1516
"path"
17+
"path/filepath"
1618
"regexp"
1719
"strings"
1820
"testing"
@@ -58,6 +60,100 @@ func doCreateRemoteAnnexRepository(t *testing.T, u *url.URL, ctx APITestContext,
5860
return nil
5961
}
6062

63+
func doGitAnnexPushToCreateTest(t *testing.T, u *url.URL, ctx APITestContext, repo string) {
64+
// create a local repository
65+
repoPath, err := os.MkdirTemp(os.TempDir(), "git-annex-repo-*")
66+
require.NoError(t, err)
67+
_, _, err = git.NewCommandContextNoGlobals(git.DefaultContext, "init", ".").RunStdString(&git.RunOpts{Dir: repoPath})
68+
require.NoError(t, err)
69+
err = doInitAnnexRepository(repoPath)
70+
require.NoError(t, err)
71+
72+
// add a remote
73+
repoURL := createSSHUrl(repo, u)
74+
_, _, err = git.NewCommandContextNoGlobals(git.DefaultContext, "remote", "add", "origin").AddDynamicArguments(repoURL.String()).RunStdString(&git.RunOpts{Dir: repoPath})
75+
require.NoError(t, err)
76+
77+
// sync to gitea
78+
withAnnexCtxKeyFile(t, ctx, func() {
79+
_, _, err = git.NewCommandContextNoGlobals(git.DefaultContext, "annex", "sync", "--content").RunStdString(&git.RunOpts{Dir: repoPath})
80+
require.NoError(t, err)
81+
})
82+
83+
// check that the repo was created and synced as expected
84+
// // repo exists on the gitea server
85+
remoteRepoPath := path.Join(setting.RepoRootPath, repo+".git")
86+
require.DirExists(t, remoteRepoPath)
87+
88+
// // default branch matches
89+
localRepo, err := git.OpenRepository(git.DefaultContext, repoPath)
90+
require.NoError(t, err)
91+
expectedDefaultBranch, err := localRepo.GetDefaultBranch()
92+
require.NoError(t, err)
93+
94+
remoteRepo, err := git.OpenRepository(git.DefaultContext, remoteRepoPath)
95+
require.NoError(t, err)
96+
actualDefaultBranch, err := remoteRepo.GetDefaultBranch()
97+
require.NoError(t, err)
98+
99+
require.Equal(t, expectedDefaultBranch, actualDefaultBranch)
100+
101+
// // repo contains the same annexed files
102+
err = filepath.WalkDir(repoPath, func(localPath string, d fs.DirEntry, err error) error {
103+
if err != nil {
104+
return err
105+
}
106+
107+
// skip anything below .git/
108+
if d.IsDir() && strings.HasSuffix(localPath, "/.git") {
109+
return filepath.SkipDir
110+
}
111+
112+
if d.IsDir() {
113+
return nil
114+
}
115+
116+
// local (expected) content
117+
f, err := os.Open(localPath)
118+
require.NoError(t, err)
119+
defer f.Close()
120+
expectedContent, err := io.ReadAll(f)
121+
require.NoError(t, err)
122+
123+
// remote (actual) content
124+
remotePath, err := contentLocation(remoteRepoPath, strings.TrimPrefix(localPath, repoPath+"/"))
125+
if err == annex.ErrInvalidPointer {
126+
// skip non-annex files
127+
return nil
128+
}
129+
require.FileExists(t, remotePath)
130+
f, err = os.Open(remotePath)
131+
require.NoError(t, err)
132+
defer f.Close()
133+
actualContent, err := io.ReadAll(f)
134+
require.NoError(t, err)
135+
136+
require.Equal(t, expectedContent, actualContent)
137+
138+
return nil
139+
})
140+
require.NoError(t, err)
141+
}
142+
143+
func TestGitAnnexPushToCreate(t *testing.T) {
144+
onGiteaRun(t, func(t *testing.T, u *url.URL) {
145+
t.Run("User", func(t *testing.T) {
146+
ctx := NewAPITestContext(t, "user2", "annex-push-to-create-test", auth_model.AccessTokenScopeWriteRepository)
147+
doGitAnnexPushToCreateTest(t, u, ctx, "user2/annex-push-to-create-test")
148+
})
149+
150+
t.Run("Org", func(t *testing.T) {
151+
ctx := NewAPITestContext(t, "user2", "annex-push-to-create-test", auth_model.AccessTokenScopeWriteRepository)
152+
doGitAnnexPushToCreateTest(t, u, ctx, "org3/annex-push-to-create-test")
153+
})
154+
})
155+
}
156+
61157
func TestGitAnnexMedia(t *testing.T) {
62158
if !setting.Annex.Enabled {
63159
t.Skip("Skipping since annex support is disabled.")

tests/mssql.ini.tmpl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ TYPE = immediate
2828

2929
[repository]
3030
ROOT = {{REPO_TEST_DIR}}tests/{{TEST_TYPE}}/gitea-{{TEST_TYPE}}-mssql/gitea-repositories
31+
ENABLE_PUSH_CREATE_USER = true
32+
ENABLE_PUSH_CREATE_ORG = true
3133

3234
[repository.local]
3335
LOCAL_COPY_PATH = tests/{{TEST_TYPE}}/gitea-{{TEST_TYPE}}-mssql/tmp/local-repo

tests/mysql.ini.tmpl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ TYPE = immediate
3030

3131
[repository]
3232
ROOT = {{REPO_TEST_DIR}}tests/{{TEST_TYPE}}/gitea-{{TEST_TYPE}}-mysql/gitea-repositories
33+
ENABLE_PUSH_CREATE_USER = true
34+
ENABLE_PUSH_CREATE_ORG = true
3335

3436
[repository.local]
3537
LOCAL_COPY_PATH = tests/{{TEST_TYPE}}/gitea-{{TEST_TYPE}}-mysql/tmp/local-repo

tests/pgsql.ini.tmpl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ TYPE = immediate
2929

3030
[repository]
3131
ROOT = {{REPO_TEST_DIR}}tests/{{TEST_TYPE}}/gitea-{{TEST_TYPE}}-pgsql/gitea-repositories
32+
ENABLE_PUSH_CREATE_USER = true
33+
ENABLE_PUSH_CREATE_ORG = true
3234

3335
[repository.local]
3436
LOCAL_COPY_PATH = tests/{{TEST_TYPE}}/gitea-{{TEST_TYPE}}-pgsql/tmp/local-repo

tests/sqlite.ini.tmpl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ TYPE = immediate
2424

2525
[repository]
2626
ROOT = {{REPO_TEST_DIR}}tests/{{TEST_TYPE}}/gitea-{{TEST_TYPE}}-sqlite/gitea-repositories
27+
ENABLE_PUSH_CREATE_USER = true
28+
ENABLE_PUSH_CREATE_ORG = true
2729

2830
[repository.local]
2931
LOCAL_COPY_PATH = tests/{{TEST_TYPE}}/gitea-{{TEST_TYPE}}-sqlite/tmp/local-repo

0 commit comments

Comments
 (0)