Skip to content

Commit 0e87843

Browse files
committed
fix(lint): add test file exemption and improve error handling
- Add black-box test file exemption to LatestImportsPredecessor cop (consistent with ConfigVersionImport and ConfigPackageName) - Check strconv.Atoi error in versionFromImport for consistency - Use 'must' instead of 'should' in error message for consistency Assisted-By: docker-agent
1 parent 8422c8d commit 0e87843

3 files changed

Lines changed: 12 additions & 2 deletions

File tree

lint/config_version_import.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func importViolation(path string, dirVersion int, isLatest bool) string {
6767
if _, ok := versionFromImport(path); ok {
6868
return ""
6969
}
70-
return "pkg/config/latest should only import config version or types packages, not " + path
70+
return "pkg/config/latest must only import config version or types packages, not " + path
7171
}
7272

7373
// Versioned package (vN).

lint/configpath.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,11 @@ func versionFromImport(importPath string) (int, bool) {
5555
if m == nil {
5656
return 0, false
5757
}
58-
n, _ := strconv.Atoi(m[1])
58+
n, err := strconv.Atoi(m[1])
59+
if err != nil {
60+
// Should never happen since regex only captures digits.
61+
return 0, false
62+
}
5963
return n, true
6064
}
6165

lint/latest_imports_predecessor.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"go/ast"
66
"go/token"
7+
"strings"
78

89
"github.com/dgageot/rubocop-go/cop"
910
)
@@ -33,6 +34,11 @@ func (c *LatestImportsPredecessor) Check(fset *token.FileSet, file *ast.File) []
3334
if configDir(filename) != "latest" {
3435
return nil
3536
}
37+
// Black-box test files (package latest_test) are external to the package
38+
// and may import what they please.
39+
if strings.HasSuffix(file.Name.Name, "_test") {
40+
return nil
41+
}
3642
highest, ok := highestSiblingVersion(filename)
3743
if !ok {
3844
return nil

0 commit comments

Comments
 (0)