1
0
mirror of https://github.com/mgechev/revive.git synced 2025-10-30 23:37:49 +02:00

package-directory-mismatch: ignore mismatch in root directory (#1548)

This commit is contained in:
Godsgift Uloamaka Ebite
2025-10-24 17:26:03 +01:00
committed by GitHub
parent 91147bf0b5
commit 26d803fc96
7 changed files with 64 additions and 1 deletions

View File

@@ -2,6 +2,7 @@ package rule
import (
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
@@ -96,7 +97,12 @@ func (r *PackageDirectoryMismatchRule) Apply(file *lint.File, _ lint.Arguments)
return nil
}
dirPath := filepath.Dir(file.Name)
absPath, err := filepath.Abs(file.Name)
if err != nil {
return nil
}
dirPath := filepath.Dir(absPath)
dirName := filepath.Base(dirPath)
if r.ignoredDirs != nil && r.ignoredDirs.MatchString(dirPath) {
@@ -120,6 +126,10 @@ func (r *PackageDirectoryMismatchRule) Apply(file *lint.File, _ lint.Arguments)
return nil
}
if isRootDir(dirPath) {
return nil
}
if file.IsTest() {
// treat main_test differently because it's a common package name for tests
if packageName == "main_test" {
@@ -161,6 +171,23 @@ func (r *PackageDirectoryMismatchRule) Apply(file *lint.File, _ lint.Arguments)
}
}
// isRootDir checks if the given directory contains go.mod or .git, indicating it's a root directory.
func isRootDir(dirPath string) bool {
entries, err := os.ReadDir(dirPath)
if err != nil {
return false
}
for _, e := range entries {
switch e.Name() {
case "go.mod", ".git":
return true
}
}
return false
}
// Name returns the rule name.
func (*PackageDirectoryMismatchRule) Name() string {
return "package-directory-mismatch"

View File

@@ -55,6 +55,14 @@ func TestPackageDirectoryMismatch(t *testing.T) {
testRule(t, "package_directory_mismatch/test/bad_test", &rule.PackageDirectoryMismatchRule{}, config)
testRule(t, "package_directory_mismatch/test/bad", &rule.PackageDirectoryMismatchRule{}, config)
testRule(t, "package_directory_mismatch/test/main_test", &rule.PackageDirectoryMismatchRule{}, config)
// Test handling of root directories with go.mod
testRule(t, "package_directory_mismatch/rootdir/good/client", &rule.PackageDirectoryMismatchRule{}, config)
testRule(t, "package_directory_mismatch/rootdir/bad/client", &rule.PackageDirectoryMismatchRule{}, config)
// Test handling of root directories with .git
mkdirTempDotGit(t, "package_directory_mismatch/rootdir/withgit")
testRule(t, "package_directory_mismatch/rootdir/withgit/client", &rule.PackageDirectoryMismatchRule{}, config)
}
func TestPackageDirectoryMismatchWithDefaultConfig(t *testing.T) {

View File

@@ -412,3 +412,27 @@ func TestExportedType(t *testing.T) {
}
}
}
// mkdirTempDotGit adds a temporary .git directory to the given root directory in testdata.
// We can't commit .git directly because of the Git restrictions.
func mkdirTempDotGit(t *testing.T, root string) {
t.Helper()
baseDir := filepath.Join("..", "testdata", root)
dir, err := filepath.Abs(baseDir)
if err != nil {
t.Fatalf("Failed to resolve abs path: %v", err)
}
gitDir := filepath.Join(dir, ".git")
if err := os.MkdirAll(gitDir, 0o755); err != nil {
t.Fatalf("Failed to create .git directory: %v", err)
}
t.Cleanup(func() {
err = os.RemoveAll(gitDir)
if err != nil {
t.Logf("Failed to remove %v: %v", gitDir, err)
}
})
}

View File

@@ -0,0 +1 @@
package somepackage // MATCH /package name "somepackage" does not match directory name "bad"/

View File

@@ -0,0 +1 @@
package somepackage

View File

@@ -0,0 +1 @@
module "example.com/project"

View File

@@ -0,0 +1 @@
package somePackage