2020-02-13 01:27:37 -06:00
|
|
|
package runner
|
|
|
|
|
|
|
|
import (
|
2022-06-17 10:55:21 -05:00
|
|
|
"context"
|
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"
|
|
|
|
|
2022-05-24 08:36:06 -05:00
|
|
|
"github.com/nektos/act/pkg/exprparser"
|
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
|
|
|
)
|
|
|
|
|
2022-01-21 10:07:20 -06:00
|
|
|
func createRunContext(t *testing.T) *RunContext {
|
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)
|
|
|
|
|
2022-01-21 10:07:20 -06:00
|
|
|
return &RunContext{
|
2020-02-13 01:27:37 -06:00
|
|
|
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",
|
|
|
|
},
|
2021-12-22 13:52:09 -06:00
|
|
|
StepResults: map[string]*model.StepResult{
|
2020-06-24 09:08:45 -05:00
|
|
|
"idwithnothing": {
|
2021-12-22 13:52:09 -06:00
|
|
|
Conclusion: model.StepStatusSuccess,
|
|
|
|
Outcome: model.StepStatusFailure,
|
2020-02-14 02:41:20 -06:00
|
|
|
Outputs: map[string]string{
|
2020-06-24 09:08:45 -05:00
|
|
|
"foowithnothing": "barwithnothing",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"id-with-hyphens": {
|
2021-12-22 13:52:09 -06:00
|
|
|
Conclusion: model.StepStatusSuccess,
|
|
|
|
Outcome: model.StepStatusFailure,
|
2020-06-24 09:08:45 -05:00
|
|
|
Outputs: map[string]string{
|
|
|
|
"foo-with-hyphens": "bar-with-hyphens",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
"id_with_underscores": {
|
2021-12-22 13:52:09 -06:00
|
|
|
Conclusion: model.StepStatusSuccess,
|
|
|
|
Outcome: model.StepStatusFailure,
|
2020-06-24 09:08:45 -05:00
|
|
|
Outputs: map[string]string{
|
|
|
|
"foo_with_underscores": "bar_with_underscores",
|
2020-02-14 02:41:20 -06:00
|
|
|
},
|
2020-02-13 13:47:38 -06:00
|
|
|
},
|
|
|
|
},
|
2020-02-13 01:27:37 -06:00
|
|
|
}
|
2022-01-21 10:07:20 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestEvaluateRunContext(t *testing.T) {
|
|
|
|
rc := createRunContext(t)
|
2022-06-17 10:55:21 -05:00
|
|
|
ee := rc.NewExpressionEvaluator(context.Background())
|
2020-02-13 01:27:37 -06:00
|
|
|
|
|
|
|
tables := []struct {
|
|
|
|
in string
|
2022-01-21 10:07:20 -06:00
|
|
|
out interface{}
|
2020-02-13 01:27:37 -06:00
|
|
|
errMesg string
|
|
|
|
}{
|
2022-01-21 10:07:20 -06:00
|
|
|
{" 1 ", 1, ""},
|
|
|
|
// {"1 + 3", "4", ""},
|
|
|
|
// {"(1 + 3) * -2", "-8", ""},
|
2020-02-13 01:27:37 -06:00
|
|
|
{"'my text'", "my text", ""},
|
2022-01-21 10:07:20 -06:00
|
|
|
{"contains('my text', 'te')", true, ""},
|
|
|
|
{"contains('my TEXT', 'te')", true, ""},
|
|
|
|
{"contains(fromJSON('[\"my text\"]'), 'te')", false, ""},
|
|
|
|
{"contains(fromJSON('[\"foo\",\"bar\"]'), 'bar')", true, ""},
|
|
|
|
{"startsWith('hello world', 'He')", true, ""},
|
|
|
|
{"endsWith('hello world', 'ld')", true, ""},
|
2020-02-13 01:27:37 -06:00
|
|
|
{"format('0:{0} 2:{2} 1:{1}', 'zero', 'one', 'two')", "0:zero 2:two 1:one", ""},
|
2022-01-21 10:07:20 -06:00
|
|
|
{"join(fromJSON('[\"hello\"]'),'octocat')", "hello", ""},
|
|
|
|
{"join(fromJSON('[\"hello\",\"mona\",\"the\"]'),'octocat')", "hellooctocatmonaoctocatthe", ""},
|
|
|
|
{"join('hello','mona')", "hello", ""},
|
|
|
|
{"toJSON(env)", "{\n \"ACT\": \"true\",\n \"key\": \"value\"\n}", ""},
|
|
|
|
{"toJson(env)", "{\n \"ACT\": \"true\",\n \"key\": \"value\"\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", ""},
|
2022-01-21 10:07:20 -06:00
|
|
|
// github does return an empty string for non-existent files
|
|
|
|
{"hashFiles('**/non-extant-files')", "", ""},
|
|
|
|
{"hashFiles('**/non-extant-files', '**/more-non-extant-files')", "", ""},
|
|
|
|
{"hashFiles('**/non.extant.files')", "", ""},
|
|
|
|
{"hashFiles('**/non''extant''files')", "", ""},
|
|
|
|
{"success()", true, ""},
|
|
|
|
{"failure()", false, ""},
|
|
|
|
{"always()", true, ""},
|
|
|
|
{"cancelled()", false, ""},
|
2020-02-13 13:47:38 -06:00
|
|
|
{"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-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)
|
2022-06-17 10:55:21 -05:00
|
|
|
out, err := ee.evaluate(context.Background(), table.in, exprparser.DefaultStatusCheckNone)
|
2022-01-21 10:07:20 -06:00
|
|
|
if table.errMesg == "" {
|
|
|
|
assertObject.NoError(err, table.in)
|
|
|
|
assertObject.Equal(table.out, out, table.in)
|
|
|
|
} else {
|
|
|
|
assertObject.Error(err, table.in)
|
|
|
|
assertObject.Equal(table.errMesg, err.Error(), table.in)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-22 16:13:00 -05:00
|
|
|
func TestEvaluateStep(t *testing.T) {
|
2022-01-21 10:07:20 -06:00
|
|
|
rc := createRunContext(t)
|
2022-03-22 16:13:00 -05:00
|
|
|
step := &stepRun{
|
2022-01-21 10:07:20 -06:00
|
|
|
RunContext: rc,
|
|
|
|
}
|
2022-03-22 16:13:00 -05:00
|
|
|
|
2022-06-17 10:55:21 -05:00
|
|
|
ee := rc.NewStepExpressionEvaluator(context.Background(), step)
|
2022-01-21 10:07:20 -06:00
|
|
|
|
|
|
|
tables := []struct {
|
|
|
|
in string
|
|
|
|
out interface{}
|
|
|
|
errMesg string
|
|
|
|
}{
|
2022-04-26 15:24:13 -05:00
|
|
|
{"steps.idwithnothing.conclusion", model.StepStatusSuccess.String(), ""},
|
|
|
|
{"steps.idwithnothing.outcome", model.StepStatusFailure.String(), ""},
|
2022-01-21 10:07:20 -06:00
|
|
|
{"steps.idwithnothing.outputs.foowithnothing", "barwithnothing", ""},
|
2022-04-26 15:24:13 -05:00
|
|
|
{"steps.id-with-hyphens.conclusion", model.StepStatusSuccess.String(), ""},
|
|
|
|
{"steps.id-with-hyphens.outcome", model.StepStatusFailure.String(), ""},
|
2022-01-21 10:07:20 -06:00
|
|
|
{"steps.id-with-hyphens.outputs.foo-with-hyphens", "bar-with-hyphens", ""},
|
2022-04-26 15:24:13 -05:00
|
|
|
{"steps.id_with_underscores.conclusion", model.StepStatusSuccess.String(), ""},
|
|
|
|
{"steps.id_with_underscores.outcome", model.StepStatusFailure.String(), ""},
|
2022-01-21 10:07:20 -06:00
|
|
|
{"steps.id_with_underscores.outputs.foo_with_underscores", "bar_with_underscores", ""},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, table := range tables {
|
|
|
|
table := table
|
|
|
|
t.Run(table.in, func(t *testing.T) {
|
|
|
|
assertObject := assert.New(t)
|
2022-06-17 10:55:21 -05:00
|
|
|
out, err := ee.evaluate(context.Background(), table.in, exprparser.DefaultStatusCheckNone)
|
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
|
|
|
}
|
2022-06-17 10:55:21 -05:00
|
|
|
ee := rc.NewExpressionEvaluator(context.Background())
|
2020-06-18 10:21:55 -05:00
|
|
|
tables := []struct {
|
|
|
|
in string
|
2020-05-04 14:18:13 -05:00
|
|
|
out string
|
|
|
|
}{
|
2022-01-21 10:07:20 -06:00
|
|
|
{" text ", " text "},
|
|
|
|
{" $text ", " $text "},
|
|
|
|
{" ${text} ", " ${text} "},
|
|
|
|
{" ${{ 1 }} to ${{2}} ", " 1 to 2 "},
|
|
|
|
{" ${{ (true || false) }} to ${{2}} ", " true to 2 "},
|
|
|
|
{" ${{ (false || '}}' ) }} to ${{2}} ", " }} 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"},
|
2022-01-21 10:07:20 -06:00
|
|
|
{"${{ fromJSON('{}') < 2 }}", "false"},
|
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
|
2022-01-21 10:07:20 -06:00
|
|
|
t.Run("interpolate", func(t *testing.T) {
|
2021-05-18 01:14:49 -05:00
|
|
|
assertObject := assert.New(t)
|
2022-06-17 10:55:21 -05:00
|
|
|
out := ee.Interpolate(context.Background(), 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])
|
|
|
|
}
|
|
|
|
|
2021-12-22 13:52:09 -06:00
|
|
|
// editorconfig-checker-disable
|
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)
|
2021-12-22 13:52:09 -06:00
|
|
|
// editorconfig-checker-enable
|
2020-11-17 11:31:05 -06:00
|
|
|
for _, table := range tables {
|
2022-01-21 10:07:20 -06:00
|
|
|
expressionPattern := regexp.MustCompile(`\${{\s*(.+?)\s*}}`)
|
2020-11-17 11:31:05 -06:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-21 10:07:20 -06:00
|
|
|
func TestRewriteSubExpression(t *testing.T) {
|
|
|
|
table := []struct {
|
|
|
|
in string
|
|
|
|
out string
|
|
|
|
}{
|
|
|
|
{in: "Hello World", out: "Hello World"},
|
|
|
|
{in: "${{ true }}", out: "${{ true }}"},
|
|
|
|
{in: "${{ true }} ${{ true }}", out: "format('{0} {1}', true, true)"},
|
|
|
|
{in: "${{ true || false }} ${{ true && true }}", out: "format('{0} {1}', true || false, true && true)"},
|
|
|
|
{in: "${{ '}}' }}", out: "${{ '}}' }}"},
|
|
|
|
{in: "${{ '''}}''' }}", out: "${{ '''}}''' }}"},
|
|
|
|
{in: "${{ '''' }}", out: "${{ '''' }}"},
|
|
|
|
{in: `${{ fromJSON('"}}"') }}`, out: `${{ fromJSON('"}}"') }}`},
|
|
|
|
{in: `${{ fromJSON('"\"}}\""') }}`, out: `${{ fromJSON('"\"}}\""') }}`},
|
|
|
|
{in: `${{ fromJSON('"''}}"') }}`, out: `${{ fromJSON('"''}}"') }}`},
|
|
|
|
{in: "Hello ${{ 'World' }}", out: "format('Hello {0}', 'World')"},
|
2020-06-24 09:08:45 -05:00
|
|
|
}
|
|
|
|
|
2022-01-21 10:07:20 -06:00
|
|
|
for _, table := range table {
|
|
|
|
t.Run("TestRewriteSubExpression", func(t *testing.T) {
|
|
|
|
assertObject := assert.New(t)
|
2022-06-17 10:55:21 -05:00
|
|
|
out, err := rewriteSubExpression(context.Background(), table.in, false)
|
2022-01-21 10:07:20 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
assertObject.Equal(table.out, out, table.in)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRewriteSubExpressionForceFormat(t *testing.T) {
|
|
|
|
table := []struct {
|
|
|
|
in string
|
|
|
|
out string
|
2020-06-24 09:08:45 -05:00
|
|
|
}{
|
2022-01-21 10:07:20 -06:00
|
|
|
{in: "Hello World", out: "Hello World"},
|
|
|
|
{in: "${{ true }}", out: "format('{0}', true)"},
|
|
|
|
{in: "${{ '}}' }}", out: "format('{0}', '}}')"},
|
|
|
|
{in: `${{ fromJSON('"}}"') }}`, out: `format('{0}', fromJSON('"}}"'))`},
|
|
|
|
{in: "Hello ${{ 'World' }}", out: "format('Hello {0}', 'World')"},
|
2020-06-24 09:08:45 -05:00
|
|
|
}
|
|
|
|
|
2022-01-21 10:07:20 -06:00
|
|
|
for _, table := range table {
|
|
|
|
t.Run("TestRewriteSubExpressionForceFormat", func(t *testing.T) {
|
2021-05-18 01:14:49 -05:00
|
|
|
assertObject := assert.New(t)
|
2022-06-17 10:55:21 -05:00
|
|
|
out, err := rewriteSubExpression(context.Background(), table.in, true)
|
2022-01-21 10:07:20 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
assertObject.Equal(table.out, out, table.in)
|
2020-06-24 09:08:45 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|