Skip to content

Commit 46ee5ee

Browse files
committed
extract option defaults logic
Signed-off-by: Cian Johnston <[email protected]>
1 parent ac0fbc4 commit 46ee5ee

File tree

2 files changed

+151
-0
lines changed

2 files changed

+151
-0
lines changed

options/defaults.go

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package options
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
7+
"github.com/go-git/go-billy/v5/osfs"
8+
9+
giturls "github.com/chainguard-dev/git-urls"
10+
"github.com/coder/envbuilder/constants"
11+
"github.com/coder/envbuilder/internal/chmodfs"
12+
)
13+
14+
// DefaultWorkspaceFolder returns the default workspace folder
15+
// for a given repository URL.
16+
func DefaultWorkspaceFolder(repoURL string) string {
17+
if repoURL == "" {
18+
return constants.EmptyWorkspaceDir
19+
}
20+
parsed, err := giturls.Parse(repoURL)
21+
if err != nil {
22+
return constants.EmptyWorkspaceDir
23+
}
24+
name := strings.Split(parsed.Path, "/")
25+
hasOwnerAndRepo := len(name) >= 2
26+
if !hasOwnerAndRepo {
27+
return constants.EmptyWorkspaceDir
28+
}
29+
repo := strings.TrimSuffix(name[len(name)-1], ".git")
30+
return fmt.Sprintf("/workspaces/%s", repo)
31+
}
32+
33+
func (o *Options) SetDefaults() {
34+
// Temporarily removed these from the default settings to prevent conflicts
35+
// between current and legacy environment variables that add default values.
36+
// Once the legacy environment variables are phased out, this can be
37+
// reinstated to the previous default values.
38+
if len(o.IgnorePaths) == 0 {
39+
o.IgnorePaths = []string{
40+
"/var/run",
41+
// KinD adds these paths to pods, so ignore them by default.
42+
"/product_uuid", "/product_name",
43+
}
44+
}
45+
if o.InitScript == "" {
46+
o.InitScript = "sleep infinity"
47+
}
48+
if o.InitCommand == "" {
49+
o.InitCommand = "/bin/sh"
50+
}
51+
52+
if o.Filesystem == nil {
53+
o.Filesystem = chmodfs.New(osfs.New("/"))
54+
}
55+
if o.WorkspaceFolder == "" {
56+
o.WorkspaceFolder = DefaultWorkspaceFolder(o.GitURL)
57+
}
58+
}

options/defaults_test.go

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package options_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/coder/envbuilder/internal/chmodfs"
7+
"github.com/go-git/go-billy/v5/osfs"
8+
9+
"github.com/stretchr/testify/assert"
10+
11+
"github.com/coder/envbuilder/constants"
12+
"github.com/coder/envbuilder/options"
13+
"github.com/stretchr/testify/require"
14+
)
15+
16+
func TestDefaultWorkspaceFolder(t *testing.T) {
17+
t.Parallel()
18+
19+
successTests := []struct {
20+
name string
21+
gitURL string
22+
expected string
23+
}{
24+
{
25+
name: "HTTP",
26+
gitURL: "https://github.com/coder/envbuilder.git",
27+
expected: "/workspaces/envbuilder",
28+
},
29+
{
30+
name: "SSH",
31+
gitURL: "[email protected]:coder/envbuilder.git",
32+
expected: "/workspaces/envbuilder",
33+
},
34+
{
35+
name: "username and password",
36+
gitURL: "https://username:[email protected]/coder/envbuilder.git",
37+
expected: "/workspaces/envbuilder",
38+
},
39+
{
40+
name: "fragment",
41+
gitURL: "https://github.com/coder/envbuilder.git#feature-branch",
42+
expected: "/workspaces/envbuilder",
43+
},
44+
{
45+
name: "empty",
46+
gitURL: "",
47+
expected: constants.EmptyWorkspaceDir,
48+
},
49+
}
50+
for _, tt := range successTests {
51+
t.Run(tt.name, func(t *testing.T) {
52+
dir := options.DefaultWorkspaceFolder(tt.gitURL)
53+
require.Equal(t, tt.expected, dir)
54+
})
55+
}
56+
57+
invalidTests := []struct {
58+
name string
59+
invalidURL string
60+
}{
61+
{
62+
name: "simple text",
63+
invalidURL: "not a valid URL",
64+
},
65+
{
66+
name: "website URL",
67+
invalidURL: "www.google.com",
68+
},
69+
}
70+
for _, tt := range invalidTests {
71+
t.Run(tt.name, func(t *testing.T) {
72+
dir := options.DefaultWorkspaceFolder(tt.invalidURL)
73+
require.Equal(t, constants.EmptyWorkspaceDir, dir)
74+
})
75+
}
76+
}
77+
78+
func TestOptions_SetDefaults(t *testing.T) {
79+
t.Parallel()
80+
81+
expected := options.Options{
82+
InitScript: "sleep infinity",
83+
InitCommand: "/bin/sh",
84+
IgnorePaths: []string{"/var/run", "/product_uuid", "/product_name"},
85+
Filesystem: chmodfs.New(osfs.New("/")),
86+
GitURL: "",
87+
WorkspaceFolder: constants.EmptyWorkspaceDir,
88+
}
89+
90+
var actual options.Options
91+
actual.SetDefaults()
92+
assert.Equal(t, expected, actual)
93+
}

0 commit comments

Comments
 (0)