Skip to content

test(Docker): cover getDockerCredentials() paths - #3448

Draft
Dave Shoup (shouples) wants to merge 1 commit into
mainfrom
djs/issue-1941-execsync-wrapper
Draft

test(Docker): cover getDockerCredentials() paths#3448
Dave Shoup (shouples) wants to merge 1 commit into
mainfrom
djs/issue-1941-execsync-wrapper

Conversation

@shouples

Copy link
Copy Markdown
Contributor

Summary of Changes

getDockerCredentials() (which reads the local Docker credential helper so the extension can authenticate to a private registry) shipped in #1940 without unit tests, because it called execSync imported straight from child_process and Sinon can only stub module exports. This PR closes that gap.

  • Add a thin, stubbable execSync wrapper to src/utils/fsWrappers.ts (the same module getDockerCredentials() already imports readFileSync from) and route the credential lookup through it.
  • Add a getDockerCredentials() test suite covering all eight branches: cache hit, missing/unreadable Docker config, no credsStore configured, the happy path (query + validate + cache), the docker-credential- prefix dedup, command failure, invalid credentials, and non-JSON output.
  • Harden one path found while writing the tests: JSON.parse() of the helper's stdout now runs inside the command try/catch, so a helper that prints a plain-text error instead of JSON is handled like any other failure (log and return undefined) rather than throwing to the caller.

Click-testing instructions

Not applicable - this PR only adds tests and hardens one error path (see above), so there's nothing to click-test. Verified via the unit tests noted in the section below.

Optional: Any additional details or context that should be provided?

  • Wrapper placement. The wrapper lives in fsWrappers.ts per the issue title. That module is already the home for stubbable native (C-backed) wrappers, not strictly filesystem ones - it also wraps os.tmpdir() - so its doc comment was broadened to say so. A separate, sidecar-specific child_process.spawn() wrapper exists in src/sidecar/utils.ts; consolidating the two child_process wrappers into one home is a reasonable follow-up but out of scope here.
  • Wrapper contract. execSync always decodes as UTF-8 and returns a string, so encoding is intentionally not a caller-overridable option (Omit<ExecSyncOptions, "encoding">), keeping the : string return type honest.
  • Testing. npx gulp check, npx gulp lint (0 errors), and npx gulp test -t "credentials" all pass - 30 tests, including the 8 new getDockerCredentials() cases.

Closes #1941

Pull request checklist

Please check if your PR fulfills the following (if applicable):

Tests

  • Added new
  • Updated existing
  • Deleted existing

Release notes

  • Does anything in this PR need to be mentioned in the user-facing CHANGELOG?

`execSync` was imported straight from `child_process`, so Sinon could not
intercept it and `getDockerCredentials()` shipped untested (deferred from
PR #1940). Route it through a new `fsWrappers.execSync` wrapper and add
coverage for the cache hit, missing/unreadable config, happy path, store
prefix dedup, command failure, invalid creds, and non-JSON output paths.

Also move the credential `JSON.parse()` inside the command try/catch so a
helper emitting non-JSON on stdout is handled like any other failure
(log and return undefined) rather than throwing to the caller.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Copilot AI lite review requested due to automatic review settings September 1, 2026 14:41

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds unit test coverage for getDockerCredentials() (Docker credential helper lookup + SecretStorage caching) by introducing a stubbable execSync wrapper and hardening one error path so non-JSON helper stdout is handled gracefully.

Changes:

  • Added a thin execSync wrapper in src/utils/fsWrappers.ts to enable Sinon stubbing.
  • Updated getDockerCredentials() to use the wrapper and to perform JSON.parse() inside the command try/catch.
  • Added a focused getDockerCredentials() unit test suite covering cache, config/read failures, helper invocation behavior, and invalid/non-JSON outputs.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
src/utils/fsWrappers.ts Adds a stubbable execSync wrapper returning UTF-8 decoded output as a string.
src/docker/credentials.ts Routes credential helper execution through the wrapper and catches non-JSON helper stdout by parsing inside try/catch.
src/docker/credentials.test.ts Adds an 8-case test suite covering getDockerCredentials() control-flow branches and caching behavior.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@sonarqube-confluent

Copy link
Copy Markdown

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.

Add fsWrapper for execSync and tests for getDockerCredentials()

2 participants