-
Notifications
You must be signed in to change notification settings - Fork 1.7k
QL4QL: Add query suggesting use of inline test expectations #18767
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
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,67 @@ | ||
/** | ||
* Provides classes for working with YAML data. | ||
* | ||
* YAML documents are represented as abstract syntax trees whose nodes | ||
* are either YAML values or alias nodes referring to another YAML value. | ||
*/ | ||
|
||
private import codeql.yaml.Yaml as LibYaml | ||
|
||
private module YamlSig implements LibYaml::InputSig { | ||
import codeql.Locations | ||
|
||
class LocatableBase extends @yaml_locatable { | ||
Location getLocation() { yaml_locations(this, result) } | ||
|
||
string toString() { none() } | ||
} | ||
|
||
class NodeBase extends LocatableBase, @yaml_node { | ||
NodeBase getChildNode(int i) { yaml(result, _, this, i, _, _) } | ||
|
||
string getTag() { yaml(this, _, _, _, result, _) } | ||
|
||
string getAnchor() { yaml_anchors(this, result) } | ||
|
||
override string toString() { yaml(this, _, _, _, _, result) } | ||
} | ||
|
||
class ScalarNodeBase extends NodeBase, @yaml_scalar_node { | ||
int getStyle() { yaml_scalars(this, result, _) } | ||
|
||
string getValue() { yaml_scalars(this, _, result) } | ||
} | ||
|
||
class CollectionNodeBase extends NodeBase, @yaml_collection_node { } | ||
|
||
class MappingNodeBase extends CollectionNodeBase, @yaml_mapping_node { } | ||
|
||
class SequenceNodeBase extends CollectionNodeBase, @yaml_sequence_node { } | ||
|
||
class AliasNodeBase extends NodeBase, @yaml_alias_node { | ||
string getTarget() { yaml_aliases(this, result) } | ||
} | ||
|
||
class ParseErrorBase extends LocatableBase, @yaml_error { | ||
string getMessage() { yaml_errors(this, result) } | ||
} | ||
} | ||
|
||
import LibYaml::Make<YamlSig> | ||
|
||
/** A `.qlref` YAML document. */ | ||
class QlRefDocument extends YamlDocument { | ||
QlRefDocument() { this.getFile().getExtension() = "qlref" } | ||
|
||
/** Holds if this `.qlref` file uses inline test expectations. */ | ||
predicate usesInlineExpectations() { | ||
exists(YamlMapping n, YamlScalar value | | ||
n.getDocument() = this and | ||
value.getValue().matches("%InlineExpectations%") | ||
| | ||
value = n.lookup("postprocess") | ||
or | ||
value = n.lookup("postprocess").(YamlSequence).getElement(_) | ||
) | ||
} | ||
} |
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,15 @@ | ||
/** | ||
* @name Query test without inline test expectations | ||
* @description Using inline test expectations is a best practice for writing query tests. | ||
* @kind problem | ||
* @problem.severity warning | ||
* @id ql/qlref-inline-expectations | ||
* @precision high | ||
*/ | ||
|
||
import ql | ||
import codeql_ql.ast.Yaml | ||
|
||
from QlRefDocument f | ||
where not f.usesInlineExpectations() | ||
select f, "Query test does not use inline test expectations." |
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,21 @@ | ||
/** | ||
* @kind test-postprocess | ||
*/ | ||
|
||
private import ql | ||
private import codeql.util.test.InlineExpectationsTest as T | ||
private import internal.InlineExpectationsTestImpl | ||
import T::TestPostProcessing | ||
import T::TestPostProcessing::Make<Impl, Input> | ||
|
||
private module Input implements T::TestPostProcessing::InputSig<Impl> { | ||
string getRelativeUrl(Location location) { | ||
exists(File f, int startline, int startcolumn, int endline, int endcolumn | | ||
location.hasLocationInfo(_, startline, startcolumn, endline, endcolumn) and | ||
f = location.getFile() | ||
| | ||
result = | ||
f.getRelativePath() + ":" + startline + ":" + startcolumn + ":" + endline + ":" + endcolumn | ||
) | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
ql/ql/src/utils/test/internal/InlineExpectationsTestImpl.qll
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,28 @@ | ||
private import ql as QL | ||
private import codeql.util.test.InlineExpectationsTest | ||
|
||
module Impl implements InlineExpectationsTestSig { | ||
private import codeql_ql.ast.internal.TreeSitter as TS | ||
|
||
private newtype TExpectationComment = MkExpectationComment(TS::QL::LineComment comment) | ||
|
||
/** | ||
* Represents a line comment. | ||
*/ | ||
class ExpectationComment extends TExpectationComment { | ||
TS::QL::LineComment comment; | ||
|
||
ExpectationComment() { this = MkExpectationComment(comment) } | ||
|
||
/** Returns the contents of the given comment, _without_ the preceding comment marker (`//`). */ | ||
string getContents() { result = comment.getValue().suffix(2) } | ||
|
||
/** Gets a textual representation of this element. */ | ||
string toString() { result = comment.toString() } | ||
|
||
/** Gets the location of this comment. */ | ||
Location getLocation() { result = comment.getLocation() } | ||
} | ||
|
||
class Location = QL::Location; | ||
} |
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 @@ | ||
import ql |
2 changes: 2 additions & 0 deletions
2
ql/ql/test/queries/style/QlRefInlineExpectations/QlRefInlineExpectations.expected
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,2 @@ | ||
| QlRefInlineExpectations.qlref:1:1:1:40 | queries ... ions.ql | Query test does not use inline test expectations. | | ||
| Test3.qlref:1:1:1:39 | query: ... ists.ql | Query test does not use inline test expectations. | |
1 change: 1 addition & 0 deletions
1
ql/ql/test/queries/style/QlRefInlineExpectations/QlRefInlineExpectations.qlref
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 @@ | ||
queries/style/QlRefInlineExpectations.ql | ||
Check warningCode scanning / CodeQL Query test without inline test expectations Warning test
Query test does not use inline test expectations.
|
Empty file.
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,2 @@ | ||
query: queries/style/OmittableExists.ql | ||
postprocess: utils/test/InlineExpectationsTestQuery.ql |
Empty file.
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 @@ | ||
query: queries/style/OmittableExists.ql | ||
postprocess: | ||
- utils/test/InlineExpectationsTestQuery.ql |
Empty file.
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 @@ | ||
query: queries/style/OmittableExists.ql | ||
Check warningCode scanning / CodeQL Query test without inline test expectations Warning test
Query test does not use inline test expectations.
|
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
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
Oops, something went wrong.
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.
Check warning
Code scanning / CodeQL
Class QLDoc style. Warning test