-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #83 from gostaticanalysis/add-no-testmodfile
Add -copy-parent-gomod flag
- Loading branch information
Showing
9 changed files
with
209 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,13 @@ | ||
package skeleton | ||
|
||
type Info struct { | ||
Kind Kind | ||
Checker Checker | ||
Pkg string | ||
Path string | ||
Cmd bool | ||
Plugin bool | ||
GoMod bool | ||
GoVersion string | ||
Kind Kind | ||
Checker Checker | ||
Pkg string | ||
Path string | ||
Cmd bool | ||
Plugin bool | ||
GoMod bool | ||
GoVersion string | ||
CopyParentGoMod bool | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
v2/skeleton/testdata/kind-inspect-copy-parent-gomod-to-testdata-go-test.golden
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- FAIL: TestAnalyzer (0000s) | ||
analysistest.go:550: a/a.go:6: diagnostic "identifier is gopher" does not match pattern `pattern` | ||
analysistest.go:614: a/a.go:6: no diagnostic was reported matching `pattern` | ||
FAIL | ||
exit status 1 | ||
FAIL example.com/example 0000s |
85 changes: 85 additions & 0 deletions
85
v2/skeleton/testdata/kind-inspect-copy-parent-gomod-to-testdata.golden
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
-- example/cmd/example/main.go -- | ||
package main | ||
|
||
import ( | ||
"example.com/example" | ||
"golang.org/x/tools/go/analysis/unitchecker" | ||
) | ||
|
||
func main() { unitchecker.Main(example.Analyzer) } | ||
-- example/example.go -- | ||
package example | ||
|
||
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 = "example is ..." | ||
|
||
// Analyzer is ... | ||
var Analyzer = &analysis.Analyzer{ | ||
Name: "example", | ||
Doc: doc, | ||
Run: run, | ||
Requires: []*analysis.Analyzer{ | ||
inspect.Analyzer, | ||
}, | ||
} | ||
|
||
func run(pass *analysis.Pass) (any, 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 | ||
} | ||
-- example/example_test.go -- | ||
package example_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"example.com/example" | ||
"github.com/gostaticanalysis/testutil" | ||
"golang.org/x/tools/go/analysis/analysistest" | ||
) | ||
|
||
// TestAnalyzer is a test for Analyzer. | ||
func TestAnalyzer(t *testing.T) { | ||
modfile := testutil.ModFile(t, ".", nil) | ||
testdata := testutil.WithModules(t, analysistest.TestData(), modfile) | ||
analysistest.Run(t, testdata, example.Analyzer, "a") | ||
} | ||
-- example/go.mod -- | ||
module example.com/example | ||
|
||
go 1.23.3 | ||
|
||
-- example/testdata/src/a/a.go -- | ||
package a | ||
|
||
func f() { | ||
// The pattern can be written in regular expression. | ||
var gopher int // want "pattern" | ||
print(gopher) // want "identifier is gopher" | ||
} | ||
-- example/testdata/src/a/go.mod -- | ||
module a | ||
|
||
go 1.23.3 | ||
|
12 changes: 12 additions & 0 deletions
12
v2/skeleton/testdata/kind-ssa-copy-parent-gomod-to-testdata-go-test.golden
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
a.f | ||
Block 0 | ||
*ssa.Call print(0:int) | ||
*ssa.Builtin builtin print | ||
*ssa.Const 0:int | ||
*ssa.Return return | ||
--- FAIL: TestAnalyzer (0000s) | ||
analysistest.go:614: a/a.go:6: no diagnostic was reported matching `pattern` | ||
analysistest.go:614: a/a.go:7: no diagnostic was reported matching `identifier is gopher` | ||
FAIL | ||
exit status 1 | ||
FAIL example.com/example 0000s |
84 changes: 84 additions & 0 deletions
84
v2/skeleton/testdata/kind-ssa-copy-parent-gomod-to-testdata.golden
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
-- example/cmd/example/main.go -- | ||
package main | ||
|
||
import ( | ||
"example.com/example" | ||
"golang.org/x/tools/go/analysis/unitchecker" | ||
) | ||
|
||
func main() { unitchecker.Main(example.Analyzer) } | ||
-- example/example.go -- | ||
package example | ||
|
||
import ( | ||
"fmt" | ||
|
||
"golang.org/x/tools/go/analysis" | ||
"golang.org/x/tools/go/analysis/passes/buildssa" | ||
) | ||
|
||
const doc = "example is ..." | ||
|
||
// Analyzer is ... | ||
var Analyzer = &analysis.Analyzer{ | ||
Name: "example", | ||
Doc: doc, | ||
Run: run, | ||
Requires: []*analysis.Analyzer{ | ||
buildssa.Analyzer, | ||
}, | ||
} | ||
|
||
func run(pass *analysis.Pass) (any, error) { | ||
s := pass.ResultOf[buildssa.Analyzer].(*buildssa.SSA) | ||
for _, f := range s.SrcFuncs { | ||
fmt.Println(f) | ||
for _, b := range f.Blocks { | ||
fmt.Printf("\tBlock %d\n", b.Index) | ||
for _, instr := range b.Instrs { | ||
fmt.Printf("\t\t%[1]T\t%[1]v\n", instr) | ||
for _, v := range instr.Operands(nil) { | ||
if v != nil { | ||
fmt.Printf("\t\t\t%[1]T\t%[1]v\n", *v) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
return nil, nil | ||
} | ||
-- example/example_test.go -- | ||
package example_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"example.com/example" | ||
"github.com/gostaticanalysis/testutil" | ||
"golang.org/x/tools/go/analysis/analysistest" | ||
) | ||
|
||
// TestAnalyzer is a test for Analyzer. | ||
func TestAnalyzer(t *testing.T) { | ||
modfile := testutil.ModFile(t, ".", nil) | ||
testdata := testutil.WithModules(t, analysistest.TestData(), modfile) | ||
analysistest.Run(t, testdata, example.Analyzer, "a") | ||
} | ||
-- example/go.mod -- | ||
module example.com/example | ||
|
||
go 1.23.3 | ||
|
||
-- example/testdata/src/a/a.go -- | ||
package a | ||
|
||
func f() { | ||
// The pattern can be written in regular expression. | ||
var gopher int // want "pattern" | ||
print(gopher) // want "identifier is gopher" | ||
} | ||
-- example/testdata/src/a/go.mod -- | ||
module a | ||
|
||
go 1.23.3 | ||
|