From 8f00a10ad7df2f5ea224d9285e8d5b1906d7222c Mon Sep 17 00:00:00 2001 From: Ludovic Fernandez Date: Mon, 12 Sep 2022 09:01:27 +0200 Subject: [PATCH] dev: improve tests on Windows (#3211) --- pkg/golinters/depguard.go | 19 ++++++++++++++++++- pkg/result/processors/fixer.go | 7 ++++--- test/testdata/depguard_ignore_file_rules.go | 4 +--- 3 files changed, 23 insertions(+), 7 deletions(-) diff --git a/pkg/golinters/depguard.go b/pkg/golinters/depguard.go index b9241d18..3e0171f5 100644 --- a/pkg/golinters/depguard.go +++ b/pkg/golinters/depguard.go @@ -2,6 +2,8 @@ package golinters import ( "fmt" + "path/filepath" + "regexp" "strings" "sync" @@ -104,16 +106,31 @@ func (d depGuard) run(pass *analysis.Pass) ([]goanalysis.Issue, error) { 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 { *depguard.Depguard pkgsWithErrorMessage map[string]string } func newGuardian(settings *config.DepGuardSettings) (*guardian, error) { + var ignoreFileRules []string + for _, rule := range settings.IgnoreFileRules { + ignoreFileRules = append(ignoreFileRules, normalizePathInRegex(rule)) + } + dg := &depguard.Depguard{ Packages: settings.Packages, IncludeGoRoot: settings.IncludeGoRoot, - IgnoreFileRules: settings.IgnoreFileRules, + IgnoreFileRules: ignoreFileRules, } var err error diff --git a/pkg/result/processors/fixer.go b/pkg/result/processors/fixer.go index 17f519e3..d125e157 100644 --- a/pkg/result/processors/fixer.go +++ b/pkg/result/processors/fixer.go @@ -10,6 +10,7 @@ import ( "github.com/pkg/errors" + "github.com/golangci/golangci-lint/internal/robustio" "github.com/golangci/golangci-lint/pkg/config" "github.com/golangci/golangci-lint/pkg/fsutils" "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 { tmpOutFile.Close() - os.Remove(tmpOutFile.Name()) + _ = robustio.RemoveAll(tmpOutFile.Name()) return err } tmpOutFile.Close() - if err = os.Rename(tmpOutFile.Name(), filePath); err != nil { - os.Remove(tmpOutFile.Name()) + if err = robustio.Rename(tmpOutFile.Name(), filePath); err != nil { + _ = robustio.RemoveAll(tmpOutFile.Name()) return errors.Wrapf(err, "failed to rename %s -> %s", tmpOutFile.Name(), filePath) } diff --git a/test/testdata/depguard_ignore_file_rules.go b/test/testdata/depguard_ignore_file_rules.go index 0fcc766c..76729237 100644 --- a/test/testdata/depguard_ignore_file_rules.go +++ b/test/testdata/depguard_ignore_file_rules.go @@ -1,11 +1,9 @@ -//go:build !windows - //golangcitest:args -Edepguard //golangcitest:config_path testdata/configs/depguard_ignore_file_rules.yml //golangcitest:expected_exitcode 0 package testdata -// NOTE - No lint errors becuase this file is ignored +// NOTE - No lint errors because this file is ignored import ( "compress/gzip" "log"