Skip to content

Commit

Permalink
test: improve tests
Browse files Browse the repository at this point in the history
  • Loading branch information
christophwitzko committed Nov 2, 2020
1 parent 1e2978d commit c70d912
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 28 deletions.
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ jobs:
with:
go-version: 1.15
- run: go build ./cmd/provider-git/
- run: ./scripts/start-gitea.sh
- run: go test -v ./...
release:
runs-on: ubuntu-latest
Expand Down
39 changes: 38 additions & 1 deletion pkg/provider/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@ import (

"github.com/Masterminds/semver/v3"
"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/config"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/go-git/go-git/v5/plumbing/storer"
"github.com/go-git/go-git/v5/plumbing/transport"
"github.com/go-git/go-git/v5/plumbing/transport/http"
"github.com/go-git/go-git/v5/plumbing/transport/ssh"
"github.com/go-semantic-release/semantic-release/v2/pkg/provider"
"github.com/go-semantic-release/semantic-release/v2/pkg/semrel"
)
Expand All @@ -21,6 +25,8 @@ type Repository struct {
defaultBranch string
taggerName string
taggerEmail string
remoteName string
auth transport.AuthMethod
repo *git.Repository
}

Expand All @@ -40,6 +46,30 @@ func (repo *Repository) Init(config map[string]string) error {
repo.taggerEmail = "[email protected]"
}

repo.remoteName = config["remote_name"]
if repo.remoteName == "" {
repo.remoteName = "origin"
}

if config["auth_username"] == "" {
config["auth_username"] = "git"
}

if config["auth"] == "basic" {
repo.auth = &http.BasicAuth{
Username: config["auth_username"],
Password: config["auth_password"],
}
} else if config["auth"] == "ssh" {
auth, err := ssh.NewPublicKeysFromFile(config["auth_username"], config["auth_private_key"], config["auth_password"])
if err != nil {
return err
}
repo.auth = auth
} else {
repo.auth = nil
}

gitPath := config["git_path"]
if gitPath == "" {
gitPath = "."
Expand Down Expand Up @@ -144,7 +174,14 @@ func (repo *Repository) CreateRelease(release *provider.CreateReleaseConfig) err
if err != nil {
return err
}
return nil
err = repo.repo.Push(&git.PushOptions{
RemoteName: repo.remoteName,
RefSpecs: []config.RefSpec{
config.RefSpec(fmt.Sprintf("refs/tags/%s:refs/tags/%s", tag, tag)),
},
Auth: repo.auth,
})
return err
}

func (repo *Repository) Name() string {
Expand Down
92 changes: 65 additions & 27 deletions pkg/provider/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,49 @@ import (
"time"

"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/config"
"github.com/go-git/go-git/v5/plumbing"
"github.com/go-git/go-git/v5/plumbing/object"
"github.com/go-git/go-git/v5/plumbing/transport/http"
"github.com/go-semantic-release/semantic-release/v2/pkg/provider"
"github.com/stretchr/testify/require"
)

func TestNewRepository(t *testing.T) {
var testGitPath string

func TestGit(t *testing.T) {
var err error
testGitPath, err = setupRepo()
require.NoError(t, err)
t.Run("NewRepository", newRepository)
t.Run("GetInfo", getInfo)
t.Run("GetReleases", getReleases)
t.Run("GetCommits", getCommits)
t.Run("CreateRelease", createRelease)
}

func newRepository(t *testing.T) {
require := require.New(t)
repo := &Repository{}
err := repo.Init(map[string]string{})
require.EqualError(err, "repository does not exist")

gitPath, err := setupRepo()
require.NoError(err)

repo = &Repository{}
err = repo.Init(map[string]string{
"git_path": gitPath,
"git_path": testGitPath,
"default_branch": "development",
"tagger_name": "test",
"tagger_email": "[email protected]",
"auth": "basic",
"auth_username": "test",
"auth_password": "test",
})

require.NoError(err)
require.Equal(repo.defaultBranch, "development")
require.Equal(repo.taggerName, "test")
require.Equal(repo.taggerEmail, "[email protected]")
require.Equal("development", repo.defaultBranch)
require.Equal("test", repo.taggerName)
require.Equal("[email protected]", repo.taggerEmail)
require.NotNil(repo.auth)
}

func setupRepo() (string, error) {
Expand All @@ -45,6 +62,14 @@ func setupRepo() (string, error) {
if err != nil {
return "", err
}

_, err = repo.CreateRemote(&config.RemoteConfig{
Name: "origin",
URLs: []string{"http://localhost:3000/test/test.git"},
})
if err != nil {
return "", err
}
w, err := repo.Worktree()
if err != nil {
return "", err
Expand Down Expand Up @@ -91,38 +116,51 @@ func setupRepo() (string, error) {
return "", err
}

return dir, nil
}

func createRepo() (*Repository, string, error) {
gitPath, err := setupRepo()
err = repo.Push(&git.PushOptions{
RemoteName: "origin",
RefSpecs: []config.RefSpec{
"refs/heads/*:refs/heads/*",
"refs/tags/*:refs/tags/*",
},
Auth: &http.BasicAuth{
Username: "test",
Password: "test",
},
Force: true,
})
if err != nil {
return nil, "", err
return "", err
}
return dir, nil
}

func createRepo() (*Repository, error) {
repo := &Repository{}
err = repo.Init(map[string]string{
"git_path": gitPath,
err := repo.Init(map[string]string{
"git_path": testGitPath,
"auth": "basic",
"auth_username": "test",
"auth_password": "test",
})
if err != nil {
return nil, "", err
return nil, err
}

return repo, gitPath, nil
return repo, nil
}

func TestGetInfo(t *testing.T) {
func getInfo(t *testing.T) {
require := require.New(t)
repo, _, err := createRepo()
repo, err := createRepo()
require.NoError(err)
repoInfo, err := repo.GetInfo()
require.NoError(err)
require.Equal("master", repoInfo.DefaultBranch)
}

func TestGetCommits(t *testing.T) {
func getCommits(t *testing.T) {
require := require.New(t)
repo, _, err := createRepo()
repo, err := createRepo()
require.NoError(err)
commits, err := repo.GetCommits("", "master")
require.NoError(err)
Expand All @@ -133,12 +171,12 @@ func TestGetCommits(t *testing.T) {
}
}

func TestGithubCreateRelease(t *testing.T) {
func createRelease(t *testing.T) {
require := require.New(t)
repo, gitPath, err := createRepo()
repo, err := createRepo()
require.NoError(err)

gRepo, err := git.PlainOpen(gitPath)
gRepo, err := git.PlainOpen(testGitPath)
require.NoError(err)
head, err := gRepo.Head()
require.NoError(err)
Expand All @@ -159,9 +197,9 @@ func TestGithubCreateRelease(t *testing.T) {
require.Equal("new feature\n", tagObj.Message)
}

func TestGetReleases(t *testing.T) {
func getReleases(t *testing.T) {
require := require.New(t)
repo, _, err := createRepo()
repo, err := createRepo()
require.NoError(err)

releases, err := repo.GetReleases("")
Expand Down

0 comments on commit c70d912

Please sign in to comment.