Skip to content

Commit

Permalink
Terraform test directory must be local to the config directory (#33760)
Browse files Browse the repository at this point in the history
  • Loading branch information
liamcervante authored Aug 29, 2023
1 parent c761efb commit 4c83e2d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
13 changes: 13 additions & 0 deletions internal/command/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package command

import (
"context"
"path/filepath"
"strings"
"time"

Expand Down Expand Up @@ -94,6 +95,18 @@ func (c *TestCommand) Run(rawArgs []string) int {

view := views.NewTest(args.ViewType, c.View)

// The specified testing directory must be a relative path, and it must
// point to a directory that is a descendent of the configuration directory.
if !filepath.IsLocal(args.TestDirectory) {
diags = diags.Append(tfdiags.Sourceless(
tfdiags.Error,
"Invalid testing directory",
"The testing directory must be a relative path pointing to a directory local to the configuration directory."))

view.Diagnostics(nil, nil, diags)
return 1
}

config, configDiags := c.loadConfigWithTests(".", args.TestDirectory)
diags = diags.Append(configDiags)
if configDiags.HasErrors() {
Expand Down
14 changes: 13 additions & 1 deletion internal/command/test_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,18 @@ func TestTest(t *testing.T) {
expected: "1 passed, 0 failed.",
code: 0,
},
"simple_pass_bad_test_directory": {
override: "simple_pass",
args: []string{"-test-directory", "../tests"},
expected: "Invalid testing directory",
code: 1,
},
"simple_pass_bad_test_directory_abs": {
override: "simple_pass",
args: []string{"-test-directory", "/home/username/config/tests"},
expected: "Invalid testing directory",
code: 1,
},
"pass_with_locals": {
expected: "1 passed, 0 failed.",
code: 0,
Expand Down Expand Up @@ -206,7 +218,7 @@ func TestTest(t *testing.T) {
t.Errorf("expected status code %d but got %d", tc.code, code)
}

if !strings.Contains(output.Stdout(), tc.expected) {
if !strings.Contains(output.All(), tc.expected) {
t.Errorf("output didn't contain expected string:\n\n%s", output.All())
}

Expand Down

0 comments on commit 4c83e2d

Please sign in to comment.