Skip to content

Commit c336149

Browse files
committed
fix: support filters through strings or files
#967
1 parent 6d865b3 commit c336149

File tree

3 files changed

+45
-17
lines changed

3 files changed

+45
-17
lines changed

internal/check/filter.go

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,32 @@ import (
1212
)
1313

1414
func filter(mgr *Manager) (map[string]Rule, error) {
15-
stringOrPath := mgr.Config.Flags.Filter
15+
var filter string
1616

17+
stringOrPath := mgr.Config.Flags.Filter
1718
if stringOrPath == "" {
1819
return mgr.rules, nil
19-
} else if !system.FileExists(stringOrPath) {
20-
name := stringOrPath
21-
22-
stringOrPath = core.FindAsset(mgr.Config, name)
23-
if stringOrPath == "" {
24-
return nil, fmt.Errorf("filter '%s' not found", name)
25-
}
2620
}
2721

28-
b, err := os.ReadFile(stringOrPath)
29-
if err != nil {
30-
return nil, fmt.Errorf("failed to read filter '%s': %w", stringOrPath, err)
22+
if system.FileExists(stringOrPath) {
23+
// Case 1: The user has provided a valid path to a filter file.
24+
b, err := os.ReadFile(stringOrPath)
25+
if err != nil {
26+
return nil, fmt.Errorf("failed to read filter '%s': %w", stringOrPath, err)
27+
}
28+
filter = string(b)
29+
} else if found := core.FindAsset(mgr.Config, stringOrPath); found != "" {
30+
// Case 2: The user has referenced a filter stored on the `StylesPath`.
31+
b, err := os.ReadFile(found)
32+
if err != nil {
33+
return nil, fmt.Errorf("failed to read filter '%s': %w", found, err)
34+
}
35+
filter = string(b)
36+
} else {
37+
// Case 3: Assume the user has provided a string.
38+
filter = stringOrPath
3139
}
3240

33-
filter := string(b)
3441
// .Name, .Level -> override
3542
// .Scope, .Message, .Description, .Extends, .Link
3643
//

testdata/features/CLI.feature

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ Feature: CLI
134134
And the exit status should be 0
135135

136136
Scenario: Filter --minAlertLevel
137-
When I use filter "min"
137+
When I use filter file "min"
138138
Then the output should contain exactly:
139139
"""
140140
test.md:1:3:demo.Cap:'intro' should be in title case ('Intro').
@@ -143,15 +143,15 @@ Feature: CLI
143143
And the exit status should be 1
144144

145145
Scenario: Filter by single scope
146-
When I use filter "scope"
146+
When I use filter file "scope"
147147
Then the output should contain exactly:
148148
"""
149149
test.md:1:3:demo.Cap:'intro' should be in title case ('Intro').
150150
"""
151151
And the exit status should be 1
152152

153153
Scenario: Filter by multiple levels
154-
When I use filter "levels"
154+
When I use filter file "levels"
155155
Then the output should contain exactly:
156156
"""
157157
test.md:1:3:demo.HeadingStartsWithCapital:'intro' should be capitalized
@@ -160,13 +160,29 @@ Feature: CLI
160160
And the exit status should be 1
161161

162162
Scenario: Filter by extends
163-
When I use filter "extends"
163+
When I use filter file "extends"
164164
Then the output should contain exactly:
165165
"""
166166
test.md:1:3:demo.HeadingStartsWithCapital:'intro' should be capitalized
167167
"""
168168
And the exit status should be 0
169169

170+
Scenario: Filter by extends [expr]
171+
When I use filter expr ".Extends in ['existence']"
172+
Then the output should contain exactly:
173+
"""
174+
test.md:1:3:demo.HeadingStartsWithCapital:'intro' should be capitalized
175+
"""
176+
And the exit status should be 0
177+
178+
Scenario: Filter by multiple levels [expr]
179+
When I use filter expr ".Level in ['error', 'suggestion'] and .Name != 'demo.Cap'"
180+
Then the output should contain exactly:
181+
"""
182+
test.md:1:3:demo.HeadingStartsWithCapital:'intro' should be capitalized
183+
test.md:5:8:Vale.Repetition:'is' is repeated!
184+
"""
185+
170186
Scenario: Script-computed suggestions
171187
When I fix "script.json"
172188
Then the output should contain exactly:

testdata/features/steps.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,16 @@
2626
step %(I run `#{cmd} fix #{c}`)
2727
end
2828

29-
When(/^I use filter "(.*)"$/) do |f|
29+
When(/^I use filter file "(.*)"$/) do |f|
3030
step %(I cd to "../../fixtures/filters")
3131
step %(I run `#{cmd} --filter="#{f}.expr" .`)
3232
end
3333

34+
When(/^I use filter expr "(.*)"$/) do |e|
35+
step %(I cd to "../../fixtures/filters")
36+
step %(I run `#{cmd} --filter="#{e}" .`)
37+
end
38+
3439
When(/^I lint simple "(.*)"$/) do |flag|
3540
step %(I cd to "../../fixtures/formats")
3641
step %(I run `#{cmd} --ignore-syntax #{flag}`)

0 commit comments

Comments
 (0)