Skip to content

test(display): cover the template and descriptor metadata handlers - #2164

Merged
TerryHowe merged 1 commit into
oras-project:mainfrom
akshita317:test/metadata-template-descriptor-coverage
Sep 13, 2026
Merged

TerryHowe merged 1 commit into
oras-project:mainfrom
akshita317:test/metadata-template-descriptor-coverage

Conversation

@akshita317

Copy link
Copy Markdown
Contributor

Problem

Two metadata packages had no test file at all:

cmd/oras/internal/display/metadata/template     0.0% of statements
cmd/oras/internal/display/metadata/descriptor   0.0% of statements

Both render user-facing output — --format go-template for every command that supports it, and oras manifest fetch --descriptor — so a regression in either is immediately visible to users but was caught by nothing.

Changes

Tests for every handler in both packages. They assert the rendered bytes, not merely that no error came back, so a change in output format fails the test.

descriptor

  • compact and pretty output both round-trip back to the same descriptor
  • only the pretty form is indented and newline-terminated; the compact form stays on one line so it can be piped
  • a failing writer surfaces its error
  • the reference and raw manifest bytes handed to OnFetched do not leak into the output

template — attach, discover, manifest fetch, push, repo ls, repo tags: each over its Render path and an unparsable template, plus the branches worth pinning down:

  • discover rejects a referrer whose subject was never seen, and accepts a referrer of a previously discovered referrer
  • manifest fetch tolerates non-JSON content by rendering a null manifest rather than failing
  • push does not record a digest reference as a tag, and records nothing when no reference is given
cmd/oras/internal/display/metadata/template     85.3% of statements
cmd/oras/internal/display/metadata/descriptor   80.0% of statements

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

One thing noticed while writing these

model.NewPush builds ReferenceAsTags with var refAsTags []string and only ever appends, so when nothing is tagged the field marshals as null rather than []. model.Repositories and model.Tags both initialise their slices to []string{} instead.

The visible effect is that a template calling {{len .referenceAsTags}} errors with len of nil pointer after an untagged push, while the same template shape works for repo ls and repo tags. I left it alone here to keep this PR test-only — happy to open an issue or a follow-up PR if you'd like it made consistent.

Both packages had no test file at all:

    cmd/oras/internal/display/metadata/template     0.0% of statements
    cmd/oras/internal/display/metadata/descriptor   0.0% of statements

They render user-facing output for --format go-template and for
`oras manifest fetch --descriptor`, so a regression in either is visible
to users but was caught by nothing.

Tests are added for every handler in both packages, asserting the rendered
bytes rather than just that no error is returned:

  - descriptor: compact and pretty output round-trip back to the same
    descriptor, only the pretty form is indented and newline-terminated,
    a failing writer surfaces its error, and the reference and raw manifest
    bytes passed to OnFetched stay out of the output.

  - template: attach, discover, manifest fetch, push, repo ls and repo tags
    handlers, each over its Render path plus an unparsable template. Also
    covered are discover rejecting a referrer whose subject was never seen,
    manifest fetch tolerating non-JSON content by rendering a null manifest
    instead of failing, and push not recording a digest reference as a tag.

    cmd/oras/internal/display/metadata/template     85.3% of statements
    cmd/oras/internal/display/metadata/descriptor   80.0% of statements

No production code is changed.

Signed-off-by: Akshita <[email protected]>
@codecov

codecov Bot commented Sep 12, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 87.78%. Comparing base (d0da4a9) to head (a77ccb3).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2164      +/-   ##
==========================================
+ Coverage   87.64%   87.78%   +0.14%     
==========================================
  Files         139      139              
  Lines        5689     5689              
==========================================
+ Hits         4986     4994       +8     
+ Misses        420      415       -5     
+ Partials      283      280       -3     

☔ 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. I fetched the branch and ran it locally: tests pass, -race is clean, go vet and gofmt are clean, and the coverage numbers reproduce exactly as claimed (template 85.3%, descriptor 80.0%).

What I like here is that the assertions are on the exact rendered output rather than just a nil error, so a format regression actually fails. The branch selection is thoughtful too — unknown-subject rejection in discover, referrer-of-a-referrer acceptance, non-JSON content rendering a null manifest, and digest-reference-is-not-a-tag are exactly the branches that would otherwise rot silently. OnFetched_ignoresNameAndContent pins a real contract, and the descriptor round-trip test checking that only the pretty form is indented and newline-terminated is what protects the pipe-ability of the compact form.

Two follow-ups, neither blocking:

  • template/pull.go is the one handler in the package still at 0%, and it is the entire remaining gap. Filed as #2165.
  • repo_test.go covers both repo_list.go and repo_tags.go; every other test file under display/metadata/ pairs 1:1 with its source. Worth splitting into repo_list_test.go and repo_tags_test.go whenever this area is next touched.

And your model.NewPush observation is correct — var refAsTags []string marshals to null, so {{len .referenceAsTags}} fails after an untagged push where the same template works for repo ls and repo tags. Agreed it belongs in a separate PR; please do open an issue for it.

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