@@ -595,38 +595,52 @@ func ValidateUserID(id int64) bool {
595595 gitAddOutput , gitAddErr := gitAddCmd .CombinedOutput ()
596596 require .NoError (t , gitAddErr , "git add should succeed: %s" , string (gitAddOutput ))
597597
598- // Register custom code
598+ // Try to register custom code with new file - should fail
599599 customCodeCmd := exec .Command (speakeasyBinary , "customcode" , "--output" , "console" )
600600 customCodeCmd .Dir = temp
601601 customCodeOutput , customCodeErr := customCodeCmd .CombinedOutput ()
602- require .NoError (t , customCodeErr , "customcode command should succeed: %s" , string (customCodeOutput ))
602+ require .Error (t , customCodeErr , "customcode command should fail with new file: %s" , string (customCodeOutput ))
603+ require .Contains (t , string (customCodeOutput ), "Cannot register new files through customcode" , "Error should mention new files not supported" )
603604
604- // Verify patch file was created
605+ // Commit the new file to make it part of the codebase
606+ gitCommitCmd := exec .Command ("git" , "commit" , "-m" , "Add helper file" )
607+ gitCommitCmd .Dir = temp
608+ _ , err = gitCommitCmd .CombinedOutput ()
609+ require .NoError (t , err , "git commit should succeed" )
610+
611+ // Now make a modification to an existing file to test normal customcode flow
612+ getUserByNamePath := filepath .Join (temp , "models" , "operations" , "getuserbyname.go" )
613+ registerCustomCodeByPrefix (t , speakeasyBinary , temp , getUserByNamePath , "// The name that needs to be fetched" , "\t // custom code in existing file" )
614+
615+ // Verify patch file was created for the modification
605616 patchFile := filepath .Join (temp , ".speakeasy" , "patches" , "custom-code.diff" )
606617 _ , err = os .Stat (patchFile )
607- require .NoError (t , err , "patch file should exist at %s" , patchFile )
618+ require .NoError (t , err , "patch file should exist for file modifications" )
608619
609620 // Verify the file exists after registration (before regeneration)
610621 _ , err = os .Stat (helperFilePath )
611622 require .NoError (t , err , "Helper file should exist after registration" )
612623
613624 // Run speakeasy run to regenerate the SDK
614- // This should apply the patch and preserve the new file
625+ // This should apply the patch for the existing file modification and preserve the committed helper file
615626 runRegeneration (t , speakeasyBinary , temp , true )
616627
617- // Verify the new file still exists after regeneration
628+ // Verify the helper file still exists after regeneration (it was committed, so should be preserved)
618629 _ , err = os .Stat (helperFilePath )
619630 require .NoError (t , err , "Helper file should exist after regeneration" )
620631
621- // Verify the file contents are preserved exactly
632+ // Verify the helper file contents are preserved exactly
622633 verifyCustomCodePresent (t , helperFilePath , "FormatUserID" )
623634 verifyCustomCodePresent (t , helperFilePath , "ValidateUserID" )
624635 verifyCustomCodePresent (t , helperFilePath , "package utils" )
625636
626- // Read the entire file and verify exact content match
637+ // Read the entire helper file and verify exact content match
627638 actualContent , err := os .ReadFile (helperFilePath )
628639 require .NoError (t , err , "Failed to read helper file after regeneration" )
629640 require .Equal (t , helperFileContent , string (actualContent ), "Helper file content should be preserved exactly" )
641+
642+ // Verify the custom code modification was applied to the existing file
643+ verifyCustomCodePresent (t , getUserByNamePath , "// custom code in existing file" )
630644}
631645
632646// testCustomCodeNewFileDeletion tests that deleting a custom file is properly registered and persisted
@@ -659,44 +673,38 @@ func FormatUserID(id int64) string {
659673 _ , err = gitAddCmd .CombinedOutput ()
660674 require .NoError (t , err , "git add should succeed" )
661675
662- // Register custom code (registers the new file)
663- customCodeCmd := exec .Command (speakeasyBinary , "customcode" , "--output" , "console" )
664- customCodeCmd .Dir = temp
665- customCodeOutput , customCodeErr := customCodeCmd .CombinedOutput ()
666- require .NoError (t , customCodeErr , "customcode command should succeed: %s" , string (customCodeOutput ))
667-
668- // Verify patch file was created
669- patchFile := filepath .Join (temp , ".speakeasy" , "patches" , "custom-code.diff" )
670- _ , err = os .Stat (patchFile )
671- require .NoError (t , err , "patch file should exist after registering new file" )
672-
673- // Regenerate and verify the file is preserved
674- runRegeneration (t , speakeasyBinary , temp , true )
675- _ , err = os .Stat (helperFilePath )
676- require .NoError (t , err , "Helper file should exist after first regeneration" )
677-
678- // Commit the regeneration so the file becomes part of HEAD
679- gitCommitCmd := exec .Command ("git" , "commit" , "-am" , "regeneration with custom file" )
676+ // Commit the new file to make it part of the codebase
677+ gitCommitCmd := exec .Command ("git" , "commit" , "-m" , "Add helper file" )
680678 gitCommitCmd .Dir = temp
681679 _ , err = gitCommitCmd .CombinedOutput ()
682- require .NoError (t , err , "git commit should succeed after regeneration " )
680+ require .NoError (t , err , "git commit should succeed" )
683681
684682 // Now delete the file
685683 err = os .Remove (helperFilePath )
686684 require .NoError (t , err , "Failed to delete helper file" )
687685
688- // Register the deletion
689- customCodeCmd = exec .Command (speakeasyBinary , "customcode" , "--output" , "console" )
690- customCodeCmd .Dir = temp
691- customCodeOutput , customCodeErr = customCodeCmd .CombinedOutput ()
692- require .NoError (t , customCodeErr , "customcode command should succeed after deletion: %s" , string (customCodeOutput ))
686+ // Commit the deletion
687+ gitCommitDeleteCmd := exec .Command ("git" , "commit" , "-am" , "Delete helper file" )
688+ gitCommitDeleteCmd .Dir = temp
689+ _ , err = gitCommitDeleteCmd .CombinedOutput ()
690+ require .NoError (t , err , "git commit for deletion should succeed" )
691+
692+ // Now make a modification to an existing file to test normal customcode flow
693+ getUserByNamePath := filepath .Join (temp , "models" , "operations" , "getuserbyname.go" )
694+ registerCustomCodeByPrefix (t , speakeasyBinary , temp , getUserByNamePath , "// The name that needs to be fetched" , "\t // custom code for deletion test" )
693695
694- // Verify patch file was removed (no custom code remaining)
696+ // Verify patch file was created for the modification
697+ patchFile := filepath .Join (temp , ".speakeasy" , "patches" , "custom-code.diff" )
695698 _ , err = os .Stat (patchFile )
696- require .True (t , os . IsNotExist ( err ) , "patch file should not exist after deleting the only custom file" )
699+ require .NoError (t , err , "patch file should exist for file modifications " )
697700
698- // Regenerate and verify the file remains deleted
701+ // Run regeneration to test that the custom code is preserved and deleted file stays deleted
699702 runRegeneration (t , speakeasyBinary , temp , true )
703+
704+ // Verify the helper file does NOT exist after regeneration (it was deleted and committed)
700705 _ , err = os .Stat (helperFilePath )
701- require .True (t , os .IsNotExist (err ), "Helper file should not exist after regeneration with deletion registered" )
706+ require .True (t , os .IsNotExist (err ), "Helper file should not exist after regeneration (was deleted)" )
707+
708+ // Verify the custom code modification was applied to the existing file
709+ verifyCustomCodePresent (t , getUserByNamePath , "// custom code for deletion test" )
702710}
0 commit comments