Skip to content

test(credential): fix TestNewStoreError failing on Windows - #2159

Merged
TerryHowe merged 1 commit into
oras-project:mainfrom
akshita317:fix/credential-store-test-windows
Sep 8, 2026
Merged

TerryHowe merged 1 commit into
oras-project:mainfrom
akshita317:fix/credential-store-test-windows

Conversation

@akshita317

Copy link
Copy Markdown
Contributor

Problem

go test ./... fails on Windows on a clean checkout of main:

=== RUN   TestNewStoreError
    store_test.go:41: Expected NewStore to return nil but actually returned &{0x...  {true false}  ...}
    store_test.go:47: Expected err to be not nil
--- FAIL: TestNewStoreError (0.00s)
FAIL    oras.land/oras/internal/credential

The test makes a config file unreadable with os.Chmod(filename, 000) and expects NewStore to fail opening it. On Windows os.Chmod can only toggle the read-only attribute — it cannot clear the read permission. The file stays readable, NewStore succeeds, and both assertions fire.

build.yml runs ubuntu-latest only (the matrix is over Go versions, not OS), so CI never sees this. It only bites people developing on Windows.

Changes

Skip the permission case where permissions don't apply — on Windows, and on Unix when running as root (a root container hits the same failure for the same underlying reason: the file remains readable).

Add TestNewStoreMalformedConfig so the error path stays covered everywhere. It reaches the same NewStore failure through JSON decoding, which behaves identically on every platform, so skipping the permission case does not leave the error path untested on Windows.

Assert the error message, which the test never actually did:

ok := strings.Contains(err.Error(), "failed to open config file")
reflect.DeepEqual(ok, true)   // result discarded — no-op

reflect.DeepEqual returns a bool that was thrown away, so the test passed on any error. Now checked with strings.Contains. Both expected strings are verified against the code that produces them, credentials/internal/config/config.go:

return nil, fmt.Errorf("failed to open config file at %s: %w", configPath, err)
return nil, fmt.Errorf("failed to decode config file %s: %w: %v", configPath, ErrInvalidConfigFormat, err)

path.Join → filepath.Join — path is for slash-separated paths, not filesystem paths.

t.Errorf → t.Fatalf for setup failures, which previously let the test continue against a file it had failed to create or chmod.

Verification

Windows, before → FAIL. After:

$ go test ./internal/credential/ -v -cover
=== RUN   TestNewStoreMalformedConfig
--- PASS: TestNewStoreMalformedConfig (0.08s)
=== RUN   TestNewStoreError
    store_test.go:51: os.Chmod cannot make a file unreadable on Windows
--- SKIP: TestNewStoreError (0.00s)
PASS
ok      oras.land/oras/internal/credential      coverage: 76.9% of statements

On Linux both tests run; the permission case keeps its original behaviour, now with the assertion actually enforced.

gofmt and go vet are clean.

TestNewStoreError makes a config file unreadable with os.Chmod(f, 000) and
expects NewStore to fail. On Windows os.Chmod can only toggle the read-only
attribute and cannot clear the read permission, so the file stays readable,
NewStore succeeds, and both assertions fire:

    store_test.go:41: Expected NewStore to return nil but actually returned &{...}
    store_test.go:47: Expected err to be not nil

CI runs ubuntu-latest only, so this is invisible there, but `go test ./...`
fails for anyone developing on Windows.

Skip the permission case on Windows, and on Unix when running as root, where
permission bits do not restrict access either. To keep the error path covered
on every platform, add TestNewStoreMalformedConfig, which reaches the same
failure through JSON decoding and so is OS-independent.

Also fix two existing problems in the test:

  - reflect.DeepEqual(ok, true) discarded its result, so the expected
    "failed to open config file" text was never actually asserted; the test
    would have passed on any error at all. It is now checked with
    strings.Contains. Both expected strings are verified against the
    producing code in oras-go's credentials/internal/config/config.go.

  - path.Join builds slash-separated paths and is not intended for
    filesystem paths; use filepath.Join.

Setup failures now use t.Fatalf rather than t.Errorf, which previously let
the test continue against a file it had failed to create or chmod.

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 4728be6 into oras-project:main Sep 8, 2026
7 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