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

216 lines
4.6 KiB
Go
Raw Normal View History

package goreleaserlib
2017-04-21 20:25:32 +02:00
import (
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"testing"
"github.com/goreleaser/goreleaser/config"
2017-04-21 20:25:32 +02:00
"github.com/stretchr/testify/assert"
2017-06-22 05:09:14 +02:00
yaml "gopkg.in/yaml.v1"
2017-04-21 20:25:32 +02:00
)
func init() {
_ = os.Unsetenv("GITHUB_TOKEN")
}
func TestRelease(t *testing.T) {
var assert = assert.New(t)
_, back := setup(t)
defer back()
var flags = fakeFlags{
flags: map[string]string{
"skip-publish": "true",
"skip-validate": "true",
2017-06-22 05:52:45 +02:00
"debug": "true",
2017-04-21 20:25:32 +02:00
},
}
assert.NoError(Release(flags))
}
2017-05-01 15:04:41 +02:00
func TestSnapshotRelease(t *testing.T) {
var assert = assert.New(t)
_, back := setup(t)
defer back()
var flags = fakeFlags{
flags: map[string]string{
"snapshot": "true",
},
}
assert.NoError(Release(flags))
}
2017-04-21 20:25:32 +02:00
func TestConfigFileIsSetAndDontExist(t *testing.T) {
var assert = assert.New(t)
var flags = fakeFlags{
flags: map[string]string{
"config": "/this/wont/exist",
},
}
assert.Error(Release(flags))
}
2017-07-05 04:51:45 +02:00
func TestConfigFlagNotSetButExists(t *testing.T) {
var assert = assert.New(t)
folder, back := setup(t)
defer back()
os.Rename(
filepath.Join(folder, "goreleaser.yml"),
filepath.Join(folder, ".goreleaser.yml"),
)
var flags = fakeFlags{
flags: map[string]string{},
}
2017-07-05 04:58:10 +02:00
assert.Equal(".goreleaser.yml", getConfigFile(flags))
2017-07-05 04:51:45 +02:00
}
2017-04-21 20:25:32 +02:00
func TestReleaseNotesFileDontExist(t *testing.T) {
var assert = assert.New(t)
var flags = fakeFlags{
flags: map[string]string{
"release-notes": "/this/also/wont/exist",
},
}
assert.Error(Release(flags))
}
func TestCustomReleaseNotesFile(t *testing.T) {
var assert = assert.New(t)
folder, back := setup(t)
defer back()
var releaseNotes = filepath.Join(folder, "notes.md")
createFile(t, releaseNotes, "nothing important at all")
var flags = fakeFlags{
flags: map[string]string{
"release-notes": releaseNotes,
"skip-publish": "true",
"skip-validate": "true",
},
}
assert.NoError(Release(flags))
}
func TestBrokenPipe(t *testing.T) {
var assert = assert.New(t)
_, back := setup(t)
defer back()
createFile(t, "main.go", "not a valid go file")
var flags = fakeFlags{
flags: map[string]string{
"skip-publish": "true",
"skip-validate": "true",
},
}
assert.Error(Release(flags))
}
2017-04-28 17:57:53 +02:00
func TestInitProject(t *testing.T) {
2017-04-28 18:07:50 +02:00
var assert = assert.New(t)
_, back := setup(t)
defer back()
2017-04-28 17:57:53 +02:00
var filename = "test_goreleaser.yml"
2017-04-28 18:07:50 +02:00
assert.NoError(InitProject(filename))
2017-04-28 17:57:53 +02:00
file, err := os.Open(filename)
2017-04-28 18:07:50 +02:00
assert.NoError(err)
2017-04-28 17:57:53 +02:00
out, err := ioutil.ReadAll(file)
2017-04-28 18:07:50 +02:00
assert.NoError(err)
2017-04-28 17:57:53 +02:00
2017-04-28 18:07:50 +02:00
var config = config.Project{}
assert.NoError(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
var assert = assert.New(t)
_, back := setup(t)
defer back()
2017-04-28 17:57:53 +02:00
var filename = "test_goreleaser.yml"
createFile(t, filename, "")
2017-04-28 18:07:50 +02:00
assert.Error(InitProject(filename))
2017-04-28 17:57:53 +02:00
}
2017-04-28 18:11:42 +02:00
func TestInitProjectDefaultPipeFails(t *testing.T) {
var assert = assert.New(t)
_, back := setup(t)
defer back()
var filename = "test_goreleaser.yml"
assert.NoError(os.RemoveAll(".git"))
assert.Error(InitProject(filename))
}
2017-04-21 20:25:32 +02:00
// fakeFlags is a mock of the cli flags
type fakeFlags struct {
flags map[string]string
}
func (f fakeFlags) IsSet(s string) bool {
return f.flags[s] != ""
}
func (f fakeFlags) String(s string) string {
return f.flags[s]
}
func (f fakeFlags) Bool(s string) bool {
return f.flags[s] == "true"
}
func setup(t *testing.T) (current string, back func()) {
var assert = assert.New(t)
folder, err := ioutil.TempDir("", "goreleaser")
assert.NoError(err)
previous, err := os.Getwd()
assert.NoError(err)
assert.NoError(os.Chdir(folder))
var gitCmds = [][]string{
{"init"},
{"add", "-A"},
{"commit", "--allow-empty", "-m", "asdf"},
{"tag", "v0.0.1"},
{"commit", "--allow-empty", "-m", "asas89d"},
{"commit", "--allow-empty", "-m", "assssf"},
{"commit", "--allow-empty", "-m", "assd"},
{"tag", "v0.0.2"},
{"remote", "add", "origin", "git@github.com:goreleaser/fake.git"},
}
createGoreleaserYaml(t)
createMainGo(t)
for _, cmd := range gitCmds {
2017-04-21 20:29:45 +02:00
var args = []string{
"-c",
"user.name='GoReleaser'",
"-c",
"user.email='test@goreleaser.github.com'",
}
args = append(args, cmd...)
assert.NoError(exec.Command("git", args...).Run())
2017-04-21 20:25:32 +02:00
}
return folder, func() {
assert.NoError(os.Chdir(previous))
}
}
func createFile(t *testing.T, filename, contents string) {
var assert = assert.New(t)
assert.NoError(ioutil.WriteFile(filename, []byte(contents), 0644))
}
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)
}