Skip to content

Commit

Permalink
return if already absPath
Browse files Browse the repository at this point in the history
Signed-off-by: Simar <[email protected]>
  • Loading branch information
simar7 committed Apr 10, 2023
1 parent a681ae1 commit a9440c6
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 11 deletions.
5 changes: 3 additions & 2 deletions pkg/scanners/terraform/parser/load_vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (
"github.com/zclconf/go-cty/cty"
)

func getAbsPath(path string) (string, error) {
p, err := filepath.Abs(path)
func getAbsPath(inputPath string) (string, error) {
p, err := filepath.Abs(inputPath)
if err != nil {
return "", fmt.Errorf("unable to determine path: %w", err)
}
Expand Down Expand Up @@ -71,6 +71,7 @@ func loadTFVarsFile(srcFS fs.FS, filename string) (map[string]cty.Value, error)
if err != nil {
return nil, err
}
absPath = filepath.ToSlash(absPath) // in memory fs is only slash based

src, err := fs.ReadFile(srcFS, absPath)
if err != nil {
Expand Down
13 changes: 4 additions & 9 deletions pkg/scanners/terraform/parser/load_vars_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,22 @@ package parser
import (
"testing"

"github.com/zclconf/go-cty/cty"

"github.com/aquasecurity/defsec/pkg/extrafs"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/zclconf/go-cty/cty"
)

func Test_TFVarsFile(t *testing.T) {
t.Run("tfvars file", func(t *testing.T) {
absPath, err := getAbsPath("testdata/tfvars/terraform.tfvars")
require.NoError(t, err)

vars, err := loadTFVars(extrafs.OSDir("/"), []string{absPath})
vars, err := loadTFVars(extrafs.OSDir("/"), []string{"testdata/tfvars/terraform.tfvars"})
require.NoError(t, err)
assert.Equal(t, "t2.large", vars["instance_type"].AsString())
})

t.Run("tfvars json file", func(t *testing.T) {
absPath, err := getAbsPath("testdata/tfvars/terraform.tfvars.json")
require.NoError(t, err)

vars, err := loadTFVars(extrafs.OSDir("/"), []string{absPath})
vars, err := loadTFVars(extrafs.OSDir("/"), []string{"testdata/tfvars/terraform.tfvars.json"})
require.NoError(t, err)
assert.Equal(t, "bar", vars["variable"].GetAttr("foo").GetAttr("default").AsString())
assert.Equal(t, "qux", vars["variable"].GetAttr("baz").AsString())
Expand Down

0 comments on commit a9440c6

Please sign in to comment.