-
Notifications
You must be signed in to change notification settings - Fork 154
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'check-if-cond' (fix #272)
- Loading branch information
Showing
9 changed files
with
301 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package actionlint | ||
|
||
import ( | ||
"strings" | ||
) | ||
|
||
// RuleIfCond is a rule to check if: conditions. | ||
type RuleIfCond struct { | ||
RuleBase | ||
} | ||
|
||
// NewRuleIfCond creates new RuleIfCond instance. | ||
func NewRuleIfCond() *RuleIfCond { | ||
return &RuleIfCond{ | ||
RuleBase: RuleBase{ | ||
name: "if-cond", | ||
desc: "Checks for if: conditions which are always true/false", | ||
}, | ||
} | ||
} | ||
|
||
// VisitStep is callback when visiting Step node. | ||
func (rule *RuleIfCond) VisitStep(n *Step) error { | ||
rule.checkIfCond(n.If) | ||
return nil | ||
} | ||
|
||
// VisitJobPre is callback when visiting Job node before visiting its children. | ||
func (rule *RuleIfCond) VisitJobPre(n *Job) error { | ||
rule.checkIfCond(n.If) | ||
return nil | ||
} | ||
|
||
func (rule *RuleIfCond) checkIfCond(n *String) { | ||
if n == nil { | ||
return | ||
} | ||
if !n.ContainsExpression() { | ||
return | ||
} | ||
// Check number of ${{ }} for conditions like `${{ false }} || ${{ true }}` which are always evaluated to true | ||
if strings.HasPrefix(n.Value, "${{") && strings.HasSuffix(n.Value, "}}") && strings.Count(n.Value, "${{") == 1 { | ||
return | ||
} | ||
rule.errorf( | ||
n.Pos, | ||
"if: condition %q is always evaluated to true because extra characters are around ${{ }}", | ||
n.Value, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package actionlint | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
) | ||
|
||
func TestRuleIfCond(t *testing.T) { | ||
tests := []struct { | ||
cond string | ||
valid bool | ||
}{ | ||
{"", true}, | ||
{"true", true}, | ||
{"true || false", true}, | ||
{"${{ false }}", true}, | ||
{"${{ false }}\n", false}, | ||
{"${{ false }} ", false}, | ||
{" ${{ false }}", false}, | ||
{"${{ true }} && ${{ true }}", false}, | ||
} | ||
|
||
for _, tc := range tests { | ||
t.Run(fmt.Sprintf("%q at step", tc.cond), func(t *testing.T) { | ||
var s Step | ||
if len(tc.cond) > 0 { | ||
s.If = &String{Value: tc.cond, Pos: &Pos{}} | ||
} | ||
|
||
r := NewRuleIfCond() | ||
if err := r.VisitStep(&s); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
errs := r.Errs() | ||
if tc.valid && len(errs) > 0 { | ||
t.Fatalf("wanted no error but have %q", errs) | ||
} | ||
if !tc.valid && len(errs) != 1 { | ||
t.Fatalf("wanted one error but have %q", errs) | ||
} | ||
}) | ||
} | ||
|
||
for _, tc := range tests { | ||
t.Run(fmt.Sprintf("%q at job", tc.cond), func(t *testing.T) { | ||
var j Job | ||
if len(tc.cond) > 0 { | ||
j.If = &String{Value: tc.cond, Pos: &Pos{}} | ||
} | ||
|
||
r := NewRuleIfCond() | ||
if err := r.VisitJobPre(&j); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
errs := r.Errs() | ||
if tc.valid && len(errs) > 0 { | ||
t.Fatalf("wanted no error but have %q", errs) | ||
} | ||
if !tc.valid && len(errs) != 1 { | ||
t.Fatalf("wanted one error but have %q", errs) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
test.yaml:12:13: if: condition "${{ false }}\n" is always evaluated to true because extra characters are around ${{ }} [if-cond] | ||
test.yaml:19:13: if: condition "${{ false }} " is always evaluated to true because extra characters are around ${{ }} [if-cond] | ||
test.yaml:22:13: if: condition " ${{ false }}" is always evaluated to true because extra characters are around ${{ }} [if-cond] | ||
test.yaml:47:13: if: condition "${{ false }} && ${{ false }}" is always evaluated to true because extra characters are around ${{ }} [if-cond] | ||
test.yaml:49:9: if: condition "# ERROR: True\n${{ false }}\n" is always evaluated to true because extra characters are around ${{ }} [if-cond] | ||
test.yaml:57:9: if: condition " ${{ false }}" is always evaluated to true because extra characters are around ${{ }} [if-cond] | ||
test.yaml:63:9: if: condition "${{ false }} && ${{ false }}" is always evaluated to true because extra characters are around ${{ }} [if-cond] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
on: push | ||
|
||
jobs: | ||
test-steps: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- run: echo 1 | ||
# False | ||
if: ${{ false }} | ||
- run: echo 2 | ||
# ERROR: True | ||
if: | | ||
${{ false }} | ||
- run: echo 3 | ||
# False | ||
if: '${{ false }}' | ||
- run: echo 4 | ||
# ERROR: True | ||
if: '${{ false }} ' | ||
- run: echo 5 | ||
# ERROR: True | ||
if: ' ${{ false }}' | ||
- run: echo 6 | ||
# False | ||
if: 'false' | ||
- run: echo 7 | ||
# False | ||
if: 'false ' | ||
- run: echo 8 | ||
# False | ||
if: | | ||
false | ||
- run: echo 9 | ||
# False | ||
if: ' false' | ||
- run: echo 10 | ||
# True | ||
if: false || true | ||
- run: echo 11 | ||
# False | ||
if: true && false | ||
- run: echo 12 | ||
# False | ||
if: ${{ true && false }} | ||
- run: echo 13 | ||
# ERROR: True | ||
if: ${{ false }} && ${{ false }} | ||
test-cond-1: | ||
if: | | ||
# ERROR: True | ||
${{ false }} | ||
runs-on: ubuntu-latest | ||
steps: | ||
- run: echo 1 | ||
test-cond-2: | ||
# ERROR: True | ||
if: ' ${{ false }}' | ||
runs-on: ubuntu-latest | ||
steps: | ||
- run: echo 1 | ||
test-cond-3: | ||
# ERROR: True | ||
if: ${{ false }} && ${{ false }} | ||
runs-on: ubuntu-latest | ||
steps: | ||
- run: echo 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
test.yaml:12:13: if: condition "${{ github.event_name == 'push' }}\n" is always evaluated to true because extra characters are around ${{ }} [if-cond] | ||
test.yaml:16:13: if: condition "${{ github.event_name == 'push' }} " is always evaluated to true because extra characters are around ${{ }} [if-cond] | ||
test.yaml:22:13: if: condition "${{ github.event_name == 'push' }} && ${{ github.ref_name == 'main' }}" is always evaluated to true because extra characters are around ${{ }} [if-cond] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
on: push | ||
|
||
jobs: | ||
test: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- run: echo 'Commit is pushed' | ||
# OK | ||
if: ${{ github.event_name == 'push' }} | ||
- run: echo 'Commit is pushed' | ||
# ERROR: It is always evaluated to true | ||
if: | | ||
${{ github.event_name == 'push' }} | ||
- run: echo 'Commit is pushed' | ||
# ERROR: It is always evaluated to true | ||
if: "${{ github.event_name == 'push' }} " | ||
- run: echo 'Commit is pushed to main' | ||
# OK | ||
if: github.event_name == 'push' && github.ref_name == 'main' | ||
- run: echo 'Commit is pushed to main' | ||
# ERROR: It is always evaluated to true | ||
if: ${{ github.event_name == 'push' }} && ${{ github.ref_name == 'main' }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters