-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
106 lines (88 loc) · 2.35 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
package main
import (
"fmt"
"io/ioutil"
"os"
"testing"
)
const testFlanFile = "testdata/flanfixture.json"
func TestReadFlanFileFromPath(t *testing.T) {
commands, err := readFlanFileFromPath(testFlanFile)
if err != nil {
t.Error(err)
}
if len(commands) != 2 {
t.Error("expected 2 commands, got", len(commands))
}
cmd1Annos, prs := commands["cmd_1"]
if !prs {
t.Error("cmd_1 not in commands map")
}
if len(cmd1Annos) != 2 {
t.Error("cmd_1 does not have 2 annotations in commands map")
}
if cmd1Annos[0][0] != "cmd_1 anno1" {
t.Error("missing annotation1 for cmd_1")
}
if cmd1Annos[0][1] != "cmd_1 example1" {
t.Error("missing example1 for cmd_1")
}
if cmd1Annos[1][0] != "cmd_1 anno2" {
t.Error("missing annotation2 for cmd_1")
}
if cmd1Annos[1][1] != "cmd_1 example2" {
t.Error("missing example2 for cmd_1")
}
commands, err = readFlanFileFromPath("non_existant_file.xyz")
if err != nil {
t.Error(err)
}
if len(commands) != 0 {
t.Error(`readFlanFileFromPath() did't return empty Commands map for
non existant flonfile`)
}
}
func TestWriteFlanFile(t *testing.T) {
tmpfile := "testdata/tmpfile.json"
cmd1 := make([][]string, 1)
cmd1[0] = []string{"a1", "a2"}
cmd2 := make([][]string, 1)
cmd2[0] = []string{"b1", "b2"}
cmds := commands{
"a": cmd1,
"b": cmd2,
}
if err := writeFlanFileToPath(cmds, tmpfile); err != nil {
t.Error(err)
}
defer os.Remove(tmpfile)
dat, err := ioutil.ReadFile(tmpfile)
if err != nil {
t.Error(err)
}
if string(dat) != `{"a":[["a1","a2"]],"b":[["b1","b2"]]}` {
t.Error("WriteFlanFileToPath wrote unexpected value:", string(dat))
}
}
func TestStore(t *testing.T) {
cmds := make(commands)
store("ls", "ls -a", "list all files", cmds)
checkStorage("ls", "ls -a", "list all files", cmds)
store("ls", "ls -l", "list files, showing symlinks", cmds)
checkStorage("ls", "ls -l", "list files, showing symlinks", cmds)
store("man", "man cat", "see manpage for cat", cmds)
checkStorage("man", "man cat", "see manpage for cat", cmds)
}
func checkStorage(cmd, example, anno string, cmds commands) error {
flannotations := cmds[cmd]
for _, flanno := range flannotations {
storedEx := flanno[0]
if storedEx == example {
storedAnno := flanno[1]
if storedAnno == anno {
return nil
}
}
}
return fmt.Errorf("did not find expected flannotation: %v, %v", example, anno)
}