Skip to content

Commit 3cdc998

Browse files
authored
fix: set branch in generate runner (#2189)
Fixes #2188
1 parent 07f666d commit 3cdc998

File tree

4 files changed

+33
-10
lines changed

4 files changed

+33
-10
lines changed

internal/config/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ type Config struct {
9595
APISource string
9696

9797
// APISourceDepth controls the depth of the repository closing **IF**
98-
// APISource is a github repository and it is cloned.
98+
// APISource is a GitHub repository, and it is cloned.
9999
APISourceDepth int
100100

101101
// Branch is the remote branch of the language repository to use.

internal/librarian/command.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ type commandRunner struct {
9292
librarianConfig *config.LibrarianConfig
9393
ghClient GitHubClient
9494
containerClient ContainerClient
95-
workRoot string
9695
image string
96+
workRoot string
9797
}
9898

9999
const defaultAPISourceBranch = "master"

internal/librarian/generate.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ func newGenerateRunner(cfg *config.Config) (*generateRunner, error) {
5757
return &generateRunner{
5858
api: cfg.API,
5959
apiSource: cfg.APISource,
60+
branch: cfg.Branch,
6061
build: cfg.Build,
6162
commit: cfg.Commit,
6263
containerClient: runner.containerClient,

internal/librarian/generate_test.go

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -381,15 +381,17 @@ func TestRunConfigureCommand(t *testing.T) {
381381
func TestNewGenerateRunner(t *testing.T) {
382382
t.Parallel()
383383
for _, test := range []struct {
384-
name string
385-
cfg *config.Config
386-
wantErr bool
384+
name string
385+
cfg *config.Config
386+
wantErr bool
387+
wantErrMsg string
387388
}{
388389
{
389390
name: "valid config",
390391
cfg: &config.Config{
391392
API: "some/api",
392393
APISource: newTestGitRepo(t).GetDir(),
394+
Branch: "test-branch",
393395
Repo: newTestGitRepo(t).GetDir(),
394396
WorkRoot: t.TempDir(),
395397
Image: "gcr.io/test/test-image",
@@ -406,13 +408,15 @@ func TestNewGenerateRunner(t *testing.T) {
406408
Image: "gcr.io/test/test-image",
407409
CommandName: generateCmdName,
408410
},
409-
wantErr: true,
411+
wantErr: true,
412+
wantErrMsg: "repository does not exist",
410413
},
411414
{
412415
name: "missing image",
413416
cfg: &config.Config{
414417
API: "some/api",
415418
APISource: t.TempDir(),
419+
Branch: "test-branch",
416420
Repo: "https://github.com/googleapis/librarian.git",
417421
WorkRoot: t.TempDir(),
418422
CommandName: generateCmdName,
@@ -424,6 +428,7 @@ func TestNewGenerateRunner(t *testing.T) {
424428
cfg: &config.Config{
425429
API: "some/api",
426430
APISource: newTestGitRepo(t).GetDir(),
431+
Branch: "test-branch",
427432
Repo: newTestGitRepo(t).GetDir(),
428433
WorkRoot: t.TempDir(),
429434
Image: "gcr.io/test/test-image",
@@ -437,6 +442,7 @@ func TestNewGenerateRunner(t *testing.T) {
437442
API: "some/api",
438443
APISource: "https://github.com/googleapis/googleapis", // This will trigger the clone of googleapis
439444
APISourceDepth: 1,
445+
Branch: "test-branch",
440446
Repo: newTestGitRepo(t).GetDir(),
441447
WorkRoot: t.TempDir(),
442448
Image: "gcr.io/test/test-image",
@@ -454,13 +460,15 @@ func TestNewGenerateRunner(t *testing.T) {
454460
Image: "gcr.io/test/test-image",
455461
CommandName: generateCmdName,
456462
},
457-
wantErr: true,
463+
wantErr: true,
464+
wantErrMsg: "repo must be specified",
458465
},
459466
{
460467
name: "valid config with local repo",
461468
cfg: &config.Config{
462469
API: "some/api",
463470
APISource: newTestGitRepo(t).GetDir(),
471+
Branch: "test-branch",
464472
Repo: newTestGitRepo(t).GetDir(),
465473
WorkRoot: t.TempDir(),
466474
Image: "gcr.io/test/test-image",
@@ -500,12 +508,26 @@ func TestNewGenerateRunner(t *testing.T) {
500508
}
501509

502510
r, err := newGenerateRunner(test.cfg)
503-
if (err != nil) != test.wantErr {
504-
t.Errorf("newGenerateRunner() error = %v, wantErr %v", err, test.wantErr)
505-
}
506511
if test.wantErr {
512+
if err == nil {
513+
t.Fatalf("newGenerateRunner() error = %v, wantErr %v", err, test.wantErr)
514+
}
515+
516+
if !strings.Contains(err.Error(), test.wantErrMsg) {
517+
t.Fatalf("want error message: %s, got: %s", test.wantErrMsg, err.Error())
518+
}
519+
507520
return
508521
}
522+
523+
if err != nil {
524+
t.Fatalf("newGenerateRunner() got error: %v", err)
525+
}
526+
527+
if r.branch == "" {
528+
t.Errorf("newGenerateRunner() branch is not set")
529+
}
530+
509531
if r.ghClient == nil {
510532
t.Errorf("newGenerateRunner() ghClient is nil")
511533
}

0 commit comments

Comments
 (0)