forked from hashicorp/go-getter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_file_test.go
104 lines (86 loc) · 2.14 KB
/
get_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
package getter
import (
"os"
"path/filepath"
"testing"
)
func TestFileGetter_impl(t *testing.T) {
var _ Getter = new(FileGetter)
}
func TestFileGetter(t *testing.T) {
g := new(FileGetter)
dst := tempDir(t)
// With a dir that doesn't exist
if err := g.Get(dst, testModuleURL("basic")); err != nil {
t.Fatalf("err: %s", err)
}
// Verify the destination folder is a symlink
fi, err := os.Lstat(dst)
if err != nil {
t.Fatalf("err: %s", err)
}
if fi.Mode()&os.ModeSymlink == 0 {
t.Fatal("destination is not a symlink")
}
// Verify the main file exists
mainPath := filepath.Join(dst, "main.tf")
if _, err := os.Stat(mainPath); err != nil {
t.Fatalf("err: %s", err)
}
}
func TestFileGetter_sourceFile(t *testing.T) {
g := new(FileGetter)
dst := tempDir(t)
// With a source URL that is a path to a file
u := testModuleURL("basic")
u.Path += "/main.tf"
if err := g.Get(dst, u); err == nil {
t.Fatal("should error")
}
}
func TestFileGetter_sourceNoExist(t *testing.T) {
g := new(FileGetter)
dst := tempDir(t)
// With a source URL that doesn't exist
u := testModuleURL("basic")
u.Path += "/main"
if err := g.Get(dst, u); err == nil {
t.Fatal("should error")
}
}
func TestFileGetter_dir(t *testing.T) {
g := new(FileGetter)
dst := tempDir(t)
if err := os.MkdirAll(dst, 0755); err != nil {
t.Fatalf("err: %s", err)
}
// With a dir that exists that isn't a symlink
if err := g.Get(dst, testModuleURL("basic")); err == nil {
t.Fatal("should error")
}
}
func TestFileGetter_dirSymlink(t *testing.T) {
g := new(FileGetter)
dst := tempDir(t)
dst2 := tempDir(t)
// Make parents
if err := os.MkdirAll(filepath.Dir(dst), 0755); err != nil {
t.Fatalf("err: %s", err)
}
if err := os.MkdirAll(dst2, 0755); err != nil {
t.Fatalf("err: %s", err)
}
// Make a symlink
if err := os.Symlink(dst2, dst); err != nil {
t.Fatalf("err: %s", err)
}
// With a dir that exists that isn't a symlink
if err := g.Get(dst, testModuleURL("basic")); err != nil {
t.Fatalf("err: %s", err)
}
// Verify the main file exists
mainPath := filepath.Join(dst, "main.tf")
if _, err := os.Stat(mainPath); err != nil {
t.Fatalf("err: %s", err)
}
}