-
Notifications
You must be signed in to change notification settings - Fork 238
/
detect_file_unix_test.go
73 lines (61 loc) · 1.45 KB
/
detect_file_unix_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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
// +build test unix
package getter
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
)
// If a relative symlink is passed in as the pwd to Detect, the resulting URL
// can have an invalid path.
func TestFileDetector_relativeSymlink(t *testing.T) {
tmpDir, err := ioutil.TempDir("", "go-getter")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpDir)
// We may have a symlinked tmp dir,
// e.g. OSX uses /var -> /private/var
tmpDir, err = filepath.EvalSymlinks(tmpDir)
if err != nil {
t.Fatal(err)
}
err = os.Mkdir(filepath.Join(tmpDir, "realPWD"), 0755)
if err != nil {
t.Fatal(err)
}
subdir := filepath.Join(tmpDir, "subdir")
err = os.Mkdir(subdir, 0755)
if err != nil {
t.Fatal(err)
}
prevDir, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
defer os.Chdir(prevDir)
err = os.Chdir(subdir)
if err != nil {
t.Fatal(err)
}
err = os.Symlink("../realPWD", "linkedPWD")
if err != nil {
t.Fatal(err)
}
// if detech doesn't fully resolve the pwd symlink, the output will be the
// invalid path: "file:///../modules/foo"
f := new(FileDetector)
out, ok, err := f.Detect("../modules/foo", "./linkedPWD")
if err != nil {
t.Fatalf("err: %v", err)
}
if !ok {
t.Fatal("not ok")
}
if out != "file://"+filepath.Join(tmpDir, "modules/foo") {
t.Logf("expected: %v", "file://"+filepath.Join(tmpDir, "modules/foo"))
t.Fatalf("bad: %v", out)
}
}