forked from jmcfarlane/notable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
116 lines (104 loc) · 2.7 KB
/
main_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
package main
import (
"bytes"
"fmt"
"net/http"
"net/url"
"os"
"path/filepath"
"strconv"
"testing"
"time"
"github.com/prometheus/common/log"
"github.com/stretchr/testify/assert"
)
func TestMain(m *testing.M) {
// Force the db path to be set to a safe place, we don't wanna
// blow away data just cuz we're testing right?
*dbPath = filepath.Join(os.TempDir(), "notable-testing/notes.db")
os.Exit(m.Run())
}
func TestMainFlagVersion(t *testing.T) {
v := *version
bv := buildVersion
defer func() {
buildVersion = bv
*version = v
}()
*version = true
buildVersion = fmt.Sprintf("test verison %s", time.Now())
b := new(bytes.Buffer)
run(b)
assert.Contains(t, b.String(), buildVersion)
}
func TestRunWhenAlreadyRunning(t *testing.T) {
mock := setup(t)
defer tearDown(mock)
b := new(bytes.Buffer)
u, err := url.Parse(mock.server.URL)
assert.Nil(t, err)
p, err := strconv.Atoi(u.Port())
*port = p
*browser = false
*daemon = false
start := time.Now()
run(b)
assert.True(t, time.Since(start).Seconds() < 0.1)
assert.Equal(t, "", b.String())
}
func TestMainStartStop(t *testing.T) {
b := *browser
d := *daemon
p := *dbPath
defer func() {
*browser = b
*daemon = d
*dbPath = p
}()
*daemon = false
*dbPath = "/tmp/notable-testing/notes.db"
*browser = false
url := fmt.Sprintf("http://localhost:%d/api/stop", *port)
go func() {
time.Sleep(time.Second * 2)
req, err := http.NewRequest("PUT", url, nil)
if err != nil {
log.Errorf("Unable to create http PUT request url=%v err=%s", url, err)
return
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Errorf("Unable to request stop url=%v err=%s", url, err)
return
}
resp.Body.Close()
}()
run(new(bytes.Buffer))
t.Logf("^^ foreground service unblocked, so PUT to url=%q worked!", url)
}
func TestHomeDir(t *testing.T) {
assert.NotEqual(t, "", homeDirPath("~/"))
}
func TestHomeDirError(t *testing.T) {
assert.Panics(t, func() { homeDirPath("~proc") })
}
func TestBrowserCmdDarwin(t *testing.T) {
name, args := browserCmd("darwin")
assert.Equal(t, "open", name)
assert.Equal(t, []string{fmt.Sprintf("http://%s:%d", *bind, *port)}, args)
}
func TestBrowserCmdDefautl(t *testing.T) {
name, args := browserCmd("?")
assert.Equal(t, "xdg-open", name)
assert.Equal(t, []string{fmt.Sprintf("http://%s:%d", *bind, *port)}, args)
}
func TestBrowserCmdLinux(t *testing.T) {
name, args := browserCmd("linux")
assert.Equal(t, "xdg-open", name)
assert.Equal(t, []string{fmt.Sprintf("http://%s:%d", *bind, *port)}, args)
}
func TestBrowserCmdWindows(t *testing.T) {
name, args := browserCmd("windows")
assert.Equal(t, "cmd", name)
assert.Equal(t, []string{`/c`, "start", fmt.Sprintf("http://%s:%d", *bind, *port)}, args)
}