-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
746136d
commit 1d45bef
Showing
3 changed files
with
65 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,34 +16,29 @@ | |
package util_test | ||
|
||
import ( | ||
"github.com/go-git/go-git/v5" | ||
"github.com/stretchr/testify/require" | ||
"net/url" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/go-git/go-git/v5" | ||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/snyk/code-client-go/internal/util" | ||
"github.com/snyk/code-client-go/internal/util/testutil" | ||
) | ||
|
||
func clone(t *testing.T, repoUrl string, repoDir string) (string, *git.Repository) { | ||
t.Helper() | ||
|
||
if repoDir == "" { | ||
repoDir = t.TempDir() | ||
} | ||
repo, err := git.PlainClone(repoDir, false, &git.CloneOptions{URL: repoUrl}) | ||
assert.NoError(t, err) | ||
assert.NotNil(t, repo) | ||
|
||
return repoDir, repo | ||
} | ||
|
||
func Test_GetRepositoryUrl_repo_with_credentials(t *testing.T) { | ||
// check out a repo and prepare its config to contain credentials in the URL | ||
expectedRepoUrl := "https://github.com/snyk-fixtures/shallow-goof-locked.git" | ||
|
||
repoDir, repo := clone(t, expectedRepoUrl, "") | ||
repoDir, err := testutil.SetupCustomTestRepo(t, expectedRepoUrl, "master", "", "shallow-goof-locked") | ||
require.NoError(t, err) | ||
|
||
repo, err := git.PlainOpenWithOptions(repoDir, &git.PlainOpenOptions{ | ||
DetectDotGit: true, | ||
}) | ||
require.NoError(t, err) | ||
|
||
config, err := repo.Config() | ||
assert.NoError(t, err) | ||
|
@@ -67,7 +62,8 @@ func Test_GetRepositoryUrl_repo_with_credentials(t *testing.T) { | |
func Test_GetRepositoryUrl_repo_without_credentials(t *testing.T) { | ||
// check out a repo and prepare its config to contain credentials in the URL | ||
expectedRepoUrl := "https://github.com/snyk-fixtures/shallow-goof-locked.git" | ||
repoDir, _ := clone(t, expectedRepoUrl, "") | ||
repoDir, err := testutil.SetupCustomTestRepo(t, expectedRepoUrl, "master", "", "shallow-goof-locked") | ||
require.NoError(t, err) | ||
|
||
// run method under test | ||
actualUrl, err := util.GetRepositoryUrl(repoDir) | ||
|
@@ -78,7 +74,8 @@ func Test_GetRepositoryUrl_repo_without_credentials(t *testing.T) { | |
func Test_GetRepositoryUrl_repo_with_ssh(t *testing.T) { | ||
// check out a repo and prepare its config to contain credentials in the URL | ||
expectedRepoUrl := "https://github.com/snyk-fixtures/shallow-goof-locked.git" | ||
repoDir, _ := clone(t, "[email protected]:snyk-fixtures/shallow-goof-locked.git", "") | ||
repoDir, err := testutil.SetupCustomTestRepo(t, "[email protected]:snyk-fixtures/shallow-goof-locked.git", "master", "", "shallow-goof-locked") | ||
require.NoError(t, err) | ||
|
||
// run method under test | ||
actualUrl, err := util.GetRepositoryUrl(repoDir) | ||
|
@@ -95,7 +92,8 @@ func Test_GetRepositoryUrl_no_repo(t *testing.T) { | |
|
||
func Test_GetRepositoryUrl_repo_subfolder(t *testing.T) { | ||
expectedRepoUrl := "https://github.com/snyk-fixtures/mono-repo.git" | ||
repoDir, _ := clone(t, "[email protected]:snyk-fixtures/mono-repo.git", "") | ||
repoDir, err := testutil.SetupCustomTestRepo(t, "[email protected]:snyk-fixtures/mono-repo.git", "master", "", "mono-repo") | ||
require.NoError(t, err) | ||
|
||
// run method under test | ||
actualUrl, err := util.GetRepositoryUrl(filepath.Join(repoDir, "multi-module")) | ||
|
@@ -104,8 +102,11 @@ func Test_GetRepositoryUrl_repo_subfolder(t *testing.T) { | |
} | ||
|
||
func Test_GetRepositoryUrl_repo_submodule(t *testing.T) { | ||
parentRepoDir, _ := clone(t, "https://github.com/snyk-fixtures/shallow-goof-locked.git", "") | ||
nestedRepoDir, _ := clone(t, "[email protected]:snyk-fixtures/mono-repo.git", filepath.Join(parentRepoDir, "shallow-goof-locked")) | ||
parentRepoDir, err := testutil.SetupCustomTestRepo(t, "https://github.com/snyk-fixtures/shallow-goof-locked.git", "master", "", "shallow-goof-locked") | ||
require.NoError(t, err) | ||
nestedRepoDir, err := testutil.SetupCustomTestRepo(t, "https://github.com/snyk-fixtures/mono-repo.git", "master", parentRepoDir, "mono-repo") | ||
require.NoError(t, err) | ||
|
||
// run method under test | ||
actualUrl, err := util.GetRepositoryUrl(parentRepoDir) | ||
assert.NoError(t, err) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package testutil | ||
|
||
import ( | ||
"github.com/rs/zerolog/log" | ||
"os/exec" | ||
"path/filepath" | ||
"testing" | ||
) | ||
|
||
func SetupCustomTestRepo(t *testing.T, url string, targetCommit string, parentDir, repoDir string) (string, error) { | ||
t.Helper() | ||
if parentDir == "" { | ||
parentDir = t.TempDir() | ||
} | ||
if repoDir == "" { | ||
repoDir = "1" | ||
} | ||
absoluteCloneRepoDir := filepath.Join(parentDir, repoDir) | ||
cmd := []string{"clone", url, repoDir} | ||
log.Debug().Interface("cmd", cmd).Msg("clone command") | ||
clone := exec.Command("git", cmd...) | ||
clone.Dir = parentDir | ||
reset := exec.Command("git", "reset", "--hard", targetCommit) | ||
reset.Dir = absoluteCloneRepoDir | ||
|
||
clean := exec.Command("git", "clean", "--force") | ||
clean.Dir = absoluteCloneRepoDir | ||
|
||
output, err := clone.CombinedOutput() | ||
if err != nil { | ||
t.Fatal(err, "clone didn't work") | ||
} | ||
|
||
log.Debug().Msg(string(output)) | ||
output, _ = reset.CombinedOutput() | ||
|
||
log.Debug().Msg(string(output)) | ||
output, err = clean.CombinedOutput() | ||
|
||
log.Debug().Msg(string(output)) | ||
return absoluteCloneRepoDir, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,12 +22,10 @@ import ( | |
"fmt" | ||
"net/http" | ||
"os" | ||
"os/exec" | ||
"path/filepath" | ||
"testing" | ||
"time" | ||
|
||
"github.com/rs/zerolog/log" | ||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/google/uuid" | ||
|
@@ -44,7 +42,7 @@ func Test_SmokeScan_HTTPS(t *testing.T) { | |
if os.Getenv("SMOKE_TESTS") != "true" { | ||
t.Skip() | ||
} | ||
var cloneTargetDir, err = setupCustomTestRepo(t, "https://github.com/snyk-labs/nodejs-goof", "0336589") | ||
var cloneTargetDir, err = testutil.SetupCustomTestRepo(t, "https://github.com/snyk-labs/nodejs-goof", "0336589", "", "") | ||
assert.NoError(t, err) | ||
|
||
target, err := scan.NewRepositoryTarget(cloneTargetDir) | ||
|
@@ -90,7 +88,7 @@ func Test_SmokeScan_SSH(t *testing.T) { | |
if os.Getenv("SMOKE_TESTS") != "true" { | ||
t.Skip() | ||
} | ||
var cloneTargetDir, err = setupCustomTestRepo(t, "[email protected]:snyk-labs/nodejs-goof", "0336589") | ||
var cloneTargetDir, err = testutil.SetupCustomTestRepo(t, "[email protected]:snyk-labs/nodejs-goof", "0336589", "", "") | ||
assert.NoError(t, err) | ||
|
||
target, err := scan.NewRepositoryTarget(cloneTargetDir) | ||
|
@@ -172,36 +170,6 @@ func Test_SmokeScan_SubFolder(t *testing.T) { | |
require.NotNil(t, response) | ||
} | ||
|
||
func setupCustomTestRepo(t *testing.T, url string, targetCommit string) (string, error) { | ||
t.Helper() | ||
tempDir := t.TempDir() | ||
repoDir := "1" | ||
absoluteCloneRepoDir := filepath.Join(tempDir, repoDir) | ||
cmd := []string{"clone", url, repoDir} | ||
log.Debug().Interface("cmd", cmd).Msg("clone command") | ||
clone := exec.Command("git", cmd...) | ||
clone.Dir = tempDir | ||
reset := exec.Command("git", "reset", "--hard", targetCommit) | ||
reset.Dir = absoluteCloneRepoDir | ||
|
||
clean := exec.Command("git", "clean", "--force") | ||
clean.Dir = absoluteCloneRepoDir | ||
|
||
output, err := clone.CombinedOutput() | ||
if err != nil { | ||
t.Fatal(err, "clone didn't work") | ||
} | ||
|
||
log.Debug().Msg(string(output)) | ||
output, _ = reset.CombinedOutput() | ||
|
||
log.Debug().Msg(string(output)) | ||
output, err = clean.CombinedOutput() | ||
|
||
log.Debug().Msg(string(output)) | ||
return absoluteCloneRepoDir, err | ||
} | ||
|
||
type TestAuthRoundTripper struct { | ||
http.RoundTripper | ||
} | ||
|