Skip to content

Commit

Permalink
internal/task: improve how fakes report nonexistent file and no tags
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
dmitshur authored and gopherbot committed Aug 29, 2023
1 parent d75d448 commit 88aa460
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
15 changes: 13 additions & 2 deletions internal/task/fakes.go
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,11 @@ func (repo *FakeRepo) Branch(branch, commit string) {
}

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

var _ GerritClient = (*FakeGerrit)(nil)
Expand All @@ -447,6 +451,7 @@ func (g *FakeGerrit) ReadBranchHead(ctx context.Context, project, branch string)
if err != nil {
return "", err
}
// TODO: If the branch doesn't exist, return an error matching gerrit.ErrResourceNotExist.
out, err := repo.dir.RunCommand(ctx, "rev-parse", "refs/heads/"+branch)
return strings.TrimSpace(string(out)), err
}
Expand All @@ -465,7 +470,13 @@ func (g *FakeGerrit) ListTags(ctx context.Context, project string) ([]string, er
return nil, err
}
out, err := repo.dir.RunCommand(ctx, "tag", "-l")
return strings.Split(strings.TrimSpace(string(out)), "\n"), err
if err != nil {
return nil, err
}
if len(out) == 0 {
return nil, nil // No tags.
}
return strings.Split(strings.TrimSpace(string(out)), "\n"), nil
}

func (g *FakeGerrit) GetTag(ctx context.Context, project string, tag string) (gerrit.TagInfo, error) {
Expand Down
1 change: 1 addition & 0 deletions internal/task/gerrit.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type GerritClient interface {
// ListTags returns all the tags on project.
ListTags(ctx context.Context, project string) ([]string, error)
// ReadBranchHead returns the head of a branch in project.
// If the branch doesn't exist, it returns an error matching gerrit.ErrResourceNotExist.
ReadBranchHead(ctx context.Context, project, branch string) (string, error)
// ListProjects lists all the projects on the server.
ListProjects(ctx context.Context) ([]string, error)
Expand Down

0 comments on commit 88aa460

Please sign in to comment.