1
1
mirror of https://github.com/goreleaser/nfpm synced 2024-11-19 07:34:48 +01:00
nfpm/nfpm_test.go

155 lines
3.5 KiB
Go
Raw Permalink 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 TestDefaultsOnEmptyInfo(t *testing.T) {
2018-02-28 12:26:29 +01:00
info := Info{
Version: "2.4.1",
}
2018-02-16 02:28:11 +01:00
info = WithDefaults(info)
assert.NotEmpty(t, info.Bindir)
assert.NotEmpty(t, info.Platform)
2018-02-28 12:26:29 +01:00
assert.Equal(t, "2.4.1", info.Version)
2018-02-16 02:28:11 +01:00
}
func TestDefaults(t *testing.T) {
info := Info{
2018-03-20 20:06:58 +01:00
Bindir: "/usr/bin",
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{
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{
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{
2018-04-09 18:33:51 +02:00
"package name cannot be empty": {},
"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",
},
2018-04-09 18:33:51 +02:00
"no files were provided": {
2018-04-05 04:13:47 +02:00
Name: "as",
Arch: "asd",
Version: "1.2.3",
},
} {
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{})
_, 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) Package(info Info, w io.Writer) error {
return nil
}