-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
72 lines (67 loc) · 2.04 KB
/
Copy pathmain_test.go
File metadata and controls
72 lines (67 loc) · 2.04 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
package main
import (
"path/filepath"
"strings"
"testing"
"github.com/techthos/yourdata/internal/aade"
)
func TestRunRejectsUnknownMode(t *testing.T) {
err := run([]string{"serve"})
if err == nil || !strings.Contains(err.Error(), "unknown mode") {
t.Fatalf("run(serve) err = %v, want unknown-mode error", err)
}
}
// TestRunRequiresCredentials pins the startup requirement: no mode runs
// without both credential variables, and the error names each missing one.
func TestRunRequiresCredentials(t *testing.T) {
tests := []struct {
name string
user string
key string
wantMiss []string
}{
{name: "both missing", wantMiss: []string{aade.EnvUsername, aade.EnvSubKey}},
{name: "key missing", user: "u", wantMiss: []string{aade.EnvSubKey}},
{name: "user missing", key: "k", wantMiss: []string{aade.EnvUsername}},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
t.Setenv(aade.EnvUsername, tc.user)
t.Setenv(aade.EnvSubKey, tc.key)
err := run(nil) // TUI mode; must fail before any UI or DB work
if err == nil {
t.Fatal("run() = nil, want missing-credentials error")
}
for _, name := range tc.wantMiss {
if !strings.Contains(err.Error(), name) {
t.Errorf("error %q does not name %s", err, name)
}
}
})
}
}
func TestResolveDBPathPrecedence(t *testing.T) {
t.Run("flag wins", func(t *testing.T) {
t.Setenv("YOURDATA_DB", "/env/path.db")
got, err := resolveDBPath("/flag/path.db")
if err != nil || got != "/flag/path.db" {
t.Errorf("got %q, %v", got, err)
}
})
t.Run("env wins over default", func(t *testing.T) {
t.Setenv("YOURDATA_DB", "/env/path.db")
got, err := resolveDBPath("")
if err != nil || got != "/env/path.db" {
t.Errorf("got %q, %v", got, err)
}
})
t.Run("xdg default", func(t *testing.T) {
t.Setenv("YOURDATA_DB", "")
t.Setenv("XDG_DATA_HOME", "/xdg/data")
got, err := resolveDBPath("")
want := filepath.Join("/xdg/data", "yourdata", "yourdata.db")
if err != nil || got != want {
t.Errorf("got %q, want %q (%v)", got, want, err)
}
})
}