github.com/jasei/goreleaser@v0.62.4-0.20180312171904-62cb6a8963a6/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 Name: "run-pipe", 118 GitHub: config.Repo{ 119 Owner: "test", 120 Name: "test", 121 }, 122 Description: "A run pipe test formula", 123 Homepage: "https://github.com/goreleaser", 124 Caveats: "don't do this", 125 Test: "system \"true\"\nsystem \"#{bin}/foo -h\"", 126 Plist: `<xml>whatever</xml>`, 127 Dependencies: []string{"zsh", "bash"}, 128 Conflicts: []string{"gtk+", "qt"}, 129 Install: `bin.install "foo"`, 130 }, 131 }, 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 ctx.Config.GitHubURLs.Download = "https://github.com" 152 assert.NoError(tt, doRun(ctx, client)) 153 assert.True(tt, client.CreatedFile) 154 var golden = "testdata/run_pipe.rb.golden" 155 if *update { 156 ioutil.WriteFile(golden, []byte(client.Content), 0655) 157 } 158 bts, err := ioutil.ReadFile(golden) 159 assert.NoError(tt, err) 160 assert.Equal(tt, string(bts), client.Content) 161 162 distBts, err := ioutil.ReadFile(distFile) 163 assert.NoError(tt, err) 164 assert.Equal(tt, string(bts), string(distBts)) 165 }) 166 167 t.Run("github enterprise url", func(tt *testing.T) { 168 ctx.Config.GitHubURLs.Download = "http://github.example.org" 169 assert.NoError(tt, doRun(ctx, client)) 170 assert.True(tt, client.CreatedFile) 171 var golden = "testdata/run_pipe_enterprise.rb.golden" 172 if *update { 173 ioutil.WriteFile(golden, []byte(client.Content), 0644) 174 } 175 bts, err := ioutil.ReadFile(golden) 176 assert.NoError(tt, err) 177 assert.Equal(tt, string(bts), client.Content) 178 179 distBts, err := ioutil.ReadFile(distFile) 180 assert.NoError(tt, err) 181 assert.Equal(tt, string(bts), string(distBts)) 182 }) 183 184 t.Run("custom download strategy", func(tt *testing.T) { 185 ctx.Config.Brew.DownloadStrategy = "GitHubPrivateRepositoryReleaseDownloadStrategy" 186 assert.NoError(tt, doRun(ctx, client)) 187 assert.True(tt, client.CreatedFile) 188 var golden = "testdata/run_pipe_download_strategy.rb.golden" 189 if *update { 190 ioutil.WriteFile(golden, []byte(client.Content), 0655) 191 } 192 bts, err := ioutil.ReadFile(golden) 193 assert.NoError(tt, err) 194 assert.Equal(tt, string(bts), client.Content) 195 196 distBts, err := ioutil.ReadFile(distFile) 197 assert.NoError(tt, err) 198 assert.Equal(tt, string(bts), string(distBts)) 199 }) 200 201 t.Run("custom name", func(tt *testing.T) { 202 ctx.Config.Brew.Name = "custom-brew-name" 203 assert.NoError(tt, doRun(ctx, client)) 204 assert.True(tt, client.CreatedFile) 205 206 _, err := os.Stat(filepath.Join(folder, "custom-brew-name.rb")) 207 assert.NoError(t, err) 208 }) 209 } 210 211 func TestRunPipeNoDarwin64Build(t *testing.T) { 212 var ctx = &context.Context{ 213 Config: config.Project{ 214 Archive: config.Archive{ 215 Format: "tar.gz", 216 }, 217 Brew: config.Homebrew{ 218 GitHub: config.Repo{ 219 Owner: "test", 220 Name: "test", 221 }, 222 }, 223 }, 224 } 225 client := &DummyClient{} 226 assert.Equal(t, ErrNoDarwin64Build, doRun(ctx, client)) 227 assert.False(t, client.CreatedFile) 228 } 229 230 func TestRunPipeMultipleDarwin64Build(t *testing.T) { 231 var ctx = context.New( 232 config.Project{ 233 Archive: config.Archive{ 234 Format: "tar.gz", 235 }, 236 Brew: config.Homebrew{ 237 GitHub: config.Repo{ 238 Owner: "test", 239 Name: "test", 240 }, 241 }, 242 }, 243 ) 244 ctx.Artifacts.Add(artifact.Artifact{ 245 Name: "bin1", 246 Path: "doesnt mather", 247 Goos: "darwin", 248 Goarch: "amd64", 249 Type: artifact.UploadableArchive, 250 }) 251 ctx.Artifacts.Add(artifact.Artifact{ 252 Name: "bin2", 253 Path: "doesnt mather", 254 Goos: "darwin", 255 Goarch: "amd64", 256 Type: artifact.UploadableArchive, 257 }) 258 client := &DummyClient{} 259 assert.Equal(t, ErrTooManyDarwin64Builds, doRun(ctx, client)) 260 assert.False(t, client.CreatedFile) 261 } 262 263 func TestRunPipeBrewNotSetup(t *testing.T) { 264 var ctx = &context.Context{ 265 Config: config.Project{}, 266 } 267 client := &DummyClient{} 268 testlib.AssertSkipped(t, doRun(ctx, client)) 269 assert.False(t, client.CreatedFile) 270 } 271 272 func TestRunPipeBinaryRelease(t *testing.T) { 273 var ctx = context.New( 274 config.Project{ 275 Archive: config.Archive{ 276 Format: "binary", 277 }, 278 Brew: config.Homebrew{ 279 GitHub: config.Repo{ 280 Owner: "test", 281 Name: "test", 282 }, 283 }, 284 }, 285 ) 286 ctx.Artifacts.Add(artifact.Artifact{ 287 Name: "bin", 288 Path: "doesnt mather", 289 Goos: "darwin", 290 Goarch: "amd64", 291 Type: artifact.Binary, 292 }) 293 client := &DummyClient{} 294 testlib.AssertSkipped(t, doRun(ctx, client)) 295 assert.False(t, client.CreatedFile) 296 } 297 298 func TestRunPipeNoUpload(t *testing.T) { 299 folder, err := ioutil.TempDir("", "goreleasertest") 300 assert.NoError(t, err) 301 var ctx = context.New(config.Project{ 302 Dist: folder, 303 ProjectName: "foo", 304 Release: config.Release{}, 305 Brew: config.Homebrew{ 306 GitHub: config.Repo{ 307 Owner: "test", 308 Name: "test", 309 }, 310 }, 311 }) 312 var path = filepath.Join(folder, "whatever.tar.gz") 313 _, err = os.Create(path) 314 assert.NoError(t, err) 315 ctx.Artifacts.Add(artifact.Artifact{ 316 Name: "bin", 317 Path: path, 318 Goos: "darwin", 319 Goarch: "amd64", 320 Type: artifact.UploadableArchive, 321 }) 322 client := &DummyClient{} 323 324 var assertNoPublish = func(t *testing.T) { 325 testlib.AssertSkipped(t, doRun(ctx, client)) 326 assert.False(t, client.CreatedFile) 327 } 328 t.Run("skip upload", func(tt *testing.T) { 329 ctx.Config.Release.Draft = false 330 ctx.Config.Brew.SkipUpload = true 331 ctx.SkipPublish = false 332 assertNoPublish(tt) 333 }) 334 t.Run("skip publish", func(tt *testing.T) { 335 ctx.Config.Release.Draft = false 336 ctx.Config.Brew.SkipUpload = false 337 ctx.SkipPublish = true 338 assertNoPublish(tt) 339 }) 340 t.Run("draft release", func(tt *testing.T) { 341 ctx.Config.Release.Draft = true 342 ctx.Config.Brew.SkipUpload = false 343 ctx.SkipPublish = false 344 assertNoPublish(tt) 345 }) 346 } 347 348 func TestRunPipeFormatBinary(t *testing.T) { 349 var ctx = &context.Context{ 350 Config: config.Project{ 351 Archive: config.Archive{ 352 Format: "binary", 353 }, 354 }, 355 } 356 client := &DummyClient{} 357 testlib.AssertSkipped(t, doRun(ctx, client)) 358 assert.False(t, client.CreatedFile) 359 } 360 361 func TestDefault(t *testing.T) { 362 _, back := testlib.Mktmp(t) 363 defer back() 364 365 var ctx = &context.Context{ 366 Config: config.Project{ 367 ProjectName: "myproject", 368 Builds: []config.Build{ 369 { 370 Binary: "foo", 371 Goos: []string{"linux", "darwin"}, 372 Goarch: []string{"386", "amd64"}, 373 }, 374 { 375 Binary: "bar", 376 Goos: []string{"linux", "darwin"}, 377 Goarch: []string{"386", "amd64"}, 378 Ignore: []config.IgnoredBuild{ 379 {Goos: "darwin", Goarch: "amd64"}, 380 }, 381 }, 382 { 383 Binary: "foobar", 384 Goos: []string{"linux"}, 385 Goarch: []string{"amd64"}, 386 }, 387 }, 388 }, 389 } 390 assert.NoError(t, Pipe{}.Default(ctx)) 391 assert.Equal(t, ctx.Config.ProjectName, ctx.Config.Brew.Name) 392 assert.NotEmpty(t, ctx.Config.Brew.CommitAuthor.Name) 393 assert.NotEmpty(t, ctx.Config.Brew.CommitAuthor.Email) 394 assert.Equal(t, `bin.install "foo"`, ctx.Config.Brew.Install) 395 } 396 397 type DummyClient struct { 398 CreatedFile bool 399 Content string 400 } 401 402 func (client *DummyClient) CreateRelease(ctx *context.Context, body string) (releaseID int64, err error) { 403 return 404 } 405 406 func (client *DummyClient) CreateFile(ctx *context.Context, commitAuthor config.CommitAuthor, repo config.Repo, content bytes.Buffer, path, msg string) (err error) { 407 client.CreatedFile = true 408 bts, _ := ioutil.ReadAll(&content) 409 client.Content = string(bts) 410 return 411 } 412 413 func (client *DummyClient) Upload(ctx *context.Context, releaseID int64, name string, file *os.File) (err error) { 414 return 415 }