Skip to content
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
37 changes: 37 additions & 0 deletions pkg/scenario/client.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package scenario

import (
"fmt"
"path/filepath"
"testing"
"time"

"github.com/Excoriate/tftest/pkg/utils"

"github.com/Excoriate/tftest/pkg/tfvars"

"github.com/Excoriate/tftest/pkg/cloudprovider"
"github.com/Excoriate/tftest/pkg/validation"

Expand Down Expand Up @@ -118,6 +123,38 @@ func WithVarFiles(workdir string, varFiles ...string) OptFn {
}
}

func WithScannedTFVars(workdir, fixturesDir string) OptFn {
return func(o *Options) error {
if err := validation.IsValidTFDir(workdir); err != nil {
return err
}

fixturesDirPath := filepath.Join(workdir, fixturesDir)

if err := validation.IsValidTFDir(fixturesDirPath); err != nil {
return err
}

hasTFVars, err := validation.HasTFVarFiles(fixturesDirPath)
if err != nil {
return err
}

if !hasTFVars {
return fmt.Errorf("the Terraform module %s with this fixtures directory %s does not have any .tfvars files", workdir, fixturesDir)
}

tfVarsPath, tfVarsErr := tfvars.GetTFVarsFromWorkdir(workdir)
if tfVarsErr != nil {
return tfVarsErr
}

o.varFiles = utils.MergeSlices(o.varFiles, tfVarsPath)

return nil
}
}

func NewWithOptions(t *testing.T, workdir string, opts ...OptFn) (*Client, error) {
o := &Options{}
for _, opt := range opts {
Expand Down
34 changes: 34 additions & 0 deletions pkg/tfvars/tfvars.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package tfvars

import (
"fmt"
"os"
"path/filepath"
)

// GetTFVarsFromWorkdir scans the provided workdir directory for all .tfvars files
// and returns their filenames. If workdir is empty, it returns an error.
func GetTFVarsFromWorkdir(workdir string) ([]string, error) {
if workdir == "" {
return nil, fmt.Errorf("workdir cannot be empty")
}

var tfvarFiles []string

// Use filepath.Walk to traverse the directory tree rooted at workdir
err := filepath.Walk(workdir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}

if filepath.Ext(path) == ".tfvars" {
// If it does, add its filename to the slice
tfvarFiles = append(tfvarFiles, filepath.Base(path))
}

// Return nil to continue the walk
return nil
})

return tfvarFiles, err
}
11 changes: 11 additions & 0 deletions pkg/utils/data.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package utils

func MergeSlices(slices ...[]string) []string {
var merged []string

for _, slice := range slices {
merged = append(merged, slice...)
}

return merged
}
15 changes: 15 additions & 0 deletions pkg/validation/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,18 @@ func IsValidTFModuleDir(path string) error {

return nil
}

// HasTFVarFiles checks if the given directory has Terraform variable files.
// If the directory does not have any Terraform variable files, it returns an error.
func HasTFVarFiles(path string) (bool, error) {
if path == "" {
return false, nil
}

files, err := filepath.Glob(filepath.Join(path, "*.tfvars"))
if err != nil {
return false, fmt.Errorf("failed to list files in directory: %v", err)
}

return len(files) > 0, nil
}
39 changes: 0 additions & 39 deletions test/data/tf-random/.test-data/TerraformOptions.json

This file was deleted.

8 changes: 8 additions & 0 deletions test/examples/simple/with_options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,11 @@ func TestWithRetryOptions(t *testing.T) {

s.Stg.PlanStage(t, s.GetTerraformOptions())
}

func TestWithScannedTFVars(t *testing.T) {
workdir := "../../data/tf-random"
s, err := scenario.NewWithOptions(t, workdir, scenario.WithScannedTFVars(workdir, "fixtures"))
assert.NoErrorf(t, err, "Failed to create scenario: %s", err)

s.Stg.PlanStage(t, s.GetTerraformOptions())
}