Skip to content

test: verify display handler calls in command tests instead of rendered output #2281

Closed

Description

@TerryHowe

Summary

Command tests in cmd/oras/root/... should verify that the right display handler methods are called, not assert on rendered output. Output formatting should be tested once, in cmd/oras/internal/display/....

Right now handlers are built inline in most commands, so tests can't inject a fake. Coverage gaps follow from that: on both sides of the boundary, many paths are only exercised by e2e tests.

Current coverage (origin/main @ ed26899, unit tests only)

go test -cover ./cmd/oras/internal/display/...: 73.1% overall

Package Coverage Notable gaps
display/content, metadata/json, metadata/template 100% —
metadata/table, metadata/model 96–97% —
status/progress, status/console, progress/humanize 89–92% NewManager, console.size, Bytes.String
metadata/tree 89.7% parts of OnDiscovered
metadata/descriptor 80%
status 63.6% all of discard.go; text handlers for blob push, index create/update and manifest push, and TrackTarget; tty blob-push handler and StopTracking / OnCopySkipped / PreCopy
metadata/text 60.4% handlers for manifest delete, index create, manifest push, pull, push, repo ls and repo tags all at 0%
status/track 54.9% NewReader, StopManager, target.Close / Report
metadata 50% discard.go
display (handler.go) 36.7% 10 factories at 0% (NewDiscoverHandler, NewTagHandler, NewManifestPushHandler, NewBlobPushHandler, …); push/attach/pull partially covered

Command packages: root 50.8%, root/blob 21.4%, root/manifest 38.2%, root/manifest/index 46.2%.

Problem

18 call sites in cmd/oras/root/... construct handlers inline, e.g. display.NewPushHandler(...) in push.go. A test has no seam to substitute a fake, so the only way to check behavior is to capture and compare output (pull_test.go, manifest/fetch_test.go, cp_test.go). The same formatting gets asserted in two places, and command tests break on cosmetic output changes.

Some code already has the seam:

  • cp.go: copyAndFinalize, doCopy and the other copy helpers take status.CopyHandler / metadata.CopyHandler as parameters.
  • backup.go: finalizeBackupOutput takes a metadata.BackupHandler, and backup_test.go uses a mockBackupHandler that records calls.

Proposal

1. Split responsibilities

  • cmd/oras/internal/display/...: owns exact output. Test each text/json/template/tty handler directly. Add a table test for each handler.go factory asserting the concrete handler types it returns for each format and tty combination.
  • cmd/oras/root/...: tests use fakes that record calls (and their order) and can return injected errors. Assert that the expected calls happened, e.g. pushed → tagged → Render, and that a handler error stops the command. Don't assert on output text.

2. Add a seam to each command
Split runX so it builds the handlers and passes them to a doX(ctx, statusHandler, metadataHandler, …) function, following the cp.go / backup.go pattern. Tests call doX directly with fakes. This is preferred over package-level factory variables (var newPushHandler = display.NewPushHandler), which add global mutable state and block t.Parallel().

3. Shared fakes
Put recording fakes for the status.*Handler / metadata.*Handler interfaces in a shared test package (e.g. cmd/oras/internal/display/displaytest), so each command test doesn't hand-roll its own.

Suggested plan

  • Factory table test for cmd/oras/internal/display/handler.go
  • Unit tests for the uncovered metadata/text, status text/tty and discard handlers
  • Shared recording fakes package
  • Convert one command (push or pull) as the reference implementation
  • Convert remaining commands; migrate output assertions out of pull_test.go, manifest/fetch_test.go, cp_test.go

Activity

  1. added
    test
    on Oct 23, 2026
  2. added this to the future milestone on Oct 23, 2026
  3. TanvirTian commented on Oct 24, 2026

    @TanvirTian
    Contributor

    Since no one has picked this up yet, I'll work on it. Thanks

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    test

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions