fix: added tests and improved code
This commit is contained in:
parent
03d57a80c9
commit
8d181a6a1e
|
@ -49,20 +49,23 @@ func TestRunEvent(t *testing.T) {
|
||||||
log.SetLevel(log.DebugLevel)
|
log.SetLevel(log.DebugLevel)
|
||||||
|
|
||||||
for _, table := range tables {
|
for _, table := range tables {
|
||||||
runnerConfig := &RunnerConfig{
|
table := table
|
||||||
Ctx: context.Background(),
|
t.Run(table.workflowPath, func(t *testing.T) {
|
||||||
WorkflowPath: table.workflowPath,
|
runnerConfig := &RunnerConfig{
|
||||||
WorkingDir: "testdata",
|
Ctx: context.Background(),
|
||||||
EventName: table.eventName,
|
WorkflowPath: table.workflowPath,
|
||||||
}
|
WorkingDir: "testdata",
|
||||||
runner, err := NewRunner(runnerConfig)
|
EventName: table.eventName,
|
||||||
assert.NilError(t, err, table.workflowPath)
|
}
|
||||||
|
runner, err := NewRunner(runnerConfig)
|
||||||
err = runner.RunEvent()
|
|
||||||
if table.errorMessage == "" {
|
|
||||||
assert.NilError(t, err, table.workflowPath)
|
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)
|
||||||
|
}
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,17 @@
|
||||||
workflow "test" {
|
workflow "test" {
|
||||||
on = "push"
|
on = "push"
|
||||||
resolves = ["test-action"]
|
resolves = [
|
||||||
|
"test-action-repo",
|
||||||
|
"test-action-ref",
|
||||||
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
action "test-action" {
|
action "test-action-repo" {
|
||||||
uses = "docker://alpine:3.9"
|
uses = "docker://alpine:3.9"
|
||||||
runs = ["sh", "-c", "echo $GITHUB_REPOSITORY | grep '^nektos/act$'"]
|
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/'"]
|
||||||
|
}
|
||||||
|
|
|
@ -1,8 +1,6 @@
|
||||||
package common
|
package common
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
|
||||||
"bytes"
|
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
@ -17,7 +15,6 @@ import (
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
git "gopkg.in/src-d/go-git.v4"
|
git "gopkg.in/src-d/go-git.v4"
|
||||||
"gopkg.in/src-d/go-git.v4/plumbing"
|
"gopkg.in/src-d/go-git.v4/plumbing"
|
||||||
yaml "gopkg.in/yaml.v2"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -36,15 +33,16 @@ func FindGitRevision(file string) (shortSha string, sha string, err error) {
|
||||||
return "", "", err
|
return "", "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
ref, err := FindGitRef(file)
|
bts, err := ioutil.ReadFile(filepath.Join(gitDir, "HEAD"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", "", err
|
return "", "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var ref = strings.TrimSpace(strings.TrimPrefix(string(bts), "ref:"))
|
||||||
var refBuf []byte
|
var refBuf []byte
|
||||||
if strings.HasPrefix(ref, "refs/") {
|
if strings.HasPrefix(ref, "refs/") {
|
||||||
// load commitid ref
|
// load commitid ref
|
||||||
refBuf, err = ioutil.ReadFile(fmt.Sprintf("%s/%s", gitDir, ref))
|
refBuf, err = ioutil.ReadFile(filepath.Join(gitDir, ref))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", "", err
|
return "", "", err
|
||||||
}
|
}
|
||||||
|
@ -62,34 +60,13 @@ func FindGitRef(file string) (string, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
log.Infof("Loading revision from git directory '%s'", gitDir)
|
log.Debugf("Loading revision from git directory '%s'", gitDir)
|
||||||
|
|
||||||
// load HEAD ref
|
_, ref, err := FindGitRevision(file)
|
||||||
headFile, err := os.Open(fmt.Sprintf("%s/HEAD", gitDir))
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
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)
|
log.Debugf("HEAD points to '%s'", ref)
|
||||||
|
|
||||||
tag, err := findGitPrettyRef(ref, gitDir)
|
tag, err := findGitPrettyRef(ref, gitDir)
|
||||||
|
@ -200,7 +177,7 @@ func findGitDirectory(fromFile string) (string, error) {
|
||||||
dir = path.Dir(absPath)
|
dir = path.Dir(absPath)
|
||||||
}
|
}
|
||||||
|
|
||||||
gitPath := path.Join(dir, ".git")
|
gitPath := filepath.Join(dir, ".git")
|
||||||
fi, err = os.Stat(gitPath)
|
fi, err = os.Stat(gitPath)
|
||||||
if err == nil && fi.Mode().IsDir() {
|
if err == nil && fi.Mode().IsDir() {
|
||||||
return gitPath, nil
|
return gitPath, nil
|
||||||
|
@ -209,7 +186,6 @@ func findGitDirectory(fromFile string) (string, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
return findGitDirectory(filepath.Dir(dir))
|
return findGitDirectory(filepath.Dir(dir))
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewGitCloneExecutorInput the input for the NewGitCloneExecutor
|
// NewGitCloneExecutorInput the input for the NewGitCloneExecutor
|
||||||
|
|
|
@ -6,10 +6,12 @@ import (
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"path/filepath"
|
||||||
"syscall"
|
"syscall"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestFindGitSlug(t *testing.T) {
|
func TestFindGitSlug(t *testing.T) {
|
||||||
|
@ -61,6 +63,86 @@ func TestFindGitRemoteURL(t *testing.T) {
|
||||||
assert.Equal(remoteURL, u)
|
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 {
|
func gitCmd(args ...string) error {
|
||||||
var stdout bytes.Buffer
|
var stdout bytes.Buffer
|
||||||
cmd := exec.Command("git", args...)
|
cmd := exec.Command("git", args...)
|
||||||
|
|
Loading…
Reference in New Issue