Skip to content
/ c3 Public

"Cleanup Context Checker" Go static analyzer which detects (*testing.common).Context in (*testing.common).Cleanup

License

Notifications You must be signed in to change notification settings

ikura-hamu/c3

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

c3

Cleanup Context Checker

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

package a

import (
	"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

go install github.com/ikura-hamu/c3@latest
go vet -vettool=$(which c3) ./...

golangci-lint Module Plugin

https://golangci-lint.run/plugins/module-plugins/

  1. Configure golangci-lint customization.

.custom-gcl.yml

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"
  1. Build custom golangci-lint bianry.
golangci-lint custom
  1. Set up golangci-lint configuration.

.golangci.yml

linters:
  enable:
    - c3

linters-settings:
  custom:
    c3:
      type: "module"
  1. Run custom golangci-lint.
./custom-gcl run

Footnotes

  1. https://pkg.go.dev/testing#T.Context

About

"Cleanup Context Checker" Go static analyzer which detects (*testing.common).Context in (*testing.common).Cleanup

Topics

Resources

License

Stars

Watchers

Forks

Languages