dev: improve tests on Windows ()

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
pkg
golinters
result/processors
test/testdata

@ -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

@ -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)
}

@ -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"