Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions go/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ func resultError(r core.Result) *GitError {
}
if r.Value != nil {
msg := core.Sprint(r.Value)
return &GitError{Err: core.NewError(msg), Stderr: msg}
return &GitError{Err: core.E("git.resultError", msg, nil), Stderr: msg}
}
return &GitError{Err: core.NewError("operation failed"), Stderr: "operation failed"}
return &GitError{Err: core.E("git.resultError", "operation failed", nil), Stderr: "operation failed"}
}

func gitCmd(dir string, args ...string) *core.Cmd {
Expand Down
8 changes: 4 additions & 4 deletions go/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -376,11 +376,11 @@ func gitServiceError(op, msg string, err error) *GitError {
}

func gitServiceErrorWithArgs(args []string, msg string, err error) *GitError {
if err == nil {
err = core.NewError(msg)
} else {
err = core.E("git.service", msg, err)
op := "git.service"
if len(args) > 0 && args[0] != "" {
op = args[0]
}
err = core.E(op, msg, err)
return &GitError{
Args: args,
Err: err,
Expand Down
53 changes: 28 additions & 25 deletions go/tests/cli/git/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,41 +90,42 @@

func verifyStatusResults(statuses []gitlib.RepoStatus, clean, dirty string) core.Result {
if len(statuses) != 2 {
return core.Fail(core.Errorf("expected 2 statuses, got %d", len(statuses)))
return core.Fail(core.E("git.cli.verifyStatusResults", core.Sprintf("expected 2 statuses, got %d", len(statuses)), nil))

Check failure on line 93 in go/tests/cli/git/main.go

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Define a constant instead of duplicating this literal "git.cli.verifyStatusResults" 9 times.

See more on https://sonarcloud.io/project/issues?id=dAppCore_go-git&issues=AZ3fT0mqlndhd1FnWJr_&open=AZ3fT0mqlndhd1FnWJr_&pullRequest=5
}

cleanStatus := statuses[0]
if cleanStatus.Name != "clean-repo" {
return core.Fail(core.Errorf("clean name = %q", cleanStatus.Name))
return core.Fail(core.E("git.cli.verifyStatusResults", core.Sprintf("clean name = %q", cleanStatus.Name), nil))
}
if cleanStatus.Path != clean {
return core.Fail(core.Errorf("clean file = %q", cleanStatus.Path))
return core.Fail(core.E("git.cli.verifyStatusResults", core.Sprintf("clean file = %q", cleanStatus.Path), nil))
}
if cleanStatus.Error != nil {
return core.Fail(cleanStatus.Error)
}
if cleanStatus.Branch == "" {
return core.Fail(core.NewError("clean branch should not be empty"))
return core.Fail(core.E("git.cli.verifyStatusResults", "clean branch should not be empty", nil))
}
if cleanStatus.IsDirty() {
return core.Fail(core.NewError("clean repo reported dirty"))
return core.Fail(core.E("git.cli.verifyStatusResults", "clean repo reported dirty", nil))
}

dirtyStatus := statuses[1]
if dirtyStatus.Name != "dirty-repo" {
return core.Fail(core.Errorf("dirty name = %q", dirtyStatus.Name))
return core.Fail(core.E("git.cli.verifyStatusResults", core.Sprintf("dirty name = %q", dirtyStatus.Name), nil))
}
if dirtyStatus.Path != dirty {
return core.Fail(core.Errorf("dirty file = %q", dirtyStatus.Path))
return core.Fail(core.E("git.cli.verifyStatusResults", core.Sprintf("dirty file = %q", dirtyStatus.Path), nil))
}
if dirtyStatus.Error != nil {
return core.Fail(dirtyStatus.Error)
}
if !dirtyStatus.IsDirty() {
return core.Fail(core.NewError("dirty repo reported clean"))
return core.Fail(core.E("git.cli.verifyStatusResults", "dirty repo reported clean", nil))
}
if dirtyStatus.Modified != 1 || dirtyStatus.Staged != 1 || dirtyStatus.Untracked != 1 {
return core.Fail(core.Errorf("dirty counts = modified:%d staged:%d untracked:%d", dirtyStatus.Modified, dirtyStatus.Staged, dirtyStatus.Untracked))
msg := core.Sprintf("dirty counts = modified:%d staged:%d untracked:%d", dirtyStatus.Modified, dirtyStatus.Staged, dirtyStatus.Untracked)
return core.Fail(core.E("git.cli.verifyStatusResults", msg, nil))
}

return core.Ok(nil)
Expand Down Expand Up @@ -232,7 +233,7 @@
}
pushResults := pushMultiple.Value.([]gitlib.PushResult)
if len(pushResults) != 1 || !pushResults[0].Success || pushResults[0].Name != "push" {
return core.Fail(core.Errorf("unexpected push multiple results: %+v", pushResults))
return core.Fail(core.E("git.cli.verifyPushAndPullMultiple", core.Sprintf("unexpected push multiple results: %+v", pushResults), nil))
}

pullMultiple := gitlib.PullMultiple(ctx, []string{workspace.pullClone}, map[string]string{workspace.pullClone: "pull"})
Expand All @@ -241,55 +242,56 @@
}
pullResults := pullMultiple.Value.([]gitlib.PullResult)
if len(pullResults) != 1 || !pullResults[0].Success || pullResults[0].Name != "pull" {
return core.Fail(core.Errorf("unexpected pull multiple results: %+v", pullResults))
return core.Fail(core.E("git.cli.verifyPushAndPullMultiple", core.Sprintf("unexpected pull multiple results: %+v", pullResults), nil))
}

return core.Ok(nil)
}

func expectSingleStatus(statuses []gitlib.RepoStatus, name string, ahead, behind int) core.Result {
if len(statuses) != 1 {
return core.Fail(core.Errorf("expected 1 status, got %d", len(statuses)))
return core.Fail(core.E("git.cli.expectSingleStatus", core.Sprintf("expected 1 status, got %d", len(statuses)), nil))

Check failure on line 253 in go/tests/cli/git/main.go

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Define a constant instead of duplicating this literal "git.cli.expectSingleStatus" 5 times.

See more on https://sonarcloud.io/project/issues?id=dAppCore_go-git&issues=AZ3fT0mqlndhd1FnWJr-&open=AZ3fT0mqlndhd1FnWJr-&pullRequest=5
}
status := statuses[0]
if status.Error != nil {
return core.Fail(status.Error)
}
if status.Name != name {
return core.Fail(core.Errorf("status name = %q", status.Name))
return core.Fail(core.E("git.cli.expectSingleStatus", core.Sprintf("status name = %q", status.Name), nil))
}
if status.Ahead != ahead || status.Behind != behind {
return core.Fail(core.Errorf("%s ahead/behind = %d/%d, want %d/%d", name, status.Ahead, status.Behind, ahead, behind))
msg := core.Sprintf("%s ahead/behind = %d/%d, want %d/%d", name, status.Ahead, status.Behind, ahead, behind)
return core.Fail(core.E("git.cli.expectSingleStatus", msg, nil))
}
if ahead > 0 && !status.HasUnpushed() {
return core.Fail(core.Errorf("%s should report unpushed commits", name))
return core.Fail(core.E("git.cli.expectSingleStatus", core.Sprintf("%s should report unpushed commits", name), nil))
}
if behind > 0 && !status.HasUnpulled() {
return core.Fail(core.Errorf("%s should report unpulled commits", name))
return core.Fail(core.E("git.cli.expectSingleStatus", core.Sprintf("%s should report unpulled commits", name), nil))
}
return core.Ok(nil)
}

func verifyErrors(ctx core.Context) core.Result {
statuses := gitlib.Status(ctx, gitlib.StatusOptions{Paths: []string{relativeRepoPath}})
if len(statuses) != 1 || statuses[0].Error == nil {
return core.Fail(core.NewError("relative status should fail"))
return core.Fail(core.E("git.cli.verifyErrors", "relative status should fail", nil))

Check failure on line 278 in go/tests/cli/git/main.go

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Define a constant instead of duplicating this literal "git.cli.verifyErrors" 8 times.

See more on https://sonarcloud.io/project/issues?id=dAppCore_go-git&issues=AZ3fT0mqlndhd1FnWJr9&open=AZ3fT0mqlndhd1FnWJr9&pullRequest=5
}
if !core.Contains(statuses[0].Error.Error(), "path must be absolute") {
return core.Fail(core.Errorf("relative status error = %v", statuses[0].Error))
return core.Fail(core.E("git.cli.verifyErrors", core.Sprintf("relative status error = %v", statuses[0].Error), nil))
}

if r := gitlib.Push(ctx, relativeRepoPath); r.OK {
return core.Fail(core.NewError("relative push should fail"))
return core.Fail(core.E("git.cli.verifyErrors", "relative push should fail", nil))
}
if r := gitlib.Pull(ctx, relativeRepoPath); r.OK {
return core.Fail(core.NewError("relative pull should fail"))
return core.Fail(core.E("git.cli.verifyErrors", "relative pull should fail", nil))
}
if !gitlib.IsNonFastForward(core.NewError("Updates were rejected: fetch first")) {
return core.Fail(core.NewError("non-fast-forward detection should match fetch first errors"))
if !gitlib.IsNonFastForward(core.E("git.cli.verifyErrors", "Updates were rejected: fetch first", nil)) {
return core.Fail(core.E("git.cli.verifyErrors", "non-fast-forward detection should match fetch first errors", nil))
}
if gitlib.IsNonFastForward(core.NewError("connection refused")) {
return core.Fail(core.NewError("non-fast-forward detection should ignore unrelated errors"))
if gitlib.IsNonFastForward(core.E("git.cli.verifyErrors", "connection refused", nil)) {
return core.Fail(core.E("git.cli.verifyErrors", "non-fast-forward detection should ignore unrelated errors", nil))
}

return core.Ok(nil)
Expand Down Expand Up @@ -345,7 +347,8 @@
cmd := &core.Cmd{Path: "/usr/bin/env", Args: cmdArgs, Dir: dir}
out, runErr := cmd.CombinedOutput()
if runErr != nil {
return core.Fail(core.Errorf("git %s: %s: %w", core.Join(" ", args...), core.Trim(string(out)), runErr))
msg := core.Sprintf("git %s: %s", core.Join(" ", args...), core.Trim(string(out)))
return core.Fail(core.E("git.cli.runGit", msg, runErr))
}
return core.Ok(string(out))
}
Loading