File tree 3 files changed +52
-2
lines changed
3 files changed +52
-2
lines changed Original file line number Diff line number Diff line change 4
4
"fmt"
5
5
"io"
6
6
7
+ "github.com/rcmachado/changelog/chg"
7
8
"github.com/rcmachado/changelog/parser"
8
9
"github.com/spf13/cobra"
9
10
)
@@ -20,13 +21,23 @@ func newLatestCmd(iostreams *IOStreams) *cobra.Command {
20
21
cmd .SilenceUsage = true
21
22
return fmt .Errorf ("There are no versions in the changelog yet" )
22
23
}
23
- if len (changelog .Versions ) == 1 && changelog .Versions [0 ].Name == "Unreleased" {
24
+ releasedVersions := releasedVersions (changelog )
25
+ if len (releasedVersions ) == 0 {
24
26
cmd .SilenceUsage = true
25
27
return fmt .Errorf ("There are no released versions in the changelog yet" )
26
28
}
27
- v := changelog . Versions [ 1 ]
29
+ v := releasedVersions [ 0 ]
28
30
io .WriteString (iostreams .Out , v .Name + "\n " )
29
31
return nil
30
32
},
31
33
}
32
34
}
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
+ }
Original file line number Diff line number Diff line change @@ -29,6 +29,27 @@ func TestLatestCmdShowsLatestReleasedVersion(t *testing.T) {
29
29
assert .Nil (t , err )
30
30
assert .Equal (t , expected , string (out .Bytes ()))
31
31
}
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
+ }
32
53
33
54
func TestLatestCmdError (t * testing.T ) {
34
55
changelog , err := ioutil .ReadFile ("testdata/empty-changelog.md" )
Original file line number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments