1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
|
package main
import (
"bytes"
"os"
"path/filepath"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
const _changelog = `
# Changelog
All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## Unreleased
### Added
- Upcoming feature
## [1.0.0] - 2021-08-18
Initial stable release.
[1.0.0]: http://example.com/1.0.0
## 0.3.0 - 2020-09-01
### Removed
- deprecated functionality
### Fixed
- bug
## [0.2.0] - 2020-08-19
### Added
- Fancy new feature.
[0.2.0]: http://example.com/0.2.0
## 0.1.0 - 2020-08-18
Initial release.
`
func TestMain(t *testing.T) {
t.Parallel()
changelog := filepath.Join(t.TempDir(), "CHANGELOG.md")
require.NoError(t,
os.WriteFile(changelog, []byte(_changelog), 0o644))
tests := []struct {
desc string
version string
want string // expected changelog
wantErr string // expected error, if any
}{
{
desc: "not found",
version: "0.1.1",
wantErr: `changelog for "0.1.1" not found`,
},
{
desc: "missing version",
wantErr: "please provide a version",
},
{
desc: "non-standard body",
version: "1.0.0",
want: joinLines(
"## [1.0.0] - 2021-08-18",
"Initial stable release.",
"",
"[1.0.0]: http://example.com/1.0.0",
),
},
{
desc: "unlinked",
version: "0.3.0",
want: joinLines(
"## 0.3.0 - 2020-09-01",
"### Removed",
"- deprecated functionality",
"",
"### Fixed",
"- bug",
),
},
{
desc: "end of file",
version: "0.1.0",
want: joinLines(
"## 0.1.0 - 2020-08-18",
"",
"Initial release.",
),
},
{
desc: "linked",
version: "0.2.0",
want: joinLines(
"## [0.2.0] - 2020-08-19",
"### Added",
"- Fancy new feature.",
"",
"[0.2.0]: http://example.com/0.2.0",
),
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.desc, func(t *testing.T) {
t.Parallel()
var stdout, stderr bytes.Buffer
defer func() {
assert.Empty(t, stderr.String(), "stderr should be empty")
}()
err := (&mainCmd{
Stdout: &stdout,
Stderr: &stderr,
}).Run([]string{"-i", changelog, tt.version})
if len(tt.wantErr) > 0 {
require.Error(t, err)
assert.Contains(t, err.Error(), tt.wantErr)
return
}
require.NoError(t, err)
assert.Equal(t, tt.want, stdout.String())
})
}
}
// Join a bunch of lines with a trailing newline.
func joinLines(lines ...string) string {
return strings.Join(lines, "\n") + "\n"
}
|