-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Jose Luis Vazquez Gonzalez <[email protected]>
- Loading branch information
Jose Luis Vazquez Gonzalez
committed
Dec 16, 2021
1 parent
d61d3cb
commit 699f3c0
Showing
6 changed files
with
178 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
0.3.58 | ||
0.3.59 |