forked from go-swagger/go-swagger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsupport_test.go
119 lines (97 loc) · 3.79 KB
/
support_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
package generator
import (
"os"
"path/filepath"
goruntime "runtime"
"strings"
"testing"
)
var checkprefixandfetchrelativepathtests = []struct {
childpath string
parentpath string
ok bool
path string
}{
// Positive
{"/", "/", true, "."},
{"/User/Gopher", "/", true, "User/Gopher"},
{"/User/Gopher/Go", "/User/Gopher/Go", true, "."},
{"/User/../User/Gopher", "/", true, "User/Gopher"},
// Negative cases
{"/", "/var", false, ""},
{"/User/Gopher", "/User/SomethingElse", false, ""},
{"/var", "/etc", false, ""},
{"/mnt/dev3", "/mnt/dev3/dir", false, ""},
}
var tempdir = os.TempDir()
var checkbaseimporttest = []struct {
path []string
gopath string
targetpath string
symlinksrc string
symlinkdest string // symlink is the last dir in targetpath
expectedpath string
}{
// No sym link. Positive Test Case
{[]string{tempdir + "/root/go/src/github.com/go-swagger"}, tempdir + "/root/go/", tempdir + "/root/go/src/github.com/go-swagger", "", "", "github.com/go-swagger"},
// Symlink points inside GOPATH
{[]string{tempdir + "/root/go/src/github.com/go-swagger"}, tempdir + "/root/go/", tempdir + "/root/symlink", tempdir + "/root/symlink", tempdir + "/root/go/src/", "."},
// Symlink points inside GOPATH
{[]string{tempdir + "/root/go/src/github.com/go-swagger"}, tempdir + "/root/go/", tempdir + "/root/symlink", tempdir + "/root/symlink", tempdir + "/root/go/src/github.com", "github.com"},
// Symlink point outside GOPATH : Targets Case 1: in baseImport implementation.
{[]string{tempdir + "/root/go/src/github.com/go-swagger", tempdir + "/root/gopher/go/"}, tempdir + "/root/go/", tempdir + "/root/go/src/github.com/gopher", tempdir + "/root/go/src/github.com/gopher", tempdir + "/root/gopher/go", "github.com/gopher"},
}
func TestCheckPrefixFetchRelPath(t *testing.T) {
for _, item := range checkprefixandfetchrelativepathtests {
actualok, actualpath := checkPrefixAndFetchRelativePath(item.childpath, item.parentpath)
if goruntime.GOOS == "windows" {
item.path = strings.Replace(item.path, "/", "\\", -1)
}
switch {
case actualok != item.ok:
t.Errorf("checkPrefixAndFetchRelativePath(%s, %s): expected %v, actual %v", item.childpath, item.parentpath, item.ok, actualok)
case actualpath != item.path:
t.Errorf("checkPrefixAndFetchRelativePath(%s, %s): expected %s, actual %s", item.childpath, item.parentpath, item.path, actualpath)
default:
continue
}
}
}
func TestBaseImport(t *testing.T) {
// 1. Create a root folder /tmp/root
// 2. Simulate scenario
// 2.a No Symlink
// 2.b Symlink from outside of GOPATH to inside
// 2.c Symlink from inside of GOPATH to outside.
// 3. Check results.
oldgopath := os.Getenv("GOPATH")
defer func() {
_ = os.Setenv("GOPATH", oldgopath)
_ = os.RemoveAll(filepath.Join(tempdir, "root"))
}()
for _, item := range checkbaseimporttest {
// Create Paths
for _, paths := range item.path {
_ = os.MkdirAll(paths, 0777)
}
// Change GOPATH
_ = os.Setenv("GOPATH", item.gopath)
if item.symlinksrc != "" {
// Create Symlink
if err := os.Symlink(item.symlinkdest, item.symlinksrc); err == nil {
// Test
actualpath := golang.baseImport(item.targetpath)
if goruntime.GOOS == "windows" {
item.expectedpath = strings.Replace(item.expectedpath, "/", "\\", -1)
}
if actualpath != item.expectedpath {
t.Errorf("baseImport(%s): expected %s, actual %s", item.targetpath, item.expectedpath, actualpath)
}
_ = os.RemoveAll(filepath.Join(tempdir, "root"))
} else {
t.Logf("WARNING:TestBaseImport with symlink could not be carried on. Symlink creation failed for %s -> %s: %v", item.symlinksrc, item.symlinkdest, err)
t.Logf("WARNING:TestBaseImport with symlink on Windows requires extended privileges (admin or a user with SeCreateSymbolicLinkPrivilege)")
}
}
}
}