Skip to content

test: add test for options env #161

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 7 commits into from
Apr 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 98 additions & 0 deletions options_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package envbuilder_test

import (
"bytes"
"testing"

"github.com/coder/envbuilder"
"github.com/coder/serpent"
"github.com/stretchr/testify/require"
)

// TestEnvOptionParsing tests that given environment variables of different types are handled as expected.
func TestEnvOptionParsing(t *testing.T) {
t.Run("string", func(t *testing.T) {
const val = "setup.sh"
t.Setenv("SETUP_SCRIPT", val)
o := runCLI()
require.Equal(t, o.SetupScript, val)
})

t.Run("int", func(t *testing.T) {
t.Setenv("CACHE_TTL_DAYS", "7")
o := runCLI()
require.Equal(t, o.CacheTTLDays, int64(7))
})

t.Run("string array", func(t *testing.T) {
t.Setenv("IGNORE_PATHS", "/var,/temp")
o := runCLI()
require.Equal(t, o.IgnorePaths, []string{"/var", "/temp"})
})

t.Run("bool", func(t *testing.T) {
t.Run("lowercase", func(t *testing.T) {
t.Setenv("SKIP_REBUILD", "true")
t.Setenv("GIT_CLONE_SINGLE_BRANCH", "false")
o := runCLI()
require.True(t, o.SkipRebuild)
require.False(t, o.GitCloneSingleBranch)
})

t.Run("uppercase", func(t *testing.T) {
t.Setenv("SKIP_REBUILD", "TRUE")
t.Setenv("GIT_CLONE_SINGLE_BRANCH", "FALSE")
o := runCLI()
require.True(t, o.SkipRebuild)
require.False(t, o.GitCloneSingleBranch)
})

t.Run("numeric", func(t *testing.T) {
t.Setenv("SKIP_REBUILD", "1")
t.Setenv("GIT_CLONE_SINGLE_BRANCH", "0")
o := runCLI()
require.True(t, o.SkipRebuild)
require.False(t, o.GitCloneSingleBranch)
})

t.Run("empty", func(t *testing.T) {
t.Setenv("GIT_CLONE_SINGLE_BRANCH", "")
o := runCLI()
require.False(t, o.GitCloneSingleBranch)
})
})
}

func runCLI() envbuilder.Options {
var o envbuilder.Options
cmd := serpent.Command{
Options: o.CLI(),
Handler: func(inv *serpent.Invocation) error {
return nil
},
}

i := cmd.Invoke().WithOS()
fakeIO(i)
err := i.Run()

if err != nil {
panic("failed to run CLI: " + err.Error())
}

return o
}

type ioBufs struct {
Stdin bytes.Buffer
Stdout bytes.Buffer
Stderr bytes.Buffer
}

func fakeIO(i *serpent.Invocation) *ioBufs {
var b ioBufs
i.Stdout = &b.Stdout
i.Stderr = &b.Stderr
i.Stdin = &b.Stdin
return &b
}
Loading