Skip to content
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

test: add basic tests to the TM_ARG_ env support. #1910

Merged
merged 4 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
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
129 changes: 129 additions & 0 deletions e2etests/core/cli_args_env_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
// Copyright 2024 Terramate GmbH
// SPDX-License-Identifier: MPL-2.0

package core_test

import (
"path/filepath"
"regexp"
"testing"

. "github.com/terramate-io/terramate/e2etests/internal/runner"
"github.com/terramate-io/terramate/test/sandbox"
)

func TestBasicArgEnvFlags(t *testing.T) {
t.Parallel()

s := sandbox.NoGit(t, true)
s.BuildTree([]string{
`s:stacks/a`,
`s:stacks/b`,
`s:stacks/c`,
`s:stacks/a/1`,
`s:stacks/a/2`,
`s:stacks/a/3`,

// not a stack
`d:stacks/d`,
})

t.Run("TM_ARG_CHDIR", func(t *testing.T) {
t.Parallel()

type testcase struct {
env []string
expected string
}

for _, tc := range []testcase{
{
expected: nljoin(
"stacks/a",
"stacks/a/1",
"stacks/a/2",
"stacks/a/3",
"stacks/b",
"stacks/c",
),
},
{
env: []string{"TM_ARG_CHDIR=."},
expected: nljoin(
"stacks/a",
"stacks/a/1",
"stacks/a/2",
"stacks/a/3",
"stacks/b",
"stacks/c",
),
},
{
env: []string{"TM_ARG_CHDIR=stacks"},
expected: nljoin(
"a",
"a/1",
"a/2",
"a/3",
"b",
"c",
),
},
{
env: []string{"TM_ARG_CHDIR=stacks/a"},
expected: nljoin(
".",
"1",
"2",
"3",
),
},
{
env: []string{"TM_ARG_CHDIR=" + filepath.Join(s.RootDir(), "stacks/a")},
expected: nljoin(
".",
"1",
"2",
"3",
),
},
{
env: []string{"TM_ARG_CHDIR=stacks/d"},
},
} {
tc := tc
tmcli := NewCLI(t, "", tc.env...)
tmcli.SetWorkingDir(s.RootDir())
AssertRunResult(t,
tmcli.Run("list"),
RunExpected{
Stdout: tc.expected,
},
)
}
})

t.Run("TM_ARG_RUN_NO_RECURSIVE=true", func(t *testing.T) {
t.Parallel()

// boolean flags accepts "true" and "false"
tmcli := NewCLI(t, s.RootDir(), "TM_ARG_RUN_NO_RECURSIVE=true")
AssertRunResult(t,
tmcli.Run("run", "--", HelperPath, "echo", "hello"),
RunExpected{
Status: 1,
StderrRegex: regexp.QuoteMeta(`Error: --no-recursive provided but no stack found`),
},
)

// boolean flags also accepts "0" and "1"
tmcli = NewCLI(t, s.RootDir(), "TM_ARG_RUN_NO_RECURSIVE=1")
AssertRunResult(t,
tmcli.Run("run", "--", HelperPath, "echo", "hello"),
RunExpected{
Status: 1,
StderrRegex: regexp.QuoteMeta(`Error: --no-recursive provided but no stack found`),
},
)
})
}
8 changes: 8 additions & 0 deletions e2etests/internal/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type (
environ []string
AppendEnv []string

wd string
userDir string
}

Expand Down Expand Up @@ -124,6 +125,12 @@ func NewInteropCLI(t *testing.T, chdir string, env ...string) CLI {
return tm
}

// SetWorkingDir sets the process working directory. Not to be confused with tm.Chdir
// which controls the `--chdir` flag.
func (tm *CLI) SetWorkingDir(dir string) {
tm.wd = dir
}

// PrependToPath prepend the provided directory to the OS's PATH
// environment variable in a portable way.
func (tm *CLI) PrependToPath(dir string) {
Expand Down Expand Up @@ -232,6 +239,7 @@ func (tm CLI) NewCmd(args ...string) *Cmd {
cmd.Stderr = stderr
cmd.Stdin = stdin
cmd.Env = env
cmd.Dir = tm.wd

return &Cmd{
t: t,
Expand Down
Loading