Skip to content

Commit

Permalink
test: fix git clone tests (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
teodora-sandu authored May 14, 2024
1 parent 746136d commit 1d45bef
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 54 deletions.
41 changes: 21 additions & 20 deletions internal/util/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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"))
Expand All @@ -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)
Expand Down
42 changes: 42 additions & 0 deletions internal/util/testutil/clone.go
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
}
36 changes: 2 additions & 34 deletions scan_smoke_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 1d45bef

Please sign in to comment.