Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions pkg/executor/condition_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}
41 changes: 21 additions & 20 deletions pkg/executor/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Loading