Skip to content

Commit 8f00a10

Browse files
authored
dev: improve tests on Windows (#3211)
1 parent 8a3b754 commit 8f00a10

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

pkg/golinters/depguard.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package golinters
22

33
import (
44
"fmt"
5+
"path/filepath"
6+
"regexp"
57
"strings"
68
"sync"
79

@@ -104,16 +106,31 @@ func (d depGuard) run(pass *analysis.Pass) ([]goanalysis.Issue, error) {
104106
return resIssues, nil
105107
}
106108

109+
var separatorToReplace = regexp.QuoteMeta(string(filepath.Separator))
110+
111+
// normalizePathInRegex normalizes path in regular expressions.
112+
// noop on Unix.
113+
// This replacing should be safe because "/" are disallowed in Windows
114+
// https://docs.microsoft.com/windows/win32/fileio/naming-a-file
115+
func normalizePathInRegex(path string) string {
116+
return strings.ReplaceAll(path, "/", separatorToReplace)
117+
}
118+
107119
type guardian struct {
108120
*depguard.Depguard
109121
pkgsWithErrorMessage map[string]string
110122
}
111123

112124
func newGuardian(settings *config.DepGuardSettings) (*guardian, error) {
125+
var ignoreFileRules []string
126+
for _, rule := range settings.IgnoreFileRules {
127+
ignoreFileRules = append(ignoreFileRules, normalizePathInRegex(rule))
128+
}
129+
113130
dg := &depguard.Depguard{
114131
Packages: settings.Packages,
115132
IncludeGoRoot: settings.IncludeGoRoot,
116-
IgnoreFileRules: settings.IgnoreFileRules,
133+
IgnoreFileRules: ignoreFileRules,
117134
}
118135

119136
var err error

pkg/result/processors/fixer.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
"github.com/pkg/errors"
1212

13+
"github.com/golangci/golangci-lint/internal/robustio"
1314
"github.com/golangci/golangci-lint/pkg/config"
1415
"github.com/golangci/golangci-lint/pkg/fsutils"
1516
"github.com/golangci/golangci-lint/pkg/logutils"
@@ -104,13 +105,13 @@ func (f Fixer) fixIssuesInFile(filePath string, issues []result.Issue) error {
104105

105106
if err = f.writeFixedFile(origFileLines, issues, tmpOutFile); err != nil {
106107
tmpOutFile.Close()
107-
os.Remove(tmpOutFile.Name())
108+
_ = robustio.RemoveAll(tmpOutFile.Name())
108109
return err
109110
}
110111

111112
tmpOutFile.Close()
112-
if err = os.Rename(tmpOutFile.Name(), filePath); err != nil {
113-
os.Remove(tmpOutFile.Name())
113+
if err = robustio.Rename(tmpOutFile.Name(), filePath); err != nil {
114+
_ = robustio.RemoveAll(tmpOutFile.Name())
114115
return errors.Wrapf(err, "failed to rename %s -> %s", tmpOutFile.Name(), filePath)
115116
}
116117

test/testdata/depguard_ignore_file_rules.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
//go:build !windows
2-
31
//golangcitest:args -Edepguard
42
//golangcitest:config_path testdata/configs/depguard_ignore_file_rules.yml
53
//golangcitest:expected_exitcode 0
64
package testdata
75

8-
// NOTE - No lint errors becuase this file is ignored
6+
// NOTE - No lint errors because this file is ignored
97
import (
108
"compress/gzip"
119
"log"

0 commit comments

Comments
 (0)