1
0
mirror of https://github.com/mgechev/revive.git synced 2025-07-15 01:04:40 +02:00

refactor: use "filepath" instead of "path" (#1073)

This commit is contained in:
Oleksandr Redko
2024-10-25 12:48:41 +03:00
committed by GitHub
parent 842b3e2fa3
commit 662e02cd70
3 changed files with 13 additions and 14 deletions

View File

@ -6,7 +6,6 @@ import (
"fmt" "fmt"
"go/token" "go/token"
"os" "os"
"path"
"path/filepath" "path/filepath"
"regexp" "regexp"
"strconv" "strconv"
@ -175,7 +174,7 @@ func detectGoMod(dir string) (rootDir string, ver *goversion.Version, err error)
} }
ver, err = goversion.NewVersion(modAst.Go.Version) ver, err = goversion.NewVersion(modAst.Go.Version)
return path.Dir(modFileName), ver, err return filepath.Dir(modFileName), ver, err
} }
func retrieveModFile(dir string) (string, error) { func retrieveModFile(dir string) (string, error) {
@ -185,11 +184,11 @@ func retrieveModFile(dir string) (string, error) {
return "", fmt.Errorf("did not found %q file", lookingForFile) return "", fmt.Errorf("did not found %q file", lookingForFile)
} }
lookingForFilePath := path.Join(dir, lookingForFile) lookingForFilePath := filepath.Join(dir, lookingForFile)
info, err := os.Stat(lookingForFilePath) info, err := os.Stat(lookingForFilePath)
if err != nil || info.IsDir() { if err != nil || info.IsDir() {
// lets check the parent dir // lets check the parent dir
dir = path.Dir(dir) dir = filepath.Dir(dir)
continue continue
} }

View File

@ -3,7 +3,7 @@ package test
import ( import (
"flag" "flag"
"os" "os"
"path" "path/filepath"
"regexp" "regexp"
"testing" "testing"
@ -53,7 +53,7 @@ func TestAll(t *testing.T) {
continue continue
} }
t.Run(fi.Name(), func(t *testing.T) { t.Run(fi.Name(), func(t *testing.T) {
filePath := path.Join(baseDir, fi.Name()) filePath := filepath.Join(baseDir, fi.Name())
src, err := os.ReadFile(filePath) src, err := os.ReadFile(filePath)
if err != nil { if err != nil {
t.Fatalf("Failed reading %s: %v", fi.Name(), err) t.Fatalf("Failed reading %s: %v", fi.Name(), err)
@ -64,7 +64,7 @@ func TestAll(t *testing.T) {
t.Fatalf("Failed reading %s: %v", fi.Name(), err) t.Fatalf("Failed reading %s: %v", fi.Name(), err)
} }
if err := assertFailures(t, path.Dir(baseDir), fileInfo, src, rules, map[string]lint.RuleConfig{}); err != nil { if err := assertFailures(t, filepath.Dir(baseDir), fileInfo, src, rules, map[string]lint.RuleConfig{}); err != nil {
t.Errorf("Linting %s: %v", fi.Name(), err) t.Errorf("Linting %s: %v", fi.Name(), err)
} }
}) })

View File

@ -10,7 +10,7 @@ import (
"go/token" "go/token"
"go/types" "go/types"
"os" "os"
"path" "path/filepath"
"strconv" "strconv"
"strings" "strings"
"testing" "testing"
@ -19,9 +19,9 @@ import (
) )
func testRule(t *testing.T, filename string, rule lint.Rule, config ...*lint.RuleConfig) { func testRule(t *testing.T, filename string, rule lint.Rule, config ...*lint.RuleConfig) {
baseDir := path.Join("../testdata/" + path.Dir(filename)) baseDir := filepath.Join("..", "testdata", filepath.Dir(filename))
filename = path.Base(filename) + ".go" filename = filepath.Base(filename) + ".go"
fullFilePath := path.Join(baseDir, filename) fullFilePath := filepath.Join(baseDir, filename)
src, err := os.ReadFile(fullFilePath) src, err := os.ReadFile(fullFilePath)
if err != nil { if err != nil {
t.Fatalf("Bad filename path in test for %s: %v", rule.Name(), err) t.Fatalf("Bad filename path in test for %s: %v", rule.Name(), err)
@ -46,7 +46,7 @@ func assertSuccess(t *testing.T, baseDir string, fi os.FileInfo, rules []lint.Ru
return os.ReadFile(file) return os.ReadFile(file)
}, 0) }, 0)
filePath := path.Join(baseDir, fi.Name()) filePath := filepath.Join(baseDir, fi.Name())
ps, err := l.Lint([][]string{{filePath}}, rules, lint.Config{ ps, err := l.Lint([][]string{{filePath}}, rules, lint.Config{
Rules: config, Rules: config,
}) })
@ -69,12 +69,12 @@ func assertFailures(t *testing.T, baseDir string, fi os.FileInfo, src []byte, ru
return os.ReadFile(file) return os.ReadFile(file)
}, 0) }, 0)
ins := parseInstructions(t, path.Join(baseDir, fi.Name()), src) ins := parseInstructions(t, filepath.Join(baseDir, fi.Name()), src)
if ins == nil { if ins == nil {
return fmt.Errorf("Test file %v does not have instructions", fi.Name()) return fmt.Errorf("Test file %v does not have instructions", fi.Name())
} }
ps, err := l.Lint([][]string{{path.Join(baseDir, fi.Name())}}, rules, lint.Config{ ps, err := l.Lint([][]string{{filepath.Join(baseDir, fi.Name())}}, rules, lint.Config{
Rules: config, Rules: config,
}) })
if err != nil { if err != nil {