1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2025-01-06 03:13:48 +02:00
goreleaser/goreleaserlib/goreleaser_test.go

207 lines
4.5 KiB
Go
Raw Normal View History

package goreleaserlib
2017-04-21 20:25:32 +02:00
import (
"io/ioutil"
"os"
"path/filepath"
2017-07-15 21:49:52 +02:00
"strconv"
2017-04-21 20:25:32 +02:00
"testing"
2017-12-29 21:35:22 +02:00
"time"
2017-04-21 20:25:32 +02:00
"github.com/goreleaser/goreleaser/config"
2017-07-23 21:42:09 +02:00
"github.com/goreleaser/goreleaser/internal/testlib"
2017-04-21 20:25:32 +02:00
"github.com/stretchr/testify/assert"
2017-07-08 17:05:57 +02:00
yaml "gopkg.in/yaml.v2"
2017-04-21 20:25:32 +02:00
)
func init() {
_ = os.Unsetenv("GITHUB_TOKEN")
}
func TestRelease(t *testing.T) {
_, back := setup(t)
defer back()
assert.NoError(t, Release(newFlags(t, testParams())))
}
2017-05-01 15:04:41 +02:00
func TestSnapshotRelease(t *testing.T) {
_, back := setup(t)
defer back()
params := testParams()
params["snapshot"] = "true"
assert.NoError(t, Release(newFlags(t, params)))
2017-05-01 15:04:41 +02:00
}
2017-04-21 20:25:32 +02:00
func TestConfigFileIsSetAndDontExist(t *testing.T) {
params := testParams()
params["config"] = "/this/wont/exist"
assert.Error(t, Release(newFlags(t, 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()
assert.NoError(
t,
os.Rename(
filepath.Join(folder, "goreleaser.yml"),
filepath.Join(folder, name),
),
)
assert.Equal(t, name, getConfigFile(newFlags(t, testParams())))
2017-08-28 01:17:09 +02:00
})
2017-07-05 04:51:45 +02:00
}
}
2017-04-21 20:25:32 +02:00
func TestReleaseNotesFileDontExist(t *testing.T) {
params := testParams()
params["release-notes"] = "/this/also/wont/exist"
assert.Error(t, Release(newFlags(t, params)))
2017-04-21 20:25:32 +02:00
}
func TestCustomReleaseNotesFile(t *testing.T) {
folder, back := setup(t)
defer back()
var releaseNotes = filepath.Join(folder, "notes.md")
createFile(t, releaseNotes, "nothing important at all")
var params = testParams()
params["release-notes"] = releaseNotes
assert.NoError(t, Release(newFlags(t, 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, Release(newFlags(t, 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) {
_, back := setup(t)
defer back()
var filename = "test_goreleaser.yml"
assert.NoError(t, os.RemoveAll(".git"))
assert.Error(t, InitProject(filename))
2017-04-28 18:11:42 +02:00
}
2017-04-21 20:25:32 +02:00
// fakeFlags is a mock of the cli flags
type fakeFlags struct {
2017-12-29 21:35:22 +02:00
t *testing.T
2017-04-21 20:25:32 +02:00
flags map[string]string
}
func newFlags(t *testing.T, params map[string]string) Flags {
return fakeFlags{
t: t,
flags: params,
}
}
2017-04-21 20:25:32 +02:00
func (f fakeFlags) IsSet(s string) bool {
return f.flags[s] != ""
}
2017-07-15 21:49:52 +02:00
2017-04-21 20:25:32 +02:00
func (f fakeFlags) String(s string) string {
return f.flags[s]
}
2017-07-15 21:49:52 +02:00
func (f fakeFlags) Int(s string) int {
i, _ := strconv.ParseInt(f.flags[s], 10, 32)
return int(i)
}
2017-04-21 20:25:32 +02:00
func (f fakeFlags) Bool(s string) bool {
return f.flags[s] == "true"
}
2017-12-29 21:35:22 +02:00
func (f fakeFlags) Duration(s string) time.Duration {
result, err := time.ParseDuration(f.flags[s])
assert.NoError(f.t, err)
return result
}
func testParams() map[string]string {
return map[string]string{
"debug": "true",
"parallelism": "4",
"skip-publish": "true",
"skip-validate": "true",
"timeout": "1m",
}
}
2017-04-21 20:25:32 +02:00
func setup(t *testing.T) (current string, back func()) {
folder, err := ioutil.TempDir("", "goreleaser")
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)
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 createGoreleaserYaml(t *testing.T) {
var yaml = `build:
binary: fake
goos:
- linux
goarch:
- amd64
release:
github:
owner: goreleaser
name: fake
`
createFile(t, "goreleaser.yml", yaml)
}