-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
git_test.go
72 lines (65 loc) · 1.73 KB
/
git_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package x_test
import (
"fmt"
"os"
"path/filepath"
"testing"
"time"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/config"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gitlab.com/tozd/go/x"
)
func TestInferProjectID(t *testing.T) {
t.Parallel()
tests := []struct {
remote string
projectID string
}{
{"https://gitlab.com/tozd/go/x.git", "tozd/go/x"},
{"[email protected]:tozd/go/x.git", "tozd/go/x"},
}
for k, tt := range tests {
t.Run(fmt.Sprintf("case=%d", k), func(t *testing.T) {
t.Parallel()
tempDir := t.TempDir()
repository, err := git.PlainInit(tempDir, false)
require.NoError(t, err)
workTree, err := repository.Worktree()
require.NoError(t, err)
filename := filepath.Join(tempDir, "file.txt")
author := &object.Signature{
Name: "John Doe",
Email: "[email protected]",
When: time.Now(),
}
err = os.WriteFile(filename, []byte("Hello world!"), 0o600)
require.NoError(t, err)
_, err = workTree.Add("file.txt")
require.NoError(t, err)
_, err = workTree.Commit("Initial commmit.", &git.CommitOptions{
All: false,
AllowEmptyCommits: false,
Author: author,
Committer: nil,
Parents: nil,
SignKey: nil,
Signer: nil,
Amend: false,
})
require.NoError(t, err)
_, err = repository.CreateRemote(&config.RemoteConfig{
Name: "origin",
URLs: []string{tt.remote},
Fetch: nil,
Mirror: false,
})
require.NoError(t, err)
projectID, err := x.InferGitLabProjectID(tempDir)
require.NoError(t, err, "% -+#.1v", err)
assert.Equal(t, tt.projectID, projectID)
})
}
}