testcontainers-go icon indicating copy to clipboard operation
testcontainers-go copied to clipboard

chore: enable noctx linter

Open mmorel-35 opened this issue 2 months ago โ€ข 3 comments

What does this PR do?

Enables and fixes issues from noctx linter

Why is it important?

Related issues

mmorel-35 avatar Sep 27 '25 10:09 mmorel-35

Deploy Preview for testcontainers-go ready!

Name Link
Latest commit 70c9eac0a52db1e8f088cb6da7952646a7a41531
Latest deploy log https://app.netlify.com/projects/testcontainers-go/deploys/68ed4553b8bf1c0008ff302d
Deploy Preview https://deploy-preview-3314--testcontainers-go.netlify.app
Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

netlify[bot] avatar Sep 27 '25 10:09 netlify[bot]

Summary by CodeRabbit

  • New Features
    • Improved cancellation/timeout support in internal HTTP/download flows, enhancing reliability in edge cases.
  • Refactor
    • Broader propagation of contexts across network, HTTP, DB, and subprocess operations to improve robustness.
  • Tests
    • Updated tests to use context-aware requests, DB calls, command execution, and listeners; standardized response-body closing.
  • Documentation
    • Examples updated to create context-aware HTTP requests and properly close response bodies.
  • Chores
    • Enabled an additional linter to encourage context usage and consistency.

Walkthrough

Adds the noctx linter and systematically converts many tests and internal helpers to use context-aware APIs: HTTP requests use NewRequestWithContext/Do and close bodies; DB calls use Context variants; exec, dial, and listen use context-aware functions; some internal helpers accept contexts.

Changes

Cohort / File(s) Summary
Lint configuration
\.golangci.yml
Enable the noctx linter in golangci-lint configuration.
HTTP requests โ†’ context-aware
docker_test.go, logconsumer_test.go, wait/http_test.go, docs/features/creating_container.md, examples/nginx/nginx_test.go, docker_auth_test.go, modules/.../*_{test,examples}.go
Replace http.Get/http.NewRequest/client .Get/.Post with http.NewRequestWithContext(..., http.NoBody) + Client.Do(req); add request creation error checks and ensure response bodies are closed. Thread t.Context()/ctx where applicable.
Database calls โ†’ Context variants
modules/{cockroachdb,databend,dolt,mariadb,mssql,mysql,postgres,yugabytedb} and related examples/tests
Replace Ping, Exec, Query, QueryRow, Prepare with PingContext, ExecContext, QueryContext, QueryRowContext, PrepareContext, passing ctx.
Exec/subprocess โ†’ context-aware
generic_test.go, internal/core/docker_host.go, modules/compose/compose_local.go, testcontainers_test.go, modulegen/internal/tools/exec.go
Swap exec.Command(...) for exec.CommandContext(ctx, ...); add ctx parameter to internal runCommand and execute helpers and update call sites.
Networking dial/listen โ†’ context-aware
docker_auth_test.go, wait/host_port_test.go
Replace net.Dial with DialContext (via net.Dialer) and net.Listen with net.ListenConfig{}.Listen(ctx, ...) to use test contexts.
Misc. tests & examples
modules/{artemis,chroma,consul,elasticsearch,k6,localstack,meilisearch,mockserver,openfga,opensearch,pulsar,qdrant,registry,socat,vearch,weaviate,ollama} and many example files
Thread contexts into HTTP/DB/command flows, add request creation checks and response body closes; update k6.downloadFileFromDescription signature to accept ctx. No exported API changes except internal helper signature updates noted.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  participant T as Test/Caller
  participant HTTP as http.Client
  participant S as Remote Service

  Note over T: Build request bound to ctx
  T->>HTTP: NewRequestWithContext(ctx, GET, URL, http.NoBody)
  T->>HTTP: Do(req)
  alt ctx active
    HTTP->>S: HTTP request
    S-->>HTTP: Response
    HTTP-->>T: resp
    Note right of T: defer resp.Body.Close()
  else ctx canceled/deadline
    HTTP-->>T: error (context canceled/timeout)
  end
sequenceDiagram
  autonumber
  participant T as Caller
  participant OS as exec.CommandContext
  participant P as Child Process

  T->>OS: CommandContext(ctx, cmd...)
  alt ctx active
    OS->>P: start
    P-->>OS: exit/output
    OS-->>T: result
  else ctx canceled
    OS-->>P: kill
    OS-->>T: error (killed/canceled)
  end

Estimated code review effort

๐ŸŽฏ 3 (Moderate) | โฑ๏ธ ~25 minutes

Suggested labels

chore

Suggested reviewers

  • stevenh
  • mdelapenya

Poem

A rabbit hops with context bright,
I thread each request through day and night.
PingContext hums, Do(req) takes flight,
Bodies closed and linters in sight.
Carrots for tests that run just right. ๐Ÿฅ•๐Ÿ‡

Pre-merge checks and finishing touches

โŒ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage โš ๏ธ Warning Docstring coverage is 4.55% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
โœ… Passed checks (2 passed)
Check name Status Explanation
Title Check โœ… Passed The title succinctly indicates that this PR enables the noctx linter and addresses the resulting issues, directly reflecting the main purpose of the changeset without unnecessary detail.
Description Check โœ… Passed The description states that the PR enables and fixes issues reported by the noctx linter, which aligns directly with the code modifications and configuration changes presented in the changeset.
โœจ Finishing touches
  • [ ] ๐Ÿ“ Generate docstrings
๐Ÿงช Generate unit tests (beta)
  • [ ] Create PR with unit tests
  • [ ] Post copyable unit tests in a comment

๐Ÿ“œ Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

๐Ÿ“ฅ Commits

Reviewing files that changed from the base of the PR and between 4e21ccddbb27a1507c3074ee8d6c204487f45fb1 and 70c9eac0a52db1e8f088cb6da7952646a7a41531.

๐Ÿ“’ Files selected for processing (10)
  • .golangci.yml (1 hunks)
  • modules/artemis/artemis_test.go (2 hunks)
  • modules/databend/databend_test.go (2 hunks)
  • modules/dolt/dolt.go (1 hunks)
  • modules/k6/k6.go (3 hunks)
  • modules/mariadb/mariadb_test.go (4 hunks)
  • modules/mysql/mysql_test.go (3 hunks)
  • modules/registry/registry_test.go (5 hunks)
  • modules/socat/examples_test.go (2 hunks)
  • modules/socat/socat_test.go (3 hunks)
๐Ÿšง Files skipped from review as they are similar to previous changes (5)
  • modules/socat/socat_test.go
  • modules/registry/registry_test.go
  • modules/socat/examples_test.go
  • modules/artemis/artemis_test.go
  • modules/mysql/mysql_test.go
โฐ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (8)
  • GitHub Check: lint (modules/compose) / lint: modules/compose
  • GitHub Check: lint (modules/azurite) / lint: modules/azurite
  • GitHub Check: lint (modules/vault) / lint: modules/vault
  • GitHub Check: lint (modules/qdrant) / lint: modules/qdrant
  • GitHub Check: lint (modules/milvus) / lint: modules/milvus
  • GitHub Check: lint (modules/dockermodelrunner) / lint: modules/dockermodelrunner
  • GitHub Check: lint (modules/vearch) / lint: modules/vearch
  • GitHub Check: Analyze (go)
๐Ÿ”‡ Additional comments (6)
modules/dolt/dolt.go (1)

136-136: LGTM! Context-aware database operations correctly implemented.

The conversion from non-context to context-aware database operations is correct:

  • db.PingContext(ctx) for connectivity check
  • db.ExecContext(ctx, ...) for all SQL executions

All operations properly propagate the context parameter, satisfying the noctx linter requirements.

Also applies to: 141-141, 148-148, 155-155

modules/mariadb/mariadb_test.go (1)

37-37: LGTM! Context-aware database operations.

All database calls have been correctly updated to their context-aware variants (PingContext, ExecContext, PrepareContext, QueryRowContext), ensuring proper context propagation throughout the test code.

Also applies to: 40-44, 77-77, 80-84, 114-114, 121-121, 125-125, 152-152, 155-155, 159-159

modules/k6/k6.go (3)

40-40: LGTM! Function signature updated for context propagation.

The function now accepts a context parameter, enabling proper cancellation and timeout control for the HTTP download operation.


42-42: LGTM! HTTP request improvements.

Good practices applied:

  • Using NewRequestWithContext for context-aware request handling
  • Replacing nil with http.NoBody (more idiomatic)
  • Adding defer resp.Body.Close() to prevent resource leaks

Also applies to: 57-57


107-107: LGTM! Reasonable use of context.Background().

Since WithRemoteTestScript is a customization option and doesn't receive a context parameter, using context.Background() is appropriate here. The download occurs at option creation time rather than during request customization.

modules/databend/databend_test.go (1)

35-35: LGTM! Context-aware database operations.

All database calls have been correctly updated to their context-aware variants (PingContext, ExecContext, QueryRowContext), ensuring proper context propagation throughout the test code.

Also applies to: 38-41, 61-61, 65-65, 69-72


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

โค๏ธ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

coderabbitai[bot] avatar Sep 27 '25 10:09 coderabbitai[bot]

Thanks for coming with more eyes @stevenh. I think the changes make sense, although I also considered that sending small chunks with the API updates first, and then enable the linter, would be of interest.

mdelapenya avatar Sep 29 '25 11:09 mdelapenya