2018-02-16 02:28:11 +01:00
|
|
|
package nfpm
|
|
|
|
|
|
|
|
import (
|
2018-09-14 15:48:12 +02:00
|
|
|
"fmt"
|
2018-02-16 02:28:11 +01:00
|
|
|
"io"
|
2018-09-14 15:48:12 +02:00
|
|
|
"os"
|
2018-04-10 17:39:43 +02:00
|
|
|
"reflect"
|
2018-02-16 02:28:11 +01:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2018-04-05 04:13:47 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
2018-02-16 02:28:11 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2019-11-05 13:38:13 +01:00
|
|
|
func TestDefaultsVersion(t *testing.T) {
|
2019-10-11 22:11:28 +02:00
|
|
|
info := &Info{
|
2020-02-18 14:49:10 +01:00
|
|
|
Version: "v1.0.0",
|
2018-02-28 12:26:29 +01:00
|
|
|
}
|
2018-02-16 02:28:11 +01:00
|
|
|
info = WithDefaults(info)
|
|
|
|
assert.NotEmpty(t, info.Platform)
|
2020-02-18 14:49:10 +01:00
|
|
|
assert.Equal(t, "1.0.0", info.Version)
|
|
|
|
assert.Equal(t, "", info.Release)
|
|
|
|
assert.Equal(t, "", info.Prerelease)
|
2019-11-05 13:38:13 +01:00
|
|
|
|
2020-02-18 14:49:10 +01:00
|
|
|
info = &Info{
|
|
|
|
Version: "v1.0.0-rc1",
|
2019-11-05 13:38:13 +01:00
|
|
|
}
|
|
|
|
info = WithDefaults(info)
|
2020-02-18 14:49:10 +01:00
|
|
|
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{
|
2020-05-24 21:09:47 +02:00
|
|
|
Version: "v1.0.0-1+xdg2",
|
2020-02-18 14:49:10 +01:00
|
|
|
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)
|
2020-05-24 21:09:47 +02:00
|
|
|
assert.Equal(t, "xdg2", info.Deb.VersionMetadata)
|
2018-02-16 02:28:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDefaults(t *testing.T) {
|
2019-10-11 22:11:28 +02:00
|
|
|
info := &Info{
|
2018-03-20 20:06:58 +01:00
|
|
|
Platform: "darwin",
|
|
|
|
Version: "2.4.1",
|
|
|
|
Description: "no description given",
|
2018-02-16 02:28:11 +01:00
|
|
|
}
|
|
|
|
got := WithDefaults(info)
|
|
|
|
assert.Equal(t, info, got)
|
|
|
|
}
|
|
|
|
|
2018-04-05 04:13:47 +02:00
|
|
|
func TestValidate(t *testing.T) {
|
2019-10-11 22:11:28 +02:00
|
|
|
require.NoError(t, Validate(&Info{
|
2018-04-05 04:13:47 +02:00
|
|
|
Name: "as",
|
|
|
|
Arch: "asd",
|
|
|
|
Version: "1.2.3",
|
2018-04-10 17:39:43 +02:00
|
|
|
Overridables: Overridables{
|
|
|
|
Files: map[string]string{
|
|
|
|
"asa": "asd",
|
|
|
|
},
|
2018-04-05 04:13:47 +02:00
|
|
|
},
|
|
|
|
}))
|
2019-10-11 22:11:28 +02:00
|
|
|
require.NoError(t, Validate(&Info{
|
2018-04-05 04:13:47 +02:00
|
|
|
Name: "as",
|
|
|
|
Arch: "asd",
|
|
|
|
Version: "1.2.3",
|
2018-04-10 17:39:43 +02:00
|
|
|
Overridables: Overridables{
|
|
|
|
ConfigFiles: map[string]string{
|
|
|
|
"asa": "asd",
|
|
|
|
},
|
2018-04-05 04:13:47 +02:00
|
|
|
},
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestValidateError(t *testing.T) {
|
|
|
|
for err, info := range map[string]Info{
|
2020-05-13 21:24:06 +02:00
|
|
|
"package name must be provided": {},
|
2018-04-09 18:33:51 +02:00
|
|
|
"package arch must be provided": {
|
2018-04-05 04:13:47 +02:00
|
|
|
Name: "fo",
|
|
|
|
},
|
2018-04-09 18:33:51 +02:00
|
|
|
"package version must be provided": {
|
2018-04-05 04:13:47 +02:00
|
|
|
Name: "as",
|
|
|
|
Arch: "asd",
|
|
|
|
},
|
|
|
|
} {
|
2019-03-04 14:14:05 +01:00
|
|
|
err := err
|
|
|
|
info := info
|
2018-04-05 04:13:47 +02:00
|
|
|
t.Run(err, func(t *testing.T) {
|
2019-10-11 22:11:28 +02:00
|
|
|
require.EqualError(t, Validate(&info), err)
|
2018-04-05 04:13:47 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-10 17:39:43 +02:00
|
|
|
func TestParseFile(t *testing.T) {
|
2018-04-10 21:11:35 +02:00
|
|
|
packagers = map[string]Packager{}
|
|
|
|
_, err := ParseFile("./testdata/overrides.yaml")
|
|
|
|
assert.Error(t, err)
|
|
|
|
Register("deb", &fakePackager{})
|
|
|
|
Register("rpm", &fakePackager{})
|
|
|
|
_, err = ParseFile("./testdata/overrides.yaml")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
_, err = ParseFile("./testdata/doesnotexist.yaml")
|
2018-04-10 17:39:43 +02:00
|
|
|
assert.Error(t, err)
|
2018-09-14 15:48:12 +02:00
|
|
|
config, err := ParseFile("./testdata/versionenv.yaml")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, fmt.Sprintf("v%s", os.Getenv("GOROOT")), config.Version)
|
2018-04-10 17:39:43 +02:00
|
|
|
}
|
|
|
|
|
2018-04-10 01:04:06 +02:00
|
|
|
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)
|
2018-04-10 17:39:43 +02:00
|
|
|
|
|
|
|
// no overrides
|
|
|
|
info, err := config.Get("doesnotexist")
|
|
|
|
assert.NoError(t, err)
|
2019-10-11 22:11:28 +02:00
|
|
|
assert.True(t, reflect.DeepEqual(&config.Info, info))
|
2018-04-10 01:04:06 +02:00
|
|
|
}
|
|
|
|
|
2020-05-14 14:46:46 +02:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2018-02-16 02:28:11 +01:00
|
|
|
type fakePackager struct{}
|
|
|
|
|
2020-07-09 15:16:04 +02:00
|
|
|
func (*fakePackager) ConventionalFileName(info *Info) string {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2019-10-11 22:11:28 +02:00
|
|
|
func (*fakePackager) Package(info *Info, w io.Writer) error {
|
2018-02-16 02:28:11 +01:00
|
|
|
return nil
|
|
|
|
}
|