@@ -34,6 +34,7 @@ func TestNewGenerateRunner(t *testing.T) {
3434 cfg * config.Config
3535 wantErr bool
3636 wantErrMsg string
37+ setupFunc func (* config.Config ) error
3738 }{
3839 {
3940 name : "valid config" ,
@@ -102,15 +103,27 @@ func TestNewGenerateRunner(t *testing.T) {
102103 name : "clone googleapis fails" ,
103104 cfg : & config.Config {
104105 API : "some/api" ,
105- APISource : "" , // This will trigger the clone of googleapis
106+ APISource : "https://github.com/googleapis/googleapis " , // This will trigger the clone of googleapis
106107 APISourceDepth : 1 ,
107108 Repo : newTestGitRepo (t ).GetDir (),
108109 WorkRoot : t .TempDir (),
109110 Image : "gcr.io/test/test-image" ,
110111 CommandName : generateCmdName ,
111112 },
112113 wantErr : true ,
113- wantErrMsg : "repo must be specified" ,
114+ wantErrMsg : "repository does not exist" ,
115+ setupFunc : func (cfg * config.Config ) error {
116+ // The function will try to clone googleapis into the current work directory.
117+ // To make it fail, create a non-empty, non-git directory.
118+ googleapisDir := filepath .Join (cfg .WorkRoot , "googleapis" )
119+ if err := os .MkdirAll (googleapisDir , 0755 ); err != nil {
120+ return err
121+ }
122+ if err := os .WriteFile (filepath .Join (googleapisDir , "some-file" ), []byte ("foo" ), 0644 ); err != nil {
123+ return err
124+ }
125+ return nil
126+ },
114127 },
115128 {
116129 name : "valid config with local repo" ,
@@ -127,32 +140,11 @@ func TestNewGenerateRunner(t *testing.T) {
127140 } {
128141 t .Run (test .name , func (t * testing.T ) {
129142 t .Parallel ()
130- if test .cfg .APISource == "" && test .cfg .WorkRoot != "" {
131- if test .name == "clone googleapis fails" {
132- // The function will try to clone googleapis into the current work directory.
133- // To make it fail, create a non-empty, non-git directory.
134- googleapisDir := filepath .Join (test .cfg .WorkRoot , "googleapis" )
135- if err := os .MkdirAll (googleapisDir , 0755 ); err != nil {
136- t .Fatalf ("os.MkdirAll() = %v" , err )
137- }
138- if err := os .WriteFile (filepath .Join (googleapisDir , "some-file" ), []byte ("foo" ), 0644 ); err != nil {
139- t .Fatalf ("os.WriteFile() = %v" , err )
140- }
141- } else {
142- // The function will try to clone googleapis into the current work directory.
143- // To prevent a real clone, we can pre-create a fake googleapis repo.
144- googleapisDir := filepath .Join (test .cfg .WorkRoot , "googleapis" )
145- if err := os .MkdirAll (googleapisDir , 0755 ); err != nil {
146- t .Fatalf ("os.MkdirAll() = %v" , err )
147- }
148- runGit (t , googleapisDir , "init" )
149- runGit (
t ,
googleapisDir ,
"config" ,
"user.email" ,
"[email protected] " )
150- runGit (t , googleapisDir , "config" , "user.name" , "Test User" )
151- if err := os .WriteFile (filepath .Join (googleapisDir , "README.md" ), []byte ("test" ), 0644 ); err != nil {
152- t .Fatalf ("os.WriteFile: %v" , err )
153- }
154- runGit (t , googleapisDir , "add" , "README.md" )
155- runGit (t , googleapisDir , "commit" , "-m" , "initial commit" )
143+
144+ // custom setup
145+ if test .setupFunc != nil {
146+ if err := test .setupFunc (test .cfg ); err != nil {
147+ t .Fatalf ("error in setup %v" , err )
156148 }
157149 }
158150
0 commit comments