forked from rhysd/notes-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmd_selfupdate_test.go
More file actions
138 lines (113 loc) · 2.77 KB
/
cmd_selfupdate_test.go
File metadata and controls
138 lines (113 loc) · 2.77 KB
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
package notes
import (
"bytes"
"github.com/fatih/color"
"os"
"os/exec"
"regexp"
"runtime"
"strings"
"testing"
)
func onCI() bool {
_, ok := os.LookupEnv("GITHUB_ACTIONS")
return ok
}
func maySkipTestForSelfupdate(t *testing.T) {
if onCI() && (os.Getenv("GITHUB_TOKEN") == "" || runtime.GOOS != "linux") {
t.Skip("Skipping tests for selfupdate on CI without $GITHUB_TOKEN since GitHub API almost expires on CI without API token. macOS is also skipped since they fail due to 'signal: killed' error")
}
}
func TestSelfupdateUpdateToLatest(t *testing.T) {
maySkipTestForSelfupdate(t)
oldV := Version
oldC := color.NoColor
defer func() {
Version = oldV
color.NoColor = oldC
}()
Version = "0.0.1"
color.NoColor = true
var buf bytes.Buffer
cmd := &SelfupdateCmd{
Dry: true,
Out: &buf,
}
// Check only dry-run since selfupdate replaces test executable and it maybe have some
// unexpected side effects.
if err := cmd.Do(); err != nil {
t.Fatal(err)
}
out := buf.String()
if !strings.Contains(out, "New version v") {
t.Error("New version is not output", out)
}
if !strings.Contains(out, "Release Note:") {
t.Error("Release note is not output", out)
}
}
func TestSelfupdateCurrentIsLatest(t *testing.T) {
maySkipTestForSelfupdate(t)
var buf bytes.Buffer
cmd := &SelfupdateCmd{
Dry: true,
Out: &buf,
}
if err := cmd.Do(); err != nil {
t.Fatal(err)
}
out := buf.String()
if !strings.Contains(out, "Current version is the latest") {
t.Fatal("Unexpected output:", out)
}
}
func TestSelfupdateUpdateSuccessfully(t *testing.T) {
maySkipTestForSelfupdate(t)
if !onCI() {
t.Skip("Run test for successful selfupdate only on CI since it's one-shot due to replacing test binary")
}
exe, err := os.Executable()
panicIfErr(err)
var buf bytes.Buffer
cmd := &SelfupdateCmd{
Slug: "rhysd-test/notes-cli.test",
Out: &buf,
}
if err := cmd.Do(); err != nil {
t.Fatal(err)
}
r := regexp.MustCompile(`New version v(\d+\.\d+\.\d+)`)
sub := r.FindSubmatch(buf.Bytes())
if sub == nil {
t.Fatal("Version did not update:", buf.String())
}
v := string(sub[1])
b, err := exec.Command(exe, "--version").CombinedOutput()
out := string(b)
if err != nil {
t.Fatal(err, out)
}
if !strings.Contains(out, v) {
t.Fatal("Unexpected version. Wanted:", v, "but have output:", out)
}
}
func TestSelfupdateUpdateError(t *testing.T) {
maySkipTestForSelfupdate(t)
if !onCI() {
t.Skip("Run test for selfupdate failure only on CI since takes time")
}
oldV := Version
defer func() {
Version = oldV
}()
Version = "0.0.1"
var buf bytes.Buffer
cmd := &SelfupdateCmd{Out: &buf}
err := cmd.Do()
if err == nil {
t.Fatal("Error did not occur")
}
if !strings.Contains(err.Error(), "the command is not found in") {
t.Fatal("Unexpected error:", err)
}
}