16
16
package util_test
17
17
18
18
import (
19
+ "github.com/go-git/go-git/v5"
20
+ "github.com/stretchr/testify/require"
19
21
"net/url"
20
22
"path/filepath"
21
23
"testing"
22
24
23
- "github.com/go-git/go-git/v5"
24
25
"github.com/stretchr/testify/assert"
25
26
26
27
"github.com/snyk/code-client-go/internal/util"
28
+ "github.com/snyk/code-client-go/internal/util/testutil"
27
29
)
28
30
29
- func clone (t * testing.T , repoUrl string , repoDir string ) (string , * git.Repository ) {
30
- t .Helper ()
31
-
32
- if repoDir == "" {
33
- repoDir = t .TempDir ()
34
- }
35
- repo , err := git .PlainClone (repoDir , false , & git.CloneOptions {URL : repoUrl })
36
- assert .NoError (t , err )
37
- assert .NotNil (t , repo )
38
-
39
- return repoDir , repo
40
- }
41
-
42
31
func Test_GetRepositoryUrl_repo_with_credentials (t * testing.T ) {
43
32
// check out a repo and prepare its config to contain credentials in the URL
44
33
expectedRepoUrl := "https://github.com/snyk-fixtures/shallow-goof-locked.git"
45
34
46
- repoDir , repo := clone (t , expectedRepoUrl , "" )
35
+ repoDir , err := testutil .SetupCustomTestRepo (t , expectedRepoUrl , "master" , "" , "shallow-goof-locked" )
36
+ require .NoError (t , err )
37
+
38
+ repo , err := git .PlainOpenWithOptions (repoDir , & git.PlainOpenOptions {
39
+ DetectDotGit : true ,
40
+ })
41
+ require .NoError (t , err )
47
42
48
43
config , err := repo .Config ()
49
44
assert .NoError (t , err )
@@ -67,7 +62,8 @@ func Test_GetRepositoryUrl_repo_with_credentials(t *testing.T) {
67
62
func Test_GetRepositoryUrl_repo_without_credentials (t * testing.T ) {
68
63
// check out a repo and prepare its config to contain credentials in the URL
69
64
expectedRepoUrl := "https://github.com/snyk-fixtures/shallow-goof-locked.git"
70
- repoDir , _ := clone (t , expectedRepoUrl , "" )
65
+ repoDir , err := testutil .SetupCustomTestRepo (t , expectedRepoUrl , "master" , "" , "shallow-goof-locked" )
66
+ require .NoError (t , err )
71
67
72
68
// run method under test
73
69
actualUrl , err := util .GetRepositoryUrl (repoDir )
@@ -78,7 +74,8 @@ func Test_GetRepositoryUrl_repo_without_credentials(t *testing.T) {
78
74
func Test_GetRepositoryUrl_repo_with_ssh (t * testing.T ) {
79
75
// check out a repo and prepare its config to contain credentials in the URL
80
76
expectedRepoUrl := "https://github.com/snyk-fixtures/shallow-goof-locked.git"
81
- repoDir ,
_ := clone (
t ,
"[email protected] :snyk-fixtures/shallow-goof-locked.git" ,
"" )
77
+ repoDir ,
err := testutil .
SetupCustomTestRepo (
t ,
"[email protected] :snyk-fixtures/shallow-goof-locked.git" ,
"master" ,
"" ,
"shallow-goof-locked" )
78
+ require .NoError (t , err )
82
79
83
80
// run method under test
84
81
actualUrl , err := util .GetRepositoryUrl (repoDir )
@@ -95,7 +92,8 @@ func Test_GetRepositoryUrl_no_repo(t *testing.T) {
95
92
96
93
func Test_GetRepositoryUrl_repo_subfolder (t * testing.T ) {
97
94
expectedRepoUrl := "https://github.com/snyk-fixtures/mono-repo.git"
98
- repoDir ,
_ := clone (
t ,
"[email protected] :snyk-fixtures/mono-repo.git" ,
"" )
95
+ repoDir ,
err := testutil .
SetupCustomTestRepo (
t ,
"[email protected] :snyk-fixtures/mono-repo.git" ,
"master" ,
"" ,
"mono-repo" )
96
+ require .NoError (t , err )
99
97
100
98
// run method under test
101
99
actualUrl , err := util .GetRepositoryUrl (filepath .Join (repoDir , "multi-module" ))
@@ -104,8 +102,11 @@ func Test_GetRepositoryUrl_repo_subfolder(t *testing.T) {
104
102
}
105
103
106
104
func Test_GetRepositoryUrl_repo_submodule (t * testing.T ) {
107
- parentRepoDir , _ := clone (t , "https://github.com/snyk-fixtures/shallow-goof-locked.git" , "" )
108
- nestedRepoDir ,
_ := clone (
t ,
"[email protected] :snyk-fixtures/mono-repo.git" ,
filepath .
Join (
parentRepoDir ,
"shallow-goof-locked" ))
105
+ parentRepoDir , err := testutil .SetupCustomTestRepo (t , "https://github.com/snyk-fixtures/shallow-goof-locked.git" , "master" , "" , "shallow-goof-locked" )
106
+ require .NoError (t , err )
107
+ nestedRepoDir , err := testutil .SetupCustomTestRepo (t , "https://github.com/snyk-fixtures/mono-repo.git" , "master" , parentRepoDir , "mono-repo" )
108
+ require .NoError (t , err )
109
+
109
110
// run method under test
110
111
actualUrl , err := util .GetRepositoryUrl (parentRepoDir )
111
112
assert .NoError (t , err )
0 commit comments