Repository navigation
Expand file tree
/
Copy pathcli_test.go
More file actions
101 lines (87 loc) · 2.83 KB
/
Copy pathcli_test.go
File metadata and controls
101 lines (87 loc) · 2.83 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
package main
import (
"bytes"
"errors"
"net/http"
"strings"
"testing"
)
func TestSingleIDArg(t *testing.T) {
t.Parallel()
id, err := singleIDArg("fix", []string{":abc-123"})
if err != nil {
t.Fatalf("singleIDArg returned error: %v", err)
}
if id != "abc-123" {
t.Fatalf("singleIDArg id = %q, want %q", id, "abc-123")
}
if _, err := singleIDArg("fix", nil); err == nil || !strings.Contains(err.Error(), "requires an error id") {
t.Fatalf("missing id error = %v", err)
}
if _, err := singleIDArg("fix", []string{"one", "two"}); err == nil || !strings.Contains(err.Error(), "exactly one") {
t.Fatalf("extra id error = %v", err)
}
}
func TestPrintUsage(t *testing.T) {
t.Parallel()
var out bytes.Buffer
printUsage(&out)
for _, want := range []string{"prilog projects", "prilog create-project", "prilog use-project", "prilog add-repo", "prilog status", "prilog issue import", "prilog pr <id>"} {
if !strings.Contains(out.String(), want) {
t.Fatalf("usage missing %q:\n%s", want, out.String())
}
}
if strings.Contains(out.String(), "prilog init") {
t.Fatal("removed init command remains in help")
}
if strings.Contains(out.String(), "api-url") {
t.Fatalf("usage exposed internal API override:\n%s", out.String())
}
}
func TestPrintVersion(t *testing.T) {
t.Parallel()
var out bytes.Buffer
printVersion(&out)
if got := strings.TrimSpace(out.String()); got != "prilog dev" {
t.Fatalf("version = %q, want %q", got, "prilog dev")
}
}
func TestRunWithIOHelp(t *testing.T) {
t.Parallel()
for _, command := range []string{"help", "--help", "-h"} {
t.Run(command, func(t *testing.T) {
var out bytes.Buffer
if err := runWithIO([]string{command}, nil, &out, nil); err != nil {
t.Fatalf("help returned error: %v", err)
}
for _, expected := range []string{"Usage:", "prilog status", "prilog list", "prilog fix", "prilog pr"} {
if !strings.Contains(out.String(), expected) {
t.Fatalf("help output missing %q: %s", expected, out.String())
}
}
})
}
}
func TestRunWithIORejectsUnexpectedArgs(t *testing.T) {
t.Parallel()
var out bytes.Buffer
err := runWithIO([]string{"status", "extra"}, nil, &out, nil)
if err == nil || !strings.Contains(err.Error(), "status does not accept arguments") {
t.Fatalf("unexpected status arg error = %v", err)
}
}
func TestDecodeAPIError(t *testing.T) {
t.Parallel()
err := decodeAPIError(http.StatusForbidden, []byte(`{"error":"write_required"}`))
var apiErr apiError
if !errors.As(err, &apiErr) {
t.Fatalf("decodeAPIError type = %T, want apiError", err)
}
if apiErr.StatusCode != http.StatusForbidden || apiErr.Message != "write_required" {
t.Fatalf("api error = %+v", apiErr)
}
err = decodeAPIError(http.StatusBadGateway, []byte("upstream failed"))
if err.Error() != "api returned HTTP 502: upstream failed" {
t.Fatalf("plain api error = %q", err.Error())
}
}