-
Notifications
You must be signed in to change notification settings - Fork 274
Expand file tree
/
Copy pathget_file_test.go
More file actions
225 lines (187 loc) · 5.28 KB
/
Copy pathget_file_test.go
File metadata and controls
225 lines (187 loc) · 5.28 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
// Copyright IBM Corp. 2015, 2025
// SPDX-License-Identifier: MPL-2.0
package getter
import (
"os"
"path/filepath"
"runtime"
"testing"
)
func TestFileGetter_impl(t *testing.T) {
var _ Getter = new(FileGetter)
}
func TestFileGetter(t *testing.T) {
g := new(FileGetter)
dst := filepath.Join(t.TempDir(), "target")
// With a dir that doesn't exist
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)
}
// Verify it's a symlink on Unix or junction point on Windows
fi, err := os.Lstat(dst)
if err != nil {
t.Fatalf("err: %s", err)
}
if runtime.GOOS == "windows" {
isJunction, junctionErr := isWindowsJunctionPoint(dst)
if junctionErr != nil {
t.Fatalf("failed to check if destination is a junction point: %s", junctionErr)
}
if !isJunction {
t.Fatal("destination is not a junction point")
}
// Additional verification: should be accessible as a directory
if dirInfo, err := os.Stat(dst); err != nil || !dirInfo.IsDir() {
t.Fatal("destination junction point is not accessible as a directory")
}
} else {
// On Unix, verify it's a traditional symlink
if fi.Mode()&os.ModeSymlink == 0 {
t.Fatal("destination is not a symlink")
}
}
}
func TestFileGetter_sourceFile(t *testing.T) {
g := new(FileGetter)
dst := filepath.Join(t.TempDir(), "target")
// 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 := filepath.Join(t.TempDir(), "target")
// 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 := filepath.Join(t.TempDir(), "target")
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)
tempBase := t.TempDir()
dst := filepath.Join(tempBase, "dst")
dst2 := filepath.Join(tempBase, "dst2")
// 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)
}
}
func TestFileGetter_GetFile(t *testing.T) {
g := new(FileGetter)
dst := filepath.Join(t.TempDir(), "test-file")
// With a dir that doesn't exist
if err := g.GetFile(dst, testModuleURL("basic-file/foo.txt")); err != nil {
t.Fatalf("err: %s", err)
}
// Verify the main file exists
assertContents(t, dst, "Hello\n")
// On Unix, verify it's a symlink; on Windows, just verify it works
if runtime.GOOS != "windows" {
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")
}
}
}
func TestFileGetter_GetFile_Copy(t *testing.T) {
g := new(FileGetter)
g.Copy = true
dst := filepath.Join(t.TempDir(), "test-file")
// With a dir that doesn't exist
if err := g.GetFile(dst, testModuleURL("basic-file/foo.txt")); 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 a symlink")
}
// Verify the main file exists
assertContents(t, dst, "Hello\n")
}
// https://github.com/hashicorp/terraform/issues/8418
func TestFileGetter_percent2F(t *testing.T) {
g := new(FileGetter)
dst := filepath.Join(t.TempDir(), "target")
// With a dir that doesn't exist
if err := g.Get(dst, testModuleURL("basic%2Ftest")); 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)
}
}
func TestFileGetter_ClientMode_notexist(t *testing.T) {
g := new(FileGetter)
u := testURL("nonexistent")
if _, err := g.ClientMode(u); err == nil {
t.Fatal("expect source file error")
}
}
func TestFileGetter_ClientMode_file(t *testing.T) {
g := new(FileGetter)
// Check the client mode when pointed at a file.
mode, err := g.ClientMode(testModuleURL("basic-file/foo.txt"))
if err != nil {
t.Fatalf("err: %s", err)
}
if mode != ClientModeFile {
t.Fatal("expect ClientModeFile")
}
}
func TestFileGetter_ClientMode_dir(t *testing.T) {
g := new(FileGetter)
// Check the client mode when pointed at a directory.
mode, err := g.ClientMode(testModuleURL("basic"))
if err != nil {
t.Fatalf("err: %s", err)
}
if mode != ClientModeDir {
t.Fatal("expect ClientModeDir")
}
}