Skip to content

Commit

Permalink
feat: version release/prerelease options (#111)
Browse files Browse the repository at this point in the history
* feat: version release/prerelease options

* fix: review

* fix: lint

* fix: damn linters

* fix: acceptance tests

Co-authored-by: Carlos Alexandro Becker <[email protected]>
  • Loading branch information
astorath and caarlos0 authored Feb 18, 2020
1 parent ebe75b0 commit 4c6511a
Show file tree
Hide file tree
Showing 9 changed files with 164 additions and 16 deletions.
2 changes: 1 addition & 1 deletion acceptance/testdata/rpm.env-var-version.dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ FROM fedora
ARG package
COPY ${package} /tmp/foo.rpm
ENV EXPECTVER="Version : 1.0.0" \
EXPECTREL="Release : 0.1.b1"
EXPECTREL="Release : 0.1.0.1.b1"
RUN rpm -qpi /tmp/foo.rpm | sed -e 's/ \+/ /g' | grep "Version" > found.ver
RUN rpm -qpi /tmp/foo.rpm | sed -e 's/ \+/ /g' | grep "Release" > found.rel
RUN export FOUND_VER="$(cat found.ver)" && \
Expand Down
3 changes: 2 additions & 1 deletion deb/deb.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,8 @@ const controlTemplate = `
{{- /* Mandatory fields */ -}}
Package: {{.Info.Name}}
Version: {{ if .Info.Epoch}}{{ .Info.Epoch }}:{{ end }}{{.Info.Version}}
{{- if .Info.Release}}~{{ .Info.Release }}{{- end }}
{{- if .Info.Release}}-{{ .Info.Release }}{{- end }}
{{- if .Info.Prerelease}}~{{ .Info.Prerelease }}{{- end }}
{{- if .Info.Deb.VersionMetadata}}+{{ .Info.Deb.VersionMetadata }}{{- end }}
Section: {{.Info.Section}}
Priority: {{.Info.Priority}}
Expand Down
54 changes: 54 additions & 0 deletions deb/deb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"fmt"
"io/ioutil"
"strconv"
"strings"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -75,13 +76,66 @@ func TestDeb(t *testing.T) {
}
}

func extractDebVersion(deb *bytes.Buffer) string {
for _, s := range strings.Split(deb.String(), "\n") {
if strings.Contains(s, "Version: ") {
return strings.TrimPrefix(s, "Version: ")
}
}
return ""
}

func TestDebVersionWithDash(t *testing.T) {
info := exampleInfo()
info.Version = "1.0.0-beta"
var err = Default.Package(info, ioutil.Discard)
assert.NoError(t, err)
}

func TestDebVersion(t *testing.T) {
info := exampleInfo()
info.Version = "1.0.0" //nolint:golint,goconst
var buf bytes.Buffer
var err = writeControl(&buf, controlData{info, 0})
assert.NoError(t, err)
var v = extractDebVersion(&buf)
assert.Equal(t, "1.0.0", v)
}

func TestDebVersionWithRelease(t *testing.T) {
info := exampleInfo()
info.Version = "1.0.0" //nolint:golint,goconst
info.Release = "1"
var buf bytes.Buffer
var err = writeControl(&buf, controlData{info, 0})
assert.NoError(t, err)
var v = extractDebVersion(&buf)
assert.Equal(t, "1.0.0-1", v)
}

func TestDebVersionWithPrerelease(t *testing.T) {
info := exampleInfo()
info.Version = "1.0.0" //nolint:golint,goconst
info.Prerelease = "1"
var buf bytes.Buffer
var err = writeControl(&buf, controlData{info, 0})
assert.NoError(t, err)
var v = extractDebVersion(&buf)
assert.Equal(t, "1.0.0~1", v)
}

func TestDebVersionWithReleaseAndPrerelease(t *testing.T) {
info := exampleInfo()
info.Version = "1.0.0" //nolint:golint,goconst
info.Release = "2"
info.Prerelease = "rc1"
var buf bytes.Buffer
var err = writeControl(&buf, controlData{info, 0})
assert.NoError(t, err)
var v = extractDebVersion(&buf)
assert.Equal(t, "1.0.0-2~rc1", v)
}

func TestControl(t *testing.T) {
var w bytes.Buffer
assert.NoError(t, writeControl(&w, controlData{
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ github.com/imdario/mergo v0.3.8 h1:CGgOkSJeqMRmt0D9XLWExdT4m4F1vd3FV3VPt+0VxkQ=
github.com/imdario/mergo v0.3.8/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1 h1:VkoXIwSboBpnk99O/KFauAEILuNHv5DVFKZMBN/gUgw=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
Expand All @@ -48,9 +49,12 @@ github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8/go.mod h1:HUYIGzjTL3rfEspMx
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191002192127-34f69633bfdc h1:c0o/qxkaO2LF5t6fQrT4b5hzyggAkLLlCUjqfRxd8Q4=
golang.org/x/crypto v0.0.0-20191002192127-34f69633bfdc/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3 h1:0GoQqolDA55aaLxZyTzK/Y2ePZzZTUrRacwib7cNsYQ=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d h1:+R4KGOnez64A81RvjARKc4UT5/tI9ujCIVX+P5KiHuI=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
10 changes: 10 additions & 0 deletions internal/helpers/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Package helpers provides common helper methods
package helpers

import "strconv"

// IsInt returns true if s is int, false otherwise
func IsInt(s string) bool {
_, err := strconv.Atoi(s)
return err == nil
}
9 changes: 7 additions & 2 deletions nfpm.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"sync"

"github.com/Masterminds/semver/v3"
"github.com/goreleaser/nfpm/internal/helpers"
"github.com/imdario/mergo"
"github.com/pkg/errors"

Expand Down Expand Up @@ -111,6 +112,7 @@ type Info struct {
Epoch string `yaml:"epoch,omitempty"`
Version string `yaml:"version,omitempty"`
Release string `yaml:"release,omitempty"`
Prerelease string `yaml:"prerelease,omitempty"`
Section string `yaml:"section,omitempty"`
Priority string `yaml:"priority,omitempty"`
Maintainer string `yaml:"maintainer,omitempty"`
Expand Down Expand Up @@ -196,11 +198,14 @@ func WithDefaults(info *Info) *Info {
// and support proper ordering for both rpm and deb
if v, err := semver.NewVersion(info.Version); err == nil {
info.Version = fmt.Sprintf("%d.%d.%d", v.Major(), v.Minor(), v.Patch())
if info.Release == "" {
// maybe we should concat the previous info.Release with v.Prerelease?
if info.Release == "" && helpers.IsInt(v.Prerelease()) {
info.Release = v.Prerelease()
}
if info.Prerelease == "" && !helpers.IsInt(v.Prerelease()) {
info.Prerelease = v.Prerelease()
}
info.Deb.VersionMetadata = v.Metadata()
}

return info
}
50 changes: 38 additions & 12 deletions nfpm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,51 @@ func TestGet(t *testing.T) {

func TestDefaultsVersion(t *testing.T) {
info := &Info{
Version: "v2.4.1-beta3",
Version: "v1.0.0",
}
info = WithDefaults(info)
assert.NotEmpty(t, info.Bindir)
assert.NotEmpty(t, info.Platform)
assert.Equal(t, "2.4.1", info.Version)
assert.Equal(t, "beta3", info.Release)
}
assert.Equal(t, "1.0.0", info.Version)
assert.Equal(t, "", info.Release)
assert.Equal(t, "", info.Prerelease)

func TestDefaultsVersionWithRelease(t *testing.T) {
info := &Info{
Version: "v2.4.1-beta3",
Release: "4",
info = &Info{
Version: "v1.0.0-rc1",
}
info = WithDefaults(info)
assert.NotEmpty(t, info.Bindir)
assert.NotEmpty(t, info.Platform)
assert.Equal(t, "2.4.1", info.Version)
assert.Equal(t, "4", info.Release)
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",
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)

}

func TestDefaults(t *testing.T) {
Expand Down
5 changes: 5 additions & 0 deletions rpm/rpm.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ func buildRPMMeta(info *nfpm.Info) (*rpmpack.RPMMetaData, error) {
return nil, err
}

var release = defaultTo(info.Release, "1")
if info.Prerelease != "" {
release = fmt.Sprintf("%s.%s", defaultTo(info.Release, "0.1"), info.Prerelease)
}

return &rpmpack.RPMMetaData{
Name: info.Name,
Summary: strings.Split(info.Description, "\n")[0],
Expand Down
43 changes: 43 additions & 0 deletions rpm/rpm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,49 @@ func TestWithRPMTags(t *testing.T) {
assert.Equal(t, info.Description, description)
}


func TestRPMVersion(t *testing.T) {
info := exampleInfo()
info.Version = "1.0.0" //nolint:golint,goconst
meta, err := buildRPMMeta(info)
assert.NoError(t, err)
assert.Equal(t, "1.0.0", meta.Version)
assert.Equal(t, "1", meta.Release)
}

func TestRPMVersionWithRelease(t *testing.T) {
info := exampleInfo()
info.Version = "1.0.0" //nolint:golint,goconst
info.Release = "2"
meta, err := buildRPMMeta(info)
assert.NoError(t, err)
assert.Equal(t, "1.0.0", meta.Version)
assert.Equal(t, "2", meta.Release)
}

func TestRPMVersionWithPrerelease(t *testing.T) {
// https://fedoraproject.org/wiki/Package_Versioning_Examples#Complex_versioning_examples
info := exampleInfo()
info.Version = "1.0.0" //nolint:golint,goconst
info.Prerelease = "rc1"
meta, err := buildRPMMeta(info)
assert.NoError(t, err)
assert.Equal(t, "1.0.0", meta.Version)
assert.Equal(t, "0.1.rc1", meta.Release)
}

func TestRPMVersionWithReleaseAndPrerelease(t *testing.T) {
// https://fedoraproject.org/wiki/Package_Versioning_Examples#Complex_versioning_examples
info := exampleInfo()
info.Version = "1.0.0" //nolint:golint,goconst
info.Release = "0.2"
info.Prerelease = "rc1"
meta, err := buildRPMMeta(info)
assert.NoError(t, err)
assert.Equal(t, "1.0.0", meta.Version)
assert.Equal(t, "0.2.rc1", meta.Release)
}

func TestWithInvalidEpoch(t *testing.T) {
f, err := ioutil.TempFile("", "test.rpm")
defer func() {
Expand Down

0 comments on commit 4c6511a

Please sign in to comment.