tparagen

package module
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2024 License: MIT Imports: 14 Imported by: 0

README

tparagen

tparagen inserts the T.Parallel() method from the testing package into a test function in a specific source file or an entire directory.

ci

Background

To run go tests in concurrency, you need to the T.Parallel() method from the testing package into the main/sub test you want to run in concurrency.

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

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

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

Description

tparagen is cli tool for insert the T.Parallel() method from the testing package 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(t *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() // <- inserted

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

In Go versions earlier than 1.21, code to reassign the loop variable is inserted.(see: https://go.dev/blog/loopvar-preview)

package test

import (
	"fmt"
	"testing"
)

func SampleTest(t *testing.T) {
	t.Parallel() // <- inserted

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

Demo

demo

The following cases are supported
  • Insert RunParallel helper function into the main/sub test function.
  • Loop variables are not re-initialised if the minimum version of Go is less than 1.22
  • 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).
  --ignore=IGNORE        ignore directory names. ex: foo,bar,baz (testdata directory is always ignored.)
  --min-go-version=1.21  minimum go version

Installation

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

Contribution

Bug reports and pull requests are welcome on GitHub at https://github.com/sho-hata/tparagen. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.

License

MIT

Code of Conduct

Everyone interacting in the tparagen project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.

Author

sho-hata

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GenerateTParallel added in v1.1.2

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

GenerateTParallel processes a Go source file to ensure that test functions and subtests call t.Parallel() where appropriate. It parses the provided source code, inspects the AST for test functions, and inserts t.Parallel() calls if they are missing. Additionally, it handles cases where test functions use t.Setenv() and ensures proper handling of loop variables in subtests.

Returns: - A byte slice containing the modified source code. - An error if any issues occur during parsing or formatting.

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