-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathknife_test.go
81 lines (72 loc) · 2.12 KB
/
knife_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
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"testing"
)
func TestParseArgs(t *testing.T) {
tests := []struct {
p string
c string
expected1 bool
expected2 error
}{
{p: "myproject", c: "", expected1: true, expected2: nil},
{p: "", c: "mycommand", expected1: false, expected2: fmt.Errorf("Argument project cannot be empty")},
{p: "", c: "", expected1: false, expected2: fmt.Errorf("Argument project cannot be empty")},
{p: "myproject", c: "mycommand", expected1: false, expected2: nil},
}
for _, test := range tests {
actual1, actual2 := parseArgs(test.p, test.c)
if actual1 != test.expected1 {
t.Errorf("parseArgs(%q, %q) returned %v, expected %v", test.p, test.c, actual1, test.expected1)
}
if (actual2 == nil) != (test.expected2 == nil) || (actual2 != nil && actual2.Error() != test.expected2.Error()) {
t.Errorf("parseArgs(%q, %q) returned error %v, expected error %v", test.p, test.c, actual2, test.expected2)
}
}
}
func TestGetLastToken(t *testing.T) {
tests := []struct {
input string
expected string
}{
{input: "abc/def/ghi", expected: "ghi"},
{input: "foo/bar", expected: "bar"},
{input: "bar", expected: "bar"},
{input: "", expected: ""},
}
for _, test := range tests {
actual := getLastToken(test.input)
if actual != test.expected {
t.Errorf("getLastToken(%q) returned %q, expected %q", test.input, actual, test.expected)
}
}
}
func TestGetUser(t *testing.T) {
tests := []struct {
host string
user string
expected string
}{
{host: "example.com", user: "", expected: os.Getenv("USER")},
{host: "myserver", user: "", expected: "myuser"},
{host: "myserver", user: "myuser", expected: "myuser"},
}
d, err := ioutil.TempDir("", "myuser")
if err != nil {
log.Fatal(err)
}
defer os.RemoveAll(d)
os.Setenv("HOME", d)
os.MkdirAll(d+"/.ssh", 0700)
ioutil.WriteFile(d+"/.ssh/config", []byte("Host myserver\nUser myuser\n"), 0600)
for _, test := range tests {
actual := getUser(test.host, test.user)
if actual != test.expected {
t.Errorf("getUser(%q, %q) returned %q, expected %q", test.host, test.user, actual, test.expected)
}
}
}