-
Notifications
You must be signed in to change notification settings - Fork 9
/
config_test.go
229 lines (203 loc) · 5.93 KB
/
config_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
223
224
225
226
227
228
229
// Copyright 2017 Debpkg authors. All rights reserved.
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.
package debpkg
import (
"runtime"
"testing"
"github.com/stretchr/testify/assert"
"github.com/xor-gate/debpkg/internal/test"
)
// TestExampleConfig verifies if the config example in the root is correctly loaded
func TestExampleConfig(t *testing.T) {
const configFile = `name: debpkg
version: 7.6.5
architecture: all
maintainer: Deb Pkg
maintainer_email: [email protected]
homepage: https://github.com/xor-gate/debpkg
section: devel
priority: standard
depends: lsb-release
recommends: nano
suggests: curl
conflicts: pico
provides: editor
replaces: vim
built_using: golang
description:
short: This is a short description
long: >
Bla bla
Bla Bla
.
Dusse
files:
- file: LICENSE
dest: {{.DATAROOTDIR}}/foobar/LICENSE
- file: debpkg.go
- file: debpkg_test.go
- file: README.md
dest: {{.DATAROOTDIR}}/foobar/README.md
- dest: /bin/hello
content: >
#!/bin/bash
echo "hello"
directories:
- ./internal
emptydirs:
- /var/cache/foobar
control_extra:
postrm: Makefile
prerm: Makefile
postinst: Makefile
preinst: Makefile
`
filepath, err := test.WriteTempFile("debpkg.yml", configFile)
assert.Nil(t, err)
deb := New()
defer deb.Close()
assert.Nil(t, deb.Config(filepath))
assert.Equal(t, "7.6.5", deb.control.info.version.full,
"Unexpected deb.control.info.version.full")
assert.Equal(t, "Deb Pkg", deb.control.info.maintainer,
"Unexpected deb.control.info.maintainer")
assert.Equal(t, "[email protected]", deb.control.info.maintainerEmail,
"Unexpected deb.control.info.maintainerEmail")
assert.Equal(t, "https://github.com/xor-gate/debpkg", deb.control.info.homepage,
"Unexpected deb.control.info.homepage")
assert.Equal(t, "This is a short description", deb.control.info.descrShort,
"Unexpected short description")
assert.Equal(t, "golang", deb.control.info.builtUsing,
"unexpected built using")
assert.Equal(t, "devel", deb.control.info.section,
"unexpected section")
assert.Equal(t, PriorityStandard, deb.control.info.priority,
"unexpected priority")
assert.Nil(t, testWrite(t, deb))
}
func TestExampleConfigWithControlExtraContent(t *testing.T) {
const configFile = `name: foo-bar
version: 1.2.3
architecture: amd64
maintainer: Mr. Foo Bar
maintainer_email: [email protected]
homepage: https://www.debian.org
section: net
priority: important
control_extra:
postrm: >
#!/bin/bash
echo "post rm!!"
prerm: >
#!/bin/bash
echo "pre rm!!"
postinst: >
#!/bin/bash
echo "post inst!!"
preinst: >
#!/bin/bash
echo "pre inst!!"
`
filepath, err := test.WriteTempFile(t.Name()+".yml", configFile)
assert.Nil(t, err)
deb := New()
defer deb.Close()
assert.Nil(t, deb.Config(filepath))
assert.Equal(t, "1.2.3", deb.control.info.version.full,
"Unexpected deb.control.info.version.full")
assert.Equal(t, "Mr. Foo Bar", deb.control.info.maintainer,
"Unexpected deb.control.info.maintainer")
assert.Equal(t, "[email protected]", deb.control.info.maintainerEmail,
"Unexpected deb.control.info.maintainerEmail")
assert.Equal(t, "https://www.debian.org", deb.control.info.homepage,
"Unexpected deb.control.info.homepage")
assert.Equal(t, "net", deb.control.info.section,
"unexpected section")
assert.Equal(t, PriorityImportant, deb.control.info.priority,
"unexpected priority")
assert.Nil(t, testWrite(t, deb))
}
func TestExampleConfigWithConfigFile(t *testing.T) {
const configFile = `name: bar-bar
version: 1.1.1
architecture: amd64
maintainer: Mr. Foo Bar
maintainer_email: [email protected]
homepage: https://www.debian.org
section: net
priority: important
files:
- dest: /etc/hello
conffile: true
content: >
#!/bin/bash
echo "hello"
- dest: /my/awesome/makefile
conffile: true
file: Makefile
`
filepath, err := test.WriteTempFile(t.Name()+".yml", configFile)
assert.Nil(t, err)
deb := New()
defer deb.Close()
assert.Nil(t, deb.Config(filepath))
assert.Equal(t, "1.1.1", deb.control.info.version.full,
"Unexpected deb.control.info.version.full")
assert.Equal(t, "/etc/hello\n/my/awesome/makefile\n", deb.control.conffiles)
assert.Nil(t, testWrite(t, deb))
}
func TestDefaultConfig(t *testing.T) {
filepath, err := test.WriteTempFile(t.Name()+".yml", "")
assert.Nil(t, err)
deb := New()
defer deb.Close()
assert.Nil(t, deb.Config(filepath))
assert.Equal(t, "any", deb.control.info.architecture,
"unexpected architecture")
assert.Equal(t, "anonymous", deb.control.info.maintainer,
"unexpected maintainer")
assert.Equal(t, "[email protected]", deb.control.info.maintainerEmail,
"unexpected maintainer email")
assert.Equal(t, "https://www.google.com", deb.control.info.homepage,
"unexpected homepage")
assert.Equal(t, PriorityOptional, deb.control.info.priority,
"unexpected priority")
assert.Equal(t, "0.1.0+dev", deb.control.info.version.full,
"unexpected version")
assert.Equal(t, "misc", deb.control.info.section,
"unexpected section")
assert.Equal(t, "unknown", deb.control.info.name,
"unexpected name")
assert.Equal(t, runtime.Version(), deb.control.info.builtUsing,
"unexpected built using")
assert.Equal(t, "-", deb.control.info.descrShort,
"unexpected short description")
assert.Equal(t, " -", deb.control.info.descr,
"unexpected long description")
}
func TestNonExistingConfig(t *testing.T) {
deb := New()
defer deb.Close()
assert.NotNil(t, deb.Config("/non/existent/config/file"))
}
func TestInvalidYAML(t *testing.T) {
deb := New()
defer deb.Close()
const configFile = `name: debpkg
foo: bar
`
filepath, err := test.WriteTempFile(t.Name()+".yml", configFile)
assert.Nil(t, err)
assert.NotNil(t, deb.Config(filepath))
}
func TestInvalidTemplateVar(t *testing.T) {
deb := New()
defer deb.Close()
const configFile = `name: debpkg
foo: {{.bar}}
`
filepath, err := test.WriteTempFile(t.Name()+".yml", configFile)
assert.Nil(t, err)
assert.NotNil(t, deb.Config(filepath))
}