-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli_test.go
More file actions
173 lines (158 loc) · 4.56 KB
/
cli_test.go
File metadata and controls
173 lines (158 loc) · 4.56 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package main_test
import (
"encoding/json"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
)
var binaryPath string
func TestMain(m *testing.M) {
dir, err := os.MkdirTemp("", "pakt-cli-test-*")
if err != nil {
panic(err)
}
defer func() { _ = os.RemoveAll(dir) }()
binaryPath = filepath.Join(dir, "pakt")
cmd := exec.Command("go", "build", "-o", binaryPath, ".")
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
panic("failed to build binary: " + err.Error())
}
os.Exit(m.Run())
}
func TestParseTextOutput(t *testing.T) {
out, err := exec.Command(binaryPath, "parse", "testdata/valid/scalars.pakt").Output()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
lines := strings.Split(strings.TrimSpace(string(out)), "\n")
if len(lines) == 0 {
t.Fatal("expected output lines, got none")
}
for i, line := range lines {
if !strings.Contains(line, "\t") {
t.Errorf("line %d: expected tab-separated fields, got %q", i, line)
}
}
}
func TestParseJSONOutput(t *testing.T) {
out, err := exec.Command(binaryPath, "parse", "testdata/valid/scalars.pakt", "--format", "json").Output()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
lines := strings.Split(strings.TrimSpace(string(out)), "\n")
if len(lines) == 0 {
t.Fatal("expected output lines, got none")
}
for i, line := range lines {
var obj map[string]interface{}
if err := json.Unmarshal([]byte(line), &obj); err != nil {
t.Errorf("line %d: invalid JSON: %v\n line: %s", i, err, line)
continue
}
if _, ok := obj["kind"]; !ok {
t.Errorf("line %d: missing 'kind' field", i)
}
if _, ok := obj["pos"]; !ok {
t.Errorf("line %d: missing 'pos' field", i)
}
}
}
func TestParseExplicitText(t *testing.T) {
out, err := exec.Command(binaryPath, "parse", "testdata/valid/scalars.pakt", "--format", "text").Output()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
lines := strings.Split(strings.TrimSpace(string(out)), "\n")
if len(lines) == 0 {
t.Fatal("expected output lines, got none")
}
// Should be tab-separated text, not JSON
for i, line := range lines {
if strings.HasPrefix(line, "{") {
t.Errorf("line %d: expected text format, got JSON-like output: %q", i, line)
}
if !strings.Contains(line, "\t") {
t.Errorf("line %d: expected tab-separated fields, got %q", i, line)
}
}
}
func TestValidateSuccess(t *testing.T) {
cmd := exec.Command(binaryPath, "validate", "testdata/valid/scalars.pakt")
if err := cmd.Run(); err != nil {
t.Fatalf("expected exit code 0, got error: %v", err)
}
}
func TestValidateFailure(t *testing.T) {
cmd := exec.Command(binaryPath, "validate", "testdata/invalid/nil-non-nullable.pakt")
out, err := cmd.CombinedOutput()
if err == nil {
t.Fatal("expected non-zero exit code, got 0")
}
if exitErr, ok := err.(*exec.ExitError); ok {
if exitErr.ExitCode() == 0 {
t.Fatal("expected non-zero exit code")
}
}
if len(out) == 0 {
t.Fatal("expected error output on stderr, got none")
}
}
func TestParseStdin(t *testing.T) {
f, err := os.Open("testdata/valid/scalars.pakt")
if err != nil {
t.Fatalf("opening test file: %v", err)
}
defer func() { _ = f.Close() }()
cmd := exec.Command(binaryPath, "parse", "-")
cmd.Stdin = f
out, err := cmd.Output()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
lines := strings.Split(strings.TrimSpace(string(out)), "\n")
if len(lines) == 0 {
t.Fatal("expected output from stdin, got none")
}
}
func TestFormatEnvVar(t *testing.T) {
cmd := exec.Command(binaryPath, "parse", "testdata/valid/scalars.pakt")
cmd.Env = append(os.Environ(), "PAKT_FORMAT=json")
out, err := cmd.Output()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
lines := strings.Split(strings.TrimSpace(string(out)), "\n")
if len(lines) == 0 {
t.Fatal("expected output lines, got none")
}
// Every line should be valid JSON
for i, line := range lines {
var obj map[string]interface{}
if err := json.Unmarshal([]byte(line), &obj); err != nil {
t.Errorf("line %d: expected JSON from env var override, got invalid JSON: %v", i, err)
}
}
}
func TestVersion(t *testing.T) {
out, err := exec.Command(binaryPath, "version").Output()
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
s := strings.TrimSpace(string(out))
if !strings.HasPrefix(s, "pakt ") {
t.Errorf("expected version string starting with 'pakt ', got %q", s)
}
}
func TestNoArgs(t *testing.T) {
cmd := exec.Command(binaryPath)
out, err := cmd.CombinedOutput()
if err == nil {
t.Fatal("expected non-zero exit code with no args, got 0")
}
if len(out) == 0 {
t.Fatal("expected usage output, got none")
}
}