tparagen

package module
v1.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 10, 2024 License: MIT Imports: 13 Imported by: 0

README

tparagen

tparagen insert testing/T.Parallel() in a test function in a specific source file or in an entire directory.

ci

Background

To run go tests in parallel, you need to insert testing/T.Parallel() into the main/sub test you want to run in parallel.

func SampleTest(t *testing.T) {
	t.Parallel()

	testCases := []struct {
		name string
	}{{name: "foo"}}
	for _, tc := range testCases {
		tc := tc
		t.Run(tc.name, func(x *testing.T) {
			x.Parallel()
			// do anything...
		})
	}
}

If there is your application in production already, you must add a testing/T.Parallel() into any main/sub test. It is a very time-consuming and tedious task.

Description

tparagen is cli tool for insert testing/T.Parallel() into all main/sub test in specified directory.

Before code is below,

package sample

import (
	"fmt"
	"testing"
)

func SampleTest(t *testing.T) {

	testCases := []struct {
		name string
	}{{name: "foo"}}
	for _, tc := range testCases {
		t.Run(tc.name, func(x *testing.T) {
			fmt.Println(tc.name)
		})
	}
}

After execute tparagen, modified code is below.

package test

import (
	"fmt"
	"testing"
)

func SampleTest(t *testing.T) {
	t.Parallel()

	testCases := []struct {
		name string
	}{{name: "foo"}}
	for _, tc := range testCases {
		tc := tc
		t.Run(tc.name, func(x *testing.T) {
			x.Parallel()
			fmt.Println(tc.name)
		})
	}
}

Demo

demo

Features

Before code is below,

func SampleTest(t *testing.T) {

	testCases := []struct {
		name string
	}{{name: "foo"}}
	for _, tc := range testCases {
		t.Run(tc.name, func(x *testing.T) {
			fmt.Println(tc.name)
		})
	}
}

After execute tparagen, modified code is below.

func SampleTest(t *testing.T) {
	t.Parallel()

	testCases := []struct {
		name string
	}{{name: "foo"}}
	for _, tc := range testCases {
		tc := tc
		// ^ tc variable reinitialized
		t.Run(tc.name, func(x *testing.T) {
			x.Parallel()
			fmt.Println(tc.name)
		})
	}
}
The following cases are supported
  • Insert RunParallel helper function into the main/sub test function.
  • Is called in the range method and test case variable tc being used, but is not reinitialised
  • Do not insert if t.Setenv() is called in the test function
  • Ignore specified directories with cli option -i/-ignore
  • nolint comment support: parallel,paralleltest
The following cases are not supported
  • Don't insert if the test function calls another function that calls Setenv().

Synopsis

$ tparagen

Options

$ tparagen --help
usage: tparagen [<flags>]


Flags:
      --[no-]help      Show context-sensitive help (also try --help-long and --help-man).
  -i, --ignore=IGNORE  ignore directory names. ex: foo,bar,baz (testdata directory is always ignored.)

Installation

go install github.com/sho-hata/tparagen/cmd/tparagen@latest

Contribution

  1. Fork (https://github.com/sho-hata/tparagen/fork)
  2. Create a feature branch
  3. Commit your changes
  4. Rebase your local changes against the master branch
  5. Run test suite with the go test ./... command and confirm that it passes
  6. Run gofmt -s
  7. Create new Pull Request

License

MIT

Author

sho-hata

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Process

func Process(filename string, src []byte, needFixLoopVar bool) ([]byte, error)

func Run

func Run(outStream, errStream io.Writer, ignoreDirectories []string, minGoVersion float64) error

Run is entry point.

Types

This section is empty.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL