fix: added tests and improved code

This commit is contained in:
Carlos Alexandro Becker 2019-05-23 01:39:57 -03:00
parent 03d57a80c9
commit 8d181a6a1e
No known key found for this signature in database
GPG Key ID: E61E2F7DC14AB940
4 changed files with 116 additions and 47 deletions

View File

@ -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)
}
})
}
}

View File

@ -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/'"]
}

View File

@ -1,8 +1,6 @@
package common
import (
"bufio"
"bytes"
"errors"
"fmt"
"io/ioutil"
@ -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 (
@ -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
}
@ -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)
@ -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
@ -209,7 +186,6 @@ func findGitDirectory(fromFile string) (string, error) {
}
return findGitDirectory(filepath.Dir(dir))
}
// NewGitCloneExecutorInput the input for the NewGitCloneExecutor

View File

@ -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) {
@ -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...)