Repository: goreleaser/nfpm
Pull request #66 (2019-08-31 12:59:19)

fix: rules tests, overridables
improved #49 and moved RPM to overridables.


Patch:
diff --git a/deb/deb.go b/deb/deb.go
index b1be898..26571d4 100644
--- a/deb/deb.go
+++ b/deb/deb.go
@@ -219,11 +219,11 @@ func createControl(instSize int64, md5sums []byte, info nfpm.Info) (controlTarGz
 	}
 
 	for script, dest := range map[string]string{
-		info.Scripts.PreInstall:  "preinst",
-		info.Scripts.PostInstall: "postinst",
-		info.Scripts.PreRemove:   "prerm",
-		info.Scripts.PostRemove:  "postrm",
-		info.Scripts.Rules:       "rules",
+		info.Scripts.PreInstall:             "preinst",
+		info.Scripts.PostInstall:            "postinst",
+		info.Scripts.PreRemove:              "prerm",
+		info.Scripts.PostRemove:             "postrm",
+		info.Overridables.Deb.Scripts.Rules: "rules",
 	} {
 		if script != "" {
 			if err := newScriptInsideTarGz(out, script, dest); err != nil {
@@ -350,7 +350,7 @@ Maintainer: {{.Info.Maintainer}}
 {{- if .Info.Vendor}}
 Vendor: {{.Info.Vendor}}
 {{- end }}
-Installed-Size: {{.InstalledSize}}	
+Installed-Size: {{.InstalledSize}}
 {{- with .Info.Replaces}}
 Replaces: {{join .}}
 {{- end }}
diff --git a/nfpm.go b/nfpm.go
index 8486d4a..fac4f89 100644
--- a/nfpm.go
+++ b/nfpm.go
@@ -116,7 +116,6 @@ type Info struct {
 	Homepage     string `yaml:"homepage,omitempty"`
 	License      string `yaml:"license,omitempty"`
 	Bindir       string `yaml:"bindir,omitempty"`
-	RPM          RPM    `yaml:"rpm,omitempty"`
 }
 
 // Overridables contain the field which are overridable in a package
@@ -131,8 +130,11 @@ type Overridables struct {
 	ConfigFiles  map[string]string `yaml:"config_files,omitempty"`
 	EmptyFolders []string          `yaml:"empty_folders,omitempty"`
 	Scripts      Scripts           `yaml:"scripts,omitempty"`
+	RPM          RPM               `yaml:"rpm,omitempty"`
+	Deb          Deb               `yaml:"deb,omitempty"`
 }
 
+// RPM is custom configs that are only available on RPM packages
 type RPM struct {
 	Group       string `yaml:"group,omitempty"`
 	Prefix      string `yaml:"prefix,omitempty"`
@@ -140,13 +142,22 @@ type RPM struct {
 	Release     string `yaml:"release,omitempty"`
 }
 
+// Deb is custom configs that are only available on deb packages
+type Deb struct {
+	Scripts DebScripts `yaml:"scripts,omitempty"`
+}
+
+// DebScripts is scripts only available on deb packages
+type DebScripts struct {
+	Rules string `yaml:"rules,omitempty"`
+}
+
 // Scripts contains information about maintainer scripts for packages
 type Scripts struct {
 	PreInstall  string `yaml:"preinstall,omitempty"`
 	PostInstall string `yaml:"postinstall,omitempty"`
 	PreRemove   string `yaml:"preremove,omitempty"`
 	PostRemove  string `yaml:"postremove,omitempty"`
-	Rules       string `yaml:"rules,omitempty"`
 }
 
 // Validate the given Info and returns an error if it is invalid.
diff --git a/rpm/rpm.go b/rpm/rpm.go
index 26beae8..62c1a11 100644
--- a/rpm/rpm.go
+++ b/rpm/rpm.go
@@ -319,15 +319,15 @@ const specTemplate = `
 %define __os_install_post %{_dbpath}/brp-compress
 %define _arch {{ .Info.Arch }}
 %define _bindir {{ .Info.Bindir }}
-{{- if eq .Info.RPM.Compression "gzip"}}
+{{- if eq .Info.Overridables.RPM.Compression "gzip"}}
 %define _source_payload w9.gzdio
 %define _binary_payload w9.gzdio
 {{- end}}
-{{- if eq .Info.RPM.Compression "xz"}}
+{{- if eq .Info.Overridables.RPM.Compression "xz"}}
 %define _source_payload w6.xzdio
 %define _binary_payload w6.xzdio
 {{- end}}
-{{- if eq .Info.RPM.Compression "lzma"}}
+{{- if eq .Info.Overridables.RPM.Compression "lzma"}}
 %define _source_payload w6.lzdio
 %define _binary_payload w6.lzdio
 {{- end}}
@@ -338,7 +338,7 @@ Summary: {{ first_line .Info.Description }}
 Epoch: {{ . }}
 {{- end }}
 Version: {{ .Info.Version }}
-{{- with .Info.RPM.Release }}
+{{- with .Info.Overridables.RPM.Release }}
 Release: {{ . }}
 {{- else }}
 Release: 1
@@ -346,7 +346,7 @@ Release: 1
 {{- with .Info.License }}
 License: {{ . }}
 {{- end }}
-{{- with .Info.RPM.Group }}
+{{- with .Info.Overridables.RPM.Group }}
 Group: {{ . }}
 {{- else }}
 Group: Development/Tools
@@ -359,7 +359,7 @@ URL: {{ . }}
 Packager: {{ . }}
 {{- end }}
 BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root
-{{- with .Info.RPM.Prefix }}
+{{- with .Info.Overridables.RPM.Prefix }}
 Prefix: {{ . }}
 {{- end }}
 


Test patch:
diff --git a/acceptance/acceptance_test.go b/acceptance/acceptance_test.go
index 8876ba2..280b573 100644
--- a/acceptance/acceptance_test.go
+++ b/acceptance/acceptance_test.go
@@ -140,6 +140,15 @@ func TestRPMRelease(t *testing.T) {
 	})
 }
 
+func TestDebRules(t *testing.T) {
+	accept(t, acceptParms{
+		Name:       "rules.deb",
+		Conf:       "rules.deb.yaml",
+		Format:     "deb",
+		Dockerfile: "rules.deb.dockerfile",
+	})
+}
+
 type acceptParms struct {
 	Name       string
 	Conf       string
diff --git a/acceptance/testdata/rules.deb.dockerfile b/acceptance/testdata/rules.deb.dockerfile
new file mode 100644
index 0000000..8d8f89b
--- /dev/null
+++ b/acceptance/testdata/rules.deb.dockerfile
@@ -0,0 +1,5 @@
+FROM ubuntu
+ARG package
+COPY ${package} /tmp/foo.deb
+RUN dpkg -i /tmp/foo.deb
+RUN dpkg -r foo
diff --git a/acceptance/testdata/rules.deb.yaml b/acceptance/testdata/rules.deb.yaml
new file mode 100644
index 0000000..67e2c4c
--- /dev/null
+++ b/acceptance/testdata/rules.deb.yaml
@@ -0,0 +1,18 @@
+name: "foo"
+arch: "amd64"
+platform: "linux"
+version: "v1.2.3"
+maintainer: "Foo Bar"
+description: |
+  Foo bar
+    Multiple lines
+vendor: "foobar"
+homepage: "https://foobar.org"
+license: "MIT"
+files:
+  ../testdata/fake: "/usr/local/bin/fake"
+config_files:
+  ../testdata/whatever.conf: "/etc/foo/whatever.conf"
+deb:
+  scripts:
+    rules: ./testdata/scripts/rules.sh
diff --git a/acceptance/testdata/scripts/postinstall.sh b/acceptance/testdata/scripts/postinstall.sh
old mode 100644
new mode 100755
diff --git a/acceptance/testdata/scripts/postremove.sh b/acceptance/testdata/scripts/postremove.sh
old mode 100644
new mode 100755
diff --git a/acceptance/testdata/scripts/preinstall.sh b/acceptance/testdata/scripts/preinstall.sh
old mode 100644
new mode 100755
diff --git a/acceptance/testdata/scripts/preremove.sh b/acceptance/testdata/scripts/preremove.sh
old mode 100644
new mode 100755
diff --git a/acceptance/testdata/scripts/rules.sh b/acceptance/testdata/scripts/rules.sh
new file mode 100755
index 0000000..1ea546b
--- /dev/null
+++ b/acceptance/testdata/scripts/rules.sh
@@ -0,0 +1,6 @@
+#!/usr/bin/make -f
+#export DH_VERBOSE = 1
+
+%:
+	echo "deb rules"
+	dh $@
diff --git a/deb/deb_test.go b/deb/deb_test.go
index 49ac53d..0a0464e 100644
--- a/deb/deb_test.go
+++ b/deb/deb_test.go
@@ -275,3 +275,32 @@ func TestDebEpoch(t *testing.T) {
 	assert.NoError(t, err)
 	assert.Equal(t, string(bts), w.String())
 }
+
+func TestDebRules(t *testing.T) {
+	var w bytes.Buffer
+	assert.NoError(t, writeControl(&w, controlData{
+		Info: nfpm.WithDefaults(nfpm.Info{
+			Name:        "lala",
+			Arch:        "arm64",
+			Description: "Has rules script",
+			Priority:    "extra",
+			Epoch:       "2",
+			Version:     "1.2.0",
+			Section:     "default",
+			Overridables: nfpm.Overridables{
+				Deb: nfpm.Deb{
+					Scripts: nfpm.DebScripts{
+						Rules: "foo.sh",
+					},
+				},
+			},
+		}),
+	}))
+	var golden = "testdata/rules.golden"
+	if *update {
+		require.NoError(t, ioutil.WriteFile(golden, w.Bytes(), 0655))
+	}
+	bts, err := ioutil.ReadFile(golden) //nolint:gosec
+	assert.NoError(t, err)
+	assert.Equal(t, string(bts), w.String())
+}
diff --git a/deb/testdata/rules.golden b/deb/testdata/rules.golden
new file mode 100644
index 0000000..707a63e
--- /dev/null
+++ b/deb/testdata/rules.golden
@@ -0,0 +1,7 @@
+Package: lala
+Version: 2:1.2.0
+Section: default
+Priority: extra
+Architecture: arm64
+Installed-Size: 0
+Description: Has rules script
diff --git a/rpm/rpm_test.go b/rpm/rpm_test.go
index b608888..edeac9c 100644
--- a/rpm/rpm_test.go
+++ b/rpm/rpm_test.go
@@ -3,7 +3,6 @@ package rpm
 import (
 	"bytes"
 	"flag"
-	"fmt"
 	"io/ioutil"
 	"os"
 	"testing"
@@ -96,7 +95,7 @@ func TestRPM(t *testing.T) {
 
 func TestWithRPMTags(t *testing.T) {
 	var info = exampleInfo()
-	info.RPM = nfpm.RPM{
+	info.Overridables.RPM = nfpm.RPM{
 		Group:   "default",
 		Prefix:  "/usr",
 		Release: "3",
@@ -107,7 +106,7 @@ func TestWithRPMTags(t *testing.T) {
 
 func TestRPMTagsSpec(t *testing.T) {
 	var info = exampleInfo()
-	info.RPM = nfpm.RPM{
+	info.Overridables.RPM = nfpm.RPM{
 		Group:   "default",
 		Prefix:  "/usr",
 		Release: "5",
@@ -133,22 +132,6 @@ func TestRPMVersionWithDash(t *testing.T) {
 	assert.NoError(t, err)
 }
 
-func TestRPMScripts(t *testing.T) {
-	info := exampleInfo()
-	scripts, err := readScripts(info)
-	assert.NoError(t, err)
-	for actual, src := range map[string]string{
-		scripts.Pre:    info.Scripts.PreInstall,
-		scripts.Post:   info.Scripts.PostInstall,
-		scripts.Preun:  info.Scripts.PreRemove,
-		scripts.Postun: info.Scripts.PostRemove,
-	} {
-		data, err := ioutil.ReadFile(src)         //nolint:gosec
-		fmt.Printf("%s %s %s", actual, src, data) //nolint.govet
-		assert.NoError(t, err)
-	}
-}
-
 func TestRPMNoFiles(t *testing.T) {
 	info := exampleInfo()
 	info.Files = map[string]string{}