Skip to content

Commit

Permalink
test: add a test for newVersion
Browse files Browse the repository at this point in the history
  • Loading branch information
johnmanjiro13 committed Jun 5, 2022
1 parent 39e3818 commit fbd69a2
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
6 changes: 4 additions & 2 deletions bump/bumper.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func (b *bumper) currentVersion() (*semver.Version, bool, error) {
sout, eout, err := b.gh.ViewRelease(b.repository, b.isCurrent)
if err != nil {
if strings.Contains(eout.String(), "HTTP 404: Not Found") {
current, err := newVersion()
current, err := newVersion(os.Stdin, os.Stdout)
if err != nil {
return nil, isInitial, err
}
Expand All @@ -174,7 +174,7 @@ func (b *bumper) currentVersion() (*semver.Version, bool, error) {
return current, isInitial, nil
}

func newVersion() (*semver.Version, error) {
func newVersion(sin io.ReadCloser, sout io.WriteCloser) (*semver.Version, error) {
validate := func(input string) error {
_, err := semver.NewVersion(input)
if err != nil {
Expand All @@ -186,6 +186,8 @@ func newVersion() (*semver.Version, error) {
prompt := promptui.Prompt{
Label: "New version",
Validate: validate,
Stdin: sin,
Stdout: sout,
}
result, err := prompt.Run()
if err != nil {
Expand Down
15 changes: 14 additions & 1 deletion bump/bumper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,14 @@ func (m *mockWriteCloser) Close() error {
return nil
}

func TestNewVersion(t *testing.T) {
sin := io.NopCloser(strings.NewReader("v0.1.0\n"))
sout := &mockWriteCloser{bytes.Buffer{}}
newVer, err := bump.NewVersion(sin, sout)
assert.NoError(t, err)
assert.Equal(t, semver.MustParse("v0.1.0"), newVer)
}

func TestNextVersion(t *testing.T) {
sin := io.NopCloser(strings.NewReader(string(arrowDownAndEnter)))
sout := &mockWriteCloser{bytes.Buffer{}}
Expand Down Expand Up @@ -252,6 +260,7 @@ func TestIncrementVersion(t *testing.T) {
tests := map[string]struct {
bumpType string
want *semver.Version
wantErr error
}{
"major": {
bumpType: "major",
Expand All @@ -265,12 +274,16 @@ func TestIncrementVersion(t *testing.T) {
bumpType: "patch",
want: semver.MustParse("v0.1.1"),
},
"invalid": {
bumpType: "invalid",
wantErr: fmt.Errorf("invalid type"),
},
}

for name, tt := range tests {
t.Run(name, func(t *testing.T) {
got, err := bump.IncrementVersion(current, tt.bumpType)
assert.NoError(t, err)
assert.Equal(t, tt.wantErr, err)
assert.Equal(t, tt.want, got)
})
}
Expand Down
1 change: 1 addition & 0 deletions bump/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var (
ListReleases = (*bumper).listReleases
CreateRelease = (*bumper).createRelease
CurrentVersion = (*bumper).currentVersion
NewVersion = newVersion
NextVersion = nextVersion
IncrementVersion = incrementVersion
Approve = approve
Expand Down

0 comments on commit fbd69a2

Please sign in to comment.