-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathfile_test.go
46 lines (39 loc) · 1.05 KB
/
file_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
package main
import (
"io/ioutil"
"log"
"os"
"path"
"testing"
)
func TestTryReadFile(t *testing.T) {
tmpdir, _ := ioutil.TempDir("", "gjfy_test")
defer os.Remove(tmpdir)
fileName := "gjfy_testfile"
testContent := []byte("test")
testFileA := path.Join(tmpdir, fileName)
ioutil.WriteFile(testFileA, testContent, 0644)
defer os.Remove(testFileA)
configDir = tmpdir
log.SetOutput(ioutil.Discard)
// Test file in configDir
bytes := tryReadFile(fileName)
if string(bytes) != string(testContent) {
t.Errorf("got %v, wanted %v", bytes, testContent)
}
// Test file does not exist
bytes = tryReadFile("doesnotexist")
if string(bytes) != "" {
t.Errorf("got %v, wanted empty slice", bytes)
}
// Test file in pwd shadows file in configDir
testContentPwd := []byte("testPwd")
wd, _ := os.Getwd()
testFileB := path.Join(wd, fileName)
ioutil.WriteFile(testFileB, testContentPwd, 0644)
defer os.Remove(testFileB)
bytes = tryReadFile(fileName)
if string(bytes) != string(testContentPwd) {
t.Errorf("got %v, wanted %v", bytes, testContentPwd)
}
}