-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
121 lines (96 loc) · 2.33 KB
/
main_test.go
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
package main
import (
"fmt"
"io/fs"
"strings"
"testing"
"time"
)
func TestWriteHeader(t *testing.T) {
version := "v0.0.1"
expectedVersionLine := fmt.Sprintf(
"## %s (%s)",
version,
time.Now().Format("2006-01-02"),
)
var builder strings.Builder
writeHeader(version, &builder)
header := builder.String()
for _, expected := range []string{PLACEHOLDER, expectedVersionLine} {
if !strings.Contains(header, expected) {
t.Errorf("want %s, got %s", expected, header)
}
}
}
func TestGetEntries(t *testing.T) {
removed := []string{}
removeFile = func(path string) error {
removed = append(removed, path)
return nil
}
expectedRemovedFiled := 5
entries, err := getEntries("./testdata")
if err != nil {
t.Fatal(err)
}
if actual := len(entries["added"]); actual != 2 {
t.Errorf("want 2; got %d", actual)
}
expected := "The deprecated feature from the version X\n"
if actual := entries["removed"][0]; actual != expected {
t.Errorf("want %s; got %s", expected, actual)
}
if actual := len(removed); actual != expectedRemovedFiled {
t.Errorf("want %d, got %d", expectedRemovedFiled, actual)
}
}
func TestBuildReleaseNotes(t *testing.T) {
removeFile = func(path string) error { return nil }
version := "v0.0.1"
notes, err := buildReleaseNotes(version, "./testdata")
if err != nil {
t.Fatal(err)
}
for _, expected := range []string{version, "### Added", "### Fixed", "### Removed", "### Changed"} {
if !strings.Contains(notes, expected) {
t.Errorf("want %s, got %s", expected, notes)
}
}
}
func TestUpdateChangelog(t *testing.T) {
readFile = func(_ string) ([]byte, error) {
content := `
# Changelog
[changelogger-notes]::
## v0.0.1 (2020-05-27)
* First Version :tada:
`
return []byte(content), nil
}
actualChangelog := ""
writeFile = func(_ string, content []byte, _ fs.FileMode) error {
actualChangelog = string(content)
return nil
}
expectedChangelog := `
# Changelog
[changelogger-notes]::
## v0.0.2 (2020-05-27)
### Fixed
* A bug on some cool feature
## v0.0.1 (2020-05-27)
* First Version :tada:
`
notes := `
[changelogger-notes]::
## v0.0.2 (2020-05-27)
### Fixed
* A bug on some cool feature
`
if err := updateChangelog("some-path", notes); err != nil {
t.Fatal(err)
}
if actualChangelog != expectedChangelog {
t.Errorf("want %s, got %s", expectedChangelog, actualChangelog)
}
}