2020-02-13 01:27:37 -06:00
|
|
|
package runner
|
|
|
|
|
|
|
|
import (
|
2020-11-17 11:31:05 -06:00
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"regexp"
|
2020-02-13 01:27:37 -06:00
|
|
|
"testing"
|
|
|
|
|
2020-02-13 13:47:38 -06:00
|
|
|
"github.com/nektos/act/pkg/model"
|
2020-05-04 14:18:13 -05:00
|
|
|
a "github.com/stretchr/testify/assert"
|
2020-02-13 01:27:37 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestEvaluate(t *testing.T) {
|
|
|
|
rc := &RunContext{
|
|
|
|
Config: &Config{
|
|
|
|
Workdir: ".",
|
|
|
|
},
|
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{
|
|
|
|
Matrix: map[string][]interface{}{
|
2020-02-25 18:58:26 -06:00
|
|
|
"os": {"Linux", "Windows"},
|
|
|
|
"foo": {"bar", "baz"},
|
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", ""},
|
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", ""},
|
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", ""},
|
2020-02-13 01:27:37 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, table := range tables {
|
|
|
|
table := table
|
|
|
|
t.Run(table.in, func(t *testing.T) {
|
2020-11-12 10:15:09 -06:00
|
|
|
assert := a.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 == "" {
|
|
|
|
assert.NoError(err, table.in)
|
2020-02-14 02:41:20 -06:00
|
|
|
assert.Equal(table.out, out, table.in)
|
2020-02-13 01:27:37 -06:00
|
|
|
} else {
|
2020-02-14 02:41:20 -06:00
|
|
|
assert.Error(err, table.in)
|
|
|
|
assert.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: ".",
|
|
|
|
},
|
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 "},
|
|
|
|
{"${{ 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) {
|
2020-11-12 10:15:09 -06:00
|
|
|
assert := a.New(t)
|
2020-05-04 14:18:13 -05:00
|
|
|
out := ee.Interpolate(table.in)
|
|
|
|
assert.Equal(table.out, out, table.in)
|
|
|
|
})
|
|
|
|
}
|
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
|
|
|
|
for k, v := range rc.Env {
|
|
|
|
envs += fmt.Sprintf(
|
|
|
|
` %s: %s
|
|
|
|
`, k, v)
|
|
|
|
}
|
|
|
|
workflow := fmt.Sprintf(`
|
|
|
|
name: "Test how expressions are handled on Github"
|
|
|
|
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"))
|
|
|
|
})
|
|
|
|
name := fmt.Sprintf(`%s -> %s should be equal to %s`,expr, table.in, table.out)
|
|
|
|
echo := `run: echo "Done "`
|
|
|
|
workflow += fmt.Sprintf(`
|
|
|
|
- name: %s
|
|
|
|
%s
|
|
|
|
`, name, echo)
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2020-11-12 10:15:09 -06:00
|
|
|
assert := a.New(t)
|
2020-06-24 09:08:45 -05:00
|
|
|
re := ee.Rewrite(table.in)
|
|
|
|
assert.Equal(table.re, re, table.in)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|