github.phpd.cn/goreleaser/goreleaser@v0.92.0/internal/pipe/scoop/scoop_test.go (about) 1 package scoop 2 3 import ( 4 "bytes" 5 "flag" 6 "io/ioutil" 7 "os" 8 "testing" 9 10 "path/filepath" 11 12 "github.com/goreleaser/goreleaser/internal/artifact" 13 "github.com/goreleaser/goreleaser/internal/client" 14 "github.com/goreleaser/goreleaser/internal/pipe" 15 "github.com/goreleaser/goreleaser/internal/testlib" 16 "github.com/goreleaser/goreleaser/pkg/config" 17 "github.com/goreleaser/goreleaser/pkg/context" 18 "github.com/stretchr/testify/assert" 19 "github.com/stretchr/testify/require" 20 ) 21 22 var update = flag.Bool("update", false, "update .golden files") 23 24 func TestDescription(t *testing.T) { 25 assert.NotEmpty(t, Pipe{}.String()) 26 } 27 28 func TestDefault(t *testing.T) { 29 _, back := testlib.Mktmp(t) 30 defer back() 31 32 var ctx = &context.Context{ 33 Config: config.Project{ 34 ProjectName: "barr", 35 Builds: []config.Build{ 36 { 37 Binary: "foo", 38 Goos: []string{"linux", "darwin"}, 39 Goarch: []string{"386", "amd64"}, 40 }, 41 { 42 Binary: "bar", 43 Goos: []string{"linux", "darwin"}, 44 Goarch: []string{"386", "amd64"}, 45 Ignore: []config.IgnoredBuild{ 46 {Goos: "darwin", Goarch: "amd64"}, 47 }, 48 }, 49 { 50 Binary: "foobar", 51 Goos: []string{"linux"}, 52 Goarch: []string{"amd64"}, 53 }, 54 }, 55 }, 56 } 57 assert.NoError(t, Pipe{}.Default(ctx)) 58 assert.Equal(t, ctx.Config.ProjectName, ctx.Config.Scoop.Name) 59 assert.NotEmpty(t, ctx.Config.Scoop.CommitAuthor.Name) 60 assert.NotEmpty(t, ctx.Config.Scoop.CommitAuthor.Email) 61 } 62 63 func Test_doRun(t *testing.T) { 64 folder, back := testlib.Mktmp(t) 65 defer back() 66 var file = filepath.Join(folder, "archive") 67 require.NoError(t, ioutil.WriteFile(file, []byte("lorem ipsum"), 0644)) 68 69 type errChecker func(*testing.T, error) 70 var shouldErr = func(msg string) errChecker { 71 return func(t *testing.T, err error) { 72 assert.Error(t, err) 73 assert.EqualError(t, err, msg) 74 } 75 } 76 var shouldNotErr = func(t *testing.T, err error) { 77 assert.NoError(t, err) 78 } 79 type args struct { 80 ctx *context.Context 81 client client.Client 82 } 83 tests := []struct { 84 name string 85 args args 86 artifacts []artifact.Artifact 87 assertError errChecker 88 }{ 89 { 90 "valid", 91 args{ 92 &context.Context{ 93 Git: context.GitInfo{ 94 CurrentTag: "v1.0.1", 95 }, 96 Version: "1.0.1", 97 Artifacts: artifact.New(), 98 Config: config.Project{ 99 Builds: []config.Build{ 100 {Binary: "test", Goarch: []string{"amd64"}, Goos: []string{"windows"}}, 101 }, 102 Dist: ".", 103 ProjectName: "run-pipe", 104 Archive: config.Archive{ 105 Format: "tar.gz", 106 }, 107 Release: config.Release{ 108 GitHub: config.Repo{ 109 Owner: "test", 110 Name: "test", 111 }, 112 }, 113 Scoop: config.Scoop{ 114 Bucket: config.Repo{ 115 Owner: "test", 116 Name: "test", 117 }, 118 Description: "A run pipe test formula", 119 Homepage: "https://github.com/goreleaser", 120 }, 121 }, 122 }, 123 &DummyClient{}, 124 }, 125 []artifact.Artifact{ 126 {Name: "foo_1.0.1_windows_amd64.tar.gz", Goos: "windows", Goarch: "amd64", Path: file}, 127 {Name: "foo_1.0.1_windows_386.tar.gz", Goos: "windows", Goarch: "386", Path: file}, 128 }, 129 shouldNotErr, 130 }, 131 { 132 "valid", 133 args{ 134 &context.Context{ 135 Git: context.GitInfo{ 136 CurrentTag: "v1.0.1", 137 }, 138 Version: "1.0.1", 139 Artifacts: artifact.New(), 140 Config: config.Project{ 141 GitHubURLs: config.GitHubURLs{Download: "https://api.custom.github.enterprise.com"}, 142 Builds: []config.Build{ 143 {Binary: "test", Goarch: []string{"amd64"}, Goos: []string{"windows"}}, 144 }, 145 Dist: ".", 146 ProjectName: "run-pipe", 147 Archive: config.Archive{ 148 Format: "tar.gz", 149 }, 150 Release: config.Release{ 151 GitHub: config.Repo{ 152 Owner: "test", 153 Name: "test", 154 }, 155 }, 156 Scoop: config.Scoop{ 157 Bucket: config.Repo{ 158 Owner: "test", 159 Name: "test", 160 }, 161 Description: "A run pipe test formula", 162 Homepage: "https://github.com/goreleaser", 163 }, 164 }, 165 }, 166 &DummyClient{}, 167 }, 168 []artifact.Artifact{ 169 {Name: "foo_1.0.1_windows_amd64.tar.gz", Goos: "windows", Goarch: "amd64", Path: file}, 170 {Name: "foo_1.0.1_windows_386.tar.gz", Goos: "windows", Goarch: "386", Path: file}, 171 }, 172 shouldNotErr, 173 }, 174 { 175 "no windows build", 176 args{ 177 &context.Context{ 178 Git: context.GitInfo{ 179 CurrentTag: "v1.0.1", 180 }, 181 Version: "1.0.1", 182 Artifacts: artifact.New(), 183 Config: config.Project{ 184 Builds: []config.Build{ 185 {Binary: "test"}, 186 }, 187 Dist: ".", 188 ProjectName: "run-pipe", 189 Archive: config.Archive{ 190 Format: "tar.gz", 191 }, 192 Release: config.Release{ 193 GitHub: config.Repo{ 194 Owner: "test", 195 Name: "test", 196 }, 197 }, 198 Scoop: config.Scoop{ 199 Bucket: config.Repo{ 200 Owner: "test", 201 Name: "test", 202 }, 203 Description: "A run pipe test formula", 204 Homepage: "https://github.com/goreleaser", 205 }, 206 }, 207 }, 208 &DummyClient{}, 209 }, 210 []artifact.Artifact{ 211 {Name: "foo_1.0.1_linux_amd64.tar.gz", Goos: "linux", Goarch: "amd64"}, 212 {Name: "foo_1.0.1_linux_386.tar.gz", Goos: "linux", Goarch: "386"}, 213 }, 214 shouldErr("scoop requires a windows build"), 215 }, 216 { 217 "no scoop", 218 args{ 219 &context.Context{ 220 Git: context.GitInfo{ 221 CurrentTag: "v1.0.1", 222 }, 223 Version: "1.0.1", 224 Artifacts: artifact.New(), 225 Config: config.Project{ 226 Builds: []config.Build{ 227 {Binary: "test", Goarch: []string{"amd64"}, Goos: []string{"windows"}}, 228 }, 229 Dist: ".", 230 ProjectName: "run-pipe", 231 Archive: config.Archive{ 232 Format: "tar.gz", 233 }, 234 Release: config.Release{ 235 GitHub: config.Repo{ 236 Owner: "test", 237 Name: "test", 238 }, 239 }, 240 }, 241 }, 242 &DummyClient{}, 243 }, 244 []artifact.Artifact{ 245 {Name: "foo_1.0.1_windows_amd64.tar.gz", Goos: "windows", Goarch: "amd64"}, 246 {Name: "foo_1.0.1_windows_386.tar.gz", Goos: "windows", Goarch: "386"}, 247 }, 248 shouldErr("scoop section is not configured"), 249 }, 250 { 251 "no publish", 252 args{ 253 &context.Context{ 254 Git: context.GitInfo{ 255 CurrentTag: "v1.0.1", 256 }, 257 Version: "1.0.1", 258 Artifacts: artifact.New(), 259 Config: config.Project{ 260 Builds: []config.Build{ 261 {Binary: "test", Goarch: []string{"amd64"}, Goos: []string{"windows"}}, 262 }, 263 Dist: ".", 264 ProjectName: "run-pipe", 265 Archive: config.Archive{ 266 Format: "tar.gz", 267 }, 268 Release: config.Release{ 269 GitHub: config.Repo{ 270 Owner: "test", 271 Name: "test", 272 }, 273 }, 274 Scoop: config.Scoop{ 275 Bucket: config.Repo{ 276 Owner: "test", 277 Name: "test", 278 }, 279 Description: "A run pipe test formula", 280 Homepage: "https://github.com/goreleaser", 281 }, 282 }, 283 SkipPublish: true, 284 }, 285 &DummyClient{}, 286 }, 287 []artifact.Artifact{ 288 {Name: "foo_1.0.1_windows_amd64.tar.gz", Goos: "windows", Goarch: "amd64", Path: file}, 289 {Name: "foo_1.0.1_windows_386.tar.gz", Goos: "windows", Goarch: "386", Path: file}, 290 }, 291 shouldErr(pipe.ErrSkipPublishEnabled.Error()), 292 }, 293 { 294 "is draft", 295 args{ 296 &context.Context{ 297 Git: context.GitInfo{ 298 CurrentTag: "v1.0.1", 299 }, 300 Version: "1.0.1", 301 Artifacts: artifact.New(), 302 Config: config.Project{ 303 Builds: []config.Build{ 304 {Binary: "test", Goarch: []string{"amd64"}, Goos: []string{"windows"}}, 305 }, 306 Dist: ".", 307 ProjectName: "run-pipe", 308 Archive: config.Archive{ 309 Format: "tar.gz", 310 }, 311 Release: config.Release{ 312 Draft: true, 313 }, 314 Scoop: config.Scoop{ 315 Bucket: config.Repo{ 316 Owner: "test", 317 Name: "test", 318 }, 319 Description: "A run pipe test formula", 320 Homepage: "https://github.com/goreleaser", 321 }, 322 }, 323 }, 324 &DummyClient{}, 325 }, 326 []artifact.Artifact{ 327 {Name: "foo_1.0.1_windows_amd64.tar.gz", Goos: "windows", Goarch: "amd64", Path: file}, 328 {Name: "foo_1.0.1_windows_386.tar.gz", Goos: "windows", Goarch: "386", Path: file}, 329 }, 330 shouldErr("release is marked as draft"), 331 }, 332 { 333 "no archive", 334 args{ 335 &context.Context{ 336 Git: context.GitInfo{ 337 CurrentTag: "v1.0.1", 338 }, 339 Version: "1.0.1", 340 Artifacts: artifact.New(), 341 Config: config.Project{ 342 Builds: []config.Build{ 343 {Binary: "test", Goarch: []string{"amd64"}, Goos: []string{"windows"}}, 344 }, 345 Dist: ".", 346 ProjectName: "run-pipe", 347 Archive: config.Archive{ 348 Format: "binary", 349 }, 350 Release: config.Release{ 351 Draft: true, 352 }, 353 Scoop: config.Scoop{ 354 Bucket: config.Repo{ 355 Owner: "test", 356 Name: "test", 357 }, 358 Description: "A run pipe test formula", 359 Homepage: "https://github.com/goreleaser", 360 }, 361 }, 362 }, 363 &DummyClient{}, 364 }, 365 []artifact.Artifact{ 366 {Name: "foo_1.0.1_windows_amd64.tar.gz", Goos: "windows", Goarch: "amd64"}, 367 {Name: "foo_1.0.1_windows_386.tar.gz", Goos: "windows", Goarch: "386"}, 368 }, 369 shouldErr("archive format is binary"), 370 }, 371 } 372 for _, tt := range tests { 373 t.Run(tt.name, func(t *testing.T) { 374 var ctx = tt.args.ctx 375 for _, a := range tt.artifacts { 376 ctx.Artifacts.Add(a) 377 } 378 require.NoError(t, Pipe{}.Default(ctx)) 379 tt.assertError(t, doRun(ctx, tt.args.client)) 380 }) 381 } 382 } 383 384 func Test_buildManifest(t *testing.T) { 385 folder, err := ioutil.TempDir("", "goreleasertest") 386 require.NoError(t, err) 387 var file = filepath.Join(folder, "archive") 388 require.NoError(t, ioutil.WriteFile(file, []byte("lorem ipsum"), 0644)) 389 390 tests := []struct { 391 filename string 392 ctx *context.Context 393 }{ 394 { 395 "testdata/test_buildmanifest.json.golden", 396 &context.Context{ 397 Git: context.GitInfo{ 398 CurrentTag: "v1.0.1", 399 }, 400 Version: "1.0.1", 401 Artifacts: artifact.New(), 402 Config: config.Project{ 403 GitHubURLs: config.GitHubURLs{ 404 Download: "https://github.com", 405 }, 406 Builds: []config.Build{ 407 {Binary: "test"}, 408 }, 409 Dist: ".", 410 ProjectName: "run-pipe", 411 Archive: config.Archive{ 412 Format: "tar.gz", 413 }, 414 Release: config.Release{ 415 GitHub: config.Repo{ 416 Owner: "test", 417 Name: "test", 418 }, 419 }, 420 Scoop: config.Scoop{ 421 Bucket: config.Repo{ 422 Owner: "test", 423 Name: "test", 424 }, 425 Description: "A run pipe test formula", 426 Homepage: "https://github.com/goreleaser", 427 Persist: []string{"data", "config", "test.ini"}, 428 }, 429 }, 430 }, 431 }, 432 { 433 "testdata/test_buildmanifest_url_template.json.golden", 434 &context.Context{ 435 Git: context.GitInfo{ 436 CurrentTag: "v1.0.1", 437 }, 438 Version: "1.0.1", 439 Artifacts: artifact.New(), 440 Config: config.Project{ 441 GitHubURLs: config.GitHubURLs{ 442 Download: "https://github.com", 443 }, 444 Builds: []config.Build{ 445 {Binary: "test"}, 446 }, 447 Dist: ".", 448 ProjectName: "run-pipe", 449 Archive: config.Archive{ 450 Format: "tar.gz", 451 }, 452 Release: config.Release{ 453 GitHub: config.Repo{ 454 Owner: "test", 455 Name: "test", 456 }, 457 }, 458 Scoop: config.Scoop{ 459 Bucket: config.Repo{ 460 Owner: "test", 461 Name: "test", 462 }, 463 Description: "A run pipe test formula", 464 Homepage: "https://github.com/goreleaser", 465 URLTemplate: "http://github.mycompany.com/foo/bar/{{ .Tag }}/{{ .ArtifactName }}", 466 Persist: []string{"data.cfg", "etc"}, 467 }, 468 }, 469 }, 470 }, 471 } 472 473 for _, tt := range tests { 474 var ctx = tt.ctx 475 Pipe{}.Default(ctx) 476 out, err := buildManifest(ctx, []artifact.Artifact{ 477 {Name: "foo_1.0.1_windows_amd64.tar.gz", Goos: "windows", Goarch: "amd64", Path: file}, 478 {Name: "foo_1.0.1_windows_386.tar.gz", Goos: "windows", Goarch: "386", Path: file}, 479 }) 480 481 require.NoError(t, err) 482 483 if *update { 484 require.NoError(t, ioutil.WriteFile(tt.filename, out.Bytes(), 0655)) 485 } 486 bts, err := ioutil.ReadFile(tt.filename) 487 require.NoError(t, err) 488 require.Equal(t, string(bts), out.String()) 489 } 490 } 491 492 type DummyClient struct { 493 CreatedFile bool 494 Content string 495 } 496 497 func (client *DummyClient) CreateRelease(ctx *context.Context, body string) (releaseID int64, err error) { 498 return 499 } 500 501 func (client *DummyClient) CreateFile(ctx *context.Context, commitAuthor config.CommitAuthor, repo config.Repo, content bytes.Buffer, path, msg string) (err error) { 502 client.CreatedFile = true 503 bts, _ := ioutil.ReadAll(&content) 504 client.Content = string(bts) 505 return 506 } 507 508 func (client *DummyClient) Upload(ctx *context.Context, releaseID int64, name string, file *os.File) (err error) { 509 return 510 }