2020-05-04 19:18:13 +00:00
|
|
|
package runner
|
|
|
|
|
|
|
|
import (
|
2020-11-12 16:15:09 +00:00
|
|
|
"fmt"
|
2020-05-04 19:18:13 +00:00
|
|
|
"github.com/nektos/act/pkg/model"
|
|
|
|
a "github.com/stretchr/testify/assert"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/sirupsen/logrus/hooks/test"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestRunContext_EvalBool(t *testing.T) {
|
|
|
|
hook := test.NewGlobal()
|
|
|
|
rc := &RunContext{
|
|
|
|
Config: &Config{
|
|
|
|
Workdir: ".",
|
|
|
|
},
|
|
|
|
Env: map[string]string{
|
2020-06-18 15:21:55 +00:00
|
|
|
"TRUE": "true",
|
|
|
|
"FALSE": "false",
|
2020-05-04 19:18:13 +00:00
|
|
|
"SOME_TEXT": "text",
|
|
|
|
},
|
|
|
|
Run: &model.Run{
|
|
|
|
JobID: "job1",
|
|
|
|
Workflow: &model.Workflow{
|
|
|
|
Name: "test-workflow",
|
|
|
|
Jobs: map[string]*model.Job{
|
|
|
|
"job1": {
|
|
|
|
Strategy: &model.Strategy{
|
|
|
|
Matrix: map[string][]interface{}{
|
|
|
|
"os": {"Linux", "Windows"},
|
|
|
|
"foo": {"bar", "baz"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Matrix: map[string]interface{}{
|
|
|
|
"os": "Linux",
|
|
|
|
"foo": "bar",
|
|
|
|
},
|
|
|
|
StepResults: map[string]*stepResult{
|
|
|
|
"id1": {
|
|
|
|
Outputs: map[string]string{
|
|
|
|
"foo": "bar",
|
|
|
|
},
|
|
|
|
Success: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
rc.ExprEval = rc.NewExpressionEvaluator()
|
|
|
|
|
|
|
|
tables := []struct {
|
2020-06-18 15:21:55 +00:00
|
|
|
in string
|
|
|
|
out bool
|
2020-05-04 19:18:13 +00:00
|
|
|
}{
|
|
|
|
// The basic ones
|
|
|
|
{"true", true},
|
|
|
|
{"false", false},
|
2020-11-12 16:15:09 +00:00
|
|
|
{"1 != 0", true},
|
|
|
|
{"1 != 1", false},
|
2020-05-04 19:18:13 +00:00
|
|
|
{"1 == 0", false},
|
|
|
|
{"1 == 1", true},
|
|
|
|
{"1 > 2", false},
|
|
|
|
{"1 < 2", true},
|
|
|
|
{"success()", true},
|
|
|
|
{"failure()", false},
|
2020-11-12 16:15:09 +00:00
|
|
|
{"always()", true},
|
|
|
|
{"failure()", false},
|
2020-05-04 19:18:13 +00:00
|
|
|
// And or
|
|
|
|
{"true && false", false},
|
|
|
|
{"true && 1 < 2", true},
|
|
|
|
{"false || 1 < 2", true},
|
|
|
|
{"false || false", false},
|
|
|
|
// None boolable
|
|
|
|
{"env.UNKNOWN == 'true'", false},
|
|
|
|
{"env.UNKNOWN", false},
|
|
|
|
// Inline expressions
|
2020-11-12 16:15:09 +00:00
|
|
|
{"env.SOME_TEXT", true}, // this is because Boolean('text') is true in Javascript
|
|
|
|
{"env.SOME_TEXT == 'text'", true},
|
2020-05-04 19:18:13 +00:00
|
|
|
{"env.TRUE == 'true'", true},
|
|
|
|
{"env.FALSE == 'true'", false},
|
2020-11-12 16:15:09 +00:00
|
|
|
{"env.TRUE", true},
|
|
|
|
{"env.FALSE", false},
|
|
|
|
{"!env.TRUE", false},
|
|
|
|
{"!env.FALSE", true},
|
|
|
|
{"${{ env.TRUE }}", true},
|
|
|
|
{"${{ env.FALSE }}", false},
|
|
|
|
{"${{ !env.TRUE }}", false},
|
|
|
|
{"${{ !env.FALSE }}", true},
|
|
|
|
{"!env.TRUE && true", false},
|
|
|
|
{"!env.FALSE && true", true},
|
|
|
|
{"!env.TRUE || true", true},
|
|
|
|
{"!env.FALSE || false", true},
|
2020-05-04 19:18:13 +00:00
|
|
|
{"${{env.TRUE == 'true'}}", true},
|
|
|
|
{"${{env.FALSE == 'true'}}", false},
|
|
|
|
{"${{env.FALSE == 'false'}}", true},
|
2020-11-12 16:15:09 +00:00
|
|
|
|
2020-05-04 19:18:13 +00:00
|
|
|
// All together now
|
|
|
|
{"false || env.TRUE == 'true'", true},
|
|
|
|
{"true || env.FALSE == 'true'", true},
|
|
|
|
{"true && env.TRUE == 'true'", true},
|
|
|
|
{"false && env.TRUE == 'true'", false},
|
|
|
|
{"env.FALSE == 'true' && env.TRUE == 'true'", false},
|
|
|
|
{"env.FALSE == 'true' && true", false},
|
|
|
|
{"${{env.FALSE == 'true'}} && true", false},
|
|
|
|
// Check github context
|
|
|
|
{"github.actor == 'nektos/act'", true},
|
|
|
|
{"github.actor == 'unknown'", false},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, table := range tables {
|
|
|
|
table := table
|
|
|
|
t.Run(table.in, func(t *testing.T) {
|
2020-11-12 16:15:09 +00:00
|
|
|
assert := a.New(t)
|
2020-05-04 19:18:13 +00:00
|
|
|
defer hook.Reset()
|
|
|
|
b := rc.EvalBool(table.in)
|
|
|
|
|
2020-11-12 16:15:09 +00:00
|
|
|
assert.Equal(table.out, b, fmt.Sprintf("Expected %s to be %v, was %v", table.in, table.out, b))
|
2020-05-04 19:18:13 +00:00
|
|
|
assert.Empty(hook.LastEntry(), table.in)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|