This repository has been archived by the owner on Feb 13, 2021. It is now read-only.
forked from goreleaser/goreleaser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
180 lines (159 loc) · 4.14 KB
/
main_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package main
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
"time"
"github.com/goreleaser/goreleaser/config"
"github.com/goreleaser/goreleaser/internal/testlib"
"github.com/stretchr/testify/assert"
yaml "gopkg.in/yaml.v2"
)
func init() {
_ = os.Unsetenv("GITHUB_TOKEN")
}
func TestReleaseProject(t *testing.T) {
_, back := setup(t)
defer back()
assert.NoError(t, releaseProject(testParams()))
}
func TestReleaseProjectSkipPublish(t *testing.T) {
_, back := setup(t)
defer back()
params := testParams()
params.Snapshot = true
params.SkipPublish = true
assert.NoError(t, releaseProject(params))
}
func TestConfigFileIsSetAndDontExist(t *testing.T) {
params := testParams()
params.Config = "/this/wont/exist"
assert.Error(t, releaseProject(params))
}
func TestConfigFlagNotSetButExists(t *testing.T) {
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),
)
assert.NoError(t, err)
proj, err := loadConfig("")
assert.NoError(t, err)
assert.NotEqual(t, config.Project{}, proj)
})
}
}
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)
}
func TestReleaseNotesFileDontExist(t *testing.T) {
params := testParams()
params.ReleaseNotes = "/this/also/wont/exist"
assert.Error(t, releaseProject(params))
}
func TestCustomReleaseNotesFile(t *testing.T) {
_, back := setup(t)
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))
}
func TestBrokenPipe(t *testing.T) {
_, back := setup(t)
defer back()
createFile(t, "main.go", "not a valid go file")
assert.Error(t, releaseProject(testParams()))
}
func TestInitProject(t *testing.T) {
_, back := setup(t)
defer back()
var filename = "test_goreleaser.yml"
assert.NoError(t, initProject(filename))
file, err := os.Open(filename)
assert.NoError(t, err)
out, err := ioutil.ReadAll(file)
assert.NoError(t, err)
var config = config.Project{}
assert.NoError(t, yaml.Unmarshal(out, &config))
}
func TestInitProjectFileExist(t *testing.T) {
_, back := setup(t)
defer back()
var filename = "test_goreleaser.yml"
createFile(t, filename, "")
assert.Error(t, initProject(filename))
}
func TestInitProjectDefaultPipeFails(t *testing.T) {
folder, back := setup(t)
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`)
}
func testParams() releaseOptions {
return releaseOptions{
Debug: true,
Parallelism: 4,
Snapshot: true,
Timeout: time.Minute,
}
}
func setup(t *testing.T) (current string, back func()) {
folder, err := ioutil.TempDir("", "")
assert.NoError(t, err)
previous, err := os.Getwd()
assert.NoError(t, err)
assert.NoError(t, os.Chdir(folder))
createGoreleaserYaml(t)
createMainGo(t)
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, "[email protected]:goreleaser/fake.git")
return folder, func() {
assert.NoError(t, os.Chdir(previous))
}
}
func createFile(t *testing.T, filename, contents string) {
assert.NoError(t, 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)
}