forked from hashicorp/go-getter
-
Notifications
You must be signed in to change notification settings - Fork 3
/
detect_file_test.go
115 lines (97 loc) · 2.44 KB
/
detect_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
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
package getter
import (
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
"testing"
)
type fileTest struct {
in, pwd, out string
err bool
}
var fileTests = []fileTest{
{"./foo", "/pwd", "file:///pwd/foo", false},
{"./foo?foo=bar", "/pwd", "file:///pwd/foo?foo=bar", false},
{"foo", "/pwd", "file:///pwd/foo", false},
}
var unixFileTests = []fileTest{
{"./foo", "testdata/detect-file-symlink-pwd/syml/pwd",
"testdata/detect-file-symlink-pwd/real/foo", false},
{"/foo", "/pwd", "file:///foo", false},
{"/foo?bar=baz", "/pwd", "file:///foo?bar=baz", false},
}
var winFileTests = []fileTest{
{"/foo", "/pwd", "file:///pwd/foo", false},
{`C:\`, `/pwd`, `file://C:/`, false},
{`C:\?bar=baz`, `/pwd`, `file://C:/?bar=baz`, false},
}
func TestFileDetector(t *testing.T) {
if runtime.GOOS == "windows" {
fileTests = append(fileTests, winFileTests...)
} else {
fileTests = append(fileTests, unixFileTests...)
}
// Get the pwd
pwdRoot, err := os.Getwd()
if err != nil {
t.Fatalf("err: %s", err)
}
pwdRoot, err = filepath.Abs(pwdRoot)
if err != nil {
t.Fatalf("err: %s", err)
}
f := new(FileDetector)
for i, tc := range fileTests {
t.Run(fmt.Sprintf("%d", i), func(t *testing.T) {
pwd := tc.pwd
out, ok, err := f.Detect(tc.in, pwd)
if err != nil {
t.Fatalf("err: %s", err)
}
if !ok {
t.Fatal("not ok")
}
expected := tc.out
if !strings.HasPrefix(expected, "file://") {
expected = "file://" + filepath.Join(pwdRoot, expected)
}
if out != expected {
t.Fatalf("input: %q\npwd: %q\nexpected: %q\nbad output: %#v",
tc.in, pwd, expected, out)
}
})
}
}
var noPwdFileTests = []fileTest{
{in: "./foo", pwd: "", out: "", err: true},
{in: "foo", pwd: "", out: "", err: true},
}
var noPwdUnixFileTests = []fileTest{
{in: "/foo", pwd: "", out: "file:///foo", err: false},
}
var noPwdWinFileTests = []fileTest{
{in: "/foo", pwd: "", out: "", err: true},
{in: `C:\`, pwd: ``, out: `file://C:/`, err: false},
}
func TestFileDetector_noPwd(t *testing.T) {
if runtime.GOOS == "windows" {
noPwdFileTests = append(noPwdFileTests, noPwdWinFileTests...)
} else {
noPwdFileTests = append(noPwdFileTests, noPwdUnixFileTests...)
}
f := new(FileDetector)
for i, tc := range noPwdFileTests {
out, ok, err := f.Detect(tc.in, tc.pwd)
if err != nil != tc.err {
t.Fatalf("%d: err: %s", i, err)
}
if !ok {
t.Fatal("not ok")
}
if out != tc.out {
t.Fatalf("%d: bad: %#v", i, out)
}
}
}