@@ -130,7 +130,7 @@ func (s *State) UpdateObject(tv *typed.TypedValue, version fieldpath.APIVersion,
130130
131131// Update the current state with the passed in object
132132func (s * State ) Update (obj typed.YAMLObject , version fieldpath.APIVersion , manager string ) error {
133- tv , err := s .Parser . Type ( string ( version )). FromYAML ( FixTabsOrDie ( obj ) )
133+ tv , err := s .parseConfig ( version , obj )
134134 if err != nil {
135135 return err
136136 }
@@ -159,7 +159,7 @@ func (s *State) ApplyObject(tv *typed.TypedValue, version fieldpath.APIVersion,
159159
160160// Apply the passed in object to the current state
161161func (s * State ) Apply (obj typed.YAMLObject , version fieldpath.APIVersion , manager string , force bool ) error {
162- tv , err := s .Parser . Type ( string ( version )). FromYAML ( FixTabsOrDie ( obj ) )
162+ tv , err := s .parseConfig ( version , obj )
163163 if err != nil {
164164 return err
165165 }
@@ -184,6 +184,10 @@ func (s *State) CompareLive(obj typed.YAMLObject, version fieldpath.APIVersion)
184184 return live .Compare (tv )
185185}
186186
187+ func (s * State ) parseConfig (apiVersion fieldpath.APIVersion , obj typed.YAMLObject ) (* typed.TypedValue , error ) {
188+ return s .Parser .Type (string (apiVersion )).FromYAML (FixTabsOrDie (obj ))
189+ }
190+
187191// dummyConverter doesn't convert, it just returns the same exact object, as long as a version is provided.
188192type dummyConverter struct {}
189193
@@ -471,12 +475,12 @@ func (tc TestCase) TestWithConverter(parser Parser, converter merge.Converter) e
471475
472476 // If LastObject was specified, compare it with LiveState
473477 if tc .Object != typed .YAMLObject ("" ) {
474- comparison , err := state .CompareLive (tc .Object , tc .APIVersion )
475- if err != nil {
476- return fmt .Errorf ("failed to compare live with config: %v" , err )
478+ expect := ExpectState {
479+ APIVersion : tc .APIVersion , Object : tc .Object ,
477480 }
478- if ! comparison .IsSame () {
479- return fmt .Errorf ("expected live and config to be the same:\n %v\n Config: %v\n " , comparison , value .ToString (state .Live .AsValue ()))
481+
482+ if err := expect .run (& state ); err != nil {
483+ return err
480484 }
481485 }
482486
@@ -505,3 +509,44 @@ func (tc TestCase) TestWithConverter(parser Parser, converter merge.Converter) e
505509
506510 return nil
507511}
512+
513+ // PrintState is an Operation printing the current state to help with debugging tests
514+ type PrintState struct {}
515+
516+ var _ Operation = PrintState {}
517+
518+ func (op PrintState ) run (s * State ) error {
519+ fmt .Println (value .ToString (s .Live .AsValue ()))
520+ return nil
521+ }
522+
523+ func (op PrintState ) preprocess (_ Parser ) (Operation , error ) {
524+ return op , nil
525+ }
526+
527+ // ExpectState is an Operation comparing the current state to the defined config to help with debugging tests
528+ type ExpectState struct {
529+ APIVersion fieldpath.APIVersion
530+ Object typed.YAMLObject
531+ }
532+
533+ var _ Operation = ExpectState {}
534+
535+ func (op ExpectState ) run (state * State ) error {
536+ comparison , err := state .CompareLive (op .Object , op .APIVersion )
537+ if err != nil {
538+ return fmt .Errorf ("failed to compare live with config: %v" , err )
539+ }
540+ if ! comparison .IsSame () {
541+ config , err := state .parseConfig (op .APIVersion , op .Object )
542+ if err != nil {
543+ return fmt .Errorf ("failed to parse config: %w" , err )
544+ }
545+ return fmt .Errorf ("expected live and config to be the same:\n %v\n Config: %v\n Live: %v\n " , comparison , value .ToString (config .AsValue ()), value .ToString (state .Live .AsValue ()))
546+ }
547+ return nil
548+ }
549+
550+ func (op ExpectState ) preprocess (parser Parser ) (Operation , error ) {
551+ return op , nil
552+ }
0 commit comments