Skip to content

chore(utilities): add tests for previously-uncovered helpers#2510

Open
mastermanas805 wants to merge 1 commit intosupabase:masterfrom
mastermanas805:test/utilities-coverage
Open

chore(utilities): add tests for previously-uncovered helpers#2510
mastermanas805 wants to merge 1 commit intosupabase:masterfrom
mastermanas805:test/utilities-coverage

Conversation

@mastermanas805
Copy link
Copy Markdown

What

Adds unit tests for three small files in internal/utilities/ that had zero coverage:

  • strings.goStringValue and StringPtr
  • context.gocontextKey.String, WithRequestID, GetRequestID, WaitForCleanup
  • io.goSafeClose

Why

Coverage of internal/utilities/ was the lowest among utility-style packages in the module:

internal/utilities                   50.8%
internal/utilities/siwe              96.2%
internal/utilities/siws              97.2%
internal/utilities/version          100.0%
internal/crypto                      91.2%
internal/conf                        99.1%

Each of the helpers covered by this PR is a pure-ish function used widely across the API layer, so a behavior change here would propagate quickly. Locking the contracts in tests makes the next maintenance pass safer without changing any production code.

Coverage delta

Package Before After
internal/utilities 50.8% 62.4%

(+11.6 percentage points, no production code touched.)

What's tested

strings.go

  • StringValue(nil) and StringValue(&"") both return ""
  • StringValue(&s) returns s for non-empty s
  • StringPtr("") returns nil; StringPtr(s) returns *s for non-empty
  • The pointer returned by StringPtr is independent of the input variable (defends against future refactors that take the address of a closure-captured variable)
  • StringValue(StringPtr(c)) == c round-trip across edge cases (empty, ASCII, whitespace, newline, multi-byte rune)

context.go

  • contextKey.String() formatting (covers both populated and empty key)
  • WithRequestID / GetRequestID round-trip
  • GetRequestID returns "" when key absent
  • Replacing a previously-set request ID
  • Inheritance through context.WithCancel (defends the public-API guarantee that derived contexts see the parent value)
  • WaitForCleanup returns when the wait group completes
  • WaitForCleanup returns when the context is cancelled, even if the wait group never completes (covers the leak path)

io.go

  • SafeClose invokes Close() on the underlying closer
  • SafeClose does not panic when Close() returns an error
  • SafeClose does not panic on io.NopCloser(nil)

How to verify (for reviewers)

go test ./internal/utilities/ -count=1 -v -run 'TestStringValue|TestStringPtr|TestContextKey|TestRequestID|TestWaitForCleanup|TestSafeClose|TestStringValueAndStringPtr'
go test -cover ./internal/utilities/

The -cover line should report 62.4%.

Verification I ran locally

  • go test ./internal/utilities/ -count=1 -v — all new tests pass
  • go test ./... -count=1 -p 1 -race — full module suite green
  • gofmt -l on the three new files — empty
  • go vet ./... — empty

Notes

  • No production source files were touched; this is purely a test addition.
  • Naming follows the existing repo convention (<file>_test.go per source file).
  • WaitForCleanup is exercised end-to-end via real goroutines plus context.WithCancel, matching how the production callers use it. No mocking of channels.
  • I deliberately stopped at "small simple changes" scope: the package's other low-coverage areas (postgres.go, hibpcache.go, request.go::GetBodyBytes) need real Postgres errors / bloom-filter setup / multipart bodies and would each be its own PR.

@mastermanas805 mastermanas805 force-pushed the test/utilities-coverage branch from d2818fc to 4ec0d5c Compare April 26, 2026 06:49
@mastermanas805 mastermanas805 marked this pull request as ready for review April 26, 2026 06:50
@mastermanas805 mastermanas805 requested a review from a team as a code owner April 26, 2026 06:50
internal/utilities/strings.go, context.go, and io.go held small
pure-ish helpers but had no test coverage. Add focused unit tests so
behaviour changes get caught: round-trip tests for StringValue and
StringPtr; a context-key formatting check plus set/read/replace/derive
cases for WithRequestID and GetRequestID; cancellation and completion
cases for WaitForCleanup; and behaviour assertions for SafeClose
covering the happy path, the error path, and io.NopCloser.

Coverage for the package goes from 50.8% to 62.4% with no behaviour
changes.

Signed-off-by: Manas Srivastava <mastermanas805@gmail.com>
@mastermanas805 mastermanas805 force-pushed the test/utilities-coverage branch from 4ec0d5c to bf5de18 Compare April 26, 2026 07:12
@mastermanas805 mastermanas805 changed the title test(utilities): add tests for previously-uncovered helpers chore(utilities): add tests for previously-uncovered helpers Apr 26, 2026
@mastermanas805 mastermanas805 marked this pull request as draft April 26, 2026 07:12
@mastermanas805 mastermanas805 marked this pull request as ready for review April 26, 2026 07:15
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.

1 participant