Skip to content

Commit

Permalink
WIP - Decode Terraform Test files individually as they don't share co…
Browse files Browse the repository at this point in the history
…ntext
  • Loading branch information
ansgarm committed Dec 6, 2024
1 parent c0ab11d commit b936c64
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 24 deletions.
18 changes: 4 additions & 14 deletions earlydecoder/tests/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,17 @@
package earlydecoder

import (
"sort"

"github.com/hashicorp/hcl/v2"
tftest "github.com/hashicorp/terraform-schema/test"
)

func LoadTest(path string, files map[string]*hcl.File) (*tftest.Meta, hcl.Diagnostics) {
var diags hcl.Diagnostics
filenames := make([]string, 0)

func LoadTest(path string, filename string, file *hcl.File) (*tftest.Meta, hcl.Diagnostics) {
mod := newDecodedTest()
for filename, f := range files {
filenames = append(filenames, filename)
fDiags := loadTestFromFile(f, mod)
diags = append(diags, fDiags...)
}
diags := loadTestFromFile(file, mod)

sort.Strings(filenames)
// TODO: a lot of mapping to do here from decoded test to tftest.Meta

return &tftest.Meta{
Path: path,
Filenames: filenames,
Path: path,
}, diags
}
11 changes: 4 additions & 7 deletions earlydecoder/tests/decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ func TestLoadTest(t *testing.T) {
"empty config",
``,
&tftest.Meta{
Path: path,
Filenames: []string{"test.tftest.hcl"},
Path: path,
},
nil,
},
Expand All @@ -49,15 +48,13 @@ func TestLoadTest(t *testing.T) {
func runTestCases(testCases []testCase, t *testing.T, path string) {
for i, tc := range testCases {
t.Run(fmt.Sprintf("%d-%s", i, tc.name), func(t *testing.T) {
f, diags := hclsyntax.ParseConfig([]byte(tc.cfg), "test.tftest.hcl", hcl.InitialPos)
file, diags := hclsyntax.ParseConfig([]byte(tc.cfg), "test.tftest.hcl", hcl.InitialPos)
if len(diags) > 0 {
t.Fatal(diags)
}
files := map[string]*hcl.File{
"test.tftest.hcl": f,
}
filename := "test.tftest.hcl"

meta, diags := LoadTest(path, files)
meta, diags := LoadTest(path, filename, file)

if diff := cmp.Diff(tc.expectedError, diags, customComparer...); diff != "" {
t.Fatalf("expected errors doesn't match: %s", diff)
Expand Down
3 changes: 3 additions & 0 deletions earlydecoder/tests/load_tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import (

// decodedTest is the type representing a decoded Terraform test.
type decodedTest struct {

// all the blocks of a single test file

}

func newDecodedTest() *decodedTest {
Expand Down
7 changes: 4 additions & 3 deletions test/meta.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0

package stack
package test

type Meta struct {
Path string
Filenames []string
Path string

RunBlocks []string // TODO: just for testing, change to proper type
}

0 comments on commit b936c64

Please sign in to comment.