Skip to content

Commit d39ed20

Browse files
committed
Add another test case and refactor
1 parent 4ee4b1f commit d39ed20

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

cmd/latest.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"io"
66

7+
"github.com/rcmachado/changelog/chg"
78
"github.com/rcmachado/changelog/parser"
89
"github.com/spf13/cobra"
910
)
@@ -20,13 +21,23 @@ func newLatestCmd(iostreams *IOStreams) *cobra.Command {
2021
cmd.SilenceUsage = true
2122
return fmt.Errorf("There are no versions in the changelog yet")
2223
}
23-
if len(changelog.Versions) == 1 && changelog.Versions[0].Name == "Unreleased" {
24+
releasedVersions := releasedVersions(changelog)
25+
if len(releasedVersions) == 0 {
2426
cmd.SilenceUsage = true
2527
return fmt.Errorf("There are no released versions in the changelog yet")
2628
}
27-
v := changelog.Versions[1]
29+
v := releasedVersions[0]
2830
io.WriteString(iostreams.Out, v.Name+"\n")
2931
return nil
3032
},
3133
}
3234
}
35+
36+
func releasedVersions(changelog *chg.Changelog) (result []chg.Version) {
37+
for _, version := range changelog.Versions {
38+
if version.Name != "Unreleased" {
39+
result = append(result, *version)
40+
}
41+
}
42+
return
43+
}

cmd/latest_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,27 @@ func TestLatestCmdShowsLatestReleasedVersion(t *testing.T) {
2929
assert.Nil(t, err)
3030
assert.Equal(t, expected, string(out.Bytes()))
3131
}
32+
func TestLatestCmdShowsLatestReleasedVersionEvenWhenNoUnreleased(t *testing.T) {
33+
changelog, err := ioutil.ReadFile("testdata/legacy-changelog.md")
34+
if err != nil {
35+
t.Fatal(err)
36+
}
37+
38+
expected := `1.0.0
39+
`
40+
41+
out := new(bytes.Buffer)
42+
iostreams := &IOStreams{
43+
In: bytes.NewBuffer(changelog),
44+
Out: out,
45+
}
46+
47+
cmd := newLatestCmd(iostreams)
48+
_, err = cmd.ExecuteC()
49+
50+
assert.Nil(t, err)
51+
assert.Equal(t, expected, string(out.Bytes()))
52+
}
3253

3354
func TestLatestCmdError(t *testing.T) {
3455
changelog, err := ioutil.ReadFile("testdata/empty-changelog.md")

cmd/testdata/legacy-changelog.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Changelog
2+
3+
All notable changes to this project will be documented in this file.
4+
5+
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
6+
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
7+
8+
This is an example of a legacy changelog we might have inherited that hasn't been properly formatted and doesn't have an Unreleased section.
9+
10+
## [1.0.0] - 2020-01-08
11+
### Added
12+
- Item 1
13+
- Item 2
14+
15+
### Changed
16+
- Item 3
17+
18+
[1.0.0]: https://github.com/rcmachado/changelog/compare/ae761ff...1.0.0

0 commit comments

Comments
 (0)