@@ -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
1617func 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\n terraform {}" ,
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+ }
0 commit comments