|
| 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