-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Metalint for checking against the deprecaetd lint.RegisterLint functi…
…on (#775) * Metalint for checking against the deprecaetd lint.RegisterLint function * go imports
- Loading branch information
1 parent
ebf2071
commit 7f6ef92
Showing
7 changed files
with
178 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package lints | ||
|
||
/* | ||
* ZLint Copyright 2023 Regents of the University of Michigan | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
* use this file except in compliance with the License. You may obtain a copy | ||
* of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or | ||
* implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
import ( | ||
"go/ast" | ||
|
||
"github.com/zmap/zlint/v3/integration/lints/filters" | ||
"github.com/zmap/zlint/v3/integration/lints/lint" | ||
) | ||
|
||
type RegisterLintDeprecated struct{} | ||
|
||
func (r *RegisterLintDeprecated) CheckApplies(tree *ast.File, file *lint.File) bool { | ||
return filters.IsALint(file) | ||
} | ||
|
||
func (r *RegisterLintDeprecated) Lint(tree *ast.File, file *lint.File) *lint.Result { | ||
var result *lint.Result | ||
visitor := &selectorExprVisitor{fn: func(expr *ast.SelectorExpr, node ast.Node) { | ||
if expr.Sel.Name != "RegisterLint" { | ||
return | ||
} | ||
result = lint.NewResult("lint.RegisterLint is deprecated and should not be used. "+ | ||
"Please use the register function specific to your lint classification (I.E. "+ | ||
"lint.RegisterCertificateLint for certificate lints and lint.RegisterRevocationListLint for CRL lints)."). | ||
AddCodeCitation(node.Pos(), node.End(), file). | ||
SetCitations("https://github.com/zmap/zlint/issues/765") | ||
}} | ||
ast.Walk(visitor, tree) | ||
return result | ||
} | ||
|
||
type selectorExprVisitor struct { | ||
fn func(expr *ast.SelectorExpr, node ast.Node) | ||
} | ||
|
||
func (v *selectorExprVisitor) Visit(node ast.Node) ast.Visitor { | ||
selectorExpr, ok := node.(*ast.SelectorExpr) | ||
if !ok { | ||
return v | ||
} | ||
v.fn(selectorExpr, node) | ||
return nil | ||
} |
36 changes: 36 additions & 0 deletions
36
v3/integration/lints/lints/register_lint_deprecated_test.go
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,36 @@ | ||
package lints | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/zmap/zlint/v3/integration/lints/lint" | ||
) | ||
|
||
func TestRegisterLintDeprecated_Lint(t *testing.T) { | ||
|
||
data := []struct { | ||
inputFile string | ||
expectPass bool | ||
}{ | ||
{inputFile: "testdata/lint_usesRegisterLint.go", expectPass: false}, | ||
{inputFile: "testdata/lint_usesRegisterCertificateLint.go", expectPass: true}, | ||
{inputFile: "testdata/lint_usesRegisterProfile.go", expectPass: true}, | ||
{inputFile: "testdata/lint_usesRegisterRevocationListLint.go", expectPass: true}, | ||
} | ||
l := &RegisterLintDeprecated{} | ||
for _, test := range data { | ||
file := test.inputFile | ||
want := test.expectPass | ||
t.Run(file, func(t *testing.T) { | ||
r, err := lint.RunLintForFile(file, l) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
if want && r != nil { | ||
t.Errorf("got unexepcted error result, %s", r) | ||
} else if !want && r == nil { | ||
t.Errorf("expected failure but got nothing") | ||
} | ||
}) | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
v3/integration/lints/lints/testdata/lint_usesRegisterCertificateLint.go
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,21 @@ | ||
package testdata | ||
|
||
import "github.com/zmap/zlint/v3/lint" | ||
|
||
/* | ||
* ZLint Copyright 2023 Regents of the University of Michigan | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
* use this file except in compliance with the License. You may obtain a copy | ||
* of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or | ||
* implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
func init() { | ||
lint.RegisterCertificateLint(nil) | ||
} |
21 changes: 21 additions & 0 deletions
21
v3/integration/lints/lints/testdata/lint_usesRegisterLint.go
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,21 @@ | ||
package testdata | ||
|
||
import "github.com/zmap/zlint/v3/lint" | ||
|
||
/* | ||
* ZLint Copyright 2023 Regents of the University of Michigan | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
* use this file except in compliance with the License. You may obtain a copy | ||
* of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or | ||
* implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
func init() { | ||
lint.RegisterLint(nil) | ||
} |
21 changes: 21 additions & 0 deletions
21
v3/integration/lints/lints/testdata/lint_usesRegisterProfile.go
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,21 @@ | ||
package testdata | ||
|
||
import "github.com/zmap/zlint/v3/lint" | ||
|
||
/* | ||
* ZLint Copyright 2023 Regents of the University of Michigan | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
* use this file except in compliance with the License. You may obtain a copy | ||
* of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or | ||
* implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
func init() { | ||
lint.RegisterProfile(lint.Profile{}) | ||
} |
21 changes: 21 additions & 0 deletions
21
v3/integration/lints/lints/testdata/lint_usesRegisterRevocationListLint.go
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,21 @@ | ||
package testdata | ||
|
||
import "github.com/zmap/zlint/v3/lint" | ||
|
||
/* | ||
* ZLint Copyright 2023 Regents of the University of Michigan | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
* use this file except in compliance with the License. You may obtain a copy | ||
* of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or | ||
* implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
func init() { | ||
lint.RegisterRevocationListLint(nil) | ||
} |
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