2020-12-15 17:47:00 +01:00
|
|
|
package nfpm_test
|
2018-02-16 02:28:11 +01:00
|
|
|
|
|
|
|
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"
|
2020-09-17 14:18:44 +02:00
|
|
|
"strings"
|
2018-02-16 02:28:11 +01:00
|
|
|
"testing"
|
|
|
|
|
2018-04-05 04:13:47 +02:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-12-15 17:47:00 +01:00
|
|
|
|
2020-12-23 14:25:57 +01:00
|
|
|
"github.com/goreleaser/nfpm/v2"
|
|
|
|
"github.com/goreleaser/nfpm/v2/files"
|
2018-02-16 02:28:11 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestRegister(t *testing.T) {
|
|
|
|
format := "TestRegister"
|
|
|
|
pkgr := &fakePackager{}
|
2020-12-15 17:47:00 +01:00
|
|
|
nfpm.RegisterPackager(format, pkgr)
|
|
|
|
got, err := nfpm.Get(format)
|
2020-08-04 19:55:52 +02:00
|
|
|
require.NoError(t, err)
|
2020-12-23 01:28:32 +01:00
|
|
|
require.Equal(t, pkgr, got)
|
2018-02-16 02:28:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGet(t *testing.T) {
|
|
|
|
format := "TestGet"
|
2020-12-15 17:47:00 +01:00
|
|
|
got, err := nfpm.Get(format)
|
2020-08-04 19:55:52 +02:00
|
|
|
require.Error(t, err)
|
2020-12-23 01:28:32 +01:00
|
|
|
require.EqualError(t, err, "no packager registered for the format "+format)
|
|
|
|
require.Nil(t, got)
|
2018-02-16 02:28:11 +01:00
|
|
|
pkgr := &fakePackager{}
|
2020-12-15 17:47:00 +01:00
|
|
|
nfpm.RegisterPackager(format, pkgr)
|
|
|
|
got, err = nfpm.Get(format)
|
2020-08-04 19:55:52 +02:00
|
|
|
require.NoError(t, err)
|
2020-12-23 01:28:32 +01:00
|
|
|
require.Equal(t, pkgr, got)
|
2018-02-16 02:28:11 +01:00
|
|
|
}
|
|
|
|
|
2019-11-05 13:38:13 +01:00
|
|
|
func TestDefaultsVersion(t *testing.T) {
|
2020-12-15 17:47:00 +01:00
|
|
|
info := &nfpm.Info{
|
2020-02-18 14:49:10 +01:00
|
|
|
Version: "v1.0.0",
|
2018-02-28 12:26:29 +01:00
|
|
|
}
|
2020-12-15 17:47:00 +01:00
|
|
|
info = nfpm.WithDefaults(info)
|
2020-12-23 01:28:32 +01:00
|
|
|
require.NotEmpty(t, info.Platform)
|
|
|
|
require.Equal(t, "1.0.0", info.Version)
|
|
|
|
require.Equal(t, "", info.Release)
|
|
|
|
require.Equal(t, "", info.Prerelease)
|
2019-11-05 13:38:13 +01:00
|
|
|
|
2020-12-15 17:47:00 +01:00
|
|
|
info = &nfpm.Info{
|
2020-02-18 14:49:10 +01:00
|
|
|
Version: "v1.0.0-rc1",
|
2019-11-05 13:38:13 +01:00
|
|
|
}
|
2020-12-15 17:47:00 +01:00
|
|
|
info = nfpm.WithDefaults(info)
|
2020-12-23 01:28:32 +01:00
|
|
|
require.Equal(t, "1.0.0", info.Version)
|
|
|
|
require.Equal(t, "", info.Release)
|
|
|
|
require.Equal(t, "rc1", info.Prerelease)
|
2020-02-18 14:49:10 +01:00
|
|
|
|
2020-12-15 17:47:00 +01:00
|
|
|
info = &nfpm.Info{
|
2020-08-19 15:20:02 +02:00
|
|
|
Version: "v1.0.0-beta1",
|
2020-02-18 14:49:10 +01:00
|
|
|
}
|
2020-12-15 17:47:00 +01:00
|
|
|
info = nfpm.WithDefaults(info)
|
2020-12-23 01:28:32 +01:00
|
|
|
require.Equal(t, "1.0.0", info.Version)
|
|
|
|
require.Equal(t, "", info.Release)
|
|
|
|
require.Equal(t, "beta1", info.Prerelease)
|
2020-02-18 14:49:10 +01:00
|
|
|
|
2020-12-15 17:47:00 +01:00
|
|
|
info = &nfpm.Info{
|
2020-02-18 14:49:10 +01:00
|
|
|
Version: "v1.0.0-1",
|
|
|
|
Release: "2",
|
|
|
|
Prerelease: "beta1",
|
|
|
|
}
|
2020-12-15 17:47:00 +01:00
|
|
|
info = nfpm.WithDefaults(info)
|
2020-12-23 01:28:32 +01:00
|
|
|
require.Equal(t, "1.0.0", info.Version)
|
|
|
|
require.Equal(t, "2", info.Release)
|
|
|
|
require.Equal(t, "beta1", info.Prerelease)
|
2020-02-18 14:49:10 +01:00
|
|
|
|
2020-12-15 17:47:00 +01:00
|
|
|
info = &nfpm.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",
|
|
|
|
}
|
2020-12-15 17:47:00 +01:00
|
|
|
info = nfpm.WithDefaults(info)
|
2020-12-23 01:28:32 +01:00
|
|
|
require.Equal(t, "1.0.0", info.Version)
|
|
|
|
require.Equal(t, "2", info.Release)
|
|
|
|
require.Equal(t, "beta1", info.Prerelease)
|
|
|
|
require.Equal(t, "", info.Deb.VersionMetadata)
|
2018-02-16 02:28:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDefaults(t *testing.T) {
|
2020-12-15 17:47:00 +01:00
|
|
|
info := &nfpm.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
|
|
|
}
|
2020-12-15 17:47:00 +01:00
|
|
|
got := nfpm.WithDefaults(info)
|
2020-12-23 01:28:32 +01:00
|
|
|
require.Equal(t, info, got)
|
2018-02-16 02:28:11 +01:00
|
|
|
}
|
|
|
|
|
2018-04-05 04:13:47 +02:00
|
|
|
func TestValidate(t *testing.T) {
|
2020-12-15 17:47:00 +01:00
|
|
|
require.NoError(t, nfpm.Validate(&nfpm.Info{
|
2018-04-05 04:13:47 +02:00
|
|
|
Name: "as",
|
|
|
|
Arch: "asd",
|
|
|
|
Version: "1.2.3",
|
2020-12-15 17:47:00 +01:00
|
|
|
Overridables: nfpm.Overridables{
|
2020-12-23 01:28:32 +01:00
|
|
|
Contents: []*files.Content{
|
|
|
|
{
|
|
|
|
Source: "./testdata/contents.yaml",
|
|
|
|
Destination: "asd",
|
|
|
|
},
|
2018-04-10 17:39:43 +02:00
|
|
|
},
|
2018-04-05 04:13:47 +02:00
|
|
|
},
|
|
|
|
}))
|
2020-12-15 17:47:00 +01:00
|
|
|
require.NoError(t, nfpm.Validate(&nfpm.Info{
|
2018-04-05 04:13:47 +02:00
|
|
|
Name: "as",
|
|
|
|
Arch: "asd",
|
|
|
|
Version: "1.2.3",
|
2020-12-15 17:47:00 +01:00
|
|
|
Overridables: nfpm.Overridables{
|
2020-12-23 01:28:32 +01:00
|
|
|
Contents: []*files.Content{
|
|
|
|
{
|
|
|
|
Source: "./testdata/contents.yaml",
|
|
|
|
Destination: "asd",
|
|
|
|
Type: "config",
|
|
|
|
},
|
2018-04-10 17:39:43 +02:00
|
|
|
},
|
2018-04-05 04:13:47 +02:00
|
|
|
},
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestValidateError(t *testing.T) {
|
2020-12-15 17:47:00 +01:00
|
|
|
for err, info := range map[string]nfpm.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) {
|
2020-12-15 17:47:00 +01:00
|
|
|
require.EqualError(t, nfpm.Validate(&info), err)
|
2018-04-05 04:13:47 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-10 17:39:43 +02:00
|
|
|
func TestParseFile(t *testing.T) {
|
2020-12-15 17:47:00 +01:00
|
|
|
nfpm.ClearPackagers()
|
|
|
|
_, err := nfpm.ParseFile("./testdata/overrides.yaml")
|
2020-08-04 19:55:52 +02:00
|
|
|
require.Error(t, err)
|
2020-12-15 17:47:00 +01:00
|
|
|
nfpm.RegisterPackager("deb", &fakePackager{})
|
|
|
|
nfpm.RegisterPackager("rpm", &fakePackager{})
|
|
|
|
nfpm.RegisterPackager("apk", &fakePackager{})
|
|
|
|
_, err = nfpm.ParseFile("./testdata/overrides.yaml")
|
2020-08-04 19:55:52 +02:00
|
|
|
require.NoError(t, err)
|
2020-12-15 17:47:00 +01:00
|
|
|
_, err = nfpm.ParseFile("./testdata/doesnotexist.yaml")
|
2020-08-04 19:55:52 +02:00
|
|
|
require.Error(t, err)
|
2020-12-15 17:47:00 +01:00
|
|
|
config, err := nfpm.ParseFile("./testdata/versionenv.yaml")
|
2020-08-04 19:55:52 +02:00
|
|
|
require.NoError(t, err)
|
2020-12-23 01:28:32 +01:00
|
|
|
require.Equal(t, fmt.Sprintf("v%s", os.Getenv("GOROOT")), config.Version)
|
2018-04-10 17:39:43 +02:00
|
|
|
}
|
|
|
|
|
2020-12-15 17:47:00 +01:00
|
|
|
func TestParseEnhancedFile(t *testing.T) {
|
|
|
|
config, err := nfpm.ParseFile("./testdata/contents.yaml")
|
|
|
|
require.NoError(t, err)
|
2020-12-23 01:28:32 +01:00
|
|
|
require.Equal(t, config.Name, "contents foo")
|
2020-12-15 17:47:00 +01:00
|
|
|
shouldFind := 5
|
|
|
|
if len(config.Contents) != shouldFind {
|
|
|
|
t.Errorf("should have had %d files but found %d", shouldFind, len(config.Contents))
|
|
|
|
for idx, f := range config.Contents {
|
|
|
|
fmt.Printf("%d => %+#v\n", idx, f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseEnhancedNestedGlobFile(t *testing.T) {
|
|
|
|
config, err := nfpm.ParseFile("./testdata/contents_glob.yaml")
|
|
|
|
require.NoError(t, err)
|
|
|
|
shouldFind := 3
|
|
|
|
if len(config.Contents) != shouldFind {
|
|
|
|
t.Errorf("should have had %d files but found %d", shouldFind, len(config.Contents))
|
|
|
|
for idx, f := range config.Contents {
|
|
|
|
fmt.Printf("%d => %+#v\n", idx, f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-17 14:18:44 +02:00
|
|
|
func TestOptionsFromEnvironment(t *testing.T) {
|
|
|
|
const (
|
|
|
|
globalPass = "hunter2"
|
|
|
|
debPass = "password123"
|
|
|
|
rpmPass = "secret"
|
|
|
|
apkPass = "foobar"
|
|
|
|
release = "3"
|
|
|
|
version = "1.0.0"
|
|
|
|
)
|
|
|
|
|
|
|
|
t.Run("version", func(t *testing.T) {
|
|
|
|
os.Clearenv()
|
|
|
|
os.Setenv("VERSION", version)
|
2020-12-15 17:47:00 +01:00
|
|
|
info, err := nfpm.Parse(strings.NewReader("name: foo\nversion: $VERSION"))
|
2020-09-17 14:18:44 +02:00
|
|
|
require.NoError(t, err)
|
2020-12-23 01:28:32 +01:00
|
|
|
require.Equal(t, version, info.Version)
|
2020-09-17 14:18:44 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("release", func(t *testing.T) {
|
|
|
|
os.Clearenv()
|
|
|
|
os.Setenv("RELEASE", release)
|
2020-12-15 17:47:00 +01:00
|
|
|
info, err := nfpm.Parse(strings.NewReader("name: foo\nrelease: $RELEASE"))
|
2020-09-17 14:18:44 +02:00
|
|
|
require.NoError(t, err)
|
2020-12-23 01:28:32 +01:00
|
|
|
require.Equal(t, release, info.Release)
|
2020-09-17 14:18:44 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("global passphrase", func(t *testing.T) {
|
|
|
|
os.Clearenv()
|
|
|
|
os.Setenv("NFPM_PASSPHRASE", globalPass)
|
2020-12-15 17:47:00 +01:00
|
|
|
info, err := nfpm.Parse(strings.NewReader("name: foo"))
|
2020-09-17 14:18:44 +02:00
|
|
|
require.NoError(t, err)
|
2020-12-23 01:28:32 +01:00
|
|
|
require.Equal(t, globalPass, info.Deb.Signature.KeyPassphrase)
|
|
|
|
require.Equal(t, globalPass, info.RPM.Signature.KeyPassphrase)
|
|
|
|
require.Equal(t, globalPass, info.APK.Signature.KeyPassphrase)
|
2020-09-17 14:18:44 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("specific passphrases", func(t *testing.T) {
|
|
|
|
os.Clearenv()
|
|
|
|
os.Setenv("NFPM_PASSPHRASE", globalPass)
|
|
|
|
os.Setenv("NFPM_DEB_PASSPHRASE", debPass)
|
|
|
|
os.Setenv("NFPM_RPM_PASSPHRASE", rpmPass)
|
|
|
|
os.Setenv("NFPM_APK_PASSPHRASE", apkPass)
|
2020-12-15 17:47:00 +01:00
|
|
|
info, err := nfpm.Parse(strings.NewReader("name: foo"))
|
2020-09-17 14:18:44 +02:00
|
|
|
require.NoError(t, err)
|
2020-12-23 01:28:32 +01:00
|
|
|
require.Equal(t, debPass, info.Deb.Signature.KeyPassphrase)
|
|
|
|
require.Equal(t, rpmPass, info.RPM.Signature.KeyPassphrase)
|
|
|
|
require.Equal(t, apkPass, info.APK.Signature.KeyPassphrase)
|
2020-09-17 14:18:44 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-04-10 01:04:06 +02:00
|
|
|
func TestOverrides(t *testing.T) {
|
2020-12-23 01:28:32 +01:00
|
|
|
nfpm.RegisterPackager("deb", &fakePackager{})
|
|
|
|
nfpm.RegisterPackager("rpm", &fakePackager{})
|
|
|
|
nfpm.RegisterPackager("apk", &fakePackager{})
|
|
|
|
|
2018-04-10 01:04:06 +02:00
|
|
|
file := "./testdata/overrides.yaml"
|
2020-12-15 17:47:00 +01:00
|
|
|
config, err := nfpm.ParseFile(file)
|
2020-08-04 19:55:52 +02:00
|
|
|
require.NoError(t, err)
|
2020-12-23 01:28:32 +01:00
|
|
|
require.Equal(t, "foo", config.Name)
|
|
|
|
require.Equal(t, "amd64", config.Arch)
|
2018-04-10 01:04:06 +02:00
|
|
|
|
2020-12-23 01:28:32 +01:00
|
|
|
for _, format := range []string{"apk", "deb", "rpm"} {
|
|
|
|
format := format
|
|
|
|
t.Run(format, func(t *testing.T) {
|
|
|
|
pkg, err := config.Get(format)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, pkg.Depends, []string{format + "_depend"})
|
|
|
|
for _, f := range pkg.Contents {
|
|
|
|
switch f.Packager {
|
|
|
|
case format:
|
|
|
|
require.Contains(t, f.Destination, "/"+format)
|
|
|
|
case "":
|
|
|
|
require.True(t, f.Destination == "/etc/foo/whatever.conf")
|
|
|
|
default:
|
|
|
|
t.Fatalf("invalid packager: %s", f.Packager)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
require.Equal(t, "amd64", pkg.Arch)
|
|
|
|
})
|
2020-12-15 17:47:00 +01:00
|
|
|
}
|
2018-04-10 17:39:43 +02:00
|
|
|
|
2020-12-23 01:28:32 +01:00
|
|
|
t.Run("no_overrides", func(t *testing.T) {
|
|
|
|
info, err := config.Get("doesnotexist")
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.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{}
|
|
|
|
|
2020-12-15 17:47:00 +01:00
|
|
|
func (*fakePackager) ConventionalFileName(info *nfpm.Info) string {
|
2020-07-09 15:16:04 +02:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2020-12-15 17:47:00 +01:00
|
|
|
func (*fakePackager) Package(info *nfpm.Info, w io.Writer) error {
|
2018-02-16 02:28:11 +01:00
|
|
|
return nil
|
|
|
|
}
|