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

Fix issue 904 #905

Merged
merged 2 commits into from
May 31, 2021
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
33 changes: 17 additions & 16 deletions modules/terraform/var-file.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import (
"fmt"
"io/ioutil"
"reflect"
"testing"

"github.com/gruntwork-io/terratest/modules/testing"

"github.com/hashicorp/hcl/v2/hclparse"
"github.com/stretchr/testify/require"
Expand All @@ -14,19 +15,19 @@ import (
ctyjson "github.com/zclconf/go-cty/cty/json"
)

// GetVariableAsStringFromVarFile Gets the string represention of a variable from a provided input file found in VarFile
// GetVariableAsStringFromVarFile Gets the string representation of a variable from a provided input file found in VarFile
// For list or map, use GetVariableAsListFromVarFile or GetVariableAsMapFromVarFile, respectively.
func GetVariableAsStringFromVarFile(t *testing.T, fileName string, key string) string {
func GetVariableAsStringFromVarFile(t testing.TestingT, fileName string, key string) string {
result, err := GetVariableAsStringFromVarFileE(t, fileName, key)
require.NoError(t, err)

return result
}

// GetVariableAsStringFromVarFileE Gets the string represention of a variable from a provided input file found in VarFile
// GetVariableAsStringFromVarFileE Gets the string representation of a variable from a provided input file found in VarFile
// Will return an error if GetAllVariablesFromVarFileE returns an error or the key provided does not exist in the file.
// For list or map, use GetVariableAsListFromVarFile or GetVariableAsMapFromVarFile, respectively.
func GetVariableAsStringFromVarFileE(t *testing.T, fileName string, key string) (string, error) {
func GetVariableAsStringFromVarFileE(t testing.TestingT, fileName string, key string) (string, error) {
var variables map[string]interface{}
err := GetAllVariablesFromVarFileE(t, fileName, &variables)
if err != nil {
Expand All @@ -42,18 +43,18 @@ func GetVariableAsStringFromVarFileE(t *testing.T, fileName string, key string)
return fmt.Sprintf("%v", variable), nil
}

// GetVariableAsMapFromVarFile Gets the map represention of a variable from a provided input file found in VarFile
// GetVariableAsMapFromVarFile Gets the map representation of a variable from a provided input file found in VarFile
// Note that this returns a map of strings. For maps containing complex types, use GetAllVariablesFromVarFile.
func GetVariableAsMapFromVarFile(t *testing.T, fileName string, key string) map[string]string {
func GetVariableAsMapFromVarFile(t testing.TestingT, fileName string, key string) map[string]string {
result, err := GetVariableAsMapFromVarFileE(t, fileName, key)
require.NoError(t, err)
return result
}

// GetVariableAsMapFromVarFileE Gets the map represention of a variable from a provided input file found in VarFile.
// GetVariableAsMapFromVarFileE Gets the map representation of a variable from a provided input file found in VarFile.
// Note that this returns a map of strings. For maps containing complex types, use GetAllVariablesFromVarFile
// Returns an error if GetAllVariablesFromVarFileE returns an error, the key provided does not exist, or the value associated with the key is not a map
func GetVariableAsMapFromVarFileE(t *testing.T, fileName string, key string) (map[string]string, error) {
func GetVariableAsMapFromVarFileE(t testing.TestingT, fileName string, key string) (map[string]string, error) {
var variables map[string]interface{}
err := GetAllVariablesFromVarFileE(t, fileName, &variables)
if err != nil {
Expand All @@ -77,19 +78,19 @@ func GetVariableAsMapFromVarFileE(t *testing.T, fileName string, key string) (ma
return resultMap, nil
}

// GetVariableAsListFromVarFile Gets the string list represention of a variable from a provided input file found in VarFile
// GetVariableAsListFromVarFile Gets the string list representation of a variable from a provided input file found in VarFile
// Note that this returns a list of strings. For lists containing complex types, use GetAllVariablesFromVarFile.
func GetVariableAsListFromVarFile(t *testing.T, fileName string, key string) []string {
func GetVariableAsListFromVarFile(t testing.TestingT, fileName string, key string) []string {
result, err := GetVariableAsListFromVarFileE(t, fileName, key)
require.NoError(t, err)

return result
}

// GetVariableAsListFromVarFileE Gets the string list represention of a variable from a provided input file found in VarFile
// GetVariableAsListFromVarFileE Gets the string list representation of a variable from a provided input file found in VarFile
// Note that this returns a list of strings. For lists containing complex types, use GetAllVariablesFromVarFile.
// Will return error if GetAllVariablesFromVarFileE returns an error, the key provided does not exist, or the value associated with the key is not a list
func GetVariableAsListFromVarFileE(t *testing.T, fileName string, key string) ([]string, error) {
func GetVariableAsListFromVarFileE(t testing.TestingT, fileName string, key string) ([]string, error) {
var variables map[string]interface{}
err := GetAllVariablesFromVarFileE(t, fileName, &variables)
if err != nil {
Expand All @@ -113,17 +114,17 @@ func GetVariableAsListFromVarFileE(t *testing.T, fileName string, key string) ([
return resultArray, nil
}

// GetAllVariablesFromVarFileE Parses all data from a provided input file found ind in VarFile and stores the result in
// GetAllVariablesFromVarFile Parses all data from a provided input file found ind in VarFile and stores the result in
// the value pointed to by out.
func GetAllVariablesFromVarFile(t *testing.T, fileName string, out interface{}) {
func GetAllVariablesFromVarFile(t testing.TestingT, fileName string, out interface{}) {
err := GetAllVariablesFromVarFileE(t, fileName, out)
require.NoError(t, err)
}

// GetAllVariablesFromVarFileE Parses all data from a provided input file found ind in VarFile and stores the result in
// the value pointed to by out. Returns an error if the specified file does not exist, the specified file is not
// readable, or the specified file cannot be decoded from HCL.
func GetAllVariablesFromVarFileE(t *testing.T, fileName string, out interface{}) error {
func GetAllVariablesFromVarFileE(t testing.TestingT, fileName string, out interface{}) error {
fileContents, err := ioutil.ReadFile(fileName)
if err != nil {
return err
Expand Down