@@ -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+
610685var fileInfoComparer = cmp .Comparer (func (x , y os.FileInfo ) bool {
611686 if x .Name () != y .Name () {
612687 return false
0 commit comments