-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add include & exclude pattern support for the linting command. …
…Move CI config include & exclude validation logic into common code"
- Loading branch information
Joseph Burnitz
committed
Apr 7, 2024
1 parent
9a668ae
commit ea3b6a6
Showing
9 changed files
with
184 additions
and
22 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,9 @@ | ||
# This is an example config to be used when running pint as a linter | ||
|
||
lint { | ||
# Check all files inside rules/alerting and rules/recording dirs. | ||
include = ["rules/(alerting|recording)/.+"] | ||
|
||
# Ignore all *.md and *.txt files. | ||
exclude = [".+.md", ".+.txt"] | ||
} |
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,17 @@ | ||
package config | ||
|
||
type Lint struct { | ||
Include []string `hcl:"include,optional" json:"include,omitempty"` | ||
Exclude []string `hcl:"exclude,optional" json:"exclude,omitempty"` | ||
} | ||
|
||
func (lint Lint) validate() error { | ||
if err := ValidatePaths(lint.Include); err != nil { | ||
return err | ||
} | ||
if err := ValidatePaths(lint.Exclude); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
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 config | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestLintSettings(t *testing.T) { | ||
type testCaseT struct { | ||
err error | ||
conf Lint | ||
} | ||
|
||
testCases := []testCaseT{ | ||
{ | ||
conf: Lint{ | ||
Include: []string{"foo/.+"}, | ||
Exclude: []string{"foo/.+"}, | ||
}, | ||
}, | ||
{ | ||
conf: Lint{ | ||
Include: []string{".+++"}, | ||
Exclude: []string{"foo/.+"}, | ||
}, | ||
err: errors.New("error parsing regexp: invalid nested repetition operator: `++`"), | ||
}, | ||
{ | ||
conf: Lint{ | ||
Include: []string{"foo/.+"}, | ||
Exclude: []string{".+++"}, | ||
}, | ||
err: errors.New("error parsing regexp: invalid nested repetition operator: `++`"), | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(fmt.Sprintf("%v", tc.conf), func(t *testing.T) { | ||
err := tc.conf.validate() | ||
if err == nil || tc.err == nil { | ||
require.Equal(t, err, tc.err) | ||
} else { | ||
require.EqualError(t, err, tc.err.Error()) | ||
} | ||
}) | ||
} | ||
} |
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,15 @@ | ||
package config | ||
|
||
import ( | ||
"regexp" | ||
) | ||
|
||
func ValidatePaths(paths []string) error { | ||
for _, pattern := range paths { | ||
_, err := regexp.Compile(pattern) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} |
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,37 @@ | ||
package config | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestValidatePaths(t *testing.T) { | ||
type testCaseT struct { | ||
err error | ||
paths []string | ||
} | ||
|
||
testCases := []testCaseT{ | ||
{ | ||
paths: []string{"foo/.+"}, | ||
}, | ||
{ | ||
paths: []string{".+++"}, | ||
err: errors.New("error parsing regexp: invalid nested repetition operator: `++`"), | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(fmt.Sprintf("%v", tc.paths), func(t *testing.T) { | ||
err := ValidatePaths(tc.paths) | ||
if err == nil || tc.err == nil { | ||
require.Equal(t, err, tc.err) | ||
} else { | ||
require.EqualError(t, err, tc.err.Error()) | ||
} | ||
}) | ||
} | ||
} |