Skip to content

test(errors): cover the CLI error type and its constructors - #2196

Merged
TerryHowe merged 2 commits into
oras-project:mainfrom
akshita317:test/cli-errors-coverage
Sep 28, 2026
Merged

TerryHowe merged 2 commits into
oras-project:mainfrom
akshita317:test/cli-errors-coverage

Conversation

@akshita317

Copy link
Copy Markdown
Contributor

Problem

cmd/oras/internal/errors is at 32.9% of statements. Everything that builds the text a user reads when a command fails has no test: Error.Error and Unwrap, CheckArgs, Command, TrimErrBasicCredentialNotFound and NewErrEmptyTagOrDigest are all at 0%.

What the tests pin

  • Error: the exact message with and without Usage and Recommendation, with usage before the recommendation, and that Unwrap exposes Err to errors.Is.
  • CheckArgs: nil when the checker passes; otherwise the exact Err, Usage and Recommendation. The command is oras blob push, so the usage has to come from the parent's command path plus Use.
  • Command: a failing RunE goes through the modifier and returns what the modifier returns; a successful run never calls the modifier.
  • TrimErrBasicCredentialNotFound: uses the shape oras-go actually produces (GET "<url>": basic credential not found, from auth.Client.Do). The request line is trimmed, an outer context such as Error from source registry for "<ref>" is kept, and the result still satisfies errors.Is(err, auth.ErrBasicCredentialNotFound).
  • reWrap: returns the inner error when the mid message is not in the outer one.
  • NewErrEmptyTagOrDigest: both forms of the message and recommendation (needsTag true and false), the usage, and OperationTypeParseArtifactReference.

I checked that none of these assertions is vacuous by breaking the source one change at a time. Each mutation fails a test:

Mutation in errors.go Failing test
reWrap always returns inner (outer context dropped) TestTrimErrBasicCredentialNotFound
Command discards the modifier's error TestCommand
CheckArgs usage built from cmd.Use alone TestCheckArgs
needsTag ignored TestNewErrEmptyTagOrDigest
Recommendation not appended TestError_Error
Unwrap returns nil TestError_Error

Result

before: coverage: 32.9% of statements
after:  coverage: 91.8% of statements

The remaining lines are the Unwrap() []error branch of TrimErrBasicCredentialNotFound. I left it unpinned on purpose: for a joined error it currently returns only the credential error and drops the sibling's message, and I didn't want to lock that in with a test without knowing whether it's intended. Happy to add a case either way.

No production code is changed. go test ./..., gofmt and go vet are clean.

Note: this adds the cobra and oras-go/v2/registry/remote/auth imports to errors_test.go, whose import block #2002 also rewrites for oras-go v3. Whichever lands second needs a one-line import update.

@codecov

codecov Bot commented Sep 28, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 87.81%. Comparing base (8c979f1) to head (aba3bc4).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2196      +/-   ##
==========================================
- Coverage   87.85%   87.81%   -0.04%     
==========================================
  Files         139      139              
  Lines        5714     5714              
==========================================
- Hits         5020     5018       -2     
- Misses        414      416       +2     
  Partials      280      280              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@TerryHowe TerryHowe left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/lgtm

@TerryHowe
TerryHowe merged commit bdf7835 into oras-project:main Sep 28, 2026
10 checks passed
@TerryHowe

Copy link
Copy Markdown
Member

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants