@@ -51,75 +51,84 @@ func TestConvertStateGoldenFiles(t *testing.T) {
5151testFiles ,err := os .ReadDir (filepath .Join (testResourceDirectories ,testDirectory .Name ()))
5252require .NoError (t ,err )
5353
54- planIdx := slices .IndexFunc (testFiles ,func (entry os.DirEntry )bool {
55- return strings .HasSuffix (entry .Name (),".tfplan.json" )
56- })
57- dotIdx := slices .IndexFunc (testFiles ,func (entry os.DirEntry )bool {
58- return strings .HasSuffix (entry .Name (),".tfplan.dot" )
59- })
54+ for _ ,step := range []string {"plan" ,"state" } {
55+ planIdx := slices .IndexFunc (testFiles ,func (entry os.DirEntry )bool {
56+ return strings .HasSuffix (entry .Name (),fmt .Sprintf (".tf%s.json" ,step ))
57+ })
58+ dotIdx := slices .IndexFunc (testFiles ,func (entry os.DirEntry )bool {
59+ return strings .HasSuffix (entry .Name (),fmt .Sprintf (".tf%s.dot" ,step ))
60+ })
6061
61- if planIdx == - 1 || dotIdx == - 1 {
62- continue
63- }
62+ if planIdx == - 1 || dotIdx == - 1 {
63+ continue
64+ }
6465
65- t .Run (testDirectory .Name (),func (t * testing.T ) {
66- testDirectoryPath := filepath .Join (testResourceDirectories ,testDirectory .Name ())
67- planFile := filepath .Join (testDirectoryPath ,testFiles [planIdx ].Name ())
68- dotFile := filepath .Join (testDirectoryPath ,testFiles [dotIdx ].Name ())
69- t .Parallel ()
70- ctx := testutil .Context (t ,testutil .WaitMedium )
71- logger := slogtest .Make (t ,nil )
66+ t .Run (step + "_" + testDirectory .Name (),func (t * testing.T ) {
67+ testDirectoryPath := filepath .Join (testResourceDirectories ,testDirectory .Name ())
68+ planFile := filepath .Join (testDirectoryPath ,testFiles [planIdx ].Name ())
69+ dotFile := filepath .Join (testDirectoryPath ,testFiles [dotIdx ].Name ())
70+ t .Parallel ()
71+ ctx := testutil .Context (t ,testutil .WaitMedium )
72+ logger := slogtest .Make (t ,nil )
7273
73- // Gather plan
74- tfPlanRaw ,err := os .ReadFile (planFile )
75- require .NoError (t ,err )
74+ // Gather plan
75+ tfStepRaw ,err := os .ReadFile (planFile )
76+ require .NoError (t ,err )
7677
77- var tfPlan tfjson.Plan
78- err = json .Unmarshal (tfPlanRaw ,& tfPlan )
79- require .NoError (t ,err )
78+ var modules []* tfjson.StateModule
79+ switch step {
80+ case "plan" :
81+ var tfPlan tfjson.Plan
82+ err = json .Unmarshal (tfStepRaw ,& tfPlan )
83+ require .NoError (t ,err )
8084
81- modules := []* tfjson.StateModule {tfPlan .PlannedValues .RootModule }
82- if tfPlan .PriorState != nil {
83- modules = append (modules ,tfPlan .PriorState .Values .RootModule )
84- }else {
85- // Ensure that resources canF be duplicated in the source state
86- // and that no errors occur!
87- modules = append (modules ,tfPlan .PlannedValues .RootModule )
88- }
85+ modules = []* tfjson.StateModule {tfPlan .PlannedValues .RootModule }
86+ if tfPlan .PriorState != nil {
87+ modules = append (modules ,tfPlan .PriorState .Values .RootModule )
88+ }
89+ case "state" :
90+ var tfState tfjson.State
91+ err = json .Unmarshal (tfStepRaw ,& tfState )
92+ require .NoError (t ,err )
93+ modules = []* tfjson.StateModule {tfState .Values .RootModule }
94+ default :
95+ t .Fatalf ("unknown step: %s" ,step )
96+ }
8997
90- // Gather graph
91- dotFileRaw ,err := os .ReadFile (dotFile )
92- require .NoError (t ,err )
98+ // Gather graph
99+ dotFileRaw ,err := os .ReadFile (dotFile )
100+ require .NoError (t ,err )
93101
94- var expectedOutput any
95- state ,err := terraform .ConvertState (ctx ,modules ,string (dotFileRaw ),logger )
96- if err == nil {
97- sortResources (state .Resources )
98- sortExternalAuthProviders (state .ExternalAuthProviders )
99- deterministicAppIDs (state .Resources )
100- expectedOutput = state
101- }else {
102- // Write the error to the file then. Track errors as much as valid paths.
103- expectedOutput = err .Error ()
104- }
102+ var expectedOutput any
103+ state ,err := terraform .ConvertState (ctx ,modules ,string (dotFileRaw ),logger )
104+ if err == nil {
105+ sortResources (state .Resources )
106+ sortExternalAuthProviders (state .ExternalAuthProviders )
107+ deterministicAppIDs (state .Resources )
108+ expectedOutput = state
109+ }else {
110+ // Write the error to the file then. Track errors as much as valid paths.
111+ expectedOutput = err .Error ()
112+ }
105113
106- expPath := filepath .Join (testDirectoryPath ,"converted_state.golden" )
107- if * updateGoldenFiles {
108- gotBytes ,err := json .MarshalIndent (expectedOutput ,"" ," " )
109- require .NoError (t ,err ,"marshaling converted state to JSON" )
110- err = os .WriteFile (expPath ,gotBytes ,0o600 )
111- require .NoError (t ,err )
112- return
113- }
114+ expPath := filepath .Join (testDirectoryPath ,fmt . Sprintf ( "converted_state.%s. golden" , step ) )
115+ if * updateGoldenFiles {
116+ gotBytes ,err := json .MarshalIndent (expectedOutput ,"" ," " )
117+ require .NoError (t ,err ,"marshaling converted state to JSON" )
118+ err = os .WriteFile (expPath ,gotBytes ,0o600 )
119+ require .NoError (t ,err )
120+ return
121+ }
114122
115- gotBytes ,err := json .Marshal (expectedOutput )
116- require .NoError (t ,err ,"marshaling converted state to JSON" )
123+ gotBytes ,err := json .Marshal (expectedOutput )
124+ require .NoError (t ,err ,"marshaling converted state to JSON" )
117125
118- expBytes ,err := os .ReadFile (expPath )
119- require .NoError (t ,err )
126+ expBytes ,err := os .ReadFile (expPath )
127+ require .NoError (t ,err )
120128
121- require .JSONEq (t ,string (expBytes ),string (gotBytes ),"converted state" )
122- })
129+ require .JSONEq (t ,string (expBytes ),string (gotBytes ),"converted state" )
130+ })
131+ }
123132}
124133}
125134