Skip to content

Commit 99d87eb

Browse files
committed
Accept optional generated config for a config directory
1 parent 26832b0 commit 99d87eb

File tree

5 files changed

+124
-1
lines changed

5 files changed

+124
-1
lines changed

internal/teststep/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ type Config interface {
4545
HasProviderBlock(context.Context) (bool, error)
4646
HasTerraformBlock(context.Context) (bool, error)
4747
Write(context.Context, string) error
48+
AppendGeneratedConfig(context.Context, string) Config
4849
}
4950

5051
// PrepareConfigurationRequest is used to simplify the generation of

internal/teststep/directory.go

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,18 @@ package teststep
55

66
import (
77
"context"
8+
"fmt"
9+
"hash/crc32"
810
"os"
911
"path/filepath"
1012
)
1113

1214
var _ Config = configurationDirectory{}
1315

16+
// not threadsafe
1417
type configurationDirectory struct {
15-
directory string
18+
directory string
19+
generatedFiles map[string]string
1620
}
1721

1822
// HasConfigurationFiles is used during validation to ensure that
@@ -85,10 +89,43 @@ func (c configurationDirectory) Write(ctx context.Context, dest string) error {
8589
}
8690

8791
err := copyFiles(configDirectory, dest)
92+
if err != nil {
93+
return err
94+
}
8895

96+
err = c.writeGeneratedFiles(dest)
8997
if err != nil {
9098
return err
9199
}
92100

93101
return nil
94102
}
103+
104+
func (c configurationDirectory) AppendGeneratedConfig(_ context.Context, config string) Config {
105+
if c.generatedFiles == nil {
106+
c.generatedFiles = make(map[string]string)
107+
}
108+
109+
checksum := crc32.Checksum([]byte(config), crc32.IEEETable)
110+
filename := fmt.Sprintf("terraform_plugin_test_%d.tf", checksum)
111+
112+
fmt.Println("Appending generated file:", filename)
113+
c.generatedFiles[filename] = config
114+
return c
115+
}
116+
117+
func (c configurationDirectory) writeGeneratedFiles(dstPath string) error {
118+
fmt.Println("Count of generated files:", len(c.generatedFiles))
119+
for filename, config := range c.generatedFiles {
120+
outFilename := filepath.Join(dstPath, filename)
121+
fmt.Println("Writing generated file:", outFilename)
122+
123+
err := os.WriteFile(outFilename, []byte(config), 0700)
124+
if err != nil {
125+
return err
126+
}
127+
fmt.Println("Wrote generated file:", outFilename)
128+
}
129+
130+
return nil
131+
}

internal/teststep/directory_test.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,81 @@ func TestConfigurationDirectory_Write_AbsolutePath(t *testing.T) {
607607
}
608608
}
609609

610+
func TestConfigurationDirectory_Write_WithGeneratedFiles(t *testing.T) {
611+
t.Parallel()
612+
613+
testCases := map[string]struct {
614+
configDirectory configurationDirectory
615+
expectedError *regexp.Regexp
616+
}{
617+
"dir-single-file": {
618+
configDirectory: configurationDirectory{
619+
directory: "testdata/random",
620+
generatedFiles: map[string]string{
621+
"import.tf": `terraform {\nimport\n{\nto = satellite.the_moon\nid = "moon"\n}\n}\n`,
622+
},
623+
},
624+
},
625+
}
626+
627+
for name, testCase := range testCases {
628+
t.Run(name, func(t *testing.T) {
629+
t.Parallel()
630+
631+
tempDir := t.TempDir()
632+
633+
err := testCase.configDirectory.Write(context.Background(), tempDir)
634+
if err != nil {
635+
t.Errorf("unexpected error %s", err)
636+
}
637+
638+
dirEntries, err := os.ReadDir(testCase.configDirectory.directory)
639+
if err != nil {
640+
t.Errorf("error reading directory: %s", err)
641+
}
642+
643+
tempDirEntries, err := os.ReadDir(tempDir)
644+
645+
if err != nil {
646+
t.Errorf("error reading temp directory: %s", err)
647+
}
648+
649+
if len(tempDirEntries)-len(dirEntries) != 1 {
650+
t.Errorf("expected %d dir entries, got %d dir entries", len(dirEntries)+1, tempDirEntries)
651+
}
652+
653+
for _, entry := range dirEntries {
654+
filename := entry.Name()
655+
expectedContent, err := os.ReadFile(filepath.Join(testCase.configDirectory.directory, filename))
656+
if err != nil {
657+
t.Errorf("error reading file from config directory %s: %s", filename, err)
658+
}
659+
660+
content, err := os.ReadFile(filepath.Join(tempDir, filename))
661+
if err != nil {
662+
t.Errorf("error reading generated file %s: %s", filename, err)
663+
}
664+
665+
if diff := cmp.Diff(expectedContent, content); diff != "" {
666+
t.Errorf("unexpected difference: %s", diff)
667+
}
668+
}
669+
670+
generatedFiles := testCase.configDirectory.generatedFiles
671+
for filename, expectedContent := range generatedFiles {
672+
content, err := os.ReadFile(filepath.Join(tempDir, filename))
673+
if err != nil {
674+
t.Errorf("error reading generated file %s: %s", filename, err)
675+
}
676+
677+
if diff := cmp.Diff([]byte(expectedContent), content); diff != "" {
678+
t.Errorf("unexpected difference: %s", diff)
679+
}
680+
}
681+
})
682+
}
683+
}
684+
610685
var fileInfoComparer = cmp.Comparer(func(x, y os.FileInfo) bool {
611686
if x.Name() != y.Name() {
612687
return false

internal/teststep/file.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,7 @@ func (c configurationFile) Write(ctx context.Context, dest string) error {
9292

9393
return nil
9494
}
95+
96+
func (c configurationFile) AppendGeneratedConfig(_ context.Context, config string) Config {
97+
return c
98+
}

internal/teststep/string.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,11 @@ func (c configurationString) Write(ctx context.Context, dest string) error {
5757
return err
5858
}
5959

60+
fmt.Println("Wrote config to", outFilename)
61+
6062
return nil
6163
}
64+
65+
func (c configurationString) AppendGeneratedConfig(_ context.Context, config string) Config {
66+
return c
67+
}

0 commit comments

Comments
 (0)