2019-01-12 22:45:25 -06:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
2021-01-14 23:24:17 -06:00
|
|
|
"context"
|
2019-01-12 22:45:25 -06:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
2019-05-22 23:39:57 -05:00
|
|
|
"path/filepath"
|
2019-01-12 22:45:25 -06:00
|
|
|
"syscall"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2019-05-22 23:39:57 -05:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-01-12 22:45:25 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestFindGitSlug(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
var slugTests = []struct {
|
|
|
|
url string // input
|
|
|
|
provider string // expected result
|
|
|
|
slug string // expected result
|
|
|
|
}{
|
|
|
|
{"https://git-codecommit.us-east-1.amazonaws.com/v1/repos/my-repo-name", "CodeCommit", "my-repo-name"},
|
|
|
|
{"ssh://git-codecommit.us-west-2.amazonaws.com/v1/repos/my-repo", "CodeCommit", "my-repo"},
|
|
|
|
{"git@github.com:nektos/act.git", "GitHub", "nektos/act"},
|
|
|
|
{"https://github.com/nektos/act.git", "GitHub", "nektos/act"},
|
|
|
|
{"http://github.com/nektos/act.git", "GitHub", "nektos/act"},
|
2019-02-14 14:35:02 -06:00
|
|
|
{"https://github.com/nektos/act", "GitHub", "nektos/act"},
|
|
|
|
{"http://github.com/nektos/act", "GitHub", "nektos/act"},
|
2019-02-12 22:08:07 -06:00
|
|
|
{"git+ssh://git@github.com/owner/repo.git", "GitHub", "owner/repo"},
|
2019-01-12 22:45:25 -06:00
|
|
|
{"http://myotherrepo.com/act.git", "", "http://myotherrepo.com/act.git"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range slugTests {
|
|
|
|
provider, slug, err := findGitSlug(tt.url)
|
|
|
|
|
2020-07-20 09:35:43 -05:00
|
|
|
assert.NoError(err)
|
2019-01-12 22:45:25 -06:00
|
|
|
assert.Equal(tt.provider, provider)
|
|
|
|
assert.Equal(tt.slug, slug)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-07-20 09:35:43 -05:00
|
|
|
func testDir(t *testing.T) string {
|
2019-01-12 22:45:25 -06:00
|
|
|
basedir, err := ioutil.TempDir("", "act-test")
|
2020-07-20 09:35:43 -05:00
|
|
|
require.NoError(t, err)
|
|
|
|
t.Cleanup(func() { os.RemoveAll(basedir) })
|
|
|
|
return basedir
|
|
|
|
}
|
2019-01-12 22:45:25 -06:00
|
|
|
|
2021-01-12 00:35:57 -06:00
|
|
|
func cleanGitHooks(dir string) error {
|
|
|
|
hooksDir := filepath.Join(dir, ".git", "hooks")
|
|
|
|
files, err := ioutil.ReadDir(hooksDir)
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, f := range files {
|
|
|
|
if f.IsDir() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
relName := filepath.Join(hooksDir, f.Name())
|
|
|
|
if err := os.Remove(relName); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-20 09:35:43 -05:00
|
|
|
func TestFindGitRemoteURL(t *testing.T) {
|
|
|
|
assert := assert.New(t)
|
2019-01-12 22:45:25 -06:00
|
|
|
|
2020-07-20 09:35:43 -05:00
|
|
|
basedir := testDir(t)
|
2020-02-20 20:01:12 -06:00
|
|
|
gitConfig()
|
2020-07-20 09:35:43 -05:00
|
|
|
err := gitCmd("init", basedir)
|
|
|
|
assert.NoError(err)
|
2021-01-12 00:35:57 -06:00
|
|
|
err = cleanGitHooks(basedir)
|
|
|
|
assert.NoError(err)
|
2019-01-12 22:45:25 -06:00
|
|
|
|
|
|
|
remoteURL := "https://git-codecommit.us-east-1.amazonaws.com/v1/repos/my-repo-name"
|
|
|
|
err = gitCmd("config", "-f", fmt.Sprintf("%s/.git/config", basedir), "--add", "remote.origin.url", remoteURL)
|
2020-07-20 09:35:43 -05:00
|
|
|
assert.NoError(err)
|
2019-01-12 22:45:25 -06:00
|
|
|
|
|
|
|
u, err := findGitRemoteURL(basedir)
|
2020-07-20 09:35:43 -05:00
|
|
|
assert.NoError(err)
|
2019-01-12 22:45:25 -06:00
|
|
|
assert.Equal(remoteURL, u)
|
|
|
|
}
|
|
|
|
|
2019-05-22 23:39:57 -05:00
|
|
|
func TestGitFindRef(t *testing.T) {
|
2020-07-20 09:35:43 -05:00
|
|
|
basedir := testDir(t)
|
2020-02-20 20:01:12 -06:00
|
|
|
gitConfig()
|
2019-05-28 11:47:40 -05:00
|
|
|
|
2019-05-22 23:39:57 -05:00
|
|
|
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))
|
2021-03-30 14:26:25 -05:00
|
|
|
require.NoError(t, gitCmd("-C", dir, "init", "--initial-branch=master"))
|
2021-01-12 00:35:57 -06:00
|
|
|
require.NoError(t, cleanGitHooks(dir))
|
2019-05-22 23:39:57 -05:00
|
|
|
tt.Prepare(t, dir)
|
|
|
|
ref, err := FindGitRef(dir)
|
|
|
|
tt.Assert(t, ref, err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-14 23:24:17 -06:00
|
|
|
func TestGitCloneExecutor(t *testing.T) {
|
|
|
|
for name, tt := range map[string]struct {
|
|
|
|
URL string
|
|
|
|
Ref string
|
|
|
|
Err error
|
|
|
|
}{
|
|
|
|
"tag": {
|
|
|
|
URL: "https://github.com/actions/checkout",
|
|
|
|
Ref: "v2",
|
|
|
|
Err: nil,
|
|
|
|
},
|
|
|
|
"branch": {
|
|
|
|
URL: "https://github.com/anchore/scan-action",
|
|
|
|
Ref: "act-fails",
|
|
|
|
Err: nil,
|
|
|
|
},
|
2021-02-23 11:50:28 -06:00
|
|
|
"sha": {
|
|
|
|
URL: "https://github.com/actions/checkout",
|
|
|
|
Ref: "5a4ac9002d0be2fb38bd78e4b4dbde5606d7042f", // v2
|
|
|
|
Err: nil,
|
|
|
|
},
|
2021-01-14 23:24:17 -06:00
|
|
|
} {
|
|
|
|
tt := tt
|
|
|
|
name := name
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
clone := NewGitCloneExecutor(NewGitCloneExecutorInput{
|
|
|
|
URL: tt.URL,
|
|
|
|
Ref: tt.Ref,
|
|
|
|
Dir: testDir(t),
|
|
|
|
})
|
|
|
|
err := clone(context.Background())
|
|
|
|
assert.ErrorIs(t, err, tt.Err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-28 11:47:40 -05:00
|
|
|
func gitConfig() {
|
2020-02-20 20:01:12 -06:00
|
|
|
if os.Getenv("GITHUB_ACTIONS") == "true" {
|
|
|
|
_ = gitCmd("config", "--global", "user.email", "test@test.com")
|
|
|
|
_ = gitCmd("config", "--global", "user.name", "Unit Test")
|
|
|
|
}
|
2019-05-28 11:47:40 -05:00
|
|
|
}
|
|
|
|
|
2019-01-12 22:45:25 -06:00
|
|
|
func gitCmd(args ...string) error {
|
|
|
|
cmd := exec.Command("git", args...)
|
2019-05-28 11:47:40 -05:00
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stderr
|
2019-01-12 22:45:25 -06:00
|
|
|
|
|
|
|
err := cmd.Run()
|
|
|
|
if exitError, ok := err.(*exec.ExitError); ok {
|
|
|
|
if waitStatus, ok := exitError.Sys().(syscall.WaitStatus); ok {
|
|
|
|
return fmt.Errorf("Exit error %d", waitStatus.ExitStatus())
|
|
|
|
}
|
|
|
|
return exitError
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|