Skip to content

Commit

Permalink
feat: added explicit test for git filebrowser clone, create tag, fetc…
Browse files Browse the repository at this point in the history
…h ref. Fixed tests for interactor that use FetchRef. Removed --fetch arg from Fetch()
  • Loading branch information
cameronbraid committed Apr 5, 2021
1 parent dd5ddf3 commit cf24ea4
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 14 deletions.
70 changes: 70 additions & 0 deletions pkg/filebrowser/git_file_browser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ package filebrowser

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"testing"

"github.com/jenkins-x/go-scm/scm"
"github.com/jenkins-x/lighthouse/pkg/git/v2"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
Expand Down Expand Up @@ -93,3 +97,69 @@ func assertNoScmFileExists(t *testing.T, files []*scm.FileEntry, name, message s
}
t.Logf("correctly does not include file name %s for %s", name, message)
}

func TestGitFileBrowser_Clone_CreateTag_FetchRef(t *testing.T) {
logger := logrus.WithField("client", "git")

baseDir, err := ioutil.TempDir("", "localdir")
require.NoError(t, err, "failed to find git binary")
fmt.Println(baseDir)

defer os.RemoveAll(baseDir)

repoDir := filepath.Join(baseDir, "org", "repo")

err = os.MkdirAll(repoDir, 0700)
require.NoError(t, err, "failed to make repo dir")

userGetter := func() (name, email string, err error) {
return "bot", "bot@example.com", nil
}
censor := func(content []byte) []byte { return content }

executor, err := git.NewCensoringExecutor(repoDir, censor, logger)
require.NoError(t, err, "failed to find git binary")

err = ioutil.WriteFile(filepath.Join(repoDir, "README.md"), []byte("README"), 0600)
require.NoError(t, err, "failed to write README.md file")

_, err = executor.Run("init", ".")
require.NoError(t, err, "failed to init git repo")

_, err = executor.Run("add", "README.md")
require.NoError(t, err, "failed to add README.md status")

_, err = executor.Run("commit", "-m", "add README.md")
require.NoError(t, err, "failed to add README.md status")

cf, err := git.NewLocalClientFactory(baseDir, userGetter, censor)
require.NoError(t, err, "failed to create git client factory")
fb := NewFileBrowserFromGitClient(cf)

files, err := fb.ListFiles("org", "repo", "", "master")
require.NoError(t, err, "failed to list files")

require.True(t, len(files) == 1, "exepecting 1 file")
require.Equal(t, files[0].Name, "README.md")

_, err = executor.Run("checkout", "-b", "update-1")
require.NoError(t, err, "failed to create new branch")

err = ioutil.WriteFile(filepath.Join(repoDir, "README.md"), []byte("README-update-1"), 0600)
require.NoError(t, err, "failed write updated README.md")

_, err = executor.Run("commit", "-a", "-m", "update README.md")
require.NoError(t, err, "failed to update README.md status")

_, err = executor.Run("tag", "v0.0.1")
require.NoError(t, err, "failed to create v0.0.1 tag")

files, err = fb.ListFiles("org", "repo", "", "v0.0.1")
require.NoError(t, err, "failed to lst files in v0.0.1 tag")
require.True(t, len(files) == 1, "exepecting 1 file")
require.Equal(t, files[0].Name, "README.md")

data, err := fb.GetFile("org", "repo", "README.md", "v0.0.1")
require.NoError(t, err, "failed to lst files in v0.0.1 tag")
require.Equal(t, string(data), "README-update-1")
}
4 changes: 2 additions & 2 deletions pkg/git/v2/interactor.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ func (i *interactor) Fetch() error {
return fmt.Errorf("could not resolve remote for fetching: %v", err)
}
i.logger.Debugf("Fetching from %s", remote)
if out, err := i.executor.Run("fetch", remote, "--tags"); err != nil {
if out, err := i.executor.Run("fetch", remote); err != nil {
return fmt.Errorf("error fetching: %v %v", err, string(out))
}
return nil
Expand All @@ -294,7 +294,7 @@ func (i *interactor) FetchRef(refspec string) error {
return fmt.Errorf("could not resolve remote for fetching: %v", err)
}
i.logger.Debugf("Fetching %q from %s", refspec, remote)
if out, err := i.executor.Run("fetch", remote, refspec, "--tags"); err != nil {
if out, err := i.executor.Run("fetch", "--tags", remote, refspec); err != nil {
return fmt.Errorf("error fetching %q: %v %v", refspec, err, string(out))
}
return nil
Expand Down
24 changes: 12 additions & 12 deletions pkg/git/v2/interactor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1174,12 +1174,12 @@ func TestInteractor_FetchRef(t *testing.T) {
return "someone.com", nil
},
responses: map[string]execResponse{
"fetch someone.com shasum": {
"fetch --tags someone.com shasum": {
out: []byte(`ok`),
},
},
expectedCalls: [][]string{
{"fetch", "someone.com", "shasum"},
{"fetch", "--tags", "someone.com", "shasum"},
},
expectedErr: false,
},
Expand All @@ -1200,12 +1200,12 @@ func TestInteractor_FetchRef(t *testing.T) {
return "someone.com", nil
},
responses: map[string]execResponse{
"fetch someone.com shasum": {
"fetch --tags someone.com shasum": {
err: errors.New("oops"),
},
},
expectedCalls: [][]string{
{"fetch", "someone.com", "shasum"},
{"fetch", "--tags", "someone.com", "shasum"},
},
expectedErr: true,
},
Expand Down Expand Up @@ -1340,7 +1340,7 @@ func TestInteractor_CheckoutPullRequest(t *testing.T) {
return "someone.com", nil
},
responses: map[string]execResponse{
"fetch someone.com pull/1/head": {
"fetch --tags someone.com pull/1/head": {
out: []byte(`ok`),
},
"checkout FETCH_HEAD": {
Expand All @@ -1351,7 +1351,7 @@ func TestInteractor_CheckoutPullRequest(t *testing.T) {
},
},
expectedCalls: [][]string{
{"fetch", "someone.com", "pull/1/head"},
{"fetch", "--tags", "someone.com", "pull/1/head"},
{"checkout", "FETCH_HEAD"},
{"checkout", "-b", "pull1"},
},
Expand All @@ -1374,12 +1374,12 @@ func TestInteractor_CheckoutPullRequest(t *testing.T) {
return "someone.com", nil
},
responses: map[string]execResponse{
"fetch someone.com pull/1/head": {
"fetch --tags someone.com pull/1/head": {
err: errors.New("oops"),
},
},
expectedCalls: [][]string{
{"fetch", "someone.com", "pull/1/head"},
{"fetch", "--tags", "someone.com", "pull/1/head"},
},
expectedErr: true,
},
Expand All @@ -1390,15 +1390,15 @@ func TestInteractor_CheckoutPullRequest(t *testing.T) {
return "someone.com", nil
},
responses: map[string]execResponse{
"fetch someone.com pull/1/head": {
"fetch --tags someone.com pull/1/head": {
out: []byte(`ok`),
},
"checkout FETCH_HEAD": {
err: errors.New("oops"),
},
},
expectedCalls: [][]string{
{"fetch", "someone.com", "pull/1/head"},
{"fetch", "--tags", "someone.com", "pull/1/head"},
{"checkout", "FETCH_HEAD"},
},
expectedErr: true,
Expand All @@ -1410,7 +1410,7 @@ func TestInteractor_CheckoutPullRequest(t *testing.T) {
return "someone.com", nil
},
responses: map[string]execResponse{
"fetch someone.com pull/1/head": {
"fetch --tags someone.com pull/1/head": {
out: []byte(`ok`),
},
"checkout FETCH_HEAD": {
Expand All @@ -1421,7 +1421,7 @@ func TestInteractor_CheckoutPullRequest(t *testing.T) {
},
},
expectedCalls: [][]string{
{"fetch", "someone.com", "pull/1/head"},
{"fetch", "--tags", "someone.com", "pull/1/head"},
{"checkout", "FETCH_HEAD"},
{"checkout", "-b", "pull1"},
},
Expand Down

0 comments on commit cf24ea4

Please sign in to comment.