Repository: goreleaser/nfpm
Pull request #170 (2020-07-15 11:16:21)

fix: Reflect (pre)release in conventional file names.
This pull request adds release and pre-release information to the conventional file names. I did not include this earlier because I thought that `info.Version` already contained the full version information available. This results in the following possibilities:
* `1.2.3` if `info.Release` and `info.Prerelease` are empty
* `1.2.3-4` if `info.Release` is set to `4` and `info.Prerelease` is empty
* `1.2.3-4~5` if `info.Release` is set to `4` and `info.Prerelease` is set to `5
* `1.2.3~5` if `info.Release` is empty and `info.Prerelease` is set to `5`

This only affects the default output file names.

vercel[bot]: 
This pull request is being automatically deployed with Vercel ([learn more](https://vercel.link/github-learn-more)).
To see the status of your deployment, click on the icon next to each commit.
	

Patch:
diff --git a/deb/deb.go b/deb/deb.go
index 542b284..1c99595 100644
--- a/deb/deb.go
+++ b/deb/deb.go
@@ -55,8 +55,16 @@ func (*Deb) ConventionalFileName(info *nfpm.Info) string {
 		arch = info.Arch
 	}
 
+	version := info.Version
+	if info.Release != "" {
+		version += "-" + info.Release
+	}
+	if info.Prerelease != "" {
+		version += "~" + info.Prerelease
+	}
+
 	// package_version_architecture.package-type
-	return fmt.Sprintf("%s_%s_%s.deb", info.Name, info.Version, arch)
+	return fmt.Sprintf("%s_%s_%s.deb", info.Name, version, arch)
 }
 
 // Package writes a new deb package to the given writer using the given info.
@@ -252,7 +260,7 @@ func createChangelog(info *nfpm.Info) (chglogTarGz []byte, err error) {
 }
 
 func formatChangelog(info *nfpm.Info) (string, error) {
-	pkgChglog, err := info.GetChangeLog()
+	changelog, err := info.GetChangeLog()
 	if err != nil {
 		return "", err
 	}
@@ -262,12 +270,12 @@ func formatChangelog(info *nfpm.Info) (string, error) {
 		return "", err
 	}
 
-	chglogData, err := chglog.FormatChangelog(pkgChglog, tpl)
+	formattedChangelog, err := chglog.FormatChangelog(changelog, tpl)
 	if err != nil {
 		return "", err
 	}
 
-	return chglogData, nil
+	return strings.TrimSpace(formattedChangelog) + "\n", nil
 }
 
 func createControl(instSize int64, md5sums []byte, info *nfpm.Info) (controlTarGz []byte, err error) {
diff --git a/rpm/rpm.go b/rpm/rpm.go
index 7b92441..8d240de 100644
--- a/rpm/rpm.go
+++ b/rpm/rpm.go
@@ -29,11 +29,11 @@ const (
 	tagChangelogText = 1082
 
 	changelogNotesTemplate = `
-	{{- range .Changes }}{{$note := splitList "\n" .Note}}
-	- {{ first $note }}
-	{{- range $i,$n := (rest $note) }}{{- if ne (trim $n) ""}}
-	{{$n}}{{end}}
-	{{- end}}{{- end}}`
+{{- range .Changes }}{{$note := splitList "\n" .Note}}
+- {{ first $note }}
+{{- range $i,$n := (rest $note) }}{{- if ne (trim $n) ""}}
+{{$n}}{{end}}
+{{- end}}{{- end}}`
 )
 
 // nolint: gochecknoinits
@@ -69,8 +69,17 @@ func ensureValidArch(info *nfpm.Info) *nfpm.Info {
 // http://ftp.rpm.org/max-rpm/ch-rpm-file-format.html
 func (*RPM) ConventionalFileName(info *nfpm.Info) string {
 	info = ensureValidArch(info)
+
+	version := info.Version
+	if info.Release != "" {
+		version += "-" + info.Release
+	}
+	if info.Prerelease != "" {
+		version += "~" + info.Prerelease
+	}
+
 	// name-version-release.architecture.rpm
-	return fmt.Sprintf("%s-%s.%s.rpm", info.Name, info.Version, info.Arch)
+	return fmt.Sprintf("%s-%s.%s.rpm", info.Name, version, info.Arch)
 }
 
 // Package writes a new RPM package to the given writer using the given info.


Test patch:
diff --git a/acceptance/testdata/rpm.changelog.dockerfile b/acceptance/testdata/rpm.changelog.dockerfile
index 9bf4721..c622d79 100644
--- a/acceptance/testdata/rpm.changelog.dockerfile
+++ b/acceptance/testdata/rpm.changelog.dockerfile
@@ -3,11 +3,11 @@ ARG package
 COPY ${package} /tmp/foo.rpm
 RUN rpm -ivh /tmp/foo.rpm
 RUN rpm -qp /tmp/foo.rpm --changelog | grep "Carlos A Becker <pkg@carlosbecker.com>"
-RUN rpm -qp /tmp/foo.rpm --changelog | grep "note 1"
-RUN rpm -qp /tmp/foo.rpm --changelog | grep "note 2"
-RUN rpm -qp /tmp/foo.rpm --changelog | grep "note 3"
+RUN rpm -qp /tmp/foo.rpm --changelog | grep -E "^- note 1$"
+RUN rpm -qp /tmp/foo.rpm --changelog | grep -E "^- note 2$"
+RUN rpm -qp /tmp/foo.rpm --changelog | grep -E "^- note 3$"
 RUN rpm -q foo --changelog | grep "Carlos A Becker <pkg@carlosbecker.com>"
-RUN rpm -q foo --changelog | grep "note 1"
-RUN rpm -q foo --changelog | grep "note 2"
-RUN rpm -q foo --changelog | grep "note 3"
+RUN rpm -q foo --changelog | grep -E "^- note 1$"
+RUN rpm -q foo --changelog | grep -E "^- note 2$"
+RUN rpm -q foo --changelog | grep -E "^- note 3$"
 RUN rpm -e foo
diff --git a/deb/deb_test.go b/deb/deb_test.go
index 837e39b..4555c78 100644
--- a/deb/deb_test.go
+++ b/deb/deb_test.go
@@ -386,6 +386,37 @@ func TestMultilineFields(t *testing.T) {
 	assert.Equal(t, string(bts), w.String())
 }
 
+func TestDEBConventionalFileName(t *testing.T) {
+	info := &nfpm.Info{
+		Name: "testpkg",
+		Arch: "all",
+	}
+
+	testCases := []struct {
+		Version    string
+		Release    string
+		Prerelease string
+		Expected   string
+	}{
+		{Version: "1.2.3", Release: "", Prerelease: "",
+			Expected: fmt.Sprintf("%s_1.2.3_%s.deb", info.Name, info.Arch)},
+		{Version: "1.2.3", Release: "4", Prerelease: "",
+			Expected: fmt.Sprintf("%s_1.2.3-4_%s.deb", info.Name, info.Arch)},
+		{Version: "1.2.3", Release: "4", Prerelease: "5",
+			Expected: fmt.Sprintf("%s_1.2.3-4~5_%s.deb", info.Name, info.Arch)},
+		{Version: "1.2.3", Release: "", Prerelease: "5",
+			Expected: fmt.Sprintf("%s_1.2.3~5_%s.deb", info.Name, info.Arch)},
+	}
+
+	for _, testCase := range testCases {
+		info.Version = testCase.Version
+		info.Release = testCase.Release
+		info.Prerelease = testCase.Prerelease
+
+		assert.Equal(t, testCase.Expected, Default.ConventionalFileName(info))
+	}
+}
+
 func TestDebChangelogControl(t *testing.T) {
 	info := &nfpm.Info{
 		Name:        "changelog-test",
@@ -531,5 +562,5 @@ func readAndFormatAsDebChangelog(changelogFileName, packageName string) (string,
 		return "", err
 	}
 
-	return debChangelog, nil
+	return strings.TrimSpace(debChangelog) + "\n", nil
 }
diff --git a/rpm/rpm_test.go b/rpm/rpm_test.go
index 1c8c94d..cd9ab30 100644
--- a/rpm/rpm_test.go
+++ b/rpm/rpm_test.go
@@ -302,6 +302,37 @@ func TestRPMMultiArch(t *testing.T) {
 	}
 }
 
+func TestRPMConventionalFileName(t *testing.T) {
+	info := &nfpm.Info{
+		Name: "testpkg",
+		Arch: "noarch",
+	}
+
+	testCases := []struct {
+		Version    string
+		Release    string
+		Prerelease string
+		Expected   string
+	}{
+		{Version: "1.2.3", Release: "", Prerelease: "",
+			Expected: fmt.Sprintf("%s-1.2.3.%s.rpm", info.Name, info.Arch)},
+		{Version: "1.2.3", Release: "4", Prerelease: "",
+			Expected: fmt.Sprintf("%s-1.2.3-4.%s.rpm", info.Name, info.Arch)},
+		{Version: "1.2.3", Release: "4", Prerelease: "5",
+			Expected: fmt.Sprintf("%s-1.2.3-4~5.%s.rpm", info.Name, info.Arch)},
+		{Version: "1.2.3", Release: "", Prerelease: "5",
+			Expected: fmt.Sprintf("%s-1.2.3~5.%s.rpm", info.Name, info.Arch)},
+	}
+
+	for _, testCase := range testCases {
+		info.Version = testCase.Version
+		info.Release = testCase.Release
+		info.Prerelease = testCase.Prerelease
+
+		assert.Equal(t, testCase.Expected, Default.ConventionalFileName(info))
+	}
+}
+
 func TestRPMChangelog(t *testing.T) {
 	info := exampleInfo()
 	info.Changelog = "../testdata/changelog.yaml"