@@ -20,19 +20,22 @@ package librarian
20
20
import (
21
21
"encoding/json"
22
22
"fmt"
23
+ "math/rand"
23
24
"os"
24
25
"os/exec"
25
26
"path/filepath"
27
+ "strings"
26
28
"testing"
27
- )
28
29
29
- const (
30
- repo = "repo"
31
- localRepoBackupDir = "testdata/e2e/generate/repo_backup"
32
- localAPISource = "testdata/e2e/generate/api_root"
30
+ "github.com/google/go-cmp/cmp"
33
31
)
34
32
35
33
func TestRunGenerate (t * testing.T ) {
34
+ const (
35
+ repo = "repo"
36
+ initialRepoStateDir = "testdata/e2e/generate/repo_init"
37
+ localAPISource = "testdata/e2e/generate/api_root"
38
+ )
36
39
t .Parallel ()
37
40
for _ , test := range []struct {
38
41
name string
@@ -44,15 +47,15 @@ func TestRunGenerate(t *testing.T) {
44
47
api : "google/cloud/pubsub/v1" ,
45
48
},
46
49
{
47
- name : "non existant in api source" ,
48
- api : "google/invalid /path" ,
50
+ name : "non existent in api source" ,
51
+ api : "google/non-existent /path" ,
49
52
wantErr : true ,
50
53
},
51
54
} {
52
55
t .Run (test .name , func (t * testing.T ) {
53
56
workRoot := filepath .Join (t .TempDir ())
54
57
repo := filepath .Join (workRoot , repo )
55
- if err := prepareTest (t , repo , workRoot , localRepoBackupDir ); err != nil {
58
+ if err := prepareTest (t , repo , workRoot , initialRepoStateDir ); err != nil {
56
59
t .Fatalf ("prepare test error = %v" , err )
57
60
}
58
61
@@ -101,6 +104,92 @@ func TestRunGenerate(t *testing.T) {
101
104
}
102
105
}
103
106
107
+ func TestRunConfigure (t * testing.T ) {
108
+ const (
109
+ localRepoDir = "testdata/e2e/configure/repo"
110
+ initialRepoStateDir = "testdata/e2e/configure/repo_init"
111
+ repo = "repo"
112
+ )
113
+ for _ , test := range []struct {
114
+ name string
115
+ api string
116
+ library string
117
+ apiSource string
118
+ updatedState string
119
+ wantErr bool
120
+ }{
121
+ {
122
+ name : "runs successfully" ,
123
+ api : "google/cloud/new-library-path/v2" ,
124
+ library : "new-library" ,
125
+ apiSource : "testdata/e2e/configure/api_root" ,
126
+ updatedState : "testdata/e2e/configure/updated-state.yaml" ,
127
+ },
128
+ {
129
+ name : "failed due to simulated error in configure command" ,
130
+ api : "google/cloud/another-library/v3" ,
131
+ library : "simulate-configure-error-id" ,
132
+ apiSource : "testdata/e2e/configure/api_root" ,
133
+ updatedState : "testdata/e2e/configure/updated-state.yaml" ,
134
+ wantErr : true ,
135
+ },
136
+ } {
137
+ t .Run (test .name , func (t * testing.T ) {
138
+ t .Parallel ()
139
+ workRoot := filepath .Join (os .TempDir (), fmt .Sprintf ("rand-%d" , rand .Intn (1000 )))
140
+ repo := filepath .Join (workRoot , repo )
141
+ if err := prepareTest (t , repo , workRoot , initialRepoStateDir ); err != nil {
142
+ t .Fatalf ("prepare test error = %v" , err )
143
+ }
144
+
145
+ cmd := exec .Command (
146
+ "go" ,
147
+ "run" ,
148
+ "github.com/googleapis/librarian/cmd/librarian" ,
149
+ "generate" ,
150
+ fmt .Sprintf ("--api=%s" , test .api ),
151
+ fmt .Sprintf ("--output=%s" , workRoot ),
152
+ fmt .Sprintf ("--repo=%s" , repo ),
153
+ fmt .Sprintf ("--api-source=%s" , test .apiSource ),
154
+ fmt .Sprintf ("--library=%s" , test .library ),
155
+ )
156
+ cmd .Stderr = os .Stderr
157
+ cmd .Stdout = os .Stdout
158
+ err := cmd .Run ()
159
+ if test .wantErr {
160
+ if err == nil {
161
+ t .Fatal ("Configure command should fail" )
162
+ }
163
+
164
+ // the exact message is not populated here, but we can check it's
165
+ // indeed an error returned from docker container.
166
+ if g , w := err .Error (), "exit status 1" ; ! strings .Contains (g , w ) {
167
+ t .Errorf ("got %q, wanted it to contain %q" , g , w )
168
+ }
169
+ return
170
+ }
171
+ if err != nil {
172
+ t .Fatalf ("Failed to run configure: %v" , err )
173
+ }
174
+
175
+ // Verify the file content
176
+ gotBytes , err := os .ReadFile (filepath .Join (repo , ".librarian" , "state.yaml" ))
177
+ if err != nil {
178
+ t .Fatalf ("Failed to read configure response file: %v" , err )
179
+ }
180
+
181
+ wantBytes , readErr := os .ReadFile (test .updatedState )
182
+ if readErr != nil {
183
+ t .Fatalf ("Failed to read expected state for comparison: %v" , readErr )
184
+ }
185
+
186
+ if diff := cmp .Diff (string (wantBytes ), string (gotBytes )); diff != "" {
187
+ t .Errorf ("Generated yaml mismatch (-want +got):\n %s" , diff )
188
+ }
189
+ })
190
+ }
191
+ }
192
+
104
193
func prepareTest (t * testing.T , destRepoDir , workRoot , sourceRepoDir string ) error {
105
194
if err := initTestRepo (t , destRepoDir , sourceRepoDir ); err != nil {
106
195
return err
0 commit comments