Skip to content

Commit 3345c71

Browse files
committed
fix #264: fix Windows compatibility
Fix skip-dirs regexps and 'go env' on Windows.
1 parent c02a6da commit 3345c71

File tree

3 files changed

+49
-22
lines changed

3 files changed

+49
-22
lines changed

pkg/goutil/env.go

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
package goutil
22

33
import (
4-
"bufio"
5-
"bytes"
64
"context"
5+
"encoding/json"
76
"os"
87
"os/exec"
9-
"strconv"
10-
"strings"
118

129
"github.com/pkg/errors"
1310

@@ -29,27 +26,13 @@ func NewEnv(log logutils.Log) *Env {
2926
}
3027

3128
func (e *Env) Discover(ctx context.Context) error {
32-
out, err := exec.CommandContext(ctx, "go", "env").Output()
29+
out, err := exec.CommandContext(ctx, "go", "env", "-json").Output()
3330
if err != nil {
3431
return errors.Wrap(err, "failed to run 'go env'")
3532
}
3633

37-
scanner := bufio.NewScanner(bytes.NewReader(out))
38-
scanner.Split(bufio.ScanLines)
39-
for scanner.Scan() {
40-
parts := strings.SplitN(scanner.Text(), "=", 2)
41-
if len(parts) != 2 {
42-
e.log.Warnf("Can't parse go env line %q: got %d parts", scanner.Text(), len(parts))
43-
continue
44-
}
45-
46-
v, err := strconv.Unquote(parts[1])
47-
if err != nil {
48-
e.log.Warnf("Invalid key %q with value %q: %s", parts[0], parts[1], err)
49-
continue
50-
}
51-
52-
e.vars[parts[0]] = v
34+
if err = json.Unmarshal(out, &e.vars); err != nil {
35+
return errors.Wrap(err, "failed to parse go env json")
5336
}
5437

5538
e.debugf("Read go env: %#v", e.vars)

pkg/packages/skip.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,16 @@ package packages
33
import (
44
"fmt"
55
"path/filepath"
6+
"regexp"
67
)
78

9+
func pathElemReImpl(e string, sep rune) string {
10+
escapedSep := regexp.QuoteMeta(string(sep)) // needed for windows sep '\\'
11+
return fmt.Sprintf(`(^|%s)%s($|%s)`, escapedSep, e, escapedSep)
12+
}
13+
814
func pathElemRe(e string) string {
9-
return fmt.Sprintf(`(^|%c)%s($|%c)`, filepath.Separator, e, filepath.Separator)
15+
return pathElemReImpl(e, filepath.Separator)
1016
}
1117

1218
var StdExcludeDirRegexps = []string{

pkg/packages/skip_test.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package packages
2+
3+
import (
4+
"regexp"
5+
"strings"
6+
"testing"
7+
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
func TestPathElemRe(t *testing.T) {
12+
matches := [][]string{
13+
{"dir"},
14+
{"root", "dir"},
15+
{"root", "dir", "subdir"},
16+
{"dir", "subdir"},
17+
}
18+
noMatches := [][]string{
19+
{"nodir"},
20+
{"dirno"},
21+
{"root", "dirno"},
22+
{"root", "nodir"},
23+
{"root", "dirno", "subdir"},
24+
{"root", "nodir", "subdir"},
25+
{"dirno", "subdir"},
26+
{"nodir", "subdir"},
27+
}
28+
for _, sep := range []rune{'/', '\\'} {
29+
reStr := pathElemReImpl("dir", sep)
30+
re := regexp.MustCompile(reStr)
31+
for _, m := range matches {
32+
assert.Regexp(t, re, strings.Join(m, string(sep)))
33+
}
34+
for _, m := range noMatches {
35+
assert.NotRegexp(t, re, strings.Join(m, string(sep)))
36+
}
37+
}
38+
}

0 commit comments

Comments
 (0)