1load("@bazel_skylib//lib:unittest.bzl", "asserts", "unittest")
2load("//internal/bzlmod:go_mod.bzl", "parse_go_mod", "parse_go_sum")
3
4_GO_MOD_CONTENT = """ go 1.18
5
6require (
7 github.com/bazelbuild/buildtools v0.0.0-20220531122519-a43aed7014c8
8 github.com/bazelbuild/rules_go "v0.\\n\\\\\\"33.0"
9github.com/bmatcuk/doublestar/v4 v4.0.2 // indirect
10 // some comment
11 `golang.org/x/tools` "v0.1.11" // foobar
12 github.com/go-fsnotify/fsnotify v1.5.4
13)
14
15replace github.com/go-fsnotify/fsnotify => github.com/fsnotify/fsnotify v1.4.2
16replace github.com/bmatcuk/doublestar/v4 v4.0.2 => github.com/bmatcuk/doublestar/v4 v4.0.3
17
18module github.com/bazelbuild/bazel-gazelle
19
20 exclude (
21 github.com/bazelbuild/rules_go v0.33.0
22 )
23
24 retract v1.0.0
25
26require golang.org/x/sys v0.0.0-20220624220833-87e55d714810 // indirect
27"""
28
29_EXPECTED_GO_MOD_PARSE_RESULT = struct(
30 go = (1, 18),
31 module = "github.com/bazelbuild/bazel-gazelle",
32 replace_map = {
33 "github.com/go-fsnotify/fsnotify": struct(from_version = None, to_path = "github.com/fsnotify/fsnotify", version = "1.4.2"),
34 "github.com/bmatcuk/doublestar/v4": struct(from_version = "4.0.2", to_path = "github.com/bmatcuk/doublestar/v4", version = "4.0.3"),
35 },
36 require = (
37 struct(indirect = False, path = "github.com/bazelbuild/buildtools", version = "v0.0.0-20220531122519-a43aed7014c8"),
38 struct(indirect = False, path = "github.com/bazelbuild/rules_go", version = "v0.n\\\"33.0"),
39 struct(indirect = True, path = "github.com/bmatcuk/doublestar/v4", version = "v4.0.2"),
40 struct(indirect = False, path = "golang.org/x/tools", version = "v0.1.11"),
41 struct(indirect = False, path = "github.com/go-fsnotify/fsnotify", version = "v1.5.4"),
42 struct(indirect = True, path = "golang.org/x/sys", version = "v0.0.0-20220624220833-87e55d714810"),
43 ),
44)
45
46def _go_mod_test_impl(ctx):
47 env = unittest.begin(ctx)
48 asserts.equals(env, _EXPECTED_GO_MOD_PARSE_RESULT, parse_go_mod(_GO_MOD_CONTENT, "/go.mod"))
49 return unittest.end(env)
50
51go_mod_test = unittest.make(_go_mod_test_impl)
52
53_GO_MOD_21_CONTENT = """go 1.21.0rc1
54
55module example.com
56
57toolchain go1.22.2
58"""
59
60_EXPECTED_GO_MOD_21_PARSE_RESULT = struct(
61 go = (1, 21),
62 module = "example.com",
63 replace_map = {},
64 require = (),
65)
66
67def _go_mod_21_test_impl(ctx):
68 env = unittest.begin(ctx)
69 asserts.equals(env, _EXPECTED_GO_MOD_21_PARSE_RESULT, parse_go_mod(_GO_MOD_21_CONTENT, "/go.mod"))
70 return unittest.end(env)
71
72go_mod_21_test = unittest.make(_go_mod_21_test_impl)
73
74_GO_SUM_CONTENT = """cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
75github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
76github.com/bazelbuild/buildtools v0.0.0-20220531122519-a43aed7014c8 h1:fmdo+fvvWlhldUcqkhAMpKndSxMN3vH5l7yow5cEaiQ=
77github.com/bazelbuild/buildtools v0.0.0-20220531122519-a43aed7014c8/go.mod h1:689QdV3hBP7Vo9dJMmzhoYIyo/9iMhEmHkJcnaPRCbo=
78github.com/bazelbuild/rules_go v0.33.0 h1:WW9CHmFxbE+Lm4qiLOFAPogmiAUzZtvQsWxUcm4wwaU=
79github.com/bazelbuild/rules_go v0.33.0/go.mod h1:MC23Dc/wkXEyk3Wpq6lCqz0ZAYOZDw2DR5y3N1q2i7M=
80"""
81
82_EXPECTED_GO_SUM_PARSE_RESULT = {
83 ("github.com/bazelbuild/buildtools", "0.0.0-20220531122519-a43aed7014c8"): "h1:fmdo+fvvWlhldUcqkhAMpKndSxMN3vH5l7yow5cEaiQ=",
84 ("github.com/bazelbuild/rules_go", "0.33.0"): "h1:WW9CHmFxbE+Lm4qiLOFAPogmiAUzZtvQsWxUcm4wwaU=",
85}
86
87def _go_sum_test_impl(ctx):
88 env = unittest.begin(ctx)
89 asserts.equals(env, _EXPECTED_GO_SUM_PARSE_RESULT, parse_go_sum(_GO_SUM_CONTENT))
90 return unittest.end(env)
91
92go_sum_test = unittest.make(_go_sum_test_impl)
93
94def go_mod_test_suite(name):
95 unittest.suite(
96 name,
97 go_mod_test,
98 go_mod_21_test,
99 go_sum_test,
100 )
View as plain text