1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-08 03:31:59 +02:00
goreleaser/main_test.go

290 lines
6.8 KiB
Go
Raw Normal View History

package main
2017-04-21 20:25:32 +02:00
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
2017-12-29 21:35:22 +02:00
"time"
2017-04-21 20:25:32 +02:00
2017-07-23 21:42:09 +02:00
"github.com/goreleaser/goreleaser/internal/testlib"
"github.com/goreleaser/goreleaser/pkg/config"
2017-04-21 20:25:32 +02:00
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v2"
2017-04-21 20:25:32 +02:00
)
func init() {
_ = os.Unsetenv("GITHUB_TOKEN")
feat: add gitlab for releases (#1038) * outlines gitlab client integration * makes client parameter more explicit * adds gitlab url to config * changes releaseID to string to adapt to gitlab * updates to latest gitlab client lib 0.18 * fixes copy paster in gitlab upload func * fixes gitlab typo in config * adds gitlab token to env and context * release now uses the client factory method * skips brew pipe if it is not a github release * add github tokentype to publish tests * skips scoop pipe if it is not a github release * corrects brew skip msg * adds gitlab token to main test * adds gitlab to release docs * validates config and errors accordingly * adapt release pipe name to include gitlab * fixes gitlab client after testing * moves not-configured brew and scoop pipe checks as first check * adds more debug to gitlab client * adapts changelog generation for gitlab markdown * adds debug log for gitlab changelog * env needs to run before changelog pipe * moves gitlab default download url to default pipe * moves multiple releases check to from config to release pipe * release differs now for github and gitlab * adds debug gitlab release update msgs * moves env pipe as second after before because it determines the token type other pipes depend on * adaptes error check on gitlab release creation * Revert "adaptes error check on gitlab release creation" This reverts commit 032024571c76140f8e2207ee01cc08088f37594b. * simplifies gitlab client logic. removes comments * skips tls verification for gitlab client if specified in config * updates the docs * adds clarification that brew and scoop are not supported if it is a gitlab release * fixes copy paster in release.md * adds missing blob pipe in defaults and publish due to missing in merge * updates comment in gitlab client
2019-06-29 16:02:40 +02:00
_ = os.Unsetenv("GITLAB_TOKEN")
2017-04-21 20:25:32 +02:00
}
func TestReleaseProject(t *testing.T) {
2017-04-21 20:25:32 +02:00
_, back := setup(t)
defer back()
assert.NoError(t, releaseProject(testParams()))
}
func TestCheckConfig(t *testing.T) {
_, back := setup(t)
defer back()
assert.NoError(t, checkConfig(testParams().Config))
}
func TestCheckConfigFails(t *testing.T) {
_, back := setup(t)
defer back()
var filename = "fail.yaml"
assert.NoError(t, ioutil.WriteFile(filename, []byte("nope: 1"), 0644))
assert.Error(t, checkConfig(filename))
}
func TestReleaseProjectSkipPublish(t *testing.T) {
_, back := setup(t)
defer back()
params := testParams()
params.Snapshot = true
params.SkipPublish = true
assert.NoError(t, releaseProject(params))
}
2017-04-21 20:25:32 +02:00
func TestConfigFileIsSetAndDontExist(t *testing.T) {
_, back := setup(t)
defer back()
params := testParams()
params.Config = "/this/wont/exist"
assert.Error(t, releaseProject(params))
2017-04-21 20:25:32 +02:00
}
2017-07-05 04:51:45 +02:00
func TestConfigFlagNotSetButExists(t *testing.T) {
2017-08-28 01:17:09 +02:00
for _, name := range []string{
".goreleaser.yml",
".goreleaser.yaml",
"goreleaser.yml",
"goreleaser.yaml",
} {
t.Run(name, func(t *testing.T) {
folder, back := setup(t)
defer back()
err := os.Rename(
filepath.Join(folder, "goreleaser.yml"),
filepath.Join(folder, name),
2017-08-28 01:17:09 +02:00
)
assert.NoError(t, err)
proj, err := loadConfig("")
assert.NoError(t, err)
assert.NotEqual(t, config.Project{}, proj)
2017-08-28 01:17:09 +02:00
})
2017-07-05 04:51:45 +02:00
}
}
func TestConfigFileDoesntExist(t *testing.T) {
folder, back := setup(t)
defer back()
err := os.Remove(filepath.Join(folder, "goreleaser.yml"))
assert.NoError(t, err)
proj, err := loadConfig("")
assert.NoError(t, err)
assert.Equal(t, config.Project{}, proj)
}
2017-04-21 20:25:32 +02:00
func TestReleaseNotesFileDontExist(t *testing.T) {
_, back := setup(t)
defer back()
params := testParams()
params.ReleaseNotes = "/this/also/wont/exist"
assert.Error(t, releaseProject(params))
2017-04-21 20:25:32 +02:00
}
func TestCustomReleaseNotesFile(t *testing.T) {
_, back := setup(t)
2017-04-21 20:25:32 +02:00
defer back()
releaseNotes, err := ioutil.TempFile("", "")
assert.NoError(t, err)
createFile(t, releaseNotes.Name(), "nothing important at all")
var params = testParams()
params.ReleaseNotes = releaseNotes.Name()
assert.NoError(t, releaseProject(params))
2017-04-21 20:25:32 +02:00
}
func TestCustomReleaseHeaderFileDontExist(t *testing.T) {
_, back := setup(t)
defer back()
params := testParams()
params.ReleaseHeader = "/header/that/dont/exist"
params.Snapshot = false
assert.Error(t, releaseProject(params))
}
func TestCustomReleaseHeaderFile(t *testing.T) {
_, back := setup(t)
defer back()
releaseHeader, err := ioutil.TempFile("", "")
assert.NoError(t, err)
createFile(t, releaseHeader.Name(), "some release header")
params := testParams()
params.ReleaseHeader = releaseHeader.Name()
params.Snapshot = false
params.SkipPublish = true
assert.NoError(t, releaseProject(params))
}
func TestCustomReleaseFooterFileDontExist(t *testing.T) {
_, back := setup(t)
defer back()
params := testParams()
params.ReleaseFooter = "/footer/that/dont/exist"
params.Snapshot = false
assert.Error(t, releaseProject(params))
}
func TestCustomReleaseFooterFile(t *testing.T) {
_, back := setup(t)
defer back()
releaseFooter, err := ioutil.TempFile("", "")
assert.NoError(t, err)
createFile(t, releaseFooter.Name(), "some release footer")
params := testParams()
params.ReleaseFooter = releaseFooter.Name()
params.Snapshot = false
params.SkipPublish = true
assert.NoError(t, releaseProject(params))
}
2017-04-21 20:25:32 +02:00
func TestBrokenPipe(t *testing.T) {
_, back := setup(t)
defer back()
createFile(t, "main.go", "not a valid go file")
assert.Error(t, releaseProject(testParams()))
2017-04-21 20:25:32 +02:00
}
2017-04-28 17:57:53 +02:00
func TestInitProject(t *testing.T) {
2017-04-28 18:07:50 +02:00
_, back := setup(t)
defer back()
2017-04-28 17:57:53 +02:00
var filename = "test_goreleaser.yml"
assert.NoError(t, initProject(filename))
2017-04-28 17:57:53 +02:00
file, err := os.Open(filename)
assert.NoError(t, err)
2017-04-28 17:57:53 +02:00
out, err := ioutil.ReadAll(file)
assert.NoError(t, err)
2017-04-28 17:57:53 +02:00
2017-04-28 18:07:50 +02:00
var config = config.Project{}
assert.NoError(t, yaml.Unmarshal(out, &config))
2017-04-28 17:57:53 +02:00
}
func TestInitProjectFileExist(t *testing.T) {
2017-04-28 18:07:50 +02:00
_, back := setup(t)
defer back()
2017-04-28 17:57:53 +02:00
var filename = "test_goreleaser.yml"
createFile(t, filename, "")
assert.Error(t, initProject(filename))
2017-04-28 17:57:53 +02:00
}
2017-04-28 18:11:42 +02:00
func TestInitProjectDefaultPipeFails(t *testing.T) {
folder, back := setup(t)
2017-04-28 18:11:42 +02:00
defer back()
var filename = "test_goreleaser.yml"
assert.NoError(t, os.Chmod(folder, 0000))
assert.EqualError(t, initProject(filename), `stat test_goreleaser.yml: permission denied`)
2017-04-28 18:11:42 +02:00
}
func testParams() releaseOptions {
return releaseOptions{
Parallelism: 4,
Snapshot: true,
Timeout: time.Minute,
}
}
2017-04-21 20:25:32 +02:00
func setup(t *testing.T) (current string, back func()) {
folder, err := ioutil.TempDir("", "")
assert.NoError(t, err)
2017-04-21 20:25:32 +02:00
previous, err := os.Getwd()
assert.NoError(t, err)
assert.NoError(t, os.Chdir(folder))
2017-04-21 20:25:32 +02:00
createGoreleaserYaml(t)
createMainGo(t)
goModInit(t)
2017-07-23 21:42:09 +02:00
testlib.GitInit(t)
testlib.GitAdd(t)
testlib.GitCommit(t, "asdf")
testlib.GitTag(t, "v0.0.1")
testlib.GitCommit(t, "asas89d")
testlib.GitCommit(t, "assssf")
testlib.GitCommit(t, "assd")
testlib.GitTag(t, "v0.0.2")
testlib.GitRemoteAdd(t, "git@github.com:goreleaser/fake.git")
2017-04-21 20:25:32 +02:00
return folder, func() {
assert.NoError(t, os.Chdir(previous))
2017-04-21 20:25:32 +02:00
}
}
func createFile(t *testing.T, filename, contents string) {
assert.NoError(t, ioutil.WriteFile(filename, []byte(contents), 0644))
2017-04-21 20:25:32 +02:00
}
func createMainGo(t *testing.T) {
createFile(t, "main.go", "package main\nfunc main() {println(0)}")
}
func goModInit(t *testing.T) {
createFile(t, "go.mod", `module foo
go 1.14
`)
}
2017-04-21 20:25:32 +02:00
func createGoreleaserYaml(t *testing.T) {
var yaml = `build:
binary: fake
goos:
- linux
goarch:
- amd64
release:
github:
owner: goreleaser
name: fake
`
createFile(t, "goreleaser.yml", yaml)
}
func TestVersion(t *testing.T) {
for name, tt := range map[string]struct {
version, commit, date, builtBy string
out string
}{
"all empty": {
out: "version: ",
},
"complete": {
version: "1.2.3",
date: "12/12/12",
commit: "aaaa",
builtBy: "me",
out: "version: 1.2.3\ncommit: aaaa\nbuilt at: 12/12/12\nbuilt by: me",
},
"only version": {
version: "1.2.3",
out: "version: 1.2.3",
},
"version and date": {
version: "1.2.3",
date: "12/12/12",
out: "version: 1.2.3\nbuilt at: 12/12/12",
},
"version, date, built by": {
version: "1.2.3",
date: "12/12/12",
builtBy: "me",
out: "version: 1.2.3\nbuilt at: 12/12/12\nbuilt by: me",
},
} {
tt := tt
t.Run(name, func(t *testing.T) {
require.Equal(t, tt.out, buildVersion(tt.version, tt.commit, tt.date, tt.builtBy))
})
}
}