1
0
mirror of https://github.com/mgechev/revive.git synced 2024-11-24 08:32:22 +02:00

fix: revive hangs on Windows if go.mod is not found

This commit is contained in:
Oleksandr Redko 2024-11-14 15:12:18 +02:00 committed by chavacava
parent 34e9d78df9
commit 74e2417f7d
2 changed files with 42 additions and 2 deletions

View File

@ -184,7 +184,9 @@ func detectGoMod(dir string) (rootDir string, ver *goversion.Version, err error)
func retrieveModFile(dir string) (string, error) {
const lookingForFile = "go.mod"
for {
if dir == "." || dir == "/" {
// filepath.Dir returns 'C:\' on Windows, and '/' on Unix
isRootDir := (dir == filepath.VolumeName(dir)+string(filepath.Separator))
if dir == "." || isRootDir {
return "", fmt.Errorf("did not found %q file", lookingForFile)
}

View File

@ -1,6 +1,44 @@
package lint
import "testing"
import (
"os"
"path/filepath"
"testing"
)
func TestRetrieveModFile(t *testing.T) {
t.Run("go.mod file exists", func(t *testing.T) {
nestedDir := filepath.Join(t.TempDir(), "nested", "dir", "structure")
err := os.MkdirAll(nestedDir, 0o755)
if err != nil {
t.Fatal(err)
}
modFilePath := filepath.Join(nestedDir, "go.mod")
err = os.WriteFile(modFilePath, []byte("module example.com/test"), 0o644)
if err != nil {
t.Fatal(err)
}
foundPath, err := retrieveModFile(nestedDir)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if foundPath != modFilePath {
t.Fatalf("expected %q, got %q", modFilePath, foundPath)
}
})
t.Run("go.mod file does not exist", func(t *testing.T) {
_, err := retrieveModFile(t.TempDir())
if err == nil {
t.Fatalf("expected error, got nil")
}
expectedErrMsg := `did not found "go.mod" file`
if err.Error() != expectedErrMsg {
t.Fatalf("expected error message %q, got %q", expectedErrMsg, err.Error())
}
})
}
// TestIsGenerated tests isGenerated function.
func TestIsGenerated(t *testing.T) { //revive:disable-line:exported