forked from goreleaser/nfpm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnfpm_test.go
222 lines (200 loc) · 5.37 KB
/
nfpm_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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package nfpm
import (
"fmt"
"io"
"os"
"reflect"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestRegister(t *testing.T) {
format := "TestRegister"
pkgr := &fakePackager{}
Register(format, pkgr)
got, err := Get(format)
assert.NoError(t, err)
assert.Equal(t, pkgr, got)
}
func TestGet(t *testing.T) {
format := "TestGet"
got, err := Get(format)
assert.Error(t, err)
assert.EqualError(t, err, "no packager registered for the format "+format)
assert.Nil(t, got)
pkgr := &fakePackager{}
Register(format, pkgr)
got, err = Get(format)
assert.NoError(t, err)
assert.Equal(t, pkgr, got)
}
func TestDefaultsVersion(t *testing.T) {
info := &Info{
Version: "v1.0.0",
}
info = WithDefaults(info)
assert.NotEmpty(t, info.Platform)
assert.Equal(t, "1.0.0", info.Version)
assert.Equal(t, "", info.Release)
assert.Equal(t, "", info.Prerelease)
info = &Info{
Version: "v1.0.0-rc1",
}
info = WithDefaults(info)
assert.Equal(t, "1.0.0", info.Version)
assert.Equal(t, "", info.Release)
assert.Equal(t, "rc1", info.Prerelease)
info = &Info{
Version: "v1.0.0-1",
}
info = WithDefaults(info)
assert.Equal(t, "1.0.0", info.Version)
assert.Equal(t, "1", info.Release)
assert.Equal(t, "", info.Prerelease)
info = &Info{
Version: "v1.0.0-1",
Release: "2",
Prerelease: "beta1",
}
info = WithDefaults(info)
assert.Equal(t, "1.0.0", info.Version)
assert.Equal(t, "2", info.Release)
assert.Equal(t, "beta1", info.Prerelease)
info = &Info{
Version: "v1.0.0-1+xdg2",
Release: "2",
Prerelease: "beta1",
}
info = WithDefaults(info)
assert.Equal(t, "1.0.0", info.Version)
assert.Equal(t, "2", info.Release)
assert.Equal(t, "beta1", info.Prerelease)
assert.Equal(t, "xdg2", info.Deb.VersionMetadata)
}
func TestDefaults(t *testing.T) {
info := &Info{
Platform: "darwin",
Version: "2.4.1",
Description: "no description given",
Owner: "root",
Group: "root",
}
got := WithDefaults(info)
assert.Equal(t, info, got)
}
func TestValidate(t *testing.T) {
require.NoError(t, Validate(&Info{
Name: "as",
Arch: "asd",
Version: "1.2.3",
Overridables: Overridables{
Files: map[string]string{
"asa": "asd",
},
},
}))
require.NoError(t, Validate(&Info{
Name: "as",
Arch: "asd",
Version: "1.2.3",
Overridables: Overridables{
ConfigFiles: map[string]string{
"asa": "asd",
},
},
}))
}
func TestValidateError(t *testing.T) {
for err, info := range map[string]Info{
"package name must be provided": {},
"package arch must be provided": {
Name: "fo",
},
"package version must be provided": {
Name: "as",
Arch: "asd",
},
"package files must be provided": {
Name: "as",
Arch: "asd",
Version: "1.2.3",
},
} {
err := err
info := info
t.Run(err, func(t *testing.T) {
require.EqualError(t, Validate(&info), err)
})
}
}
func TestParseFile(t *testing.T) {
packagers = map[string]Packager{}
_, err := ParseFile("./testdata/overrides.yaml")
assert.Error(t, err)
Register("deb", &fakePackager{})
Register("rpm", &fakePackager{})
overrideConfig, err := ParseFile("./testdata/overrides.yaml")
assert.NoError(t, err)
assert.Equal(t, overrideConfig.Owner, "user1")
assert.Equal(t, overrideConfig.Group, "group1")
_, err = ParseFile("./testdata/doesnotexist.yaml")
assert.Error(t, err)
config, err := ParseFile("./testdata/versionenv.yaml")
assert.NoError(t, err)
assert.Equal(t, fmt.Sprintf("v%s", os.Getenv("GOROOT")), config.Version)
}
func TestOverrides(t *testing.T) {
file := "./testdata/overrides.yaml"
config, err := ParseFile(file)
assert.NoError(t, err)
assert.Equal(t, "foo", config.Name)
assert.Equal(t, "amd64", config.Arch)
// deb overrides
deb, err := config.Get("deb")
assert.NoError(t, err)
assert.Contains(t, deb.Depends, "deb_depend")
assert.NotContains(t, deb.Depends, "rpm_depend")
assert.Contains(t, deb.ConfigFiles, "deb.conf")
assert.NotContains(t, deb.ConfigFiles, "rpm.conf")
assert.Contains(t, deb.ConfigFiles, "whatever.conf")
assert.Equal(t, "amd64", deb.Arch)
// rpm overrides
rpm, err := config.Get("rpm")
assert.NoError(t, err)
assert.Contains(t, rpm.Depends, "rpm_depend")
assert.NotContains(t, rpm.Depends, "deb_depend")
assert.Contains(t, rpm.ConfigFiles, "rpm.conf")
assert.NotContains(t, rpm.ConfigFiles, "deb.conf")
assert.Contains(t, rpm.ConfigFiles, "whatever.conf")
assert.Equal(t, "amd64", rpm.Arch)
// no overrides
info, err := config.Get("doesnotexist")
assert.NoError(t, err)
assert.True(t, reflect.DeepEqual(&config.Info, info))
}
func TestListFilesToCopy(t *testing.T) {
info := &Info{
Overridables: Overridables{
ConfigFiles: map[string]string{
"testdata/whatever.conf": "/whatever",
},
Files: map[string]string{
"testdata/scripts/**/*": "/test",
},
},
}
files, err := info.FilesToCopy()
assert.NoError(t, err)
// all the input files described in the config in sorted order by source path
assert.Equal(t, []FileToCopy{
{"testdata/scripts/postinstall.sh", "/test/postinstall.sh", false},
{"testdata/scripts/postremove.sh", "/test/postremove.sh", false},
{"testdata/scripts/preinstall.sh", "/test/preinstall.sh", false},
{"testdata/scripts/preremove.sh", "/test/preremove.sh", false},
{"testdata/whatever.conf", "/whatever", true},
}, files)
}
type fakePackager struct{}
func (*fakePackager) Package(info *Info, w io.Writer) error {
return nil
}