-
Notifications
You must be signed in to change notification settings - Fork 74
create a sqlcmd style linter #271
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
c8636b4
create a sqlcmd style linter
shueybubbles 5c2c332
print errors in red
shueybubbles 5cb1e3a
fix asserts in some packages
shueybubbles ca32f86
Add imports linter
shueybubbles 1e958ea
fix linter on linux
shueybubbles 5e777e1
Merge branch 'main' into shueybubbles/267
shueybubbles 672b7a4
Merge branch 'main' into shueybubbles/267
shueybubbles cfea8e0
Merge branch 'main' of https://github.com/microsoft/go-sqlcmd into sh…
shueybubbles 031f1d3
add file header
shueybubbles 4568589
Merge branch 'shueybubbles/267' of https://github.com/microsoft/go-sq…
shueybubbles File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,9 @@ | ||
# top-most EditorConfig file | ||
root = true | ||
|
||
[*.go] | ||
indent_style = tab | ||
# (Please don't specify an indent_size here; that has too many unintended consequences.) | ||
|
||
# IDE0073: File header | ||
file_header_template = Copyright (c) Microsoft Corporation.\nLicensed under the MIT license. |
This file contains hidden or 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 hidden or 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 hidden or 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,10 @@ | ||
package main | ||
|
||
import ( | ||
sqlcmdlinter "github.com/microsoft/go-sqlcmd/pkg/sqlcmd-linter" | ||
"golang.org/x/tools/go/analysis/multichecker" | ||
) | ||
|
||
func main() { | ||
multichecker.Main(sqlcmdlinter.AssertAnalyzer, sqlcmdlinter.ImportsAnalyzer) | ||
} |
This file contains hidden or 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 hidden or 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 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
|
||
package sqlcmdlinter | ||
|
||
import ( | ||
"go/ast" | ||
"go/token" | ||
"strings" | ||
|
||
"golang.org/x/tools/go/analysis" | ||
"golang.org/x/tools/go/analysis/passes/inspect" | ||
"golang.org/x/tools/go/ast/inspector" | ||
) | ||
|
||
var ImportsAnalyzer = &analysis.Analyzer{ | ||
Name: "importslint", | ||
Doc: "Require most external packages be imported only by internal packages", | ||
Requires: []*analysis.Analyzer{inspect.Analyzer}, | ||
Run: runImports, | ||
} | ||
|
||
var AllowedImports = map[string][]string{ | ||
`"github.com/alecthomas/kong`: {`cmd/sqlcmd`, `pkg/sqlcmd`}, | ||
`"github.com/golang-sql/sqlexp`: {`pkg/sqlcmd`}, | ||
`"github.com/google/uuid`: {}, | ||
`"github.com/peterh/liner`: {`pkg/console`}, | ||
`"github.com/microsoft/go-mssqldb`: {}, | ||
`"github.com/microsoft/go-sqlcmd`: {}, | ||
`"github.com/spf13/cobra`: {`cmd/sqlcmd`, `cmd/modern`}, | ||
`"github.com/spf13/viper`: {`cmd/sqlcmd`, `cmd/modern`}, | ||
`"github.com/stretchr/testify`: {}, | ||
} | ||
|
||
func runImports(pass *analysis.Pass) (interface{}, error) { | ||
inspectorInstance := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) | ||
nodeFilter := []ast.Node{(*ast.File)(nil)} | ||
inspectorInstance.Preorder(nodeFilter, func(n ast.Node) { | ||
|
||
f := n.(*ast.File) | ||
fileName := pass.Fset.Position(f.Package).Filename | ||
isInternal := strings.Contains(fileName, `internal\`) || strings.Contains(fileName, `internal/`) | ||
for _, s := range f.Imports { | ||
if s.Path.Kind == token.STRING { | ||
pkg := s.Path.Value | ||
if isInternal { | ||
if !isValidInternalImport(pkg) { | ||
pass.Reportf(s.Pos(), "Internal packages should not import %s", pkg) | ||
} | ||
} else if !isValidExternalImport(pkg, fileName) { | ||
pass.Reportf(s.Pos(), "Non-internal packages should not import %s", pkg) | ||
} | ||
} | ||
} | ||
}) | ||
|
||
return nil, nil | ||
} | ||
|
||
func isValidInternalImport(pkg string) bool { | ||
return !strings.HasPrefix(pkg, `"github.com/microsoft/go-sqlcmd/pkg`) && !strings.HasPrefix(pkg, `"github.com/microsoft/go-sqlcmd/cmd`) | ||
} | ||
|
||
func isValidExternalImport(pkg string, filename string) bool { | ||
if strings.HasPrefix(pkg, `"github.com`) { | ||
for key, paths := range AllowedImports { | ||
if strings.HasPrefix(pkg, key) { | ||
if len(paths) == 0 { | ||
// any package can import it | ||
return true | ||
} | ||
for _, p := range paths { | ||
// canonicalize path to Linux separator for comparison | ||
path := strings.ReplaceAll(filename, `\`, `/`) | ||
if strings.Contains(path, p) { | ||
return true | ||
} | ||
} | ||
} | ||
} | ||
return false | ||
} | ||
return true | ||
} |
This file contains hidden or 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,20 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
|
||
package sqlcmdlinter | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"golang.org/x/tools/go/analysis/analysistest" | ||
) | ||
|
||
func TestImports(t *testing.T) { | ||
wd, err := os.Getwd() | ||
if err != nil { | ||
t.Errorf("Failed to get wd: %s", err) | ||
} | ||
analysistest.Run(t, filepath.Join(wd, `testdata`), ImportsAnalyzer, "imports_linter_tests/...") | ||
} |
1 change: 1 addition & 0 deletions
1
pkg/sqlcmd-linter/testdata/src/github.com/README.md
This file contains hidden or 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 @@ | ||
This directory structure exists to provide stub package implementations for the linter tests. The `analysistest` package replaces `$GOPATH` with the local file system path. We create these stubs so the linter test files can closely mimic our production code. |
3 changes: 3 additions & 0 deletions
3
pkg/sqlcmd-linter/testdata/src/github.com/alecthomas/chroma/chroma.go
This file contains hidden or 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,3 @@ | ||
package chroma | ||
|
||
var C = 1 |
3 changes: 3 additions & 0 deletions
3
pkg/sqlcmd-linter/testdata/src/github.com/microsoft/go-sqlcmd/pkg/sqlcmd/sqlcmd.go
This file contains hidden or 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,3 @@ | ||
package sqlcmd | ||
|
||
var S = 1 |
11 changes: 11 additions & 0 deletions
11
pkg/sqlcmd-linter/testdata/src/github.com/stretchr/testify/assert/main.go
This file contains hidden or 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,11 @@ | ||
package assert | ||
|
||
import "testing" | ||
|
||
func NotNil(t *testing.T, object interface{}, msgAndArgs ...interface{}) bool { | ||
return false | ||
} | ||
|
||
func NoError(t *testing.T, err error, msgAndArgs ...interface{}) bool { | ||
return false | ||
} |
12 changes: 12 additions & 0 deletions
12
pkg/sqlcmd-linter/testdata/src/imports_linter_tests/cmd/main/main.go
This file contains hidden or 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 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
|
||
package main | ||
|
||
import ( | ||
_ "github.com/alecthomas/chroma" // want "Non-internal packages should not import \"github.com/alecthomas/chroma\"" | ||
_ "github.com/microsoft/go-sqlcmd/pkg/sqlcmd" | ||
_ "github.com/stretchr/testify/assert" | ||
) | ||
|
||
var X = 1 |
12 changes: 12 additions & 0 deletions
12
pkg/sqlcmd-linter/testdata/src/imports_linter_tests/internal/main.go
This file contains hidden or 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 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
|
||
package main | ||
|
||
import ( | ||
_ "fmt" | ||
|
||
_ "github.com/microsoft/go-sqlcmd/pkg/sqlcmd" // want "Internal packages should not import \"github.com/microsoft/go-sqlcmd/pkg/sqlcmd\"" | ||
) | ||
|
||
var X = 1 |
21 changes: 21 additions & 0 deletions
21
pkg/sqlcmd-linter/testdata/src/useassert_linter_tests/assert.go
This file contains hidden or 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,21 @@ | ||
package sqlcmd | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestDontUseFatal(t *testing.T) { | ||
t.Fatal("this should fail") // want "Use assert package methods instead of Fatal" | ||
t.Fatalf("this should %s", "fail") // want "Use assert package methods instead of Fatalf" | ||
t.Fail() // want "Use assert package methods instead of Fail" | ||
assert.NoError(t, fmt.Errorf("what")) | ||
t.FailNow() // want "Use assert package methods instead of FailNow" | ||
|
||
} | ||
|
||
func TestDontUseRecover(t *testing.T) { | ||
defer func() { assert.NotNil(t, recover(), "The code did not panic as expected") }() // want "Use assert.Panics instead of recover()" | ||
} |
This file contains hidden or 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,58 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT license. | ||
|
||
package sqlcmdlinter | ||
|
||
import ( | ||
"go/ast" | ||
|
||
"golang.org/x/tools/go/analysis" | ||
"golang.org/x/tools/go/analysis/passes/inspect" | ||
"golang.org/x/tools/go/ast/inspector" | ||
) | ||
|
||
var AssertAnalyzer = &analysis.Analyzer{ | ||
Name: "assertlint", | ||
Doc: "Require use of asserts instead of fmt.Error functions in tests", | ||
Requires: []*analysis.Analyzer{inspect.Analyzer}, | ||
Run: runAsserts, | ||
} | ||
|
||
var blockedTestingMethods = []string{"Error", "ErrorF", "Fail", "FailNow", "Fatal", "Fatalf"} | ||
|
||
func runAsserts(pass *analysis.Pass) (interface{}, error) { | ||
// pass.ResultOf[inspect.Analyzer] will be set if we've added inspect.Analyzer to Requires. | ||
// Analyze code and make an AST from the file: | ||
inspectorInstance := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector) | ||
|
||
nodeFilter := []ast.Node{(*ast.CallExpr)(nil)} | ||
|
||
inspectorInstance.Preorder(nodeFilter, func(n ast.Node) { | ||
node := n.(*ast.CallExpr) | ||
switch fun := node.Fun.(type) { | ||
case (*ast.SelectorExpr): | ||
switch funX := fun.X.(type) { | ||
case (*ast.Ident): | ||
if funX.Name == "t" && contains(blockedTestingMethods, fun.Sel.Name) { | ||
pass.Reportf(node.Pos(), "Use assert package methods instead of %s", fun.Sel.Name) | ||
} | ||
default: | ||
return | ||
} | ||
case (*ast.Ident): | ||
if fun.Name == "recover" { | ||
pass.Reportf(node.Pos(), "Use assert.Panics instead of recover()") | ||
} | ||
} | ||
}) | ||
return nil, nil | ||
} | ||
|
||
func contains(a []string, v string) bool { | ||
for _, val := range a { | ||
if val == v { | ||
return true | ||
} | ||
} | ||
return false | ||
} |
This file contains hidden or 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,17 @@ | ||
package sqlcmdlinter | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
"testing" | ||
|
||
"golang.org/x/tools/go/analysis/analysistest" | ||
) | ||
|
||
func TestUseAsserts(t *testing.T) { | ||
wd, err := os.Getwd() | ||
if err != nil { | ||
t.Errorf("Failed to get wd: %s", err) | ||
} | ||
analysistest.Run(t, filepath.Join(wd, `testdata`), AssertAnalyzer, "useassert_linter_tests/...") | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.