GitXplorerGitXplorer
l

usetesting

public
24 stars
0 forks
0 issues

Commits

List of commits on branch main.
Verified
5ebad52bebc29d3335299e0b486ce98482c179aa

chore: add PR template

lldez committed 4 days ago
Verified
e8021550f1525e50671c6f095d67d5b2eaf51a5f

docs: add hint to enable to linter (#3)

SSuperSandro2000 committed 11 days ago
Verified
0d3f6e215e25bbc18d4babad80ffdd0a09676dc6

chore: update funding

lldez committed 12 days ago
Unverified
1bbdad773c3d8700fba7d9e2500993177f93bcb9

fix: Go version prerelease

lldez committed 16 days ago
Unverified
f3850893f003b4040eb3511d14806ab7b743fd0b

refactor: minor changes

lldez committed 20 days ago
Unverified
619de3bbaa3adf3ad345470410240ece7c26b821

docs: improve readme

lldez committed 20 days ago

README

The README file for this repository.

UseTesting

Detects when some calls can be replaced by methods from the testing package.

Sponsor

Usages

Inside golangci-lint

Recommended.

linters:
  enable:
    - usetesting

linters-settings:
  usetesting:
    # Enable/disable `os.CreateTemp("", ...)` detections.
    # Default: true
    os-create-temp: false

    # Enable/disable `os.MkdirTemp()` detections.
    # Default: true
    os-mkdir-temp: false

    # Enable/disable `os.Setenv()` detections.
    # Default: false
    os-setenv: true

    # Enable/disable `os.TempDir()` detections.
    # Default: false
    os-temp-dir: true
    
    # Enable/disable `os.Chdir()` detections.
    # Disabled if Go < 1.24.
    # Default: true
    os-chdir: false

    # Enable/disable `context.Background()` detections.
    # Disabled if Go < 1.24.
    # Default: true
    context-background: false

    # Enable/disable `context.TODO()` detections.
    # Disabled if Go < 1.24.
    # Default: true
    context-todo: false

As a CLI

go install github.com/ldez/usetesting/cmd/usetesting@latest
usetesting: Reports uses of functions with replacement inside the testing package.

Usage: usetesting [-flag] [package]

Flags:
  -contextbackground
        Enable/disable context.Background() detections (default true)
  -contexttodo
        Enable/disable context.TODO() detections (default true)
  -oschdir
        Enable/disable os.Chdir() detections (default true)
  -osmkdirtemp
        Enable/disable os.MkdirTemp() detections (default true)
  -ossetenv
        Enable/disable os.Setenv() detections (default false)
  -ostempdir
        Enable/disable os.TempDir() detections (default false)
  -oscreatetemp
        Enable/disable os.CreateTemp("", ...) detections (default true)
...

Examples

os.MkdirTemp

func TestExample(t *testing.T) {
	os.MkdirTemp("a", "b")
	// ...
}

It can be replaced by:

func TestExample(t *testing.T) {
	t.TempDir()
    // ...
}

os.TempDir

func TestExample(t *testing.T) {
	os.TempDir()
	// ...
}

It can be replaced by:

func TestExample(t *testing.T) {
	t.TempDir()
    // ...
}

os.CreateTemp

func TestExample(t *testing.T) {
	os.CreateTemp("", "x")
	// ...
}

It can be replaced by:

func TestExample(t *testing.T) {
    os.CreateTemp(t.TempDir(), "x")
    // ...
}

os.Setenv

func TestExample(t *testing.T) {
	os.Setenv("A", "b")
	// ...
}

It can be replaced by:

func TestExample(t *testing.T) {
	t.Setenv("A", "b")
    // ...
}

os.Chdir (Go >= 1.24)

func TestExample(t *testing.T) {
	os.Chdir("x")
	// ...
}

It can be replaced by:

func TestExample(t *testing.T) {
	t.Chdir("x")
    // ...
}

context.Background (Go >= 1.24)

func TestExample(t *testing.T) {
    ctx := context.Background()
	// ...
}

It can be replaced by:

func TestExample(t *testing.T) {
    ctx := t.Context()
    // ...
}

context.TODO (Go >= 1.24)

func TestExample(t *testing.T) {
    ctx := context.TODO()
	// ...
}

It can be replaced by:

func TestExample(t *testing.T) {
    ctx := t.Context()
    // ...
}

References