-
Notifications
You must be signed in to change notification settings - Fork 881
Expand file tree
/
Copy pathmod_test.go
More file actions
82 lines (73 loc) · 2.48 KB
/
mod_test.go
File metadata and controls
82 lines (73 loc) · 2.48 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package sei_test
import (
"bufio"
"os"
"strings"
"testing"
"github.com/stretchr/testify/require"
"golang.org/x/mod/modfile"
"golang.org/x/mod/module"
)
// bannedUpstream lists original upstream modules that sei has forked into local
// subdirectories. Neither go.mod nor go.sum should reference these.
var bannedUpstream = []string{
"github.com/tendermint/tendermint",
"github.com/cosmos/cosmos-sdk",
"github.com/CosmWasm/wasmd",
"github.com/CosmWasm/wasmvm",
"github.com/cosmos/ibc-go",
}
// isBannedModule reports whether modPath (after stripping any /vN suffix)
// matches a banned upstream module exactly.
func isBannedModule(modPath string) (banned string, ok bool) {
prefix, _, valid := module.SplitPathVersion(modPath)
if !valid {
prefix = modPath
}
for _, b := range bannedUpstream {
if prefix == b {
return b, true
}
}
return "", false
}
// TestGoMod_NoBannedDependencies ensures that go.mod require directives never
// reference original upstream modules that sei has forked.
//
// Module paths may carry a major-version suffix (e.g. github.com/cosmos/ibc-go/v6).
// The check strips the suffix with module.SplitPathVersion before comparing,
// so both github.com/cosmos/ibc-go and github.com/cosmos/ibc-go/v6 are caught.
//
// Modules that share a prefix but are distinct (e.g. github.com/tendermint/tm-db,
// github.com/tendermint/go-amino) are intentionally NOT banned.
func TestGoMod_NoBannedDependencies(t *testing.T) {
data, err := os.ReadFile("go.mod")
require.NoError(t, err)
f, err := modfile.Parse("go.mod", data, nil)
require.NoError(t, err)
for _, req := range f.Require {
banned, ok := isBannedModule(req.Mod.Path)
require.Falsef(t, ok, "go.mod must not depend on %s (use local fork instead), but found: %s %s",
banned, req.Mod.Path, req.Mod.Version)
}
}
// TestGoSum_NoBannedDependencies ensures that go.sum never contains checksums
// for original upstream modules that sei has forked.
func TestGoSum_NoBannedDependencies(t *testing.T) {
f, err := os.Open("go.sum")
require.NoError(t, err)
defer func() { require.NoError(t, f.Close()) }()
scanner := bufio.NewScanner(f)
for scanner.Scan() {
line := scanner.Text()
// go.sum lines have the format: <module> <version>[/go.mod] <hash>
modPath, _, _ := strings.Cut(line, " ")
if modPath == "" {
continue
}
banned, ok := isBannedModule(modPath)
require.Falsef(t, ok, "go.sum must not reference %s (use local fork instead), but found: %s",
banned, line)
}
require.NoError(t, scanner.Err())
}