dev: improve tests on Windows (#3211)

This commit is contained in:
Ludovic Fernandez 2022-09-12 09:01:27 +02:00 committed by GitHub
parent 8a3b754ca2
commit 8f00a10ad7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 7 deletions

View File

@ -2,6 +2,8 @@ package golinters
import ( import (
"fmt" "fmt"
"path/filepath"
"regexp"
"strings" "strings"
"sync" "sync"
@ -104,16 +106,31 @@ func (d depGuard) run(pass *analysis.Pass) ([]goanalysis.Issue, error) {
return resIssues, nil return resIssues, nil
} }
var separatorToReplace = regexp.QuoteMeta(string(filepath.Separator))
// normalizePathInRegex normalizes path in regular expressions.
// noop on Unix.
// This replacing should be safe because "/" are disallowed in Windows
// https://docs.microsoft.com/windows/win32/fileio/naming-a-file
func normalizePathInRegex(path string) string {
return strings.ReplaceAll(path, "/", separatorToReplace)
}
type guardian struct { type guardian struct {
*depguard.Depguard *depguard.Depguard
pkgsWithErrorMessage map[string]string pkgsWithErrorMessage map[string]string
} }
func newGuardian(settings *config.DepGuardSettings) (*guardian, error) { func newGuardian(settings *config.DepGuardSettings) (*guardian, error) {
var ignoreFileRules []string
for _, rule := range settings.IgnoreFileRules {
ignoreFileRules = append(ignoreFileRules, normalizePathInRegex(rule))
}
dg := &depguard.Depguard{ dg := &depguard.Depguard{
Packages: settings.Packages, Packages: settings.Packages,
IncludeGoRoot: settings.IncludeGoRoot, IncludeGoRoot: settings.IncludeGoRoot,
IgnoreFileRules: settings.IgnoreFileRules, IgnoreFileRules: ignoreFileRules,
} }
var err error var err error

View File

@ -10,6 +10,7 @@ import (
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/golangci/golangci-lint/internal/robustio"
"github.com/golangci/golangci-lint/pkg/config" "github.com/golangci/golangci-lint/pkg/config"
"github.com/golangci/golangci-lint/pkg/fsutils" "github.com/golangci/golangci-lint/pkg/fsutils"
"github.com/golangci/golangci-lint/pkg/logutils" "github.com/golangci/golangci-lint/pkg/logutils"
@ -104,13 +105,13 @@ func (f Fixer) fixIssuesInFile(filePath string, issues []result.Issue) error {
if err = f.writeFixedFile(origFileLines, issues, tmpOutFile); err != nil { if err = f.writeFixedFile(origFileLines, issues, tmpOutFile); err != nil {
tmpOutFile.Close() tmpOutFile.Close()
os.Remove(tmpOutFile.Name()) _ = robustio.RemoveAll(tmpOutFile.Name())
return err return err
} }
tmpOutFile.Close() tmpOutFile.Close()
if err = os.Rename(tmpOutFile.Name(), filePath); err != nil { if err = robustio.Rename(tmpOutFile.Name(), filePath); err != nil {
os.Remove(tmpOutFile.Name()) _ = robustio.RemoveAll(tmpOutFile.Name())
return errors.Wrapf(err, "failed to rename %s -> %s", tmpOutFile.Name(), filePath) return errors.Wrapf(err, "failed to rename %s -> %s", tmpOutFile.Name(), filePath)
} }

View File

@ -1,11 +1,9 @@
//go:build !windows
//golangcitest:args -Edepguard //golangcitest:args -Edepguard
//golangcitest:config_path testdata/configs/depguard_ignore_file_rules.yml //golangcitest:config_path testdata/configs/depguard_ignore_file_rules.yml
//golangcitest:expected_exitcode 0 //golangcitest:expected_exitcode 0
package testdata package testdata
// NOTE - No lint errors becuase this file is ignored // NOTE - No lint errors because this file is ignored
import ( import (
"compress/gzip" "compress/gzip"
"log" "log"