-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathdefaults_test.go
151 lines (141 loc) · 3.47 KB
/
defaults_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
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
package options_test
import (
"testing"
"github.com/coder/envbuilder/internal/chmodfs"
"github.com/go-git/go-billy/v5/osfs"
"github.com/stretchr/testify/assert"
"github.com/coder/envbuilder/options"
"github.com/stretchr/testify/require"
)
func TestDefaultWorkspaceFolder(t *testing.T) {
t.Parallel()
successTests := []struct {
name string
gitURL string
expected string
}{
{
name: "HTTP",
gitURL: "https://github.com/coder/envbuilder.git",
expected: "/workspaces/envbuilder",
},
{
name: "SSH",
gitURL: "[email protected]:coder/envbuilder.git",
expected: "/workspaces/envbuilder",
},
{
name: "username and password",
gitURL: "https://username:[email protected]/coder/envbuilder.git",
expected: "/workspaces/envbuilder",
},
{
name: "trailing",
gitURL: "https://github.com/coder/envbuilder.git/",
expected: "/workspaces/envbuilder",
},
{
name: "trailing-x2",
gitURL: "https://github.com/coder/envbuilder.git//",
expected: "/workspaces/envbuilder",
},
{
name: "no .git",
gitURL: "https://github.com/coder/envbuilder",
expected: "/workspaces/envbuilder",
},
{
name: "trailing no .git",
gitURL: "https://github.com/coder/envbuilder/",
expected: "/workspaces/envbuilder",
},
{
name: "fragment",
gitURL: "https://github.com/coder/envbuilder.git#feature-branch",
expected: "/workspaces/envbuilder",
},
{
name: "fragment-trailing",
gitURL: "https://github.com/coder/envbuilder.git/#refs/heads/feature-branch",
expected: "/workspaces/envbuilder",
},
{
name: "fragment-trailing no .git",
gitURL: "https://github.com/coder/envbuilder/#refs/heads/feature-branch",
expected: "/workspaces/envbuilder",
},
{
name: "space",
gitURL: "https://github.com/coder/env%20builder.git",
expected: "/workspaces/env builder",
},
{
name: "Unix path",
gitURL: "/repo",
expected: "/workspaces/repo",
},
{
name: "Unix subpath",
gitURL: "/path/to/repo",
expected: "/workspaces/repo",
},
{
name: "empty",
gitURL: "",
expected: options.EmptyWorkspaceDir,
},
}
for _, tt := range successTests {
t.Run(tt.name, func(t *testing.T) {
dir := options.DefaultWorkspaceFolder(tt.gitURL)
require.Equal(t, tt.expected, dir)
})
}
invalidTests := []struct {
name string
invalidURL string
}{
{
name: "simple text",
invalidURL: "not a valid URL",
},
{
name: "website URL",
invalidURL: "www.google.com",
},
{
name: "Unix root",
invalidURL: "/",
},
{
name: "Path consists entirely of slash",
invalidURL: "//",
},
{
name: "Git URL with no path",
invalidURL: "http://127.0.0.1:41073",
},
}
for _, tt := range invalidTests {
t.Run(tt.name, func(t *testing.T) {
dir := options.DefaultWorkspaceFolder(tt.invalidURL)
require.Equal(t, options.EmptyWorkspaceDir, dir)
})
}
}
func TestOptions_SetDefaults(t *testing.T) {
t.Parallel()
expected := options.Options{
InitScript: "sleep infinity",
InitCommand: "/bin/sh",
IgnorePaths: []string{"/var/run", "/product_uuid", "/product_name"},
Filesystem: chmodfs.New(osfs.New("/")),
GitURL: "",
WorkspaceFolder: options.EmptyWorkspaceDir,
WorkingDirBase: "/.envbuilder",
BinaryPath: "/.envbuilder/bin/envbuilder",
}
var actual options.Options
actual.SetDefaults()
assert.Equal(t, expected, actual)
}