forked from tcnksm/ghr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli_test.go
144 lines (117 loc) · 3.82 KB
/
cli_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
141
142
143
144
package main
import (
"bytes"
"context"
"fmt"
"strings"
"testing"
"time"
)
func TestRun(t *testing.T) {
client := testGithubClient(t)
t.Parallel()
outStream, errStream := new(bytes.Buffer), new(bytes.Buffer)
cli := &CLI{outStream: outStream, errStream: errStream}
tag := "run"
command := fmt.Sprintf(
"ghr -username %s -repository %s %s %s", TestOwner, TestRepo, tag, TestDir)
args := strings.Split(command, " ")
if got, want := cli.Run(args), ExitCodeOK; got != want {
t.Fatalf("%q exits %d, want %d\n\n%s", command, got, want, errStream.String())
}
release, err := client.GetRelease(context.TODO(), tag)
if err != nil {
t.Fatalf("GetRelease failed: %s\n\n%s", err, outStream.String())
}
defer func() {
if err := client.DeleteRelease(context.TODO(), *release.ID); err != nil {
t.Fatal("DeleteRelease failed:", err)
}
if err := client.DeleteTag(context.TODO(), tag); err != nil {
t.Fatal("DeleteTag failed:", err)
}
}()
want := "==> Create a new release"
if got := outStream.String(); !strings.Contains(got, want) {
t.Fatalf("Run outputs %q, want %q", got, want)
}
if got, want := outStream.String(), "--> Uploading:"; !strings.Contains(got, want) {
t.Fatalf("Run outputs %q, want %q", got, want)
}
assets, err := client.ListAssets(context.TODO(), *release.ID)
if err != nil {
t.Fatal("ListAssets failed:", err)
}
if got, want := len(assets), 4; got != want {
t.Fatalf("ListAssets number = %d, want %d", got, want)
}
}
func TestRun_recreate(t *testing.T) {
client := testGithubClient(t)
outStream, errStream := new(bytes.Buffer), new(bytes.Buffer)
cli := &CLI{outStream: outStream, errStream: errStream}
tag := "run-recreate"
command := fmt.Sprintf(
"ghr -username %s -repository %s %s %s", TestOwner, TestRepo, tag, TestDir)
args := strings.Split(command, " ")
if got, want := cli.Run(args), ExitCodeOK; got != want {
t.Fatalf("%q exits %d, want %d\n\n%s", command, got, want, errStream.String())
}
// Prevent sending requests too much at same time
time.Sleep(3 * time.Second)
command = fmt.Sprintf(
"ghr -username %s -repository %s -recreate %s %s", TestOwner, TestRepo, tag, TestDir)
args = strings.Split(command, " ")
if got, want := cli.Run(args), ExitCodeOK; got != want {
t.Fatalf("%q exits %d, want %d\n\n%s", command, got, want, errStream.String())
}
release, err := client.GetRelease(context.TODO(), tag)
if err != nil {
t.Fatalf("GetRelease failed: %s\n\n%s", err, outStream.String())
}
defer func() {
time.Sleep(5 * time.Second)
if err := client.DeleteRelease(context.TODO(), *release.ID); err != nil {
t.Fatal("DeleteRelease failed:", err)
}
if err := client.DeleteTag(context.TODO(), tag); err != nil {
t.Fatal("DeleteTag failed:", err)
}
}()
want := "==> Recreate a release"
if got := outStream.String(); !strings.Contains(got, want) {
t.Fatalf("Run outputs %q, want %q", got, want)
}
}
func TestRun_versionFlag(t *testing.T) {
outStream, errStream := new(bytes.Buffer), new(bytes.Buffer)
cli := &CLI{outStream: outStream, errStream: errStream}
command := "ghr --version"
args := strings.Split(command, " ")
if got, want := cli.Run(args), ExitCodeOK; got != want {
t.Fatalf("%q exits %d, want %d", command, got, want)
}
want := fmt.Sprintf("ghr version v%s", Version)
got := outStream.String()
if !strings.Contains(got, want) {
t.Fatalf("%q output %q, want = %q", command, got, want)
}
}
func TestRetrieveOwnerName(t *testing.T) {
testCases := []struct {
in, expected string
}{
{"git@github.com:tcnksm/ghr.git", "tcnksm"},
{"https://github.com/motemen/ghq.git", "motemen"},
{"git://github.com/Songmu/ghg.git", "Songmu"},
{"hoge", ""},
}
for _, tc := range testCases {
t.Run(tc.in, func(t *testing.T) {
out := retrieveOwnerName(tc.in)
if out != tc.expected {
t.Errorf("out: %s, expected: %s", out, tc.expected)
}
})
}
}