-
Notifications
You must be signed in to change notification settings - Fork 14
/
main_test.go
140 lines (119 loc) · 3.05 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package main
import (
"os"
"os/exec"
"regexp"
"runtime"
"strings"
"testing"
"github.com/google/go-cmp/cmp"
)
var testExecutablePath = ""
func ensureExecutable(t *testing.T) string {
t.Helper()
if testExecutablePath == "" {
b, err := exec.Command("go", "build").CombinedOutput()
if err != nil {
t.Fatal("Compile error while building `changelog-from-release` executable:", err, ":", string(b))
}
if runtime.GOOS == "windows" {
testExecutablePath = `.\changelog-from-release.exe`
} else {
testExecutablePath = `./changelog-from-release`
}
}
return testExecutablePath
}
func TestGenerateChangelog(t *testing.T) {
exe := ensureExecutable(t)
b, err := os.ReadFile("CHANGELOG.md")
if err != nil {
panic(err)
}
want := strings.ReplaceAll(string(b), "\r\n", "\n") // Replace \r\n for Windows
b, err = exec.Command(exe).CombinedOutput()
have := string(b)
if err != nil {
t.Fatal(err, have, exe)
}
if want != have {
t.Fatalf("Generated output was different from CHANGELOG.md\n\n%s", cmp.Diff(have, want))
}
}
func TestVersion(t *testing.T) {
exe := ensureExecutable(t)
b, err := exec.Command(exe, "-v").CombinedOutput()
out := string(b)
if err != nil {
t.Fatal(err, out, exe)
}
r := regexp.MustCompile(`^v\d+\.\d+\.\d+\n$`)
if !r.Match(b) {
t.Fatalf("Version output is unexpected: %q", out)
}
}
func TestGenerateWithRemoteURL(t *testing.T) {
exe := ensureExecutable(t)
b, err := os.ReadFile("CHANGELOG.md")
if err != nil {
panic(err)
}
want := strings.ReplaceAll(string(b), "\r\n", "\n") // Replace \r\n for Windows
b, err = exec.Command(exe, "-r", "https://github.com/rhysd/changelog-from-release.git").CombinedOutput()
have := string(b)
if err != nil {
t.Fatal(err, have, exe)
}
if want != have {
t.Fatalf("Generated output was different from CHANGELOG.md\n\n%s", cmp.Diff(have, want))
}
}
func TestInvalidRemoteURL(t *testing.T) {
exe := ensureExecutable(t)
tests := []struct {
what string
input string
want string
}{
{
what: "invalid URL",
input: "hello",
want: `could not send HEAD request to Git remote URL "hello"`,
},
{
what: "file URL",
input: "file:///path/to/file.txt",
want: `unsupported protocol scheme "file"`,
},
{
what: "repository does not exist",
input: "https://github.com/rhysd/this-repository-does-not-exist-oops",
want: "404 Not Found",
},
}
for _, tc := range tests {
t.Run(tc.what, func(t *testing.T) {
b, err := exec.Command(exe, "-r", tc.input).CombinedOutput()
out := string(b)
if err == nil {
t.Fatal("Error did not occur", out, exe)
}
if !strings.Contains(out, tc.want) {
t.Fatalf("Output %q does not contain %q", out, tc.want)
}
})
}
}
func TestInvalidGitHubToken(t *testing.T) {
exe := ensureExecutable(t)
c := exec.Command(exe)
c.Env = append(c.Environ(), "GITHUB_TOKEN=invalid")
b, err := c.CombinedOutput()
out := string(b)
if err == nil {
t.Fatalf("error did not happen: %q", out)
}
if !strings.Contains(out, "401 Bad credentials") {
t.Fatalf("Wanted 401 bad credential error but got %q", out)
}
}