Skip to content

Commit bcbbb42

Browse files
committed
Exclude methods in tests in framework mode
This excludes methods defined in tests in framework mode, significantly cutting down on the number of methods shown that would need to be modeled. For C#, this just checks that the file is not a test file, as defined by the QL library. For Java, this makes a copy of the internal [`ModelExclusions.qll`](https://github.com/github/codeql/blob/249f9f863db1e94e3c46ca85b49fb0ec32f8ca92/java/ql/lib/semmle/code/java/dataflow/internal/ModelExclusions.qll) file to avoid the use of internal modules. This module will tell us whether a method is "interesting" to model or not. Not all of the checks in this module need to happen for framework mode, but these checks might be useful for telling a user whether a method is interesting to model in application mode.
1 parent 6465786 commit bcbbb42

File tree

2 files changed

+81
-6
lines changed

2 files changed

+81
-6
lines changed

extensions/ql-vscode/src/data-extensions-editor/queries/csharp.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,11 @@ select usage, apiName, supported.toString(), "supported", api.getFile().getBaseN
3939
4040
private import csharp
4141
private import dotnet
42+
private import semmle.code.csharp.frameworks.Test
4243
private import AutomodelVsCode
4344
4445
class PublicMethod extends CallableMethod {
45-
PublicMethod() { this.fromSource() }
46+
PublicMethod() { this.fromSource() and not this.getFile() instanceof TestFile }
4647
}
4748
4849
from PublicMethod publicMethod, string apiName, boolean supported

extensions/ql-vscode/src/data-extensions-editor/queries/java.ts

Lines changed: 79 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,7 @@ select usage, apiName, supported.toString(), "supported", externalApi.jarContain
3939
import java
4040
import AutomodelVsCode
4141
42-
class PublicMethodFromSource extends CallableMethod {
43-
PublicMethodFromSource() { this.isPublic() and this.fromSource() }
44-
}
42+
class PublicMethodFromSource extends CallableMethod, ModelApi { }
4543
4644
from PublicMethodFromSource publicMethod, string apiName, boolean supported
4745
where
@@ -81,8 +79,8 @@ class CallableMethod extends Method {
8179
*/
8280
string getApiName() {
8381
result =
84-
this.getDeclaringType().getPackage() + "." + this.getDeclaringType().nestedName() +
85-
"#" + this.getName() + paramsString(this)
82+
this.getDeclaringType().getPackage() + "." + this.getDeclaringType().nestedName() + "#" +
83+
this.getName() + paramsString(this)
8684
}
8785
8886
private string getJarName() {
@@ -148,6 +146,82 @@ boolean isSupported(CallableMethod method) {
148146
or
149147
not method.isSupported() and result = false
150148
}
149+
150+
// The below is a copy of https://github.com/github/codeql/blob/249f9f863db1e94e3c46ca85b49fb0ec32f8ca92/java/ql/lib/semmle/code/java/dataflow/internal/ModelExclusions.qll
151+
// to avoid the use of internal modules.
152+
/** Holds if the given package \`p\` is a test package. */
153+
pragma[nomagic]
154+
private predicate isTestPackage(Package p) {
155+
p.getName()
156+
.matches([
157+
"org.junit%", "junit.%", "org.mockito%", "org.assertj%",
158+
"com.github.tomakehurst.wiremock%", "org.hamcrest%", "org.springframework.test.%",
159+
"org.springframework.mock.%", "org.springframework.boot.test.%", "reactor.test%",
160+
"org.xmlunit%", "org.testcontainers.%", "org.opentest4j%", "org.mockserver%",
161+
"org.powermock%", "org.skyscreamer.jsonassert%", "org.rnorth.visibleassertions",
162+
"org.openqa.selenium%", "com.gargoylesoftware.htmlunit%", "org.jboss.arquillian.testng%",
163+
"org.testng%"
164+
])
165+
}
166+
167+
/**
168+
* A test library.
169+
*/
170+
class TestLibrary extends RefType {
171+
TestLibrary() { isTestPackage(this.getPackage()) }
172+
}
173+
174+
/** Holds if the given file is a test file. */
175+
private predicate isInTestFile(File file) {
176+
file.getAbsolutePath().matches(["%/test/%", "%/guava-tests/%", "%/guava-testlib/%"]) and
177+
not file.getAbsolutePath().matches("%/ql/test/%") // allows our test cases to work
178+
}
179+
180+
/** Holds if the given compilation unit's package is a JDK internal. */
181+
private predicate isJdkInternal(CompilationUnit cu) {
182+
cu.getPackage().getName().matches("org.graalvm%") or
183+
cu.getPackage().getName().matches("com.sun%") or
184+
cu.getPackage().getName().matches("sun%") or
185+
cu.getPackage().getName().matches("jdk%") or
186+
cu.getPackage().getName().matches("java2d%") or
187+
cu.getPackage().getName().matches("build.tools%") or
188+
cu.getPackage().getName().matches("propertiesparser%") or
189+
cu.getPackage().getName().matches("org.jcp%") or
190+
cu.getPackage().getName().matches("org.w3c%") or
191+
cu.getPackage().getName().matches("org.ietf.jgss%") or
192+
cu.getPackage().getName().matches("org.xml.sax%") or
193+
cu.getPackage().getName().matches("com.oracle%") or
194+
cu.getPackage().getName().matches("org.omg%") or
195+
cu.getPackage().getName().matches("org.relaxng%") or
196+
cu.getPackage().getName() = "compileproperties" or
197+
cu.getPackage().getName() = "transparentruler" or
198+
cu.getPackage().getName() = "genstubs" or
199+
cu.getPackage().getName() = "netscape.javascript" or
200+
cu.getPackage().getName() = ""
201+
}
202+
203+
/** Holds if the given callable is not worth modeling. */
204+
predicate isUninterestingForModels(Callable c) {
205+
isInTestFile(c.getCompilationUnit().getFile()) or
206+
isJdkInternal(c.getCompilationUnit()) or
207+
c instanceof MainMethod or
208+
c instanceof StaticInitializer or
209+
exists(FunctionalExpr funcExpr | c = funcExpr.asMethod()) or
210+
c.getDeclaringType() instanceof TestLibrary or
211+
c.(Constructor).isParameterless()
212+
}
213+
214+
/**
215+
* A class that represents all callables for which we might be
216+
* interested in having a MaD model.
217+
*/
218+
class ModelApi extends SrcCallable {
219+
ModelApi() {
220+
this.fromSource() and
221+
this.isEffectivelyPublic() and
222+
not isUninterestingForModels(this)
223+
}
224+
}
151225
`,
152226
},
153227
};

0 commit comments

Comments
 (0)