-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsuite_test.go
161 lines (146 loc) · 4.58 KB
/
suite_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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package main_test
import (
"fmt"
"io"
"os"
"os/exec"
"strings"
"testing"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gbytes"
"github.com/tommy351/goldga"
)
func TestNTPeek(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "NTPeek Suite")
}
/// Integration Tests for `NTPeek`
var _ = Describe("Integration test", func() {
// Env vars for testing. note, these are burned
// https://www.notion.so/mikof/979bf78281914ca5895555168b2f7396?v=2a121bd645e5476fb0c6a0fe3d44366d
const TEST_DB_ID = "979bf78281914ca5895555168b2f7396"
const TEST_ACCESS = "secret_rhsxWWqTWhEd1pLlEOLB2z5eVfilG1iqPGPjeqSU934"
BeforeEach(func() {
inf, err := os.Stat("./nt")
if inf == nil || err != nil {
out, err := exec.Command("go", "generate").CombinedOutput()
if err != nil {
panic(fmt.Sprintf("failed to generate `nt` data, required for integration testing: %s\n%s", err, string(out)))
}
out, err = exec.Command("go", "build", "-o", "./nt").CombinedOutput()
if err != nil {
panic(fmt.Sprintf("failed to build `nt` binary, required for integration testing: %s\n%s", err, string(out)))
}
}
os.Setenv("NOTION_SECRET", TEST_ACCESS)
os.Setenv("NOTION_DEFAULT_DB", TEST_DB_ID)
})
Context("when running `nt v`", func() {
It("should return version information", func() {
toRun := []string{"./nt", "v"}
output, code := captureCrasher(toRun)
Expect(output).To(ContainSubstring("version"))
Expect(output).To(MatchRegexp(`v\d+\.\d+`), "should contain a git version tag")
Expect(code).To(Equal(0), "should exit with code 0")
})
})
Context("when running `nt h`", func() {
It("should return help information", func() {
toRun := []string{"./nt", "h"}
output, code := captureCrasher(toRun)
Expect(output).To(ContainSubstring("Usage"))
Expect(code).To(Equal(0), "should exit with code 0")
})
It("should match snapshot", func() {
toRun := []string{"./nt", "h"}
output, code := captureCrasher(toRun)
Expect(output).To(goldga.Match())
Expect(code).To(Equal(0), "should exit with code 0")
})
})
Context("when running `nt p`", func() {
It("should return a list of items", func() {
toRun := []string{"./nt", "p"}
output, code := captureCrasher(toRun)
Expect(output).ToNot(ContainSubstring("Usage"))
Expect(output).ToNot(ContainSubstring("Err"))
spl := strings.Split(output, "\n")
Expect(len(spl) > 2).To(BeTrue(), "should have at least 3 items")
Expect(code).To(Equal(0))
})
It("should be default", func() {
toRun := []string{"./nt"}
output, code := captureCrasher(toRun)
Expect(output).ToNot(ContainSubstring("Usage"))
Expect(code).To(Equal(0))
})
It("should match simple snapshot", func() {
toRun := []string{"./nt",
"--select=\"%Class:right% // %Name:left% %Due:full% %_p% %_id:short%\""}
output, code := captureCrasher(toRun)
Expect(output).To(goldga.Match())
Expect(code).To(Equal(0))
})
It("should match complex snapshot", func() {
toRun := []string{"./nt",
"--select=\"%Class:right% // %Name:left% %Due:full% %_p% %_id:short%\"",
"--sort", "Due:desc",
"--filter", "Due:date >= 2023/01/25",
"--limit=2",
}
output, code := captureCrasher(toRun)
Expect(output).To(goldga.Match())
Expect(code).To(Equal(0))
})
})
Context("when running malformed commands", func() {
It("errors on unknown command", func() {
toRun := []string{"./nt", "unknown"}
output, code := captureCrasher(toRun)
Expect(output).To(ContainSubstring("unknown command"))
Expect(output).To(ContainSubstring("./nt unknown"), "should show passed args")
Expect(output).To(ContainSubstring("Usage"))
Expect(code).To(Equal(1))
})
It("matches unknown command snapshot", func() {
toRun := []string{"./nt", "unknown"}
output, _ := captureCrasher(toRun)
Expect(output).To(goldga.Match())
})
})
})
/// Utilities for safe IO Capture, from:
/// https://medium.com/@hau12a1/golang-capturing-log-println-and-fmt-println-output-770209c791b4
func captureBufferOutput(f func()) string {
out_bak := os.Stdout
err_bak := os.Stderr
r, w, err := os.Pipe()
if err != nil {
panic(err)
}
os.Stdout = w
os.Stderr = w
defer func() {
os.Stdout = out_bak
os.Stderr = err_bak
}()
buf := gbytes.NewBuffer()
go func() {
defer func() {
_ = recover()
w.Close()
}()
f()
}()
io.Copy(buf, r)
return string(buf.Contents())
}
func captureCrasher(args []string) (string, int) {
cmd := exec.Command(args[0], args[1:]...)
out, err := cmd.CombinedOutput()
if err != nil {
return fmt.Sprintf("%s\n%s", err, string(out)), 1
}
return string(out), cmd.ProcessState.ExitCode()
}