Skip to content

Commit

Permalink
version: adapt FormatedVersion to use String
Browse files Browse the repository at this point in the history
Since FormatedVersion was essentially the same thing as `String` with
the extra GitCommit if defined, we change its implementation to rely on
the code committed for String.
  • Loading branch information
lbajolet-hashicorp committed Apr 15, 2024
1 parent 2a28d48 commit ddbda65
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 14 deletions.
6 changes: 3 additions & 3 deletions plugin/set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ func TestSet(t *testing.T) {
set.RegisterProvisioner("example-2", new(MockProvisioner))
set.RegisterDatasource("example", new(MockDatasource))
set.RegisterDatasource("example-2", new(MockDatasource))
set.SetVersion(pluginVersion.InitializePluginVersion(
"1.1.1", ""))
set.SetVersion(pluginVersion.NewPluginVersion(
"1.1.1", "", ""))

outputDesc := set.description()

sdkVersion := pluginVersion.InitializePluginVersion(pluginVersion.Version, pluginVersion.VersionPrerelease)
sdkVersion := pluginVersion.NewPluginVersion(pluginVersion.Version, pluginVersion.VersionPrerelease, "")
if diff := cmp.Diff(SetDescription{
Version: "1.1.1",
SDKVersion: sdkVersion.String(),
Expand Down
20 changes: 10 additions & 10 deletions version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
package version

import (
"bytes"
"fmt"

"github.com/hashicorp/go-version"
Expand Down Expand Up @@ -81,6 +80,7 @@ func NewPluginVersion(vers, versionPrerelease, versionMetadata string) *PluginVe
if versionMetadata != "" {
versionRawString = fmt.Sprintf("%s+%s", versionRawString, versionMetadata)
}

// This call initializes the SemVer to make sure that if Packer crashes due
// to an invalid SemVer it's at the very beginning of the Packer run.
semVer := version.Must(version.NewVersion(versionRawString))
Expand Down Expand Up @@ -112,18 +112,18 @@ type PluginVersion struct {
func (p *PluginVersion) SetMetadata(meta string) {
p.versionMetadata = meta
}

func (p *PluginVersion) FormattedVersion() string {
var versionString bytes.Buffer
fmt.Fprintf(&versionString, "%s", p.version)
if p.versionPrerelease != "" {
fmt.Fprintf(&versionString, "-%s", p.versionPrerelease)

if GitCommit != "" {
fmt.Fprintf(&versionString, " (%s)", GitCommit)
}
versionString := p.semVer.String()

// Given there could be some metadata already, we add the commit to the
// reported version as part of the metadata, with a `-` spearator if
// the metadata is already there, otherwise we make it the metadata
if GitCommit != "" {
versionString = fmt.Sprintf("%s (%s)", versionString, GitCommit)
}

return versionString.String()
return versionString
}

func (p *PluginVersion) SemVer() *version.Version {
Expand Down
9 changes: 8 additions & 1 deletion version/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,13 @@ func TestFormattedVersionString(t *testing.T) {
ver := InitializePluginVersion("1.0.0", "dev")
formatted := ver.FormattedVersion()
if formatted != expectedVersion {
t.Fatalf("Expected formatted version %q; got %q", expectedVersion, formatted)
t.Errorf("Expected formatted version %q; got %q", expectedVersion, formatted)
}

expectedVersion = fmt.Sprintf("1.0.0-dev+meta (%s)", GitCommit)
ver = NewPluginVersion("1.0.0", "dev", "meta")
formatted = ver.FormattedVersion()
if formatted != expectedVersion {
t.Errorf("Expected formatted version %q; got %q", expectedVersion, formatted)
}
}

0 comments on commit ddbda65

Please sign in to comment.