Skip to content

Commit 88aa460

Browse files
dmitshurgopherbot
authored andcommitted
internal/task: improve how fakes report nonexistent file and no tags
The TagXReposTasks.readRepo method calls ReadBranchHead and ReadFile, and handles errors matching gerrit.ErrResourceNotExist that the real Gerrit implementation returns. Improve the fakes accordingly, enough to make the upcoming changes to the tagging workflow its tests happy. For golang/go#56530. Change-Id: I4d92d578210b20752e65bdc2719b74bc5c7259ff Reviewed-on: https://go-review.googlesource.com/c/build/+/523155 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Heschi Kreinick <[email protected]> Auto-Submit: Dmitri Shuralyov <[email protected]> Run-TryBot: Dmitri Shuralyov <[email protected]> Reviewed-by: Dmitri Shuralyov <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent d75d448 commit 88aa460

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

internal/task/fakes.go

+13-2
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,11 @@ func (repo *FakeRepo) Branch(branch, commit string) {
421421
}
422422

423423
func (repo *FakeRepo) ReadFile(commit, file string) ([]byte, error) {
424-
return repo.dir.RunCommand(context.Background(), "show", commit+":"+file)
424+
b, err := repo.dir.RunCommand(context.Background(), "show", commit+":"+file)
425+
if err != nil && strings.Contains(err.Error(), " does not exist ") {
426+
err = errors.Join(gerrit.ErrResourceNotExist, err)
427+
}
428+
return b, err
425429
}
426430

427431
var _ GerritClient = (*FakeGerrit)(nil)
@@ -447,6 +451,7 @@ func (g *FakeGerrit) ReadBranchHead(ctx context.Context, project, branch string)
447451
if err != nil {
448452
return "", err
449453
}
454+
// TODO: If the branch doesn't exist, return an error matching gerrit.ErrResourceNotExist.
450455
out, err := repo.dir.RunCommand(ctx, "rev-parse", "refs/heads/"+branch)
451456
return strings.TrimSpace(string(out)), err
452457
}
@@ -465,7 +470,13 @@ func (g *FakeGerrit) ListTags(ctx context.Context, project string) ([]string, er
465470
return nil, err
466471
}
467472
out, err := repo.dir.RunCommand(ctx, "tag", "-l")
468-
return strings.Split(strings.TrimSpace(string(out)), "\n"), err
473+
if err != nil {
474+
return nil, err
475+
}
476+
if len(out) == 0 {
477+
return nil, nil // No tags.
478+
}
479+
return strings.Split(strings.TrimSpace(string(out)), "\n"), nil
469480
}
470481

471482
func (g *FakeGerrit) GetTag(ctx context.Context, project string, tag string) (gerrit.TagInfo, error) {

internal/task/gerrit.go

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ type GerritClient interface {
3434
// ListTags returns all the tags on project.
3535
ListTags(ctx context.Context, project string) ([]string, error)
3636
// ReadBranchHead returns the head of a branch in project.
37+
// If the branch doesn't exist, it returns an error matching gerrit.ErrResourceNotExist.
3738
ReadBranchHead(ctx context.Context, project, branch string) (string, error)
3839
// ListProjects lists all the projects on the server.
3940
ListProjects(ctx context.Context) ([]string, error)

0 commit comments

Comments
 (0)