Skip to content

Commit

Permalink
fix: added tests and improved code
Browse files Browse the repository at this point in the history
  • Loading branch information
caarlos0 committed May 23, 2019
1 parent 03d57a8 commit 8d181a6
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 47 deletions.
31 changes: 17 additions & 14 deletions actions/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,20 +49,23 @@ func TestRunEvent(t *testing.T) {
log.SetLevel(log.DebugLevel)

for _, table := range tables {
runnerConfig := &RunnerConfig{
Ctx: context.Background(),
WorkflowPath: table.workflowPath,
WorkingDir: "testdata",
EventName: table.eventName,
}
runner, err := NewRunner(runnerConfig)
assert.NilError(t, err, table.workflowPath)

err = runner.RunEvent()
if table.errorMessage == "" {
table := table
t.Run(table.workflowPath, func(t *testing.T) {
runnerConfig := &RunnerConfig{
Ctx: context.Background(),
WorkflowPath: table.workflowPath,
WorkingDir: "testdata",
EventName: table.eventName,
}
runner, err := NewRunner(runnerConfig)
assert.NilError(t, err, table.workflowPath)
} else {
assert.ErrorContains(t, err, table.errorMessage)
}

err = runner.RunEvent()
if table.errorMessage == "" {
assert.NilError(t, err, table.workflowPath)
} else {
assert.ErrorContains(t, err, table.errorMessage)
}
})
}
}
14 changes: 11 additions & 3 deletions actions/testdata/env.workflow
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
workflow "test" {
on = "push"
resolves = ["test-action"]
resolves = [
"test-action-repo",
"test-action-ref",
]
}

action "test-action" {
action "test-action-repo" {
uses = "docker://alpine:3.9"
runs = ["sh", "-c", "echo $GITHUB_REPOSITORY | grep '^nektos/act$'"]
}
}

action "test-action-ref" {
uses = "docker://alpine:3.9"
runs = ["sh", "-c", "echo $GITHUB_REF | grep '^refs/'"]
}
36 changes: 6 additions & 30 deletions common/git.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package common

import (
"bufio"
"bytes"
"errors"
"fmt"
"io/ioutil"
Expand All @@ -17,7 +15,6 @@ import (
log "github.com/sirupsen/logrus"
git "gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing"
yaml "gopkg.in/yaml.v2"
)

var (
Expand All @@ -36,15 +33,16 @@ func FindGitRevision(file string) (shortSha string, sha string, err error) {
return "", "", err
}

ref, err := FindGitRef(file)
bts, err := ioutil.ReadFile(filepath.Join(gitDir, "HEAD"))
if err != nil {
return "", "", err
}

var ref = strings.TrimSpace(strings.TrimPrefix(string(bts), "ref:"))
var refBuf []byte
if strings.HasPrefix(ref, "refs/") {
// load commitid ref
refBuf, err = ioutil.ReadFile(fmt.Sprintf("%s/%s", gitDir, ref))
refBuf, err = ioutil.ReadFile(filepath.Join(gitDir, ref))
if err != nil {
return "", "", err
}
Expand All @@ -62,34 +60,13 @@ func FindGitRef(file string) (string, error) {
if err != nil {
return "", err
}
log.Infof("Loading revision from git directory '%s'", gitDir)
log.Debugf("Loading revision from git directory '%s'", gitDir)

// load HEAD ref
headFile, err := os.Open(fmt.Sprintf("%s/HEAD", gitDir))
_, ref, err := FindGitRevision(file)
if err != nil {
return "", err
}
defer func() {
headFile.Close()
}()

headBuffer := new(bytes.Buffer)
_, err = headBuffer.ReadFrom(bufio.NewReader(headFile))
if err != nil {
log.Error(err)
}
headBytes := headBuffer.Bytes()

var ref string
head := make(map[string]string)
err = yaml.Unmarshal(headBytes, head)
if err != nil {
ref = string(headBytes)
} else {
ref = head["ref"]
}

ref = strings.TrimSpace(ref)
log.Debugf("HEAD points to '%s'", ref)

tag, err := findGitPrettyRef(ref, gitDir)
Expand Down Expand Up @@ -200,7 +177,7 @@ func findGitDirectory(fromFile string) (string, error) {
dir = path.Dir(absPath)
}

gitPath := path.Join(dir, ".git")
gitPath := filepath.Join(dir, ".git")
fi, err = os.Stat(gitPath)
if err == nil && fi.Mode().IsDir() {
return gitPath, nil
Expand All @@ -209,7 +186,6 @@ func findGitDirectory(fromFile string) (string, error) {
}

return findGitDirectory(filepath.Dir(dir))

}

// NewGitCloneExecutorInput the input for the NewGitCloneExecutor
Expand Down
82 changes: 82 additions & 0 deletions common/git_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import (
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"syscall"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestFindGitSlug(t *testing.T) {
Expand Down Expand Up @@ -61,6 +63,86 @@ func TestFindGitRemoteURL(t *testing.T) {
assert.Equal(remoteURL, u)
}

func TestGitFindRef(t *testing.T) {
basedir, err := ioutil.TempDir("", "act-test")
defer os.RemoveAll(basedir)
assert.NoError(t, err)

for name, tt := range map[string]struct {
Prepare func(t *testing.T, dir string)
Assert func(t *testing.T, ref string, err error)
}{
"new_repo": {
Prepare: func(t *testing.T, dir string) {},
Assert: func(t *testing.T, ref string, err error) {
require.Error(t, err)
},
},
"new_repo_with_commit": {
Prepare: func(t *testing.T, dir string) {
require.NoError(t, gitCmd("-C", dir, "commit", "--allow-empty", "-m", "msg"))
},
Assert: func(t *testing.T, ref string, err error) {
require.NoError(t, err)
require.Equal(t, "refs/heads/master", ref)
},
},
"current_head_is_tag": {
Prepare: func(t *testing.T, dir string) {
require.NoError(t, gitCmd("-C", dir, "commit", "--allow-empty", "-m", "commit msg"))
require.NoError(t, gitCmd("-C", dir, "tag", "v1.2.3"))
require.NoError(t, gitCmd("-C", dir, "checkout", "v1.2.3"))
},
Assert: func(t *testing.T, ref string, err error) {
require.NoError(t, err)
require.Equal(t, "refs/tags/v1.2.3", ref)
},
},
"current_head_is_same_as_tag": {
Prepare: func(t *testing.T, dir string) {
require.NoError(t, gitCmd("-C", dir, "commit", "--allow-empty", "-m", "1.4.2 release"))
require.NoError(t, gitCmd("-C", dir, "tag", "v1.4.2"))
},
Assert: func(t *testing.T, ref string, err error) {
require.NoError(t, err)
require.Equal(t, "refs/tags/v1.4.2", ref)
},
},
"current_head_is_not_tag": {
Prepare: func(t *testing.T, dir string) {
require.NoError(t, gitCmd("-C", dir, "commit", "--allow-empty", "-m", "msg"))
require.NoError(t, gitCmd("-C", dir, "tag", "v1.4.2"))
require.NoError(t, gitCmd("-C", dir, "commit", "--allow-empty", "-m", "msg2"))
},
Assert: func(t *testing.T, ref string, err error) {
require.NoError(t, err)
require.Equal(t, "refs/heads/master", ref)
},
},
"current_head_is_another_branch": {
Prepare: func(t *testing.T, dir string) {
require.NoError(t, gitCmd("-C", dir, "checkout", "-b", "mybranch"))
require.NoError(t, gitCmd("-C", dir, "commit", "--allow-empty", "-m", "msg"))
},
Assert: func(t *testing.T, ref string, err error) {
require.NoError(t, err)
require.Equal(t, "refs/heads/mybranch", ref)
},
},
} {
tt := tt
name := name
t.Run(name, func(t *testing.T) {
dir := filepath.Join(basedir, name)
require.NoError(t, os.MkdirAll(dir, 0755))
require.NoError(t, gitCmd("-C", dir, "init"))
tt.Prepare(t, dir)
ref, err := FindGitRef(dir)
tt.Assert(t, ref, err)
})
}
}

func gitCmd(args ...string) error {
var stdout bytes.Buffer
cmd := exec.Command("git", args...)
Expand Down

0 comments on commit 8d181a6

Please sign in to comment.