Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Added
- `latest` command to show latest release version number ([#5](https://github.com/cucumber/changelog/pull/5))

## [0.9.2] - 2021-11-11
### Added
Expand All @@ -14,6 +16,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
### Fixed
- config for releasing to Cucumber's docker account

## [0.9.1] - 2021-11-11
Failed release

## [0.9.0] - 2021-11-11
### Fixed
- Show command was broken ([#4](https://github.com/cucumber/changelog/issues/4))
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ $ changelog init --compare-url=https://github.com/myorg/myrepo/compare/abcdef...
$ changelog added "Initial commit"
# Create release
$ changelog release 0.1.0
# Show latest release number
$ changelog latest
=> 0.1.0
```

## Installation
Expand Down Expand Up @@ -78,6 +81,7 @@ Available Commands:
fmt Reformat the change log file
help Help about any command
init Initializes a new changelog
latest Show latest released version number
release Change Unreleased to [version]
removed Add item under 'Removed' section
security Add item under 'Security' section
Expand Down
10 changes: 10 additions & 0 deletions chg/changelog.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,13 @@ func (c *Changelog) Render(w io.Writer) {
w.Write(content)
}
}

func (c *Changelog) ReleasedVersions() []Version {
var result []Version
for _, version := range c.Versions {
if version.Name != "Unreleased" {
result = append(result, *version)
}
}
return result
}
33 changes: 33 additions & 0 deletions cmd/latest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package cmd

import (
"fmt"
"io"

"github.com/cucumber/changelog/parser"
"github.com/spf13/cobra"
)

func newLatestCmd(iostreams *IOStreams) *cobra.Command {
return &cobra.Command{
Use: "latest",
Short: "Show latest released version number",
Long: `Show version number for the top (released) entry in the changelog`,
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
changelog := parser.Parse(iostreams.In)
if len(changelog.Versions) == 0 {
cmd.SilenceUsage = true
return fmt.Errorf("There are no versions in the changelog yet")
}
releasedVersions := changelog.ReleasedVersions()
if len(releasedVersions) == 0 {
cmd.SilenceUsage = true
return fmt.Errorf("There are no released versions in the changelog yet")
}
v := releasedVersions[0]
io.WriteString(iostreams.Out, v.Name+"\n")
return nil
},
}
}
92 changes: 92 additions & 0 deletions cmd/latest_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package cmd

import (
"bytes"
"io/ioutil"
"testing"

"github.com/stretchr/testify/assert"
)

func TestLatestCmdShowsLatestReleasedVersion(t *testing.T) {
changelog, err := ioutil.ReadFile("testdata/show-changelog.md")
if err != nil {
t.Fatal(err)
}

expected := `1.0.0
`

out := new(bytes.Buffer)
iostreams := &IOStreams{
In: bytes.NewBuffer(changelog),
Out: out,
}

cmd := newLatestCmd(iostreams)
_, err = cmd.ExecuteC()

assert.Nil(t, err)
assert.Equal(t, expected, string(out.Bytes()))
}
func TestLatestCmdShowsLatestReleasedVersionEvenWhenNoUnreleased(t *testing.T) {
changelog, err := ioutil.ReadFile("testdata/legacy-changelog.md")
if err != nil {
t.Fatal(err)
}

expected := `1.0.0
`

out := new(bytes.Buffer)
iostreams := &IOStreams{
In: bytes.NewBuffer(changelog),
Out: out,
}

cmd := newLatestCmd(iostreams)
_, err = cmd.ExecuteC()

assert.Nil(t, err)
assert.Equal(t, expected, string(out.Bytes()))
}

func TestLatestCmdError(t *testing.T) {
changelog, err := ioutil.ReadFile("testdata/empty-changelog.md")
if err != nil {
t.Fatal(err)
}

out := new(bytes.Buffer)
iostreams := &IOStreams{
In: bytes.NewBuffer(changelog),
Out: out,
}

cmd := newLatestCmd(iostreams)
_, err = cmd.ExecuteC()

assert.Error(t, err)
expected := "There are no versions in the changelog yet"
assert.EqualError(t, err, expected)
}

func TestLatestCmdErrorWhenNoReleasedVersions(t *testing.T) {
changelog, err := ioutil.ReadFile("testdata/minimal-changelog.md")
if err != nil {
t.Fatal(err)
}

out := new(bytes.Buffer)
iostreams := &IOStreams{
In: bytes.NewBuffer(changelog),
Out: out,
}

cmd := newLatestCmd(iostreams)
_, err = cmd.ExecuteC()

assert.Error(t, err)
expected := "There are no released versions in the changelog yet"
assert.EqualError(t, err, expected)
}
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func init() {
newFmtCmd(ioStreams),
newReleaseCmd(ioStreams),
newShowCmd(ioStreams),
newLatestCmd(ioStreams),
)

manipulationCmds := newChangeTypeCmds(ioStreams)
Expand Down
Empty file added cmd/testdata/empty-changelog.md
Empty file.
18 changes: 18 additions & 0 deletions cmd/testdata/legacy-changelog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

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.

## [1.0.0] - 2020-01-08
### Added
- Item 1
- Item 2

### Changed
- Item 3

[1.0.0]: https://github.com/rcmachado/changelog/compare/ae761ff...1.0.0