mirror of
https://code.forgejo.org/forgejo/runner.git
synced 2025-09-15 18:57:01 +00:00
Refactor expression evaluator to use parser from actionlint package (#908)
* feat: implement expression evaluator Co-authored-by: Markus Wolf <markus.wolf@new-work.se> Co-authored-by: Philipp Hinrichsen <philipp.hinrichsen@new-work.se> * feat: integrate exprparser into act Co-authored-by: Markus Wolf <markus.wolf@new-work.se> Co-authored-by: Philipp Hinrichsen <philipp.hinrichsen@new-work.se> * Escape { and }, do not fail on missing properties * Fix empty inputs context * fix: contains() comparison for complex values Co-authored-by: Markus Wolf <markus.wolf@new-work.se> Co-authored-by: Philipp Hinrichsen <philipp.hinrichsen@new-work.se> Co-authored-by: Christopher Homberger <christopher.homberger@web.de>
This commit is contained in:
parent
4920c29312
commit
61c66784dd
10 changed files with 2037 additions and 706 deletions
|
@ -75,14 +75,16 @@ func TestRunContext_EvalBool(t *testing.T) {
|
|||
{in: "success()", out: true},
|
||||
{in: "cancelled()", out: false},
|
||||
{in: "always()", out: true},
|
||||
{in: "steps.id1.conclusion == 'success'", out: true},
|
||||
{in: "steps.id1.conclusion != 'success'", out: false},
|
||||
{in: "steps.id1.outcome == 'failure'", out: true},
|
||||
{in: "steps.id1.outcome != 'failure'", out: false},
|
||||
// TODO: move to sc.NewExpressionEvaluator(), because "steps" context is not available here
|
||||
// {in: "steps.id1.conclusion == 'success'", out: true},
|
||||
// {in: "steps.id1.conclusion != 'success'", out: false},
|
||||
// {in: "steps.id1.outcome == 'failure'", out: true},
|
||||
// {in: "steps.id1.outcome != 'failure'", out: false},
|
||||
{in: "true", out: true},
|
||||
{in: "false", out: false},
|
||||
{in: "!true", wantErr: true},
|
||||
{in: "!false", wantErr: true},
|
||||
// TODO: This does not throw an error, because the evaluator does not know if the expression is inside ${{ }} or not
|
||||
// {in: "!true", wantErr: true},
|
||||
// {in: "!false", wantErr: true},
|
||||
{in: "1 != 0", out: true},
|
||||
{in: "1 != 1", out: false},
|
||||
{in: "${{ 1 != 0 }}", out: true},
|
||||
|
@ -100,14 +102,15 @@ func TestRunContext_EvalBool(t *testing.T) {
|
|||
{in: "env.UNKNOWN == 'true'", out: false},
|
||||
{in: "env.UNKNOWN", out: false},
|
||||
// Inline expressions
|
||||
{in: "env.SOME_TEXT", out: true}, // this is because Boolean('text') is true in Javascript
|
||||
{in: "env.SOME_TEXT", out: true},
|
||||
{in: "env.SOME_TEXT == 'text'", out: true},
|
||||
{in: "env.SOMETHING_TRUE == 'true'", out: true},
|
||||
{in: "env.SOMETHING_FALSE == 'true'", out: false},
|
||||
{in: "env.SOMETHING_TRUE", out: true},
|
||||
{in: "env.SOMETHING_FALSE", out: true}, // this is because Boolean('text') is true in Javascript
|
||||
{in: "!env.SOMETHING_TRUE", wantErr: true},
|
||||
{in: "!env.SOMETHING_FALSE", wantErr: true},
|
||||
{in: "env.SOMETHING_FALSE", out: true},
|
||||
// TODO: This does not throw an error, because the evaluator does not know if the expression is inside ${{ }} or not
|
||||
// {in: "!env.SOMETHING_TRUE", wantErr: true},
|
||||
// {in: "!env.SOMETHING_FALSE", wantErr: true},
|
||||
{in: "${{ !env.SOMETHING_TRUE }}", out: false},
|
||||
{in: "${{ !env.SOMETHING_FALSE }}", out: false},
|
||||
{in: "${{ ! env.SOMETHING_TRUE }}", out: false},
|
||||
|
@ -123,7 +126,8 @@ func TestRunContext_EvalBool(t *testing.T) {
|
|||
{in: "${{ env.SOMETHING_TRUE && true }}", out: true},
|
||||
{in: "${{ env.SOMETHING_FALSE || true }}", out: true},
|
||||
{in: "${{ env.SOMETHING_FALSE || false }}", out: true},
|
||||
{in: "!env.SOMETHING_TRUE || true", wantErr: true},
|
||||
// TODO: This does not throw an error, because the evaluator does not know if the expression is inside ${{ }} or not
|
||||
// {in: "!env.SOMETHING_TRUE || true", wantErr: true},
|
||||
{in: "${{ env.SOMETHING_TRUE == 'true'}}", out: true},
|
||||
{in: "${{ env.SOMETHING_FALSE == 'true'}}", out: false},
|
||||
{in: "${{ env.SOMETHING_FALSE == 'false'}}", out: true},
|
||||
|
@ -198,7 +202,7 @@ jobs:
|
|||
if table.wantErr || strings.HasPrefix(table.in, "github.actor") {
|
||||
continue
|
||||
}
|
||||
expressionPattern = regexp.MustCompile(`\${{\s*(.+?)\s*}}`)
|
||||
expressionPattern := regexp.MustCompile(`\${{\s*(.+?)\s*}}`)
|
||||
|
||||
expr := expressionPattern.ReplaceAllStringFunc(table.in, func(match string) string {
|
||||
return fmt.Sprintf("€{{ %s }}", expressionPattern.ReplaceAllString(match, "$1"))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue