selectivetesting

package module
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Sep 24, 2023 License: MIT Imports: 10 Imported by: 0

README

go-selectivetesting

Perform selective testing on a Go project based on a list of input files and usage information.

CLI

Installation

Run the following command:

$ go install github.com/pwnedgod/go-selectivetesting/cmd/selectivetesting@latest
Configuration

The following flag arguments can be passed in to the CLI command.

$ selectivetesting -relativepath="../" -prettyoutput -patterns="./..." -depth=10 pkg/package1/changedfile1.go pkg/package1/changedfile2.go pkg/package2/changedfile3.go
  • -analyzeroutpath=<string> Path to output debug information for analyzer.
  • -basepkg=<string> Base package path/module name, will be used instead of <modulepath>/go.mod.
  • -buildflags=<string,string,...> Build flags to use.
  • -cfgpath=<string> Config file to use for command configuration.
  • -depth=<int> Depth of the test search from input files.
  • -gotestargs=<string> The arguments to pass to the go test command. The arguments will be put at the end of the command.
  • -gotestparallel=int Maximum number of parallel go test processes. If not set, it will run the test in series.
  • -gotestrun Whether to run go test with the result of the output. Will output the testing information instead.
  • -moduledir=<string> Path to the directory of the module.
  • -patterns=<string,string,...> Patterns to use for package search.
  • -prettyoutput Whether to output indented json. Will be ignored if -gotestrun is set.
  • -relativepath=<string> Relative path from current working directory for input files.
  • -testall Override output with list of all packages within its groups.
  • -outputemptygroups Whether to output untested groups as a group with empty arrays. Default group included.

A configuration JSON file can also be passed in instead with -cfgpath=<string>.

{
  "relativePath": "../",
  "prettyOutput": true,
  "patterns": ["./..."],
  "moduleDir": ".",
  "depth": 10,
  "buildFlags": ["mycustombuildflag"],
  "testAll": false,
  "analyzerOutPath": "analyzer.json",
  "goTest": {
    "run": true,
    "args": "-race -count 2",
    "parallel": 10
  },
  "groups": [
    {
      "name": "entity-model",
      "patterns": ["github.com/pwnedgod/examplerepo/pkg/entity"]
    },
    {
      "name": "httphandler",
      "patterns": ["github.com/pwnedgod/examplerepo/pkg/http/handler/..."]
    },
    {
      "name": "repo-external",
      "patterns": [
        "github.com/pwnedgod/examplerepo/pkg/grpc",
        "github.com/pwnedgod/examplerepo/pkg/repository"
      ]
    },
    {
      "name": "usecase",
      "patterns": ["github.com/pwnedgod/examplerepo/pkg/usecase/..."]
    }
  ],
  "outputEmptyGroups": true,
  "miscUsages": [
    {
      "regexp": "^<<basepath>>/migration/.+\\.sql$",
      "usedBy": [
        {
          "pkgPath": "github.com/pwnedgod/go-selectivetesting/example1/...",
          "all": true
        },
        {
          "pkgPath": "github.com/pwnedgod/go-selectivetesting/example2/sub",
          "objNames": ["FuncUsingNonGoFiles1", "FuncUsingNonGoFiles2"]
        },
        {
          "pkgPath": "github.com/pwnedgod/go-selectivetesting/example2/sub",
          "fileNames": ["foo.go", "bar.go"]
        }
      ]
    }
  ]
}
JSON Output

If you choose not to use -gotestrun, the application will output a JSON containing all the testing groups.

{
  "uniqueTestCount": 17,
  "groups": [
    {
      "name": "default",
      "testedPkgs": []
    },
    {
      "name": "entity-model",
      "testedPkgs": [
        {
          "pkgPath": "github.com/pwnedgod/examplerepo/pkg/entity",
          "relativePkgPath": "./pkg/entity",
          "hasNotable": true,
          "testNames": ["TestNewWishlist", "TestWishlist_Model"],
          "runRegex": "^(TestNewWishlist|TestWishlist_Model)$"
        }
      ]
    },
    {
      "name": "httphandler",
      "testedPkgs": [
        {
          "pkgPath": "github.com/pwnedgod/examplerepo/pkg/http/handler/api/v1/wishlist",
          "relativePkgPath": "./pkg/http/handler/api/v1/wishlist",
          "hasNotable": false,
          "testNames": [
            "TestHandler_CreateWishlist",
            "TestHandler_DeleteWishlist"
          ],
          "runRegex": "^(TestHandler_CreateWishlist|TestHandler_DeleteWishlist)$"
        }
      ]
    },
    {
      "name": "repo-external",
      "testedPkgs": [
        {
          "pkgPath": "github.com/pwnedgod/examplerepo/pkg/repository",
          "relativePkgPath": "./pkg/repository",
          "hasNotable": false,
          "testNames": [
            "TestWishlist_Create",
            "TestWishlist_Delete",
            "TestWishlist_GetAllByProductSizeSummaryID",
            "TestWishlist_GetByID",
            "TestWishlist_GetByProductSizeSummaryIDAndUserID",
            "TestWishlist_GetByProductTypeAndProductCode",
            "TestWishlist_ListUserWishlists"
          ],
          "runRegex": "^(TestWishlist_Create|TestWishlist_Delete|TestWishlist_GetAllByProductSizeSummaryID|TestWishlist_GetByID|TestWishlist_GetByProductSizeSummaryIDAndUserID|TestWishlist_GetByProductTypeAndProductCode|TestWishlist_ListUserWishlists)$"
        }
      ]
    },
    {
      "name": "usecase",
      "testedPkgs": [
        {
          "pkgPath": "github.com/pwnedgod/examplerepo/pkg/usecase/wishlist",
          "relativePkgPath": "./pkg/usecase/wishlist",
          "hasNotable": false,
          "testNames": [
            "TestWishlistUsecase_CreateWishlist",
            "TestWishlistUsecase_DeleteWishlist",
            "TestWishlistUsecase_GetUserWishlistDetail",
            "TestWishlistUsecase_GetWishlist",
            "TestWishlistUsecase_ListUserWishlists",
            "TestWishlistUsecase_ProcessGeneralPriceUpdate"
          ],
          "runRegex": "^(TestWishlistUsecase_CreateWishlist|TestWishlistUsecase_DeleteWishlist|TestWishlistUsecase_GetUserWishlistDetail|TestWishlistUsecase_GetWishlist|TestWishlistUsecase_ListUserWishlists|TestWishlistUsecase_ProcessGeneralPriceUpdate)$"
        }
      ]
    }
  ]
}

Manual Usage

If you instead want to do your own procedures, you can follow these instructions.

Dependency Installation
$ go get github.com/pwnedgod/go-selectivetesting

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FileAnalyzer

type FileAnalyzer struct {
	// contains filtered or unexported fields
}

func NewFileAnalyzer

func NewFileAnalyzer(basePkg string, notableFileNames []string, options ...Option) *FileAnalyzer

func (*FileAnalyzer) DetermineTests

func (fa *FileAnalyzer) DetermineTests() (map[string]*TestedPackage, int)

func (*FileAnalyzer) Load

func (fa *FileAnalyzer) Load() error

func (*FileAnalyzer) MarshalJSON

func (fa *FileAnalyzer) MarshalJSON() ([]byte, error)

type MiscUsage

type MiscUsage struct {
	Regexp *regexp.Regexp
	UsedBy []MiscUser
}

type MiscUser

type MiscUser struct {
	PkgPath   string
	All       bool
	FileNames []string
	ObjNames  []string
}

type Option

type Option func(*FileAnalyzer)

func WithBuildFlags

func WithBuildFlags(buildFlags ...string) Option

func WithDepth

func WithDepth(depth int) Option

func WithMiscUsages

func WithMiscUsages(miscUsages ...MiscUsage) Option

func WithModuleDir

func WithModuleDir(moduleDir string) Option

func WithPatterns

func WithPatterns(patterns ...string) Option

func WithTestAll

func WithTestAll(testAll bool) Option

type TestedPackage added in v0.0.2

type TestedPackage struct {
	Names      util.Set[string]
	HasNotable bool
}

Directories

Path Synopsis
cmd
internal
app

Jump to

Keyboard shortcuts

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