Skip to content

test(display): cover the metadata model types - #2176

Merged
TerryHowe merged 1 commit into
oras-project:mainfrom
akshita317:test/model-coverage
Sep 21, 2026
Merged

TerryHowe merged 1 commit into
oras-project:mainfrom
akshita317:test/model-coverage

Conversation

@akshita317

Copy link
Copy Markdown
Contributor

Problem

cmd/oras/internal/display/metadata/model is at 13.7% of statements. push.go was the only type with a test file, so the types behind oras attach, discover, manifest fetch, repo ls and repo tags had no tests at all — despite defining the JSON and go-template output users consume.

Changes

One test file per source file, following the 1:1 pairing used elsewhere under display/metadata (and asked for on #2164). The assertions are on the marshalled shape, not merely that a constructor returns non-nil:

File What it pins
descriptor_test.go NewDigestReference; FromDescriptor copying the core fields, inlining reference/mediaType/digest/size as templates address them, and deliberately dropping URLs, Data and Platform
discover_test.go NewNode starting Referrers as an empty slice; a referrer of a previously discovered referrer attaching to that node, not the root; an unknown subject rejected with unexpected subject descriptor
attach_test.go the rendered reference and descriptor fields
fetched_test.go content round-tripping, and nil content marshalling as null rather than failing — what lets the fetch handlers tolerate non-JSON content
repo_list_test.go / repo_tags_test.go both lists initialised so an empty listing marshals as [], not null
tag_test.go Tags sorting, which push relies on for referenceAsTags

The []-not-null assertions are deliberate: that is the regression class fixed in #2170 and #2175, and nothing was pinning it for the lists that already behaved correctly.

Result

before: coverage: 13.7% of statements
after:  coverage: 58.8% of statements

Every function is now at 100% except the four in pull.go (newFile, NewPull, Files, Add). Those are left alone on purpose to avoid conflicts: #2175 covers NewPull, and #2002 also touches that file. Happy to fold them in here instead if you would rather.

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

The model package sits at 13.7% of statements: push.go was the only type
with a test file, so the types behind oras attach, discover, manifest
fetch, repo ls and repo tags were exercised by nothing, even though they
define the JSON and go-template output users depend on.

Add one test file per source file, following the 1:1 pairing used
elsewhere under display/metadata, asserting the marshalled shape rather
than only that a constructor returns non-nil:

  - descriptor: NewDigestReference and FromDescriptor, including that the
    reference and the OCI fields are inlined as templates address them,
    and that URLs, Data and Platform are deliberately dropped
  - discover: NewNode starting Referrers as an empty slice, a referrer of
    a previously discovered referrer attaching to that node rather than
    the root, and an unknown subject being rejected
  - attach and fetched: the rendered fields, and that nil content marshals
    as null rather than failing, which is what lets the fetch handlers
    tolerate content that is not JSON
  - repo_list and repo_tags: that both lists are initialised so an empty
    listing marshals as [] rather than null
  - tag: that Tags sorts, which push relies on for referenceAsTags

Coverage goes from 13.7% to 58.8% of statements, with every function at
100% except the four in pull.go. Those are left alone on purpose: oras-project#2175
covers NewPull, and oras-project#2002 also touches that file.

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

codecov Bot commented Sep 19, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 87.81%. Comparing base (87495d5) to head (887e40b).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2176   +/-   ##
=======================================
  Coverage   87.81%   87.81%           
=======================================
  Files         139      139           
  Lines        5689     5689           
=======================================
  Hits         4996     4996           
  Misses        413      413           
  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 0d1f61e into oras-project:main Sep 21, 2026
9 checks passed
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