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