1
1
mirror of https://github.com/goreleaser/nfpm synced 2024-11-18 14:54:04 +01:00
nfpm/nfpm_test.go

193 lines
4.4 KiB
Go
Raw Normal View History

2018-02-16 02:28:11 +01:00
package nfpm
import (
"fmt"
2018-02-16 02:28:11 +01:00
"io"
"os"
"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)
}
func TestDefaultsVersion(t *testing.T) {
info := &Info{
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)
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, "", info.Deb.VersionMetadata)
2018-02-16 02:28:11 +01:00
}
func TestDefaults(t *testing.T) {
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) {
require.NoError(t, Validate(&Info{
2018-04-05 04:13:47 +02:00
Name: "as",
Arch: "asd",
Version: "1.2.3",
Overridables: Overridables{
Files: map[string]string{
"asa": "asd",
},
2018-04-05 04:13:47 +02:00
},
}))
require.NoError(t, Validate(&Info{
2018-04-05 04:13:47 +02:00
Name: "as",
Arch: "asd",
Version: "1.2.3",
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{
"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) {
require.EqualError(t, Validate(&info), err)
2018-04-05 04:13:47 +02:00
})
}
}
func TestParseFile(t *testing.T) {
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")
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)
}
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)
// no overrides
info, err := config.Get("doesnotexist")
assert.NoError(t, err)
assert.True(t, reflect.DeepEqual(&config.Info, info))
2018-04-10 01:04:06 +02:00
}
2018-02-16 02:28:11 +01:00
type fakePackager struct{}
func (*fakePackager) ConventionalFileName(info *Info) string {
return ""
}
func (*fakePackager) Package(info *Info, w io.Writer) error {
2018-02-16 02:28:11 +01:00
return nil
}