From 699f3c037e30e3cbf3e6887f50a975b2aa447926 Mon Sep 17 00:00:00 2001 From: Jose Luis Vazquez Gonzalez Date: Thu, 16 Dec 2021 11:29:43 +0100 Subject: [PATCH] Allow to bump custom versions Signed-off-by: Jose Luis Vazquez Gonzalez --- bump-version.sh | 4 +- next-semver/go.mod | 5 -- next-semver/go.sum | 2 - next-semver/main.go | 59 ++++++++++++++++--- next-semver/main_test.go | 123 +++++++++++++++++++++++++++++++++++++++ version | 2 +- 6 files changed, 178 insertions(+), 17 deletions(-) delete mode 100644 next-semver/go.mod delete mode 100644 next-semver/go.sum create mode 100644 next-semver/main_test.go diff --git a/bump-version.sh b/bump-version.sh index c390a6d8..a1e01b5c 100755 --- a/bump-version.sh +++ b/bump-version.sh @@ -2,9 +2,11 @@ set -euo pipefail +desired_version=${1:-} + old_version=$(cat version) (cd next-semver && go build .) -version=$(./next-semver/next-semver "${old_version}") +version=$(./next-semver/next-semver "${old_version}" "${desired_version}") echo "${version}" > version git add version git commit -s -m "Bump version to ${version}" diff --git a/next-semver/go.mod b/next-semver/go.mod deleted file mode 100644 index cf77a39d..00000000 --- a/next-semver/go.mod +++ /dev/null @@ -1,5 +0,0 @@ -module example.com/versions/next-semver - -go 1.17 - -require github.com/Masterminds/semver/v3 v3.1.1 // indirect diff --git a/next-semver/go.sum b/next-semver/go.sum deleted file mode 100644 index 471bde90..00000000 --- a/next-semver/go.sum +++ /dev/null @@ -1,2 +0,0 @@ -github.com/Masterminds/semver/v3 v3.1.1 h1:hLg3sBzpNErnxhQtUy/mmLR2I9foDujNK030IGemrRc= -github.com/Masterminds/semver/v3 v3.1.1/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs= diff --git a/next-semver/main.go b/next-semver/main.go index f200da38..12fb968b 100644 --- a/next-semver/main.go +++ b/next-semver/main.go @@ -1,19 +1,62 @@ +// Copyright 2021 VMware, Inc. +// SPDX-License-Identifier: BSD-2-Clause + package main import ( + "errors" "fmt" "os" - semverv3 "github.com/Masterminds/semver/v3" + semver3 "github.com/Masterminds/semver/v3" ) -func main() { - if len(os.Args) != 2 { - fmt.Printf("Usage: %s {semver-version}\n$ %s 1.2.3\n", os.Args[0], os.Args[0]) - os.Exit(-1) +var ( + // ErrBadInputs when the number of CLI inputs are wrong + ErrBadInputs = errors.New("bad inputs") + + // ErrBadVersionBump when the desired version is not after the previous one + ErrBadVersionBump = errors.New("bad version bump") +) + +func nextSemVer(previous, desired *semver3.Version) (*semver3.Version, error) { + if desired == nil { + next := previous.IncPatch() + return &next, nil + } + if !desired.GreaterThan(previous) { + return nil, fmt.Errorf("%w: %v is not after %v", ErrBadVersionBump, desired, previous) + } + return desired, nil +} + +func mainE(args ...string) error { + if len(args) < 2 || len(args) > 3 { + return fmt.Errorf("%w\nusage: %s {semver-version} [{desired_semver}]\n$ %s 1.2.3", + ErrBadInputs, args[0], args[0]) + } + previous, err := semver3.NewVersion(args[1]) + if err != nil { + return fmt.Errorf("failed to parse previous version %q: %w", args[1], err) + } + var desired *semver3.Version + if len(args) > 2 && args[2] != "" { + desired, err = semver3.NewVersion(args[2]) + if err != nil { + return fmt.Errorf("failed to parse desired version %q: %w", args[2], err) + } + } + next, err := nextSemVer(previous, desired) + if err != nil { + return err } - v := os.Args[1] - version := semverv3.MustParse(v) - next := version.IncPatch() fmt.Println(next) + return nil +} + +func main() { + if err := mainE(os.Args...); err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } } diff --git a/next-semver/main_test.go b/next-semver/main_test.go new file mode 100644 index 00000000..8c1c8490 --- /dev/null +++ b/next-semver/main_test.go @@ -0,0 +1,123 @@ +// Copyright 2021 VMware, Inc. +// SPDX-License-Identifier: BSD-2-Clause + +package main + +import ( + "errors" + "fmt" + "testing" + + semver3 "github.com/Masterminds/semver/v3" +) + +var nextSemVerTests = []struct { + previous, desired string + expected string +}{ + { + previous: "0.0.0", + desired: "", + expected: "0.0.1", + }, + { + previous: "0.0.1", + desired: "", + expected: "0.0.2", + }, + { + previous: "0.0.1", + desired: "0.1.0", + expected: "0.1.0", + }, + { + previous: "0.9.1", + desired: "1.0.0", + expected: "1.0.0", + }, + { + previous: "0.9.9", + desired: "", + expected: "0.9.10", + }, + { + previous: "0.9.9-rc", + desired: "", + expected: "0.9.9", + }, +} + +func version(v string) *semver3.Version { + if v == "" { + return nil + } + return semver3.MustParse(v) +} + +func TestNextSemVer(t *testing.T) { + for _, tc := range nextSemVerTests { + testName := fmt.Sprintf("(%q, %q)->%q", tc.previous, tc.desired, tc.expected) + t.Run(testName, func(t *testing.T) { + next, err := nextSemVer(version(tc.previous), version(tc.desired)) + if err != nil { + t.Fatalf("unexpected error: %s", err) + } + got := next.String() + want := tc.expected + if got != want { + t.Fatalf("want %s got %s", want, got) + } + }) + } +} + +var mainEIssues = []struct { + args []string + expected error +}{ + { + args: []string{"next-semver", "0.0.0"}, + expected: nil, + }, + { + args: []string{"next-semver", "0.0.1", "0.0.0"}, + expected: ErrBadVersionBump, + }, + { + args: []string{"next-semver", "0.0.1", "0.1.0"}, + expected: nil, + }, + { + args: []string{"next-semver", "1.0.1", "0.9.0"}, + expected: ErrBadVersionBump, + }, + { + args: []string{"next-semver", "blah1.0.1", "0.9.0"}, + expected: semver3.ErrInvalidSemVer, + }, + { + args: []string{"next-semver", "1.0.1", "blah0.9.0"}, + expected: semver3.ErrInvalidSemVer, + }, + { + args: []string{"next-semver", "1.0.1", "0.9.0.1"}, + expected: semver3.ErrInvalidSemVer, + }, + { + args: []string{"next-semver"}, + expected: ErrBadInputs, + }, +} + +func TestMainE(t *testing.T) { + for _, tc := range mainEIssues { + testName := fmt.Sprintf("%v->%v", tc.args, tc.expected) + t.Run(testName, func(t *testing.T) { + got := mainE(tc.args...) + want := tc.expected + if !errors.Is(got, want) { + t.Fatalf("want %v got %v", want, got) + } + }) + } +} diff --git a/version b/version index 6d716d90..a6d5224c 100644 --- a/version +++ b/version @@ -1 +1 @@ -0.3.58 +0.3.59