-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind_test.go
121 lines (103 loc) · 2.89 KB
/
find_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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// dcc - dependency-driven C/C++ compiler front end
//
// Copyright © A.Newman 2015.
//
// This source code is released under version 2 of the GNU Public License.
// See the file LICENSE for details.
//
package main
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
)
func invalidateStatCache() {
statCacheMutex.Lock()
statCache = make(map[string]os.FileInfo)
statCacheMutex.Unlock()
}
const (
testDataDir = "testdata"
testFilename = "testfile"
)
var (
testProjectDccDir = filepath.Join(testDataDir, DefaultDccDir)
testProjectRootDir = filepath.Join(testDataDir, "root")
testProjectChildDir = filepath.Join(testProjectRootDir, "child")
)
func makeTestDirs(t *testing.T) {
mustMkdir := func(path string) {
if err := os.MkdirAll(path, 0777); err != nil {
t.Fatal(err)
}
}
mustMkdir(testProjectRootDir)
mustMkdir(testProjectChildDir)
mustMkdir(testProjectDccDir)
}
func removeTestDirs(t *testing.T) {
if err := os.RemoveAll("testdata"); err != nil {
t.Fatal(err)
}
}
func makeFileWithContent(t *testing.T, path, content string) {
if err := ioutil.WriteFile(path, []byte(content), 0666); err != nil {
t.Fatal(err)
}
}
func makeFile(t *testing.T, path string) {
makeFileWithContent(t, path, path)
}
func checkFile(t *testing.T, path, content string) {
if data, err := os.ReadFile(path); err != nil {
t.Fatal(err)
} else if s := string(data); s != content {
t.Fatalf("%s: content (%q) does not match expected content (%q)", path, s, content)
}
}
func setupTest(t *testing.T) {
removeTestDirs(t)
makeTestDirs(t)
}
func singleFileTestCase(t *testing.T, dirname, filename, cwd string) {
invalidateStatCache()
fullpath := filepath.Join(dirname, filename)
abspath, err := filepath.Abs(fullpath)
if err != nil {
t.Fatal(err)
}
makeFile(t, fullpath)
defer func(name string) {
if err := os.Remove(name); err != nil {
t.Fatal(err)
}
}(fullpath)
defer func(dir string) {
if err := os.Chdir(dir); err != nil {
t.Fatal(err)
}
}(MustGetwd())
if err = os.Chdir(cwd); err != nil {
t.Fatal(err)
} else if foundpath, found := FindFile(filename); !found {
t.Fatalf("%q not found", filename)
} else if foundpath != abspath {
t.Fatalf("found %q which is not the expected %q", foundpath, fullpath)
} else {
checkFile(t, foundpath, fullpath)
}
}
func Test_FindFile_DirectorySearching(t *testing.T) {
setupTest(t)
defer removeTestDirs(t)
singleFileTestCase(t, testProjectChildDir, testFilename, testProjectChildDir)
singleFileTestCase(t, testProjectRootDir, testFilename, testProjectChildDir)
singleFileTestCase(t, testProjectDccDir, testFilename, testProjectChildDir)
}
func Test_FindFile_PlatformSpecificSearches(t *testing.T) {
setupTest(t)
defer removeTestDirs(t)
singleFileTestCase(t, testProjectChildDir, OsSpecificFilename(testFilename), testProjectChildDir)
singleFileTestCase(t, testProjectDccDir, OsAndArchSpecificFilename(testFilename), testProjectChildDir)
}