How can I ignore test file in codeql?

759 Views Asked by At

I want to ignore test files in codeql result. but this query includes test files.

import codeql.ruby.AST

from RegExpLiteral t,  File f
where not f.getBaseName().regexpMatch("spec")
select t

ignore test files in the result

1

There are 1 best solutions below

0
On

regexpMatch requires that the given pattern matches the entire receiver. In your case that means it would only succeed if the file name is exactly "spec". Maybe you rather want to test for ".*spec.*" (or use matches("%spec%")).

I am not sure though if that answers your question. As far as I know there is in general no direct way to ignore test sources. You could however do one of the following things:

  • Exclude the test directory when building the CodeQL database; for GitHub code scanning see the documentation
  • For GitHub code scanning filter out non-application code alerts in the repository alerts list (see documentation)
  • Manually add conditions to your query which exclude tests, for example a file name check as you have done or checking the code for certain test-related constructs