|
| 1 | +package envbuilder_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/coder/envbuilder" |
| 8 | + "github.com/coder/serpent" |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | +) |
| 11 | + |
| 12 | +// TestEnvOptionParsing tests that given environment variables of different types are handled as expected. |
| 13 | +func TestEnvOptionParsing(t *testing.T) { |
| 14 | + t.Run("string", func(t *testing.T) { |
| 15 | + const val = "setup.sh" |
| 16 | + t.Setenv("SETUP_SCRIPT", val) |
| 17 | + o := runCLI() |
| 18 | + require.Equal(t, o.SetupScript, val) |
| 19 | + }) |
| 20 | + |
| 21 | + t.Run("int", func(t *testing.T) { |
| 22 | + t.Setenv("CACHE_TTL_DAYS", "7") |
| 23 | + o := runCLI() |
| 24 | + require.Equal(t, o.CacheTTLDays, int64(7)) |
| 25 | + }) |
| 26 | + |
| 27 | + t.Run("string array", func(t *testing.T) { |
| 28 | + t.Setenv("IGNORE_PATHS", "/var,/temp") |
| 29 | + o := runCLI() |
| 30 | + require.Equal(t, o.IgnorePaths, []string{"/var", "/temp"}) |
| 31 | + }) |
| 32 | + |
| 33 | + t.Run("bool", func(t *testing.T) { |
| 34 | + t.Run("lowercase", func(t *testing.T) { |
| 35 | + t.Setenv("SKIP_REBUILD", "true") |
| 36 | + t.Setenv("GIT_CLONE_SINGLE_BRANCH", "false") |
| 37 | + o := runCLI() |
| 38 | + require.True(t, o.SkipRebuild) |
| 39 | + require.False(t, o.GitCloneSingleBranch) |
| 40 | + }) |
| 41 | + |
| 42 | + t.Run("uppercase", func(t *testing.T) { |
| 43 | + t.Setenv("SKIP_REBUILD", "TRUE") |
| 44 | + t.Setenv("GIT_CLONE_SINGLE_BRANCH", "FALSE") |
| 45 | + o := runCLI() |
| 46 | + require.True(t, o.SkipRebuild) |
| 47 | + require.False(t, o.GitCloneSingleBranch) |
| 48 | + }) |
| 49 | + |
| 50 | + t.Run("numeric", func(t *testing.T) { |
| 51 | + t.Setenv("SKIP_REBUILD", "1") |
| 52 | + t.Setenv("GIT_CLONE_SINGLE_BRANCH", "0") |
| 53 | + o := runCLI() |
| 54 | + require.True(t, o.SkipRebuild) |
| 55 | + require.False(t, o.GitCloneSingleBranch) |
| 56 | + }) |
| 57 | + |
| 58 | + t.Run("empty", func(t *testing.T) { |
| 59 | + t.Setenv("GIT_CLONE_SINGLE_BRANCH", "") |
| 60 | + o := runCLI() |
| 61 | + require.False(t, o.GitCloneSingleBranch) |
| 62 | + }) |
| 63 | + }) |
| 64 | +} |
| 65 | + |
| 66 | +func runCLI() envbuilder.Options { |
| 67 | + var o envbuilder.Options |
| 68 | + cmd := serpent.Command{ |
| 69 | + Options: o.CLI(), |
| 70 | + Handler: func(inv *serpent.Invocation) error { |
| 71 | + return nil |
| 72 | + }, |
| 73 | + } |
| 74 | + |
| 75 | + i := cmd.Invoke().WithOS() |
| 76 | + fakeIO(i) |
| 77 | + err := i.Run() |
| 78 | + |
| 79 | + if err != nil { |
| 80 | + panic("failed to run CLI: " + err.Error()) |
| 81 | + } |
| 82 | + |
| 83 | + return o |
| 84 | +} |
| 85 | + |
| 86 | +type ioBufs struct { |
| 87 | + Stdin bytes.Buffer |
| 88 | + Stdout bytes.Buffer |
| 89 | + Stderr bytes.Buffer |
| 90 | +} |
| 91 | + |
| 92 | +func fakeIO(i *serpent.Invocation) *ioBufs { |
| 93 | + var b ioBufs |
| 94 | + i.Stdout = &b.Stdout |
| 95 | + i.Stderr = &b.Stderr |
| 96 | + i.Stdin = &b.Stdin |
| 97 | + return &b |
| 98 | +} |
0 commit comments