2020-02-13 01:27:37 -06:00
|
|
|
package runner
|
|
|
|
|
|
|
|
import (
|
2020-11-17 11:31:05 -06:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"regexp"
|
2021-03-12 18:25:10 -06:00
|
|
|
"sort"
|
2020-02-13 01:27:37 -06:00
|
|
|
"testing"
|
|
|
|
|
2020-02-13 13:47:38 -06:00
|
|
|
"github.com/nektos/act/pkg/model"
|
2021-05-18 01:14:49 -05:00
|
|
|
assert "github.com/stretchr/testify/assert"
|
2021-08-09 10:35:05 -05:00
|
|
|
yaml "gopkg.in/yaml.v3"
|
2020-02-13 01:27:37 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestEvaluate(t *testing.T) {
|
2021-08-09 10:35:05 -05:00
|
|
|
var yml yaml.Node
|
|
|
|
err := yml.Encode(map[string][]interface{}{
|
|
|
|
"os": {"Linux", "Windows"},
|
|
|
|
"foo": {"bar", "baz"},
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
2020-02-13 01:27:37 -06:00
|
|
|
rc := &RunContext{
|
|
|
|
Config: &Config{
|
|
|
|
Workdir: ".",
|
2021-01-12 11:54:53 -06:00
|
|
|
Secrets: map[string]string{
|
2021-03-12 18:25:10 -06:00
|
|
|
"CASE_INSENSITIVE_SECRET": "value",
|
2021-01-12 11:54:53 -06:00
|
|
|
},
|
2020-02-13 01:27:37 -06:00
|
|
|
},
|
2020-05-04 14:18:13 -05:00
|
|
|
Env: map[string]string{
|
|
|
|
"key": "value",
|
|
|
|
},
|
2020-02-13 13:47:38 -06:00
|
|
|
Run: &model.Run{
|
|
|
|
JobID: "job1",
|
|
|
|
Workflow: &model.Workflow{
|
|
|
|
Name: "test-workflow",
|
2020-02-14 02:41:20 -06:00
|
|
|
Jobs: map[string]*model.Job{
|
2020-02-25 18:58:26 -06:00
|
|
|
"job1": {
|
2020-02-14 02:41:20 -06:00
|
|
|
Strategy: &model.Strategy{
|
2021-08-09 10:35:05 -05:00
|
|
|
RawMatrix: yml,
|
2020-02-14 02:41:20 -06:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Matrix: map[string]interface{}{
|
|
|
|
"os": "Linux",
|
|
|
|
"foo": "bar",
|
|
|
|
},
|
|
|
|
StepResults: map[string]*stepResult{
|
2020-06-24 09:08:45 -05:00
|
|
|
"idwithnothing": {
|
2020-02-14 02:41:20 -06:00
|
|
|
Outputs: map[string]string{
|
2020-06-24 09:08:45 -05:00
|
|
|
"foowithnothing": "barwithnothing",
|
|
|
|
},
|
|
|
|
Success: true,
|
|
|
|
},
|
|
|
|
"id-with-hyphens": {
|
|
|
|
Outputs: map[string]string{
|
|
|
|
"foo-with-hyphens": "bar-with-hyphens",
|
|
|
|
},
|
|
|
|
Success: true,
|
|
|
|
},
|
|
|
|
"id_with_underscores": {
|
|
|
|
Outputs: map[string]string{
|
|
|
|
"foo_with_underscores": "bar_with_underscores",
|
2020-02-14 02:41:20 -06:00
|
|
|
},
|
|
|
|
Success: true,
|
2020-02-13 13:47:38 -06:00
|
|
|
},
|
|
|
|
},
|
2020-02-13 01:27:37 -06:00
|
|
|
}
|
|
|
|
ee := rc.NewExpressionEvaluator()
|
|
|
|
|
|
|
|
tables := []struct {
|
|
|
|
in string
|
|
|
|
out string
|
|
|
|
errMesg string
|
|
|
|
}{
|
|
|
|
{" 1 ", "1", ""},
|
|
|
|
{"1 + 3", "4", ""},
|
|
|
|
{"(1 + 3) * -2", "-8", ""},
|
|
|
|
{"'my text'", "my text", ""},
|
|
|
|
{"contains('my text', 'te')", "true", ""},
|
|
|
|
{"contains('my TEXT', 'te')", "true", ""},
|
|
|
|
{"contains(['my text'], 'te')", "false", ""},
|
|
|
|
{"contains(['foo','bar'], 'bar')", "true", ""},
|
|
|
|
{"startsWith('hello world', 'He')", "true", ""},
|
|
|
|
{"endsWith('hello world', 'ld')", "true", ""},
|
|
|
|
{"format('0:{0} 2:{2} 1:{1}', 'zero', 'one', 'two')", "0:zero 2:two 1:one", ""},
|
|
|
|
{"join(['hello'],'octocat')", "hello octocat", ""},
|
|
|
|
{"join(['hello','mona','the'],'octocat')", "hello mona the octocat", ""},
|
|
|
|
{"join('hello','mona')", "hello mona", ""},
|
|
|
|
{"toJSON({'foo':'bar'})", "{\n \"foo\": \"bar\"\n}", ""},
|
2020-02-28 17:20:31 -06:00
|
|
|
{"toJson({'foo':'bar'})", "{\n \"foo\": \"bar\"\n}", ""},
|
2020-09-01 15:55:29 -05:00
|
|
|
{"(fromJSON('{\"foo\":\"bar\"}')).foo", "bar", ""},
|
|
|
|
{"(fromJson('{\"foo\":\"bar\"}')).foo", "bar", ""},
|
2021-11-15 08:26:04 -06:00
|
|
|
{"(fromJson('[\"foo\",\"bar\"]'))[1]", "bar", ""},
|
2020-09-29 15:39:45 -05:00
|
|
|
{"hashFiles('**/non-extant-files')", "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", ""},
|
2020-11-09 11:08:57 -06:00
|
|
|
{"hashFiles('**/non-extant-files', '**/more-non-extant-files')", "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", ""},
|
2021-02-08 11:14:12 -06:00
|
|
|
{"hashFiles('**/non.extant.files')", "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", ""},
|
|
|
|
{"hashFiles('**/non''extant''files')", "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", ""},
|
2020-02-13 13:47:38 -06:00
|
|
|
{"success()", "true", ""},
|
|
|
|
{"failure()", "false", ""},
|
|
|
|
{"always()", "true", ""},
|
|
|
|
{"cancelled()", "false", ""},
|
|
|
|
{"github.workflow", "test-workflow", ""},
|
|
|
|
{"github.actor", "nektos/act", ""},
|
|
|
|
{"github.run_id", "1", ""},
|
|
|
|
{"github.run_number", "1", ""},
|
2020-02-14 02:41:20 -06:00
|
|
|
{"job.status", "success", ""},
|
2020-06-24 09:08:45 -05:00
|
|
|
{"steps.idwithnothing.outputs.foowithnothing", "barwithnothing", ""},
|
|
|
|
{"steps.id-with-hyphens.outputs.foo-with-hyphens", "bar-with-hyphens", ""},
|
|
|
|
{"steps.id_with_underscores.outputs.foo_with_underscores", "bar_with_underscores", ""},
|
2020-02-13 13:47:38 -06:00
|
|
|
{"runner.os", "Linux", ""},
|
2020-02-14 02:41:20 -06:00
|
|
|
{"matrix.os", "Linux", ""},
|
|
|
|
{"matrix.foo", "bar", ""},
|
2020-05-04 14:18:13 -05:00
|
|
|
{"env.key", "value", ""},
|
2021-03-12 18:25:10 -06:00
|
|
|
{"secrets.CASE_INSENSITIVE_SECRET", "value", ""},
|
|
|
|
{"secrets.case_insensitive_secret", "value", ""},
|
2021-08-09 10:19:10 -05:00
|
|
|
{"format('{{0}}', 'test')", "{0}", ""},
|
|
|
|
{"format('{{{0}}}', 'test')", "{test}", ""},
|
|
|
|
{"format('}}')", "}", ""},
|
|
|
|
{"format('echo Hello {0} ${{Test}}', 'World')", "echo Hello World ${Test}", ""},
|
|
|
|
{"format('echo Hello {0} ${{Test}}', github.undefined_property)", "echo Hello ${Test}", ""},
|
|
|
|
{"format('echo Hello {0}{1} ${{Te{0}st}}', github.undefined_property, 'World')", "echo Hello World ${Test}", ""},
|
|
|
|
{"format('{0}', '{1}', 'World')", "{1}", ""},
|
|
|
|
{"format('{{{0}', '{1}', 'World')", "{{1}", ""},
|
2020-02-13 01:27:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, table := range tables {
|
|
|
|
table := table
|
|
|
|
t.Run(table.in, func(t *testing.T) {
|
2021-05-18 01:14:49 -05:00
|
|
|
assertObject := assert.New(t)
|
2020-11-17 11:31:05 -06:00
|
|
|
out, _, err := ee.Evaluate(table.in)
|
2020-02-13 01:27:37 -06:00
|
|
|
if table.errMesg == "" {
|
2021-05-18 01:14:49 -05:00
|
|
|
assertObject.NoError(err, table.in)
|
|
|
|
assertObject.Equal(table.out, out, table.in)
|
2020-02-13 01:27:37 -06:00
|
|
|
} else {
|
2021-05-18 01:14:49 -05:00
|
|
|
assertObject.Error(err, table.in)
|
|
|
|
assertObject.Equal(table.errMesg, err.Error(), table.in)
|
2020-02-13 01:27:37 -06:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestInterpolate(t *testing.T) {
|
|
|
|
rc := &RunContext{
|
|
|
|
Config: &Config{
|
|
|
|
Workdir: ".",
|
2021-01-12 11:54:53 -06:00
|
|
|
Secrets: map[string]string{
|
2021-03-12 18:25:10 -06:00
|
|
|
"CASE_INSENSITIVE_SECRET": "value",
|
2021-01-12 11:54:53 -06:00
|
|
|
},
|
2020-02-13 01:27:37 -06:00
|
|
|
},
|
2020-05-04 14:18:13 -05:00
|
|
|
Env: map[string]string{
|
2020-11-12 10:15:09 -06:00
|
|
|
"KEYWITHNOTHING": "valuewithnothing",
|
|
|
|
"KEY-WITH-HYPHENS": "value-with-hyphens",
|
|
|
|
"KEY_WITH_UNDERSCORES": "value_with_underscores",
|
2020-11-17 11:31:05 -06:00
|
|
|
"SOMETHING_TRUE": "true",
|
|
|
|
"SOMETHING_FALSE": "false",
|
2020-05-04 14:18:13 -05:00
|
|
|
},
|
2020-02-13 13:47:38 -06:00
|
|
|
Run: &model.Run{
|
|
|
|
JobID: "job1",
|
|
|
|
Workflow: &model.Workflow{
|
|
|
|
Name: "test-workflow",
|
2020-02-14 02:41:20 -06:00
|
|
|
Jobs: map[string]*model.Job{
|
2020-02-25 18:58:26 -06:00
|
|
|
"job1": {},
|
2020-02-14 02:41:20 -06:00
|
|
|
},
|
2020-02-13 13:47:38 -06:00
|
|
|
},
|
|
|
|
},
|
2020-02-13 01:27:37 -06:00
|
|
|
}
|
|
|
|
ee := rc.NewExpressionEvaluator()
|
2020-06-18 10:21:55 -05:00
|
|
|
tables := []struct {
|
|
|
|
in string
|
2020-05-04 14:18:13 -05:00
|
|
|
out string
|
|
|
|
}{
|
|
|
|
{" ${{1}} to ${{2}} ", " 1 to 2 "},
|
2020-11-12 10:15:09 -06:00
|
|
|
{" ${{ env.KEYWITHNOTHING }} ", " valuewithnothing "},
|
|
|
|
{" ${{ env.KEY-WITH-HYPHENS }} ", " value-with-hyphens "},
|
|
|
|
{" ${{ env.KEY_WITH_UNDERSCORES }} ", " value_with_underscores "},
|
2021-03-12 18:25:10 -06:00
|
|
|
{"${{ secrets.CASE_INSENSITIVE_SECRET }}", "value"},
|
|
|
|
{"${{ secrets.case_insensitive_secret }}", "value"},
|
2020-11-12 10:15:09 -06:00
|
|
|
{"${{ env.UNKNOWN }}", ""},
|
2020-11-17 11:31:05 -06:00
|
|
|
{"${{ env.SOMETHING_TRUE }}", "true"},
|
|
|
|
{"${{ env.SOMETHING_FALSE }}", "false"},
|
|
|
|
{"${{ !env.SOMETHING_TRUE }}", "false"},
|
|
|
|
{"${{ !env.SOMETHING_FALSE }}", "false"},
|
|
|
|
{"${{ !env.SOMETHING_TRUE && true }}", "false"},
|
|
|
|
{"${{ !env.SOMETHING_FALSE && true }}", "false"},
|
|
|
|
{"${{ env.SOMETHING_TRUE && true }}", "true"},
|
|
|
|
{"${{ env.SOMETHING_FALSE && true }}", "true"},
|
|
|
|
{"${{ !env.SOMETHING_TRUE || true }}", "true"},
|
|
|
|
{"${{ !env.SOMETHING_FALSE || true }}", "true"},
|
|
|
|
{"${{ !env.SOMETHING_TRUE && false }}", "false"},
|
|
|
|
{"${{ !env.SOMETHING_FALSE && false }}", "false"},
|
|
|
|
{"${{ !env.SOMETHING_TRUE || false }}", "false"},
|
|
|
|
{"${{ !env.SOMETHING_FALSE || false }}", "false"},
|
|
|
|
{"${{ env.SOMETHING_TRUE || false }}", "true"},
|
|
|
|
{"${{ env.SOMETHING_FALSE || false }}", "false"},
|
|
|
|
{"${{ env.SOMETHING_FALSE }} && ${{ env.SOMETHING_TRUE }}", "false && true"},
|
2020-05-04 14:18:13 -05:00
|
|
|
}
|
2020-02-13 01:27:37 -06:00
|
|
|
|
2020-11-17 11:31:05 -06:00
|
|
|
updateTestExpressionWorkflow(t, tables, rc)
|
2020-05-04 14:18:13 -05:00
|
|
|
for _, table := range tables {
|
|
|
|
table := table
|
|
|
|
t.Run(table.in, func(t *testing.T) {
|
2021-05-18 01:14:49 -05:00
|
|
|
assertObject := assert.New(t)
|
2020-05-04 14:18:13 -05:00
|
|
|
out := ee.Interpolate(table.in)
|
2021-05-18 01:14:49 -05:00
|
|
|
assertObject.Equal(table.out, out, table.in)
|
2020-05-04 14:18:13 -05:00
|
|
|
})
|
|
|
|
}
|
2020-02-13 01:27:37 -06:00
|
|
|
}
|
2020-06-24 09:08:45 -05:00
|
|
|
|
2020-11-17 11:31:05 -06:00
|
|
|
func updateTestExpressionWorkflow(t *testing.T, tables []struct {
|
|
|
|
in string
|
|
|
|
out string
|
|
|
|
}, rc *RunContext) {
|
|
|
|
var envs string
|
2021-03-12 18:25:10 -06:00
|
|
|
keys := make([]string, 0, len(rc.Env))
|
|
|
|
for k := range rc.Env {
|
|
|
|
keys = append(keys, k)
|
2020-11-17 11:31:05 -06:00
|
|
|
}
|
2021-03-12 18:25:10 -06:00
|
|
|
sort.Strings(keys)
|
|
|
|
for _, k := range keys {
|
|
|
|
envs += fmt.Sprintf(" %s: %s\n", k, rc.Env[k])
|
|
|
|
}
|
|
|
|
|
2020-11-17 11:31:05 -06:00
|
|
|
workflow := fmt.Sprintf(`
|
2021-03-30 14:26:25 -05:00
|
|
|
name: "Test how expressions are handled on GitHub"
|
2020-11-17 11:31:05 -06:00
|
|
|
on: push
|
|
|
|
|
|
|
|
env:
|
|
|
|
%s
|
|
|
|
|
|
|
|
jobs:
|
|
|
|
test-espressions:
|
|
|
|
runs-on: ubuntu-latest
|
|
|
|
steps:
|
|
|
|
`, envs)
|
|
|
|
for _, table := range tables {
|
|
|
|
expressionPattern = regexp.MustCompile(`\${{\s*(.+?)\s*}}`)
|
|
|
|
|
|
|
|
expr := expressionPattern.ReplaceAllStringFunc(table.in, func(match string) string {
|
|
|
|
return fmt.Sprintf("€{{ %s }}", expressionPattern.ReplaceAllString(match, "$1"))
|
|
|
|
})
|
2021-01-14 23:37:38 -06:00
|
|
|
name := fmt.Sprintf(`%s -> %s should be equal to %s`, expr, table.in, table.out)
|
2020-11-17 11:31:05 -06:00
|
|
|
echo := `run: echo "Done "`
|
2021-03-12 18:25:10 -06:00
|
|
|
workflow += fmt.Sprintf("\n - name: %s\n %s\n", name, echo)
|
2020-11-17 11:31:05 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
file, err := os.Create("../../.github/workflows/test-expressions.yml")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = file.WriteString(workflow)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-24 09:08:45 -05:00
|
|
|
func TestRewrite(t *testing.T) {
|
|
|
|
rc := &RunContext{
|
|
|
|
Config: &Config{},
|
|
|
|
Run: &model.Run{
|
|
|
|
JobID: "job1",
|
|
|
|
Workflow: &model.Workflow{
|
|
|
|
Jobs: map[string]*model.Job{
|
|
|
|
"job1": {},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
ee := rc.NewExpressionEvaluator()
|
|
|
|
|
|
|
|
tables := []struct {
|
|
|
|
in string
|
|
|
|
re string
|
|
|
|
}{
|
|
|
|
{"ecole", "ecole"},
|
|
|
|
{"ecole.centrale", "ecole['centrale']"},
|
|
|
|
{"ecole['centrale']", "ecole['centrale']"},
|
|
|
|
{"ecole.centrale.paris", "ecole['centrale']['paris']"},
|
|
|
|
{"ecole['centrale'].paris", "ecole['centrale']['paris']"},
|
|
|
|
{"ecole.centrale['paris']", "ecole['centrale']['paris']"},
|
|
|
|
{"ecole['centrale']['paris']", "ecole['centrale']['paris']"},
|
|
|
|
{"ecole.centrale-paris", "ecole['centrale-paris']"},
|
|
|
|
{"ecole['centrale-paris']", "ecole['centrale-paris']"},
|
|
|
|
{"ecole.centrale_paris", "ecole['centrale_paris']"},
|
|
|
|
{"ecole['centrale_paris']", "ecole['centrale_paris']"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, table := range tables {
|
|
|
|
table := table
|
|
|
|
t.Run(table.in, func(t *testing.T) {
|
2021-05-18 01:14:49 -05:00
|
|
|
assertObject := assert.New(t)
|
2020-06-24 09:08:45 -05:00
|
|
|
re := ee.Rewrite(table.in)
|
2021-05-18 01:14:49 -05:00
|
|
|
assertObject.Equal(table.re, re, table.in)
|
2020-06-24 09:08:45 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|