From e64d9b5b6f270b1e78eeabec72860d682d20c898 Mon Sep 17 00:00:00 2001 From: Falk Harrison Date: Tue, 29 Sep 2026 10:46:31 +0200 Subject: [PATCH] fix(executor): parse conditions before substituting values --- pkg/executor/condition_test.go | 52 ++++++++++++++++++++++++++++++++++ pkg/executor/resolve.go | 41 ++++++++++++++------------- 2 files changed, 73 insertions(+), 20 deletions(-) create mode 100644 pkg/executor/condition_test.go diff --git a/pkg/executor/condition_test.go b/pkg/executor/condition_test.go new file mode 100644 index 0000000..63eac6b --- /dev/null +++ b/pkg/executor/condition_test.go @@ -0,0 +1,52 @@ +package executor + +import "testing" + +func TestEvaluateConditionConjunction(t *testing.T) { + for _, tc := range []struct { + name, condition string + scope map[string]string + want bool + }{ + {"both true", "$env == prod && $region == eu", map[string]string{"env": "prod", "region": "eu"}, true}, + {"first false", "$env == prod && $region == eu", map[string]string{"env": "dev", "region": "eu"}, false}, + {"second false", "$env == prod && $region == eu", map[string]string{"env": "prod", "region": "us"}, false}, + {"both false", "$env == prod && $region == eu", map[string]string{"env": "dev", "region": "us"}, false}, + {"mixed operators", "$env != dev && $region == eu", map[string]string{"env": "prod", "region": "eu"}, true}, + {"three predicates", "$env == prod && $region != us && $ready", map[string]string{"env": "prod", "region": "eu", "ready": "yes"}, true}, + {"empty middle predicate", "yes && && yes", nil, false}, + {"empty last predicate", "yes &&", nil, false}, + } { + t.Run(tc.name, func(t *testing.T) { + if got := EvaluateCondition(tc.condition, tc.scope); got != tc.want { + t.Fatalf("EvaluateCondition(%q) = %v, want %v", tc.condition, got, tc.want) + } + }) + } +} + +func TestEvaluateConditionLiteralOperands(t *testing.T) { + for _, tc := range []struct { + name, condition, value, expected string + want bool + }{ + {"equality containing operators", "$value == $expected", "a == b != c && d", "a == b != c && d", true}, + {"unequal values containing operators", "$value == $expected", "a == b", "a != b", false}, + {"inequality containing equality", "$value != other", "a == b", "", true}, + {"inequality containing operators", "$value != $expected", "a != b && c", "a != b && c", false}, + {"operator text is truthy", "$value", "==", "", true}, + {"conjunction text is truthy", "$value", "&&", "", true}, + {"nonempty value is truthy", "$value", "ready", "", true}, + {"empty value is false", "$value", "", "", false}, + {"whitespace value stays truthy", "$value", " \t ", "", true}, + {"literal whitespace is false", " \t ", "", "", false}, + {"comparison trims whitespace", "$value == $expected", " value ", "value", true}, + } { + t.Run(tc.name, func(t *testing.T) { + scope := map[string]string{"value": tc.value, "expected": tc.expected} + if got := EvaluateCondition(tc.condition, scope); got != tc.want { + t.Fatalf("EvaluateCondition(%q) = %v, want %v", tc.condition, got, tc.want) + } + }) + } +} diff --git a/pkg/executor/resolve.go b/pkg/executor/resolve.go index e2d004f..7cbc402 100644 --- a/pkg/executor/resolve.go +++ b/pkg/executor/resolve.go @@ -129,29 +129,30 @@ func visitDependencies(defs []parser.VarDef, addWithDeps func(string)) { // EvaluateCondition evaluates a condition expression against the scope. func EvaluateCondition(condition string, scope map[string]string) bool { - condition = strings.TrimSpace(condition) - - condition = SubstituteVars(condition, scope, "dollar") - - if strings.Contains(condition, "==") { - parts := strings.SplitN(condition, "==", 2) - if len(parts) == 2 { - left := strings.TrimSpace(parts[0]) - right := strings.TrimSpace(parts[1]) - return left == right + for _, predicate := range strings.Split(condition, "&&") { + predicate = strings.TrimSpace(predicate) + op := "" + at := -1 + for i := 0; i+1 < len(predicate); i++ { + if predicate[i:i+2] == "==" || predicate[i:i+2] == "!=" { + at, op = i, predicate[i:i+2] + break + } } - } - - if strings.Contains(condition, "!=") { - parts := strings.SplitN(condition, "!=", 2) - if len(parts) == 2 { - left := strings.TrimSpace(parts[0]) - right := strings.TrimSpace(parts[1]) - return left != right + if at < 0 { + if SubstituteVars(predicate, scope, "dollar") == "" { + return false + } + continue + } + left := strings.TrimSpace(SubstituteVars(strings.TrimSpace(predicate[:at]), scope, "dollar")) + right := strings.TrimSpace(SubstituteVars(strings.TrimSpace(predicate[at+2:]), scope, "dollar")) + equal := left == right + if (op == "==" && !equal) || (op == "!=" && equal) { + return false } } - - return condition != "" + return true } // ReplaceVar replaces variable references in cmd with replacement.