Skip to content

Commit

Permalink
test: add basic tests to the TM_ARG_ env support. (#1910)
Browse files Browse the repository at this point in the history
## What this PR does / why we need it:

Some tests for the #1850 PR.

Note: tests are failing because it's not rebased on top of #1850 as
original PR contained conflicts at the time I did this.

## Which issue(s) this PR fixes:
none

## Special notes for your reviewer:

## Does this PR introduce a user-facing change?
```
no, just tests
```
  • Loading branch information
i4ki authored Oct 15, 2024
2 parents 47f1802 + d9288d6 commit 7633e92
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 0 deletions.
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

0 comments on commit 7633e92

Please sign in to comment.