-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcommon_test.go
99 lines (90 loc) · 2.49 KB
/
common_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
package sellsword
import (
"github.com/Sirupsen/logrus"
"io/ioutil"
"os"
"os/user"
"path"
"testing"
)
func setUpTest() string {
dir, _ := os.Getwd()
tmpdir := path.Join(dir, "test/tmp")
os.MkdirAll(tmpdir, 0755)
Logger = logrus.New()
verbose := testing.Verbose()
if verbose {
Logger.Level = logrus.DebugLevel
} else {
Logger.Level = logrus.PanicLevel
}
return tmpdir
}
func TestResolveSymlinkForRealLink(t *testing.T) {
tmpdir := setUpTest()
source := path.Join(tmpdir, "source")
target := path.Join(tmpdir, "target")
ioutil.WriteFile(source, []byte{}, 0755)
os.Symlink(source, target)
actualSource, _ := resolveSymlink(target)
if actualSource != source {
t.Errorf("Expected %s but received %s", source, actualSource)
}
}
func TestResolveSymlinkForFakeLinkFails(t *testing.T) {
_, err := resolveSymlink("/does/not/exist")
if _, ok := err.(*os.PathError); !ok {
t.Error("Expected os.PathError to be raised when resolving nonexistent symlink")
}
}
func TestExpandPath(t *testing.T) {
setUpTest()
var actual string
var expected string
actual, _ = expandPath("~/.chef/foo")
usr, _ := user.Current()
expected = path.Join(usr.HomeDir, ".chef/foo")
if actual != expected {
t.Errorf("Expected %s but received %s", expected, actual)
}
wd, _ := os.Getwd()
expected = path.Join(path.Dir(wd), "foobar")
actual, _ = expandPath("../foobar")
if actual != expected {
t.Errorf("Expected %s but received %s", expected, actual)
}
}
func TestContains(t *testing.T) {
a := []string{"foo", "bar", "baz"}
if !contains(a, "foo") {
t.Errorf("Expected contains method to report that %v contains %s\n", a, "foo")
}
if contains(a, "nope") {
t.Errorf("Expected contains method to report that %v does not contain %s\n", a, "nope")
}
}
func TestAppendIfMissing(t *testing.T) {
var expectedLen int
var updated []string
a := []string{"foo", "bar", "baz"}
expectedLen = len(a)
updated = appendIfMissing(a, "foo")
if expectedLen != len(updated) {
t.Errorf("Expected method to not add duplicate value %s", "foo")
}
expectedLen = len(a)
updated = appendIfMissing(a, "blah")
if expectedLen == len(updated) {
t.Errorf("Expected method to add new value %s", "blah")
}
}
func TestArrayToMap(t *testing.T) {
myarr := []string{"foo", "bar", "baz"}
expected := map[string]string{"foo": "", "bar": "", "baz": ""}
actual := arrayToEmptyMap(myarr)
for k, v := range expected {
if v != actual[k] {
t.Errorf("Expected %s to be present. It appears that %v and %v do not match", v, actual, expected)
}
}
}