fix(misconf): do not evaluate TF when a load error occurs (#7109)

Signed-off-by: nikpivkin <nikita.pivkin@smartforce.io>
This commit is contained in:
Nikita Pivkin
2024-07-10 10:02:40 +07:00
committed by GitHub
parent 7cbdb0a0b5
commit f27c236d6e
2 changed files with 23 additions and 0 deletions

View File

@@ -268,7 +268,10 @@ func (p *Parser) EvaluateAll(ctx context.Context) (terraform.Modules, cty.Value,
e, err := p.Load(ctx)
if errors.Is(err, ErrNoFiles) {
return nil, cty.NilVal, nil
} else if err != nil {
return nil, cty.NilVal, err
}
modules, fsMap := e.EvaluateAll(ctx)
p.debug.Log("Finished parsing module '%s'.", p.moduleName)
p.fsMap = fsMap

View File

@@ -6,6 +6,7 @@ import (
"path/filepath"
"sort"
"testing"
"testing/fstest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
@@ -1725,3 +1726,22 @@ func Test_LoadLocalCachedModule(t *testing.T) {
bucketName := buckets[0].GetAttribute("bucket").Value().AsString()
assert.Equal(t, "my-s3-bucket", bucketName)
}
func TestTFVarsFileDoesNotExist(t *testing.T) {
fsys := fstest.MapFS{
"main.tf": &fstest.MapFile{
Data: []byte(``),
},
}
parser := New(
fsys, "",
OptionStopOnHCLError(true),
OptionWithDownloads(false),
OptionWithTFVarsPaths("main.tfvars"),
)
require.NoError(t, parser.ParseFS(context.TODO(), "."))
_, _, err := parser.EvaluateAll(context.TODO())
assert.ErrorContains(t, err, "file does not exist")
}