gitee.com/mirrors_opencollective/goreleaser@v0.45.0/pipeline/brew/brew_test.go (about) 1 package brew 2 3 import ( 4 "bytes" 5 "flag" 6 "io/ioutil" 7 "os" 8 "path/filepath" 9 "testing" 10 11 "github.com/goreleaser/goreleaser/config" 12 "github.com/goreleaser/goreleaser/context" 13 "github.com/goreleaser/goreleaser/internal/artifact" 14 "github.com/goreleaser/goreleaser/internal/testlib" 15 "github.com/stretchr/testify/assert" 16 ) 17 18 var update = flag.Bool("update", false, "update .golden files") 19 20 func TestDescription(t *testing.T) { 21 assert.NotEmpty(t, Pipe{}.String()) 22 } 23 24 func TestNameWithDash(t *testing.T) { 25 assert.Equal(t, formulaNameFor("some-binary"), "SomeBinary") 26 } 27 28 func TestNameWithUnderline(t *testing.T) { 29 assert.Equal(t, formulaNameFor("some_binary"), "SomeBinary") 30 } 31 32 func TestSimpleName(t *testing.T) { 33 assert.Equal(t, formulaNameFor("binary"), "Binary") 34 } 35 36 var defaultTemplateData = templateData{ 37 Desc: "Some desc", 38 Homepage: "https://google.com", 39 DownloadURL: "https://github.com", 40 Name: "Test", 41 Repo: config.Repo{ 42 Owner: "caarlos0", 43 Name: "test", 44 }, 45 Tag: "v0.1.3", 46 Version: "0.1.3", 47 File: "test_Darwin_x86_64.tar.gz", 48 SHA256: "1633f61598ab0791e213135923624eb342196b3494909c91899bcd0560f84c68", 49 } 50 51 func assertDefaultTemplateData(t *testing.T, formulae string) { 52 assert.Contains(t, formulae, "class Test < Formula") 53 assert.Contains(t, formulae, `homepage "https://google.com"`) 54 assert.Contains(t, formulae, `url "https://github.com/caarlos0/test/releases/download/v0.1.3/test_Darwin_x86_64.tar.gz"`) 55 assert.Contains(t, formulae, `sha256 "1633f61598ab0791e213135923624eb342196b3494909c91899bcd0560f84c68"`) 56 assert.Contains(t, formulae, `version "0.1.3"`) 57 } 58 59 func TestFullFormulae(t *testing.T) { 60 data := defaultTemplateData 61 data.Caveats = "Here are some caveats" 62 data.Dependencies = []string{"gtk+"} 63 data.Conflicts = []string{"svn"} 64 data.Plist = "it works" 65 data.Install = []string{"custom install script", "another install script"} 66 data.Tests = []string{`system "#{bin}/foo -version"`} 67 out, err := doBuildFormula(data) 68 assert.NoError(t, err) 69 formulae := out.String() 70 71 var golden = "testdata/test.rb.golden" 72 if *update { 73 ioutil.WriteFile(golden, []byte(formulae), 0655) 74 } 75 bts, err := ioutil.ReadFile(golden) 76 assert.NoError(t, err) 77 assert.Equal(t, string(bts), formulae) 78 } 79 80 func TestFormulaeSimple(t *testing.T) { 81 out, err := doBuildFormula(defaultTemplateData) 82 assert.NoError(t, err) 83 formulae := out.String() 84 assertDefaultTemplateData(t, formulae) 85 assert.NotContains(t, formulae, "def caveats") 86 assert.NotContains(t, formulae, "depends_on") 87 assert.NotContains(t, formulae, "def plist;") 88 } 89 90 func TestSplit(t *testing.T) { 91 var parts = split("system \"true\"\nsystem \"#{bin}/foo -h\"") 92 assert.Equal(t, []string{"system \"true\"", "system \"#{bin}/foo -h\""}, parts) 93 } 94 95 func TestRunPipe(t *testing.T) { 96 folder, err := ioutil.TempDir("", "goreleasertest") 97 assert.NoError(t, err) 98 var ctx = &context.Context{ 99 Git: context.GitInfo{ 100 CurrentTag: "v1.0.1", 101 }, 102 Version: "1.0.1", 103 Artifacts: artifact.New(), 104 Config: config.Project{ 105 Dist: folder, 106 ProjectName: "run-pipe", 107 Archive: config.Archive{ 108 Format: "tar.gz", 109 }, 110 Release: config.Release{ 111 GitHub: config.Repo{ 112 Owner: "test", 113 Name: "test", 114 }, 115 }, 116 Brew: config.Homebrew{ 117 GitHub: config.Repo{ 118 Owner: "test", 119 Name: "test", 120 }, 121 Description: "A run pipe test formula", 122 Homepage: "https://github.com/goreleaser", 123 Caveats: "don't do this", 124 Test: "system \"true\"\nsystem \"#{bin}/foo -h\"", 125 Plist: `<xml>whatever</xml>`, 126 Dependencies: []string{"zsh", "bash"}, 127 Conflicts: []string{"gtk+", "qt"}, 128 Install: `bin.install "foo"`, 129 }, 130 }, 131 Publish: true, 132 } 133 var path = filepath.Join(folder, "bin.tar.gz") 134 ctx.Artifacts.Add(artifact.Artifact{ 135 Name: "bin.tar.gz", 136 Path: path, 137 Goos: "darwin", 138 Goarch: "amd64", 139 Type: artifact.UploadableArchive, 140 }) 141 client := &DummyClient{} 142 assert.Error(t, doRun(ctx, client)) 143 assert.False(t, client.CreatedFile) 144 145 _, err = os.Create(path) 146 assert.NoError(t, err) 147 148 var distFile = filepath.Join(folder, "run-pipe.rb") 149 150 t.Run("default git url", func(tt *testing.T) { 151 assert.NoError(tt, doRun(ctx, client)) 152 assert.True(tt, client.CreatedFile) 153 var golden = "testdata/run_pipe.rb.golden" 154 if *update { 155 ioutil.WriteFile(golden, []byte(client.Content), 0655) 156 } 157 bts, err := ioutil.ReadFile(golden) 158 assert.NoError(tt, err) 159 assert.Equal(tt, string(bts), client.Content) 160 161 distBts, err := ioutil.ReadFile(distFile) 162 assert.NoError(tt, err) 163 assert.Equal(tt, string(bts), string(distBts)) 164 }) 165 166 t.Run("github enterprise url", func(tt *testing.T) { 167 ctx.Config.GitHubURLs.Download = "http://github.example.org" 168 assert.NoError(tt, doRun(ctx, client)) 169 assert.True(tt, client.CreatedFile) 170 var golden = "testdata/run_pipe_enterprise.rb.golden" 171 if *update { 172 ioutil.WriteFile(golden, []byte(client.Content), 0644) 173 } 174 bts, err := ioutil.ReadFile(golden) 175 assert.NoError(tt, err) 176 assert.Equal(tt, string(bts), client.Content) 177 178 distBts, err := ioutil.ReadFile(distFile) 179 assert.NoError(tt, err) 180 assert.Equal(tt, string(bts), string(distBts)) 181 }) 182 183 t.Run("custom download strategy", func(tt *testing.T) { 184 ctx.Config.Brew.DownloadStrategy = "GitHubPrivateRepositoryReleaseDownloadStrategy" 185 assert.NoError(tt, doRun(ctx, client)) 186 assert.True(tt, client.CreatedFile) 187 var golden = "testdata/run_pipe_download_strategy.rb.golden" 188 if *update { 189 ioutil.WriteFile(golden, []byte(client.Content), 0655) 190 } 191 bts, err := ioutil.ReadFile(golden) 192 assert.NoError(tt, err) 193 assert.Equal(tt, string(bts), client.Content) 194 195 distBts, err := ioutil.ReadFile(distFile) 196 assert.NoError(tt, err) 197 assert.Equal(tt, string(bts), string(distBts)) 198 }) 199 } 200 201 func TestRunPipeNoDarwin64Build(t *testing.T) { 202 var ctx = &context.Context{ 203 Config: config.Project{ 204 Archive: config.Archive{ 205 Format: "tar.gz", 206 }, 207 Brew: config.Homebrew{ 208 GitHub: config.Repo{ 209 Owner: "test", 210 Name: "test", 211 }, 212 }, 213 }, 214 Publish: true, 215 } 216 client := &DummyClient{} 217 assert.Equal(t, ErrNoDarwin64Build, doRun(ctx, client)) 218 assert.False(t, client.CreatedFile) 219 } 220 221 func TestRunPipeMultipleDarwin64Build(t *testing.T) { 222 var ctx = context.New( 223 config.Project{ 224 Archive: config.Archive{ 225 Format: "tar.gz", 226 }, 227 Brew: config.Homebrew{ 228 GitHub: config.Repo{ 229 Owner: "test", 230 Name: "test", 231 }, 232 }, 233 }, 234 ) 235 ctx.Publish = true 236 ctx.Artifacts.Add(artifact.Artifact{ 237 Name: "bin1", 238 Path: "doesnt mather", 239 Goos: "darwin", 240 Goarch: "amd64", 241 Type: artifact.UploadableArchive, 242 }) 243 ctx.Artifacts.Add(artifact.Artifact{ 244 Name: "bin2", 245 Path: "doesnt mather", 246 Goos: "darwin", 247 Goarch: "amd64", 248 Type: artifact.UploadableArchive, 249 }) 250 client := &DummyClient{} 251 assert.Equal(t, ErrTooManyDarwin64Builds, doRun(ctx, client)) 252 assert.False(t, client.CreatedFile) 253 } 254 255 func TestRunPipeBrewNotSetup(t *testing.T) { 256 var ctx = &context.Context{ 257 Config: config.Project{}, 258 Publish: true, 259 } 260 client := &DummyClient{} 261 testlib.AssertSkipped(t, doRun(ctx, client)) 262 assert.False(t, client.CreatedFile) 263 } 264 265 func TestRunPipeBinaryRelease(t *testing.T) { 266 var ctx = context.New( 267 config.Project{ 268 Archive: config.Archive{ 269 Format: "binary", 270 }, 271 Brew: config.Homebrew{ 272 GitHub: config.Repo{ 273 Owner: "test", 274 Name: "test", 275 }, 276 }, 277 }, 278 ) 279 ctx.Publish = true 280 ctx.Artifacts.Add(artifact.Artifact{ 281 Name: "bin", 282 Path: "doesnt mather", 283 Goos: "darwin", 284 Goarch: "amd64", 285 Type: artifact.Binary, 286 }) 287 client := &DummyClient{} 288 testlib.AssertSkipped(t, doRun(ctx, client)) 289 assert.False(t, client.CreatedFile) 290 } 291 292 func TestRunPipeNoUpload(t *testing.T) { 293 folder, err := ioutil.TempDir("", "goreleasertest") 294 assert.NoError(t, err) 295 var ctx = context.New(config.Project{ 296 Dist: folder, 297 ProjectName: "foo", 298 Release: config.Release{}, 299 Brew: config.Homebrew{ 300 GitHub: config.Repo{ 301 Owner: "test", 302 Name: "test", 303 }, 304 }, 305 }) 306 var path = filepath.Join(folder, "whatever.tar.gz") 307 _, err = os.Create(path) 308 assert.NoError(t, err) 309 ctx.Artifacts.Add(artifact.Artifact{ 310 Name: "bin", 311 Path: path, 312 Goos: "darwin", 313 Goarch: "amd64", 314 Type: artifact.UploadableArchive, 315 }) 316 client := &DummyClient{} 317 318 var assertNoPublish = func(t *testing.T) { 319 testlib.AssertSkipped(t, doRun(ctx, client)) 320 assert.False(t, client.CreatedFile) 321 } 322 t.Run("skip upload", func(tt *testing.T) { 323 ctx.Publish = true 324 ctx.Config.Brew.SkipUpload = true 325 assertNoPublish(tt) 326 }) 327 t.Run("skip publish", func(tt *testing.T) { 328 ctx.Publish = false 329 assertNoPublish(tt) 330 }) 331 t.Run("draft release", func(tt *testing.T) { 332 ctx.Publish = true 333 ctx.Config.Release.Draft = true 334 assertNoPublish(tt) 335 }) 336 } 337 338 func TestRunPipeFormatBinary(t *testing.T) { 339 var ctx = &context.Context{ 340 Config: config.Project{ 341 Archive: config.Archive{ 342 Format: "binary", 343 }, 344 }, 345 } 346 client := &DummyClient{} 347 testlib.AssertSkipped(t, doRun(ctx, client)) 348 assert.False(t, client.CreatedFile) 349 } 350 351 func TestDefault(t *testing.T) { 352 _, back := testlib.Mktmp(t) 353 defer back() 354 355 var ctx = &context.Context{ 356 Config: config.Project{ 357 Builds: []config.Build{ 358 { 359 Binary: "foo", 360 Goos: []string{"linux", "darwin"}, 361 Goarch: []string{"386", "amd64"}, 362 }, 363 { 364 Binary: "bar", 365 Goos: []string{"linux", "darwin"}, 366 Goarch: []string{"386", "amd64"}, 367 Ignore: []config.IgnoredBuild{ 368 {Goos: "darwin", Goarch: "amd64"}, 369 }, 370 }, 371 { 372 Binary: "foobar", 373 Goos: []string{"linux"}, 374 Goarch: []string{"amd64"}, 375 }, 376 }, 377 }, 378 } 379 assert.NoError(t, Pipe{}.Default(ctx)) 380 assert.NotEmpty(t, ctx.Config.Brew.CommitAuthor.Name) 381 assert.NotEmpty(t, ctx.Config.Brew.CommitAuthor.Email) 382 assert.Equal(t, `bin.install "foo"`, ctx.Config.Brew.Install) 383 } 384 385 type DummyClient struct { 386 CreatedFile bool 387 Content string 388 } 389 390 func (client *DummyClient) CreateRelease(ctx *context.Context, body string) (releaseID int, err error) { 391 return 392 } 393 394 func (client *DummyClient) CreateFile(ctx *context.Context, content bytes.Buffer, path string) (err error) { 395 client.CreatedFile = true 396 bts, _ := ioutil.ReadAll(&content) 397 client.Content = string(bts) 398 return 399 } 400 401 func (client *DummyClient) Upload(ctx *context.Context, releaseID int, name string, file *os.File) (err error) { 402 return 403 }