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
(t *testing.T)
| 23 | // exactly that command, so any replace directive slipping into go.mod breaks |
| 24 | // the advertised installation path for the next release. |
| 25 | func TestGoModHasNoReplaceDirectives(t *testing.T) { |
| 26 | data, err := os.ReadFile("go.mod") |
| 27 | if err != nil { |
| 28 | t.Fatalf("read go.mod: %v", err) |
| 29 | } |
| 30 | |
| 31 | var ( |
| 32 | inBlock bool |
| 33 | offenders []string |
| 34 | ) |
| 35 | for i, raw := range strings.Split(string(data), "\n") { |
| 36 | line := strings.TrimSpace(raw) |
| 37 | if idx := strings.Index(line, "//"); idx >= 0 { |
| 38 | line = strings.TrimSpace(line[:idx]) |
| 39 | } |
| 40 | |
| 41 | if inBlock { |
| 42 | if line == ")" { |
| 43 | inBlock = false |
| 44 | continue |
| 45 | } |
| 46 | if line != "" { |
| 47 | offenders = append(offenders, fmt.Sprintf(" go.mod:%d: %s", i+1, raw)) |
| 48 | } |
| 49 | continue |
| 50 | } |
| 51 | |
| 52 | switch { |
| 53 | case line == "replace (": |
| 54 | inBlock = true |
| 55 | case strings.HasPrefix(line, "replace "): |
| 56 | offenders = append(offenders, fmt.Sprintf(" go.mod:%d: %s", i+1, raw)) |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | if len(offenders) > 0 { |
| 61 | t.Fatalf("go.mod must not contain replace directives; "+ |
| 62 | "they break `go install github.com/sqlc-dev/sqlc/cmd/sqlc@latest`.\n"+ |
| 63 | "See https://github.com/sqlc-dev/sqlc/issues/4397\n%s", |
| 64 | strings.Join(offenders, "\n")) |
| 65 | } |
| 66 | } |