Skip to content

Commit 1d4d6b7

Browse files
committed
Add a test for appending to ConfigFile
1 parent 507b3b2 commit 1d4d6b7

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

internal/teststep/file_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"testing"
1212

1313
"github.com/google/go-cmp/cmp"
14+
"github.com/hashicorp/terraform-plugin-testing/config"
1415
)
1516

1617
func TestConfigurationFile_HasProviderBlock(t *testing.T) {
@@ -550,3 +551,49 @@ func TestConfigurationFile_Write_AbsolutePath(t *testing.T) {
550551
})
551552
}
552553
}
554+
555+
func TestConfigFile_Append(t *testing.T) {
556+
t.Parallel()
557+
558+
testCases := map[string]struct {
559+
filename string
560+
appendContent string
561+
expectedContent string
562+
}{
563+
"append content to a ConfigFile": {
564+
filename: `testdata/main.tf`, // Contains `// Hello world`
565+
appendContent: `terraform {}`,
566+
expectedContent: "// Hello world\nterraform {}",
567+
},
568+
}
569+
570+
for name, testCase := range testCases {
571+
t.Run(name, func(t *testing.T) {
572+
t.Parallel()
573+
574+
prepareConfigRequest := PrepareConfigurationRequest{
575+
File: func(config.TestStepConfigRequest) string {
576+
return testCase.filename
577+
},
578+
}
579+
580+
teststepConfig := Configuration(prepareConfigRequest.Exec())
581+
teststepConfig = teststepConfig.Append(testCase.appendContent)
582+
583+
tempdir := t.TempDir()
584+
if err := teststepConfig.Write(context.Background(), tempdir); err != nil {
585+
t.Fatalf("failed to write file: %s", err)
586+
}
587+
588+
got, err := os.ReadFile(filepath.Join(tempdir, filepath.Base(testCase.filename)))
589+
if err != nil {
590+
t.Fatalf("failed to read file: %s", err)
591+
}
592+
593+
gotS := string(got[:])
594+
if diff := cmp.Diff(testCase.expectedContent, gotS); diff != "" {
595+
t.Errorf("expected %+v, got %+v", testCase.expectedContent, gotS)
596+
}
597+
})
598+
}
599+
}

internal/teststep/testdata/main.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// Hello world

0 commit comments

Comments
 (0)