@@ -28,6 +28,9 @@ import (
2828 "testing"
2929
3030 "github.com/google/go-cmp/cmp"
31+ "github.com/google/go-cmp/cmp/cmpopts"
32+ "github.com/googleapis/librarian/internal/config"
33+ "gopkg.in/yaml.v3"
3134)
3235
3336func TestRunGenerate (t * testing.T ) {
@@ -57,10 +60,10 @@ func TestRunGenerate(t *testing.T) {
5760 workRoot := filepath .Join (t .TempDir ())
5861 repo := filepath .Join (workRoot , repo )
5962 APISourceRepo := filepath .Join (workRoot , APISourceRepo )
60- if err := prepareTest (t , repo , workRoot , initialRepoStateDir ); err != nil {
63+ if err := initRepo (t , repo , initialRepoStateDir ); err != nil {
6164 t .Fatalf ("languageRepo prepare test error = %v" , err )
6265 }
63- if err := prepareTest (t , APISourceRepo , workRoot , localAPISource ); err != nil {
66+ if err := initRepo (t , APISourceRepo , localAPISource ); err != nil {
6467 t .Fatalf ("APISouceRepo prepare test error = %v" , err )
6568 }
6669
@@ -145,10 +148,10 @@ func TestRunConfigure(t *testing.T) {
145148 workRoot := filepath .Join (os .TempDir (), fmt .Sprintf ("rand-%d" , rand .Intn (1000 )))
146149 repo := filepath .Join (workRoot , repo )
147150 APISourceRepo := filepath .Join (workRoot , APISourceRepo )
148- if err := prepareTest (t , repo , workRoot , initialRepoStateDir ); err != nil {
151+ if err := initRepo (t , repo , initialRepoStateDir ); err != nil {
149152 t .Fatalf ("prepare test error = %v" , err )
150153 }
151- if err := prepareTest (t , APISourceRepo , workRoot , test .apiSource ); err != nil {
154+ if err := initRepo (t , APISourceRepo , test .apiSource ); err != nil {
152155 t .Fatalf ("APISouceRepo prepare test error = %v" , err )
153156 }
154157
@@ -187,36 +190,35 @@ func TestRunConfigure(t *testing.T) {
187190 if err != nil {
188191 t .Fatalf ("Failed to read configure response file: %v" , err )
189192 }
190-
191193 wantBytes , readErr := os .ReadFile (test .updatedState )
192194 if readErr != nil {
193195 t .Fatalf ("Failed to read expected state for comparison: %v" , readErr )
194196 }
197+ var gotState * config.LibrarianState
198+ if err := yaml .Unmarshal (gotBytes , & gotState ); err != nil {
199+ t .Fatalf ("Failed to unmarshal configure response file: %v" , err )
200+ }
201+ var wantState * config.LibrarianState
202+ if err := yaml .Unmarshal (wantBytes , & wantState ); err != nil {
203+ t .Fatalf ("Failed to unmarshal expected state: %v" , err )
204+ }
195205
196- if diff := cmp .Diff (string (wantBytes ), string (gotBytes )); diff != "" {
197- t .Errorf ("Generated yaml mismatch (-want +got):\n %s" , diff )
206+ if diff := cmp .Diff (wantState , gotState , cmpopts .IgnoreFields (config.LibraryState {}, "LastGeneratedCommit" )); diff != "" {
207+ t .Fatalf ("Generated yaml mismatch (-want +got):\n %s" , diff )
208+ }
209+ for _ , lib := range gotState .Libraries {
210+ if lib .ID == test .library && lib .LastGeneratedCommit == "" {
211+ t .Fatal ("LastGeneratedCommit should not be empty" )
212+ }
198213 }
199- })
200- }
201- }
202214
203- func prepareTest (t * testing.T , destRepoDir , workRoot , sourceRepoDir string ) error {
204- if err := initTestRepo (t , destRepoDir , sourceRepoDir ); err != nil {
205- return err
206- }
207- if err := os .MkdirAll (workRoot , 0755 ); err != nil {
208- return err
215+ })
209216 }
210-
211- return nil
212217}
213218
214- // initTestRepo initiates an empty git repo in the given directory, copy
219+ // initRepo initiates a git repo in the given directory, copy
215220// files from source directory and create a commit.
216- func initTestRepo (t * testing.T , dir , source string ) error {
217- if err := os .MkdirAll (dir , 0755 ); err != nil {
218- return err
219- }
221+ func initRepo (t * testing.T , dir , source string ) error {
220222 if err := os .CopyFS (dir , os .DirFS (source )); err != nil {
221223 return err
222224 }
0 commit comments