Skip to content

Commit 817fd8c

Browse files
committed
Ruby: Move TestFile to modeling Util module
The TestFile class in the ModelEditor module is more accurate than the existing RelevantFile class in the Util module, so this moves the TestFile class to Util and redefines RelevantFile in terms of the TestFile.
1 parent b51379b commit 817fd8c

File tree

2 files changed

+15
-17
lines changed

2 files changed

+15
-17
lines changed

ruby/ql/src/queries/modeling/internal/Util.qll

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,23 @@
55
private import ruby
66
private import codeql.ruby.ApiGraphs
77

8+
/**
9+
* A file that probably contains tests.
10+
*/
11+
class TestFile extends File {
12+
TestFile() {
13+
this.getRelativePath().regexpMatch(".*(test|spec|examples).+") and
14+
not this.getAbsolutePath().matches("%/ql/test/%") // allows our test cases to work
15+
}
16+
}
17+
818
/**
919
* A file that is relevant in the context of library modeling.
1020
*
1121
* In practice, this means a file that is not part of test code.
1222
*/
1323
class RelevantFile extends File {
14-
RelevantFile() { not this.getRelativePath().regexpMatch(".*/?test(case)?s?/.*") }
24+
RelevantFile() { not this instanceof TestFile }
1525
}
1626

1727
/**

ruby/ql/src/utils/modeleditor/ModelEditor.qll

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,6 @@ private import codeql.ruby.frameworks.data.ModelsAsData
99
private import codeql.ruby.frameworks.data.internal.ApiGraphModelsExtensions
1010
private import queries.modeling.internal.Util as Util
1111

12-
/** Holds if the given callable is not worth supporting. */
13-
private predicate isUninteresting(DataFlow::MethodNode c) {
14-
c.getLocation().getFile() instanceof TestFile
15-
}
16-
1712
private predicate gemFileStep(Gem::GemSpec gem, Folder folder, int n) {
1813
n = 0 and folder.getAFile() = gem.(File)
1914
or
@@ -63,7 +58,7 @@ abstract class Endpoint instanceof DataFlow::Node {
6358
class MethodEndpoint extends Endpoint instanceof DataFlow::MethodNode {
6459
MethodEndpoint() {
6560
this.isPublic() and
66-
not isUninteresting(this)
61+
this.(DataFlow::MethodNode).getLocation().getFile() instanceof Util::RelevantFile
6762
}
6863

6964
DataFlow::MethodNode getNode() { result = this }
@@ -144,19 +139,12 @@ class MethodEndpoint extends Endpoint instanceof DataFlow::MethodNode {
144139
}
145140

146141
string methodClassification(Call method) {
147-
method.getFile() instanceof TestFile and result = "test"
142+
method.getFile() instanceof Util::TestFile and result = "test"
148143
or
149-
not method.getFile() instanceof TestFile and
144+
not method.getFile() instanceof Util::TestFile and
150145
result = "source"
151146
}
152147

153-
class TestFile extends File {
154-
TestFile() {
155-
this.getRelativePath().regexpMatch(".*(test|spec|examples).+") and
156-
not this.getAbsolutePath().matches("%/ql/test/%") // allows our test cases to work
157-
}
158-
}
159-
160148
/**
161149
* A callable where there exists a MaD sink model that applies to it.
162150
*/
@@ -222,7 +210,7 @@ class ModuleEndpoint extends Endpoint {
222210
n order by loc.getFile().getAbsolutePath(), loc.getStartLine(), loc.getStartColumn()
223211
) and
224212
not moduleNode.(Module).isBuiltin() and
225-
not moduleNode.getLocation().getFile() instanceof TestFile
213+
moduleNode.getLocation().getFile() instanceof Util::RelevantFile
226214
}
227215

228216
DataFlow::ModuleNode getNode() { result = moduleNode }

0 commit comments

Comments
 (0)