-
Notifications
You must be signed in to change notification settings - Fork 43
chore: extract option defaults to options package #283
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1 @@ | ||
package envbuilder_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/coder/envbuilder" | ||
"github.com/coder/envbuilder/constants" | ||
|
||
"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: "fragment", | ||
gitURL: "https://github.com/coder/envbuilder.git#feature-branch", | ||
expected: "/workspaces/envbuilder", | ||
}, | ||
{ | ||
name: "empty", | ||
gitURL: "", | ||
expected: constants.EmptyWorkspaceDir, | ||
}, | ||
} | ||
for _, tt := range successTests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
dir, err := envbuilder.DefaultWorkspaceFolder(tt.gitURL) | ||
require.NoError(t, err) | ||
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", | ||
}, | ||
} | ||
for _, tt := range invalidTests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
dir, err := envbuilder.DefaultWorkspaceFolder(tt.invalidURL) | ||
require.NoError(t, err) | ||
require.Equal(t, constants.EmptyWorkspaceDir, dir) | ||
}) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package chmodfs | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/go-git/go-billy/v5" | ||
) | ||
|
||
func New(fs billy.Filesystem) billy.Filesystem { | ||
return &osfsWithChmod{ | ||
Filesystem: fs, | ||
} | ||
} | ||
|
||
type osfsWithChmod struct { | ||
billy.Filesystem | ||
} | ||
|
||
func (fs *osfsWithChmod) Chmod(name string, mode os.FileMode) error { | ||
return os.Chmod(name, mode) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package options | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/go-git/go-billy/v5/osfs" | ||
|
||
giturls "github.com/chainguard-dev/git-urls" | ||
"github.com/coder/envbuilder/constants" | ||
"github.com/coder/envbuilder/internal/chmodfs" | ||
) | ||
|
||
// DefaultWorkspaceFolder returns the default workspace folder | ||
// for a given repository URL. | ||
func DefaultWorkspaceFolder(repoURL string) string { | ||
if repoURL == "" { | ||
return constants.EmptyWorkspaceDir | ||
} | ||
parsed, err := giturls.Parse(repoURL) | ||
if err != nil { | ||
return constants.EmptyWorkspaceDir | ||
} | ||
name := strings.Split(parsed.Path, "/") | ||
hasOwnerAndRepo := len(name) >= 2 | ||
if !hasOwnerAndRepo { | ||
return constants.EmptyWorkspaceDir | ||
} | ||
repo := strings.TrimSuffix(name[len(name)-1], ".git") | ||
return fmt.Sprintf("/workspaces/%s", repo) | ||
} | ||
|
||
func (o *Options) SetDefaults() { | ||
// Temporarily removed these from the default settings to prevent conflicts | ||
// between current and legacy environment variables that add default values. | ||
// Once the legacy environment variables are phased out, this can be | ||
// reinstated to the previous default values. | ||
if len(o.IgnorePaths) == 0 { | ||
o.IgnorePaths = []string{ | ||
"/var/run", | ||
// KinD adds these paths to pods, so ignore them by default. | ||
"/product_uuid", "/product_name", | ||
} | ||
} | ||
if o.InitScript == "" { | ||
o.InitScript = "sleep infinity" | ||
} | ||
if o.InitCommand == "" { | ||
o.InitCommand = "/bin/sh" | ||
} | ||
|
||
if o.Filesystem == nil { | ||
o.Filesystem = chmodfs.New(osfs.New("/")) | ||
} | ||
if o.WorkspaceFolder == "" { | ||
o.WorkspaceFolder = DefaultWorkspaceFolder(o.GitURL) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
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/constants" | ||
"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: "fragment", | ||
gitURL: "https://github.com/coder/envbuilder.git#feature-branch", | ||
expected: "/workspaces/envbuilder", | ||
}, | ||
{ | ||
name: "empty", | ||
gitURL: "", | ||
expected: constants.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", | ||
}, | ||
} | ||
for _, tt := range invalidTests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
dir := options.DefaultWorkspaceFolder(tt.invalidURL) | ||
require.Equal(t, constants.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: constants.EmptyWorkspaceDir, | ||
} | ||
|
||
var actual options.Options | ||
actual.SetDefaults() | ||
assert.Equal(t, expected, actual) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know this was just moved and logic didn't change, but I wonder what this comment means. 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Related to
ENVBUILDER_
env prefix.