https://github.com/ikura-hamu/c3
Go stativ analyzer which detects `(*testing.common).Context` in `(*testing.common).Cleanup`
https://github.com/ikura-hamu/c3
go golang golangci-lint govet lint
Last synced: 4 months ago
JSON representation
Go stativ analyzer which detects `(*testing.common).Context` in `(*testing.common).Cleanup`
- Host: GitHub
- URL: https://github.com/ikura-hamu/c3
- Owner: ikura-hamu
- License: mit
- Created: 2025-03-07T12:53:29.000Z (4 months ago)
- Default Branch: main
- Last Pushed: 2025-03-07T14:03:29.000Z (4 months ago)
- Last Synced: 2025-03-07T14:30:27.288Z (4 months ago)
- Topics: go, golang, golangci-lint, govet, lint
- Language: Go
- Homepage:
- Size: 6.84 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# c3
**C**leanup **C**ontext **C**hecker
c3 is a tool to analyze and warn against calling `(*testing.common).Context` within `(*testing.common).Cleanup` functions in Go tests.
`(*testing.common).Context` is canceled before `(*testing.common).Cleanup` functions are called, so calling `(*testing.common).Context` within `(*testing.common).Cleanup` functions can cause unexpected behavior.
> Context returns a context that is canceled just before Cleanup-registered functions are called.[^1]
[^1]: https://pkg.go.dev/testing#T.Context
```go
package aimport (
"context"
"testing"
)func cleanup(t *testing.T) {
t.Context()
}func f(ctx context.Context) { }
func TestA(t *testing.T) {
t.Cleanup(func() { t.Context() }) // want `avoid calling \(\*testing\.common\)\.Context inside Cleanup`
t.Cleanup(func() { cleanup(t) }) // want `avoid calling \(\*testing\.common\)\.Context inside Cleanup`
t.Cleanup(func() { f(t.Context()) }) // want `avoid calling \(\*testing\.common\)\.Context inside Cleanup`
t.Cleanup(func() { context.Background() }) // ok
}
```## Usage
### go vet
```bash
go install github.com/ikura-hamu/c3@latest
``````bash
go vet -vettool=$(which c3) ./...
```### golangci-lint Module Plugin
https://golangci-lint.run/plugins/module-plugins/
1. Configure golangci-lint customization.
`.custom-gcl.yml`
```yaml
version: "v1.64.1" # golangci-lint version
destination: "."
name: "custom-gcl"
plugins:
- module: "github.com/ikura-hamu/c3"
path: "github.com/ikura-hamu/c3"
version: "latest"
```2. Build custom golangci-lint bianry.
```bash
golangci-lint custom
```3. Set up golangci-lint configuration.
`.golangci.yml`
```yml
linters:
enable:
- c3linters-settings:
custom:
c3:
type: "module"
```4. Run custom golangci-lint.
```bash
./custom-gcl run
```