Skip to content

Commit

Permalink
make skeleton
Browse files Browse the repository at this point in the history
  • Loading branch information
moricho committed Sep 1, 2020
0 parents commit fba02b5
Show file tree
Hide file tree
Showing 7 changed files with 227 additions and 0 deletions.
9 changes: 9 additions & 0 deletions cmd/tparallel/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package main

import (
"golang.org/x/tools/go/analysis/unitchecker"

"github.com/moricho/tparallel"
)

func main() { unitchecker.Main(tparallel.Analyzer) }
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/moricho/tparallel

go 1.15

require golang.org/x/tools v0.0.0-20200831203904-5a2aa26beb65
22 changes: 22 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/mod v0.3.0 h1:RM4zey1++hCTbCVQfnWeKs9/IEsaBLA8vTkd0WVtmH4=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200822124328-c89045814202/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20200831203904-5a2aa26beb65 h1:DajXNh69ob79PCQz1N7OHxmqq6ASZC5xAnJJWIQGR6I=
golang.org/x/tools v0.0.0-20200831203904-5a2aa26beb65/go.mod h1:Cj7w3i3Rnn0Xh82ur9kSqwfTHTeVxaDqrfMjpcNT6bE=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
36 changes: 36 additions & 0 deletions plugin/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// This file can build as a plugin for golangci-lint by below command.
// go build -buildmode=plugin -o path_to_plugin_dir tparallel/plugin/tparallel
// See: https://golangci-lint.run/contributing/new-linters/#how-to-add-a-private-linter-to-golangci-lint

package main

import (
"strings"

"golang.org/x/tools/go/analysis"

"github.com/moricho/tparallel"
)

// flags for Analyzer.Flag.
// If you would like to specify flags for your plugin, you can put them via 'ldflags' as below.
// $ go build -buildmode=plugin -ldflags "-X 'main.flags=-opt val'" tparallel/plugin/tparallel
var flags string

// AnalyzerPlugin provides analyzers as a plugin.
// It follows golangci-lint style plugin.
var AnalyzerPlugin analyzerPlugin

type analyzerPlugin struct{}

func (analyzerPlugin) GetAnalyzers() []*analysis.Analyzer {
if flags != "" {
flagset := tparallel.Analyzer.Flags
if err := flagset.Parse(strings.Split(flags, " ")); err != nil {
panic("cannot parse flags of tparallel: " + err.Error())
}
}
return []*analysis.Analyzer{
tparallel.Analyzer,
}
}
99 changes: 99 additions & 0 deletions testdata/src/sample/sample.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package sample

import (
"fmt"
"testing"
)

func call(name string) {
fmt.Println(name)
}

func setup(name string) func() {
fmt.Printf("setup: %s\n", name)
return func() {
fmt.Println("clean up finished")
}
}

func Test_Func1(t *testing.T) { // NG: cleanup
teardown := setup("Test_Func1")
defer teardown()

t.Parallel()

t.Run("Func1_Sub1", func(t *testing.T) {
call("Func1_Sub1")
t.Parallel()
})

t.Run("Func1_Sub2", func(t *testing.T) {
call("Func1_Sub2")
t.Parallel()
})
}

func Test_Func2(t *testing.T) { // NG: parallel
teardown := setup("Test_Func2")
t.Cleanup(teardown)

t.Run("Func2_Sub1", func(t *testing.T) {
call("Func2_Sub1")
t.Parallel()
})

t.Run("Func2_Sub2", func(t *testing.T) {
call("Func2_Sub2")
t.Parallel()
})
}

func Test_Func6(t *testing.T) { // NG: parallel
teardown := setup("Test_Func6")
t.Cleanup(teardown)

t.Parallel()

t.Run("Func6_Sub1", func(t *testing.T) {
call("Func6_Sub1")
})

t.Run("Func6_Sub2", func(t *testing.T) {
call("Func6_Sub2")
})
}

func Test_Func3(t *testing.T) { // OK
teardown := setup("Test_Func3")
t.Cleanup(teardown)
t.Parallel()

t.Run("Func3_Sub1", func(t *testing.T) {
call("Func3_Sub1")
t.Parallel()
})

t.Run("Func3_Sub2", func(t *testing.T) {
call("Func3_Sub2")
t.Parallel()
})
}

func Test_Func4(t *testing.T) { // OK
teardown := setup("Test_Func4")
defer teardown()
t.Parallel()

t.Run("Func4_Sub1", func(t *testing.T) {
call("Func4_Sub1")
})

t.Run("Func4_Sub2", func(t *testing.T) {
call("Func4_Sub2")
})
}

func Test_Func5(t *testing.T) { // OK
teardown := setup("Test_Func5")
defer teardown()
}
41 changes: 41 additions & 0 deletions tparallel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package tparallel

import (
"go/ast"

"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
"golang.org/x/tools/go/ast/inspector"
)

const doc = "tparallel is ..."

// Analyzer is ...
var Analyzer = &analysis.Analyzer{
Name: "tparallel",
Doc: doc,
Run: run,
Requires: []*analysis.Analyzer{
inspect.Analyzer,
},
}

func run(pass *analysis.Pass) (interface{}, error) {
inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)

nodeFilter := []ast.Node{
(*ast.Ident)(nil),
}

inspect.Preorder(nodeFilter, func(n ast.Node) {
switch n := n.(type) {
case *ast.Ident:
if n.Name == "gopher" {
pass.Reportf(n.Pos(), "identifier is gopher")
}
}
})

return nil, nil
}

15 changes: 15 additions & 0 deletions tparallel_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package tparallel_test

import (
"testing"

"golang.org/x/tools/go/analysis/analysistest"

"github.com/moricho/tparallel"
)

// TestAnalyzer is a test for Analyzer.
func TestAnalyzer(t *testing.T) {
testdata := analysistest.TestData()
analysistest.Run(t, testdata, tparallel.Analyzer, "sample")
}

0 comments on commit fba02b5

Please sign in to comment.