Skip to content

test(display): cover the pulled-file model - #2189

Merged
TerryHowe merged 2 commits into
oras-project:mainfrom
akshita317:test/model-pull-coverage
Sep 27, 2026
Merged

TerryHowe merged 2 commits into
oras-project:mainfrom
akshita317:test/model-pull-coverage

Conversation

@akshita317

Copy link
Copy Markdown
Contributor

Problem

cmd/oras/internal/display/metadata/model is at 62.3% of statements, and all three uncovered functions are in pull.go: newFile, Pulled.Add and Pulled.Files. They decide the path recorded for every file oras pull reports, so a change in them reaches users through --format json and --format go-template.

What the tests pin

  • Path resolution — a relative name is resolved against the output directory, an absolute one is kept as given, and a name needing cleaning is cleaned. The recorded path is always absolute.
  • The unpack annotation — a descriptor annotated for unpacking names a directory, so its path gains a trailing separator. An unpack annotation of "false" and an unrelated annotation both leave the path alone.
  • The descriptor half of a File comes from the descriptor path, not from the name the file was written under.
  • Add preserves insertion order, and Files is empty before anything is added.
  • Files returns a copy — writing to the returned slice does not change what a later call reports.

That last one is the reason for the slices.Clone in Files, and nothing pinned it. I checked it is not a vacuous assertion by mutation — replacing the clone with a bare return p.files:

--- FAIL: TestPulled_Files_returnsCopy
    pull_test.go:207: Files() returned the stored slice; writing to it changed the recorded file

The tests build expected paths with filepath rather than literals, so they behave the same on Linux and Windows.

Result

before: coverage: 62.3% of statements
after:  coverage: 96.2% of statements

The remaining uncovered lines are the filepath.Abs error branch in newFile and the error return it feeds in Add — which the source itself notes are "not likely to go wrong since the file has already be written to file store" and cannot be reached without faking the filesystem. Happy to add a seam for them if you would rather have 100%.

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

The model package sits at 62.3% of statements, and all three of the
uncovered functions are in pull.go: newFile, Pulled.Add and
Pulled.Files. They decide the path recorded for every file that
oras pull reports, so a change in them is visible to users through
--format json and go-template.

Cover them, asserting behaviour rather than only that no error is
returned:

  - a relative name is resolved against the output directory and an
    absolute one is kept as given, so the recorded path is always
    absolute, and a name needing cleaning is cleaned
  - a descriptor annotated for unpacking names a directory, so its
    path gains a trailing separator; an unpack annotation of "false"
    and an unrelated annotation both leave the path alone
  - the descriptor half of a File comes from the descriptor path, not
    from the name the file was written under
  - Add preserves insertion order, and Files is empty beforehand
  - Files returns a copy: writing to the returned slice does not change
    what a later call reports

The last one is the reason for the slices.Clone in Files, and nothing
pinned it. Verified by mutation: replacing that call with a bare
return of the stored slice fails the new test.

Coverage goes from 62.3% to 96.2% of statements. The remaining
uncovered lines are the filepath.Abs error branches in newFile and the
error return it feeds in Add, which the source itself notes are "not
likely to go wrong since the file has already be written to file
store" and cannot be reached without faking the filesystem.

No production code is changed.

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

codecov Bot commented Sep 24, 2026 •

Copy link
Copy Markdown

Codecov Report

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

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2189      +/-   ##
==========================================
+ Coverage   87.78%   87.81%   +0.03%     
==========================================
  Files         139      139              
  Lines        5714     5714              
==========================================
+ Hits         5016     5018       +2     
+ Misses        417      416       -1     
+ Partials      281      280       -1     

☔ 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.

filepath.Join cleans its own result, so the "absolute name is cleaned"
input reached newFile already clean and a no-op in place of
filepath.Clean kept it green. Build the path by concatenation instead.

Add TestPulled_concurrent so -race has concurrent Add and Files calls
to inspect, mirroring TestTagged_Tags_concurrent.

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

@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 8c979f1 into oras-project:main Sep 27, 2026
10 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