-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathgomod_test.go
More file actions
66 lines (60 loc) · 1.85 KB
/
gomod_test.go
File metadata and controls
66 lines (60 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package sqlc
import (
"fmt"
"os"
"strings"
"testing"
)
// TestGoModHasNoReplaceDirectives guards against regressions of
// https://github.com/sqlc-dev/sqlc/issues/4397.
//
// When go.mod contains a replace directive, the Go toolchain refuses to run
// `go install github.com/sqlc-dev/sqlc/cmd/sqlc@latest` (and the equivalent
// `go run ...@latest`):
//
// go: github.com/sqlc-dev/sqlc/cmd/sqlc@latest (in github.com/sqlc-dev/sqlc@v...):
// The go.mod file for the module providing named packages contains one or
// more replace directives. It must not contain directives that would cause
// it to be interpreted differently than if it were the main module.
//
// https://docs.sqlc.dev/en/latest/overview/install.html tells users to run
// exactly that command, so any replace directive slipping into go.mod breaks
// the advertised installation path for the next release.
func TestGoModHasNoReplaceDirectives(t *testing.T) {
data, err := os.ReadFile("go.mod")
if err != nil {
t.Fatalf("read go.mod: %v", err)
}
var (
inBlock bool
offenders []string
)
for i, raw := range strings.Split(string(data), "\n") {
line := strings.TrimSpace(raw)
if idx := strings.Index(line, "//"); idx >= 0 {
line = strings.TrimSpace(line[:idx])
}
if inBlock {
if line == ")" {
inBlock = false
continue
}
if line != "" {
offenders = append(offenders, fmt.Sprintf(" go.mod:%d: %s", i+1, raw))
}
continue
}
switch {
case line == "replace (":
inBlock = true
case strings.HasPrefix(line, "replace "):
offenders = append(offenders, fmt.Sprintf(" go.mod:%d: %s", i+1, raw))
}
}
if len(offenders) > 0 {
t.Fatalf("go.mod must not contain replace directives; "+
"they break `go install github.com/sqlc-dev/sqlc/cmd/sqlc@latest`.\n"+
"See https://github.com/sqlc-dev/sqlc/issues/4397\n%s",
strings.Join(offenders, "\n"))
}
}