-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathpath_test.go
More file actions
168 lines (159 loc) · 5.18 KB
/
Copy pathpath_test.go
File metadata and controls
168 lines (159 loc) · 5.18 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
package utils
import (
"fmt"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
)
func TestGetRelativePath(t *testing.T) {
tests := []struct {
name string
basePath string
target string
expected string
}{
{
name: "relative path",
basePath: filepath.Join("dir1", "dir2"),
target: filepath.Join("dir1", "dir2", "dir3"),
expected: "dir3",
},
{
name: "absolute path",
basePath: filepath.Join("home", "user", "dir1", "dir2"),
target: filepath.Join("home", "user", "dir1", "dir2", "dir3"),
expected: "dir3",
},
{
name: "no common base path",
basePath: filepath.Join("dir1", "dir2"),
target: filepath.Join("dir3", "dir4"),
expected: filepath.Join("dir3", "dir4"),
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
result := GetRelativePath(test.target, test.basePath)
assert.Equal(t, result, filepath.ToSlash(test.expected), "expected '%s', got '%s'", filepath.ToSlash(test.expected), result)
})
}
}
func TestExtractRelativePath(t *testing.T) {
tests := []struct {
name string
fullPath string
projectPath string
expectedResult string
}{
{
name: "empty path",
fullPath: "",
projectPath: filepath.Join("Users", "user", "Desktop", "secrets_scanner"),
expectedResult: "",
},
{
name: "invalid path",
fullPath: "invalidFullPath",
projectPath: filepath.Join("Users", "user", "Desktop", "secrets_scanner"),
expectedResult: "invalidFullPath",
},
{
name: "valid full path",
fullPath: fmt.Sprintf("file://%s", filepath.Join("Users", "user", "Desktop", "secrets_scanner", "tests", "req.nodejs", "file.js")),
projectPath: fmt.Sprintf("file://%s", filepath.Join("Users", "user", "Desktop", "secrets_scanner")),
expectedResult: "tests/req.nodejs/file.js",
},
{
name: "invalid project path",
fullPath: fmt.Sprintf("file://%s", filepath.Join("Users", "user", "Desktop", "secrets_scanner", "tests", "req.nodejs", "file.js")),
projectPath: "invalidProjectPath",
expectedResult: filepath.ToSlash(fmt.Sprintf("file://%s", filepath.Join("Users", "user", "Desktop", "secrets_scanner", "tests", "req.nodejs", "file.js"))),
},
{
name: "valid full path with private",
fullPath: "file:///private/Users/user/Desktop/secrets_scanner/tests/req.nodejs/file.js",
projectPath: "file:///Users/user/Desktop/secrets_scanner/",
expectedResult: "tests/req.nodejs/file.js",
},
{
name: "invalid project path and path with private",
fullPath: "file:///private/Users/user/Desktop/secrets_scanner/tests/req.nodejs/file.js",
projectPath: "invalidProjectPath",
expectedResult: "file:///Users/user/Desktop/secrets_scanner/tests/req.nodejs/file.js",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
assert.Equal(t, test.expectedResult, GetRelativePath(test.fullPath, test.projectPath))
})
}
}
func TestGetCommonParentDir(t *testing.T) {
tests := []struct {
name string
dirs []string
expected string
}{
{
name: "common parent dir",
dirs: []string{filepath.Join("dir1", "dir2", "dir3"), filepath.Join("dir1", "dir2", "dir4")},
expected: filepath.Join("dir1", "dir2"),
},
{
name: "multi sub common parent dir",
dirs: []string{filepath.Join("dir1", "dir2", "dir3"), filepath.Join("dir1", "dir2", "dir4"), filepath.Join("dir1", "dir5")},
expected: "dir1",
},
{
name: "no common parent dir",
dirs: []string{filepath.Join("dir1", "dir2", "dir3"), filepath.Join("dir1", "dir2", "dir4"), filepath.Join("dir4", "dir5")},
expected: ".",
},
{
name: "empty dirs",
dirs: []string{},
expected: "",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
result := GetCommonParentDir(test.dirs...)
if result != test.expected {
t.Errorf("expected '%s', got '%s'", test.expected, result)
}
})
}
}
func TestGetRepositoriesScansListUrlForArtifact(t *testing.T) {
tests := []struct {
name string
baseUrl string
repoPath string
artifactName string
packageId string
expected string
}{
{
name: "basic case",
baseUrl: "http://localhost:8081/",
repoPath: "my-repo",
artifactName: "artifact.zip",
packageId: "abc123",
expected: "http://localhost:8081/ui/scans-list/repositories/my-repo/scan-descendants/artifact.zip?package_id=abc123&page_type=overview&path=my-repo%2Fartifact.zip",
},
{
name: "with sub dir in repoPath",
baseUrl: "http://localhost:8081/",
repoPath: "my-repo/subdir",
artifactName: "artifact.zip",
packageId: "abc123",
expected: "http://localhost:8081/ui/scans-list/repositories/my-repo/scan-descendants/artifact.zip?package_id=abc123&page_type=overview&path=my-repo%2Fsubdir%2Fartifact.zip",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
result := GetRepositoriesScansListUrlForArtifact(test.baseUrl, test.repoPath, test.artifactName, test.packageId)
assert.Equal(t, test.expected, result, "expected '%s', got '%s'", test.expected, result)
})
}
}