| 1 | // Copyright (c) Bartłomiej Płotka @bwplotka |
| 2 | // Licensed under the Apache License 2.0. |
| 3 | |
| 4 | package bingo |
| 5 | |
| 6 | import ( |
| 7 | "context" |
| 8 | "fmt" |
| 9 | "log" |
| 10 | "os" |
| 11 | "path/filepath" |
| 12 | "testing" |
| 13 | |
| 14 | "github.com/bwplotka/bingo/pkg/runner" |
| 15 | "github.com/bwplotka/bingo/pkg/version" |
| 16 | "github.com/efficientgo/core/testutil" |
| 17 | "golang.org/x/mod/module" |
| 18 | ) |
| 19 | |
| 20 | func goVersion(r *runner.Runner) string { |
| 21 | // Starting from Go 1.21, `go mod init` adds complete semver to modfile. |
| 22 | // Thus, we return <major>.<minor> for < 1.21, and full semver otherwise. |
| 23 | if r.GoVersion().Compare(version.Go121) == -1 { |
| 24 | return fmt.Sprintf("%v.%v", r.GoVersion().Major(), r.GoVersion().Minor()) |
| 25 | } |
| 26 | |
| 27 | return r.GoVersion().String() |
| 28 | } |
| 29 | |
| 30 | func TestCreateFromExistingOrNew(t *testing.T) { |
| 31 | logger := log.New(os.Stderr, "", 0) |
| 32 | r, err := runner.NewRunner(context.TODO(), logger, false, "go") |
| 33 | testutil.Ok(t, err) |
| 34 | |
| 35 | t.Run("create new and close should create empty mod file with basic autogenerated meta", func(t *testing.T) { |
| 36 | f, err := CreateFromExistingOrNew(context.TODO(), r, log.New(os.Stderr, "", 0), "non_existing.mod", "test.mod") |
| 37 | testutil.Ok(t, err) |
| 38 | testutil.Ok(t, f.Close()) |
| 39 | |
| 40 | expectContent(t, fmt.Sprintf(`module _ // Auto generated by https://github.com/bwplotka/bingo. DO NOT EDIT |
| 41 | |
| 42 | go %s |
| 43 | `, goVersion(r)), "test.mod") |
| 44 | }) |
| 45 | t.Run("create new and close should work and produce same output", func(t *testing.T) { |
| 46 | f, err := CreateFromExistingOrNew(context.TODO(), r, log.New(os.Stderr, "", 0), "test.mod", "test2.mod") |
| 47 | testutil.Ok(t, err) |
| 48 | testutil.Ok(t, f.Close()) |
| 49 | expectContent(t, fmt.Sprintf(`module _ // Auto generated by https://github.com/bwplotka/bingo. DO NOT EDIT |
| 50 | |
| 51 | go %s |
| 52 | `, goVersion(r)), "test.mod") |
| 53 | expectContent(t, fmt.Sprintf(`module _ // Auto generated by https://github.com/bwplotka/bingo. DO NOT EDIT |
| 54 | |
| 55 | go %s |
| 56 | `, goVersion(r)), "test2.mod") |
| 57 | }) |
| 58 | t.Run("create new and set direct require should work", func(t *testing.T) { |
| 59 | f, err := CreateFromExistingOrNew(context.TODO(), r, log.New(os.Stderr, "", 0), "", "test3.mod") |
| 60 | testutil.Ok(t, err) |
| 61 | testutil.Ok(t, f.SetDirectRequire(Package{Module: module.Version{Path: "github.com/yolo/best/v100", Version: "v100.0.0"}, RelPath: "thebest"})) |
| 62 | testutil.Equals(t, Package{Module: module.Version{Path: "github.com/yolo/best/v100", Version: "v100.0.0"}, RelPath: "thebest"}, *f.DirectPackage()) |
| 63 | testutil.Ok(t, f.Close()) |
| 64 | expectContent(t, fmt.Sprintf(`module _ // Auto generated by https://github.com/bwplotka/bingo. DO NOT EDIT |
| 65 | |
| 66 | go %s |
| 67 | |
| 68 | require github.com/yolo/best/v100 v100.0.0 // thebest |
| 69 | `, goVersion(r)), "test3.mod") |
| 70 | }) |
| 71 | t.Run("create new and set direct require2 should work", func(t *testing.T) { |
| 72 | f, err := CreateFromExistingOrNew(context.TODO(), r, log.New(os.Stderr, "", 0), "", "test4.mod") |
| 73 | testutil.Ok(t, err) |
| 74 | testutil.Ok(t, f.SetDirectRequire(Package{Module: module.Version{Path: "github.com/yolo/best/v100", Version: "v100.0.0"}})) |
| 75 | testutil.Equals(t, Package{Module: module.Version{Path: "github.com/yolo/best/v100", Version: "v100.0.0"}}, *f.DirectPackage()) |
| 76 | testutil.Ok(t, f.Close()) |
| 77 | expectContent(t, fmt.Sprintf(`module _ // Auto generated by https://github.com/bwplotka/bingo. DO NOT EDIT |
| 78 | |
| 79 | go %s |
| 80 | |
| 81 | require github.com/yolo/best/v100 v100.0.0 |
| 82 | `, goVersion(r)), "test4.mod") |
| 83 | }) |
| 84 | t.Run("copy and set direct require to something else", func(t *testing.T) { |
| 85 | f, err := CreateFromExistingOrNew(context.TODO(), r, log.New(os.Stderr, "", 0), "test3.mod", "test5.mod") |
| 86 | testutil.Ok(t, err) |
| 87 | testutil.Equals(t, Package{Module: module.Version{Path: "github.com/yolo/best/v100", Version: "v100.0.0"}, RelPath: "thebest"}, *f.DirectPackage()) |
| 88 | expectContent(t, fmt.Sprintf(`module _ // Auto generated by https://github.com/bwplotka/bingo. DO NOT EDIT |
| 89 | |
| 90 | go %s |
| 91 | |
| 92 | require github.com/yolo/best/v100 v100.0.0 // thebest |
| 93 | `, goVersion(r)), "test5.mod") |
| 94 | |
| 95 | testutil.Ok(t, f.SetDirectRequire(Package{Module: module.Version{Path: "github.com/yolo/not-best", Version: "v1"}})) |
| 96 | testutil.Equals(t, Package{Module: module.Version{Path: "github.com/yolo/not-best", Version: "v1"}}, *f.DirectPackage()) |
| 97 | testutil.Ok(t, f.Close()) |
| 98 | expectContent(t, fmt.Sprintf(`module _ // Auto generated by https://github.com/bwplotka/bingo. DO NOT EDIT |
| 99 | |
| 100 | go %s |
| 101 | |
| 102 | require github.com/yolo/not-best v1 |
| 103 | `, goVersion(r)), "test5.mod") |
| 104 | }) |
| 105 | } |
| 106 | |
| 107 | func expectContent(t *testing.T, expected string, file string) { |
| 108 | t.Helper() |
| 109 | |
| 110 | b, err := os.ReadFile(file) |
| 111 | testutil.Ok(t, err) |
| 112 | testutil.Equals(t, expected, string(b)) |
| 113 | } |
| 114 | |
| 115 | func TestModFile(t *testing.T) { |
| 116 | tmpDir := t.TempDir() |
| 117 | |
| 118 | t.Run("without auto fetch directives", func(t *testing.T) { |
| 119 | testFile := filepath.Join(tmpDir, "test.mod") |
| 120 | testutil.Ok(t, os.WriteFile(testFile, []byte(`module _ // Auto generated by https://github.com/bwplotka/bingo. DO NOT EDIT |
| 121 | |
| 122 | go 1.14 |
| 123 | |
| 124 | // bingo:no_directive_fetch |
| 125 | |
| 126 | replace ( |
| 127 | // Ridiculous but Prometheus v2.4.3 did not have Go modules |
| 128 | github.com/Azure/azure-sdk-for-go => github.com/Azure/azure-sdk-for-go v5.0.0-beta.0.20161028183111-bd73d950fa44+incompatible |
| 129 | github.com/Azure/go-autorest => github.com/Azure/go-autorest v9.9.0+incompatible |
| 130 | github.com/cockroachdb/cmux => github.com/cockroachdb/cmux v0.0.0-20170110192607-30d10be49292 |
| 131 | github.com/cockroachdb/cockroach => github.com/cockroachdb/cockroach v0.0.0-20170608034007-84bc9597164f |
| 132 | github.com/googleapis/gnostic => github.com/googleapis/gnostic v0.2.3-0.20180520015035-48a0ecefe2e4 |
| 133 | github.com/miekg/dns => github.com/miekg/dns v1.0.4 |
| 134 | github.com/prometheus/client_golang => github.com/prometheus/client_golang v0.9.0-pre1.0.20180607123607-faf4ec335fe0 |
| 135 | github.com/prometheus/common => github.com/prometheus/common v0.0.0-20180518154759-7600349dcfe1 |
| 136 | github.com/prometheus/tsdb => github.com/prometheus/tsdb v0.0.0-20180921053122-9c8ca47399a7 |
| 137 | k8s.io/api => k8s.io/api v0.0.0-20180628040859-072894a440bd |
| 138 | k8s.io/apimachinery => k8s.io/apimachinery v0.0.0-20180621070125-103fd098999d |
| 139 | k8s.io/client-go => k8s.io/client-go v8.0.0+incompatible |
| 140 | k8s.io/klog => github.com/simonpasquier/klog-gokit v0.1.0 |
| 141 | ) |
| 142 | |
| 143 | require github.com/prometheus/prometheus v2.4.3+incompatible // cmd/prometheus |
| 144 | `), os.ModePerm)) |
| 145 | |
| 146 | mf, err := OpenModFile(testFile) |
| 147 | testutil.Ok(t, err) |
| 148 | |
| 149 | testutil.Equals(t, true, mf.IsDirectivesAutoFetchDisabled()) |
| 150 | testutil.Equals(t, Package{Module: module.Version{Path: "github.com/prometheus/prometheus", Version: "v2.4.3+incompatible"}, RelPath: "cmd/prometheus"}, *mf.DirectPackage()) |
| 151 | }) |
| 152 | |
| 153 | t.Run("with auto fetch directives", func(t *testing.T) { |
| 154 | testFile := filepath.Join(tmpDir, "test.mod") |
| 155 | testutil.Ok(t, os.WriteFile(testFile, []byte(`module _ // Auto generated by https://github.com/bwplotka/bingo. DO NOT EDIT |
| 156 | |
| 157 | go 1.14 |
| 158 | |
| 159 | replace ( |
| 160 | // Ridiculous but Prometheus v2.4.3 did not have Go modules |
| 161 | github.com/Azure/azure-sdk-for-go => github.com/Azure/azure-sdk-for-go v5.0.0-beta.0.20161028183111-bd73d950fa44+incompatible |
| 162 | github.com/Azure/go-autorest => github.com/Azure/go-autorest v9.9.0+incompatible |
| 163 | github.com/cockroachdb/cmux => github.com/cockroachdb/cmux v0.0.0-20170110192607-30d10be49292 |
| 164 | github.com/cockroachdb/cockroach => github.com/cockroachdb/cockroach v0.0.0-20170608034007-84bc9597164f |
| 165 | github.com/googleapis/gnostic => github.com/googleapis/gnostic v0.2.3-0.20180520015035-48a0ecefe2e4 |
| 166 | github.com/miekg/dns => github.com/miekg/dns v1.0.4 |
| 167 | github.com/prometheus/client_golang => github.com/prometheus/client_golang v0.9.0-pre1.0.20180607123607-faf4ec335fe0 |
| 168 | github.com/prometheus/common => github.com/prometheus/common v0.0.0-20180518154759-7600349dcfe1 |
| 169 | github.com/prometheus/tsdb => github.com/prometheus/tsdb v0.0.0-20180921053122-9c8ca47399a7 |
| 170 | k8s.io/api => k8s.io/api v0.0.0-20180628040859-072894a440bd |
| 171 | k8s.io/apimachinery => k8s.io/apimachinery v0.0.0-20180621070125-103fd098999d |
| 172 | k8s.io/client-go => k8s.io/client-go v8.0.0+incompatible |
| 173 | k8s.io/klog => github.com/simonpasquier/klog-gokit v0.1.0 |
| 174 | ) |
| 175 | |
| 176 | require github.com/prometheus/prometheus v2.4.3+incompatible // cmd/prometheus |
| 177 | `), os.ModePerm)) |
| 178 | |
| 179 | mf, err := OpenModFile(testFile) |
| 180 | testutil.Ok(t, err) |
| 181 | |
| 182 | testutil.Equals(t, false, mf.IsDirectivesAutoFetchDisabled()) |
| 183 | testutil.Equals(t, Package{Module: module.Version{Path: "github.com/prometheus/prometheus", Version: "v2.4.3+incompatible"}, RelPath: "cmd/prometheus"}, *mf.DirectPackage()) |
| 184 | }) |
| 185 | |
| 186 | t.Run("with build attributes1", func(t *testing.T) { |
| 187 | testFile := filepath.Join(tmpDir, "test.mod") |
| 188 | testutil.Ok(t, os.WriteFile(testFile, []byte(`module _ // Auto generated by https://github.com/bwplotka/bingo. DO NOT EDIT |
| 189 | |
| 190 | go 1.14 |
| 191 | |
| 192 | require github.com/prometheus/prometheus v2.4.3+incompatible // cmd/prometheus -tags=yolo,linux |
| 193 | `), os.ModePerm)) |
| 194 | |
| 195 | mf, err := OpenModFile(testFile) |
| 196 | testutil.Ok(t, err) |
| 197 | |
| 198 | testutil.Equals(t, false, mf.IsDirectivesAutoFetchDisabled()) |
| 199 | testutil.Equals(t, Package{ |
| 200 | Module: module.Version{Path: "github.com/prometheus/prometheus", Version: "v2.4.3+incompatible"}, |
| 201 | RelPath: "cmd/prometheus", |
| 202 | BuildFlags: []string{"-tags=yolo,linux"}, |
| 203 | }, *mf.DirectPackage()) |
| 204 | }) |
| 205 | |
| 206 | t.Run("with build attributes2", func(t *testing.T) { |
| 207 | testFile := filepath.Join(tmpDir, "test.mod") |
| 208 | testutil.Ok(t, os.WriteFile(testFile, []byte(`module _ // Auto generated by https://github.com/bwplotka/bingo. DO NOT EDIT |
| 209 | |
| 210 | go 1.14 |
| 211 | |
| 212 | require github.com/prometheus/prometheus v2.4.3+incompatible // cmd/prometheus CGO_ENABLED=1 GOWASM=somefeature |
| 213 | `), os.ModePerm)) |
| 214 | |
| 215 | mf, err := OpenModFile(testFile) |
| 216 | testutil.Ok(t, err) |
| 217 | |
| 218 | testutil.Equals(t, false, mf.IsDirectivesAutoFetchDisabled()) |
| 219 | testutil.Equals(t, Package{ |
| 220 | Module: module.Version{Path: "github.com/prometheus/prometheus", Version: "v2.4.3+incompatible"}, |
| 221 | RelPath: "cmd/prometheus", |
| 222 | BuildEnvs: []string{"CGO_ENABLED=1", "GOWASM=somefeature"}, |
| 223 | }, *mf.DirectPackage()) |
| 224 | }) |
| 225 | |
| 226 | t.Run("with build attributes3", func(t *testing.T) { |
| 227 | testFile := filepath.Join(tmpDir, "test.mod") |
| 228 | testutil.Ok(t, os.WriteFile(testFile, []byte(`module _ // Auto generated by https://github.com/bwplotka/bingo. DO NOT EDIT |
| 229 | |
| 230 | go 1.14 |
| 231 | |
| 232 | require github.com/prometheus/prometheus v2.4.3+incompatible // cmd/prometheus CGO_ENABLED=1 GOWASM=somefeature -tags=yolo,linux |
| 233 | `), os.ModePerm)) |
| 234 | |
| 235 | mf, err := OpenModFile(testFile) |
| 236 | testutil.Ok(t, err) |
| 237 | |
| 238 | testutil.Equals(t, false, mf.IsDirectivesAutoFetchDisabled()) |
| 239 | testutil.Equals(t, Package{ |
| 240 | Module: module.Version{Path: "github.com/prometheus/prometheus", Version: "v2.4.3+incompatible"}, |
| 241 | RelPath: "cmd/prometheus", |
| 242 | BuildEnvs: []string{"CGO_ENABLED=1", "GOWASM=somefeature"}, |
| 243 | BuildFlags: []string{"-tags=yolo,linux"}, |
| 244 | }, *mf.DirectPackage()) |
| 245 | }) |
| 246 | t.Run("with build attributes without relpath", func(t *testing.T) { |
| 247 | testFile := filepath.Join(tmpDir, "test.mod") |
| 248 | testutil.Ok(t, os.WriteFile(testFile, []byte(`module _ // Auto generated by https://github.com/bwplotka/bingo. DO NOT EDIT |
| 249 | |
| 250 | go 1.14 |
| 251 | |
| 252 | require github.com/prometheus/prometheus v2.4.3+incompatible // CGO_ENABLED=1 GOWASM=somefeature -tags=yolo,linux |
| 253 | `), os.ModePerm)) |
| 254 | |
| 255 | mf, err := OpenModFile(testFile) |
| 256 | testutil.Ok(t, err) |
| 257 | |
| 258 | testutil.Equals(t, false, mf.IsDirectivesAutoFetchDisabled()) |
| 259 | testutil.Equals(t, Package{ |
| 260 | Module: module.Version{Path: "github.com/prometheus/prometheus", Version: "v2.4.3+incompatible"}, |
| 261 | BuildEnvs: []string{"CGO_ENABLED=1", "GOWASM=somefeature"}, |
| 262 | BuildFlags: []string{"-tags=yolo,linux"}, |
| 263 | }, *mf.DirectPackage()) |
| 264 | }) |
| 265 | } |