1
0
mirror of synced 2025-12-19 18:10:59 -05:00

Add new command to run after creating a test pack (#36686)

This commit is contained in:
Andrew Eisenberg
2023-05-02 09:58:05 -07:00
committed by GitHub
parent d01fcc574c
commit 3e6fe4cd41

View File

@@ -50,6 +50,14 @@ Each `test` directory is configured as a test {% data variables.product.prodname
- `query-tests` a series of subdirectories with tests for queries stored in the `src` directory. Each subdirectory contains test code and a QL reference file that specifies the query to test.
- `library-tests` a series of subdirectories with tests for QL library files. Each subdirectory contains test code and queries that were written as unit tests for a library.
After creating the `qlpack.yml` file, you need to make sure that all of the dependencies are downloaded and available to the CLI. Do this by running the following command in the same directory as the `qlpack.yml` file:
```
codeql pack install
```
This will generate a `codeql-pack.lock.yml` file that specifies all of the transitive dependencies required to run queries in this pack. This file should be checked in to source control.
## Setting up the test files for a query
For each query you want to test, you should create a sub-directory in the test {% data variables.product.prodname_codeql %} pack.
@@ -59,7 +67,7 @@ Then add the following files to the subdirectory before you run the test command
You do not need to add a query reference file if the query you want to test is stored in the test directory, but it is generally good practice to store queries separately from tests. The only exception is unit tests for QL libraries, which tend to be stored in test packs, separate from queries that generate alerts or paths.
- The example code you want to run your query against. This should consist of one or more files containing examples of the code the query is designed to identify.
- The example code you want to run your query against. This should consist of one or more files containing examples of the code the query is designed to identify.
You can also define the results you expect to see when you run the query against
the example code, by creating a file with the extension `.expected`. Alternatively, you can leave the test command to create the `.expected` file for you.
@@ -95,8 +103,8 @@ The `<test|dir>` argument can be one or more of the following:
You can also specify:
- `--threads:` optionally, the number of threads to use when running queries. The default option is `1`. You can specify more threads to speed up query execution. Specifying `0` matches the number of threads to the number of logical processors.
For full details of all the options you can use when testing queries, see "[AUTOTITLE](/code-security/codeql-cli/codeql-cli-manual/test-run/)."
For full details of all the options you can use when testing queries, see "[AUTOTITLE](/code-security/codeql-cli/codeql-cli-manual/test-run/)."
## Example
@@ -114,7 +122,7 @@ blocks in Java code:
```
import java
from IfStmt ifstmt
where ifstmt.getThen() instanceof EmptyStmt
select ifstmt, "This if statement has an empty then."
@@ -137,29 +145,31 @@ other custom queries. For example, `custom-queries/java/queries/EmptyThen.ql`.
{% data reusables.codeql-cli.test-qlpack %}
5. Within the Java test pack, create a directory to contain the test files
5. Run `codeql pack install` in the root of the test directory. This generates a `codeql-pack.lock.yml` file that specifies all of the transitive dependencies required to run queries in this pack.
6. Within the Java test pack, create a directory to contain the test files
associated with `EmptyThen.ql`. For example, `custom-queries/java/tests/EmptyThen`.
6. In the new directory, create `EmptyThen.qlref` to define the location of `EmptyThen.ql`. The path to the query must be specified relative to the root of
7. In the new directory, create `EmptyThen.qlref` to define the location of `EmptyThen.ql`. The path to the query must be specified relative to the root of
the {% data variables.product.prodname_codeql %} pack that contains the query. In this case, the query is in the
top level directory of the {% data variables.product.prodname_codeql %} pack named `my-custom-queries`,
which is declared as a dependency for `my-query-tests`. Therefore, `EmptyThen.qlref` should simply contain `EmptyThen.ql`.
7. Create a code snippet to test. The following Java code contains an empty `if` statement on the third line. Save it in `custom-queries/java/tests/EmptyThen/Test.java`.
8. Create a code snippet to test. The following Java code contains an empty `if` statement on the third line. Save it in `custom-queries/java/tests/EmptyThen/Test.java`.
```java
class Test {
public void problem(String arg) {
if (arg.isEmpty())
;
;
{
System.out.println("Empty argument");
System.out.println("Empty argument");
}
}
public void good(String arg) {
if (arg.isEmpty()) {
System.out.println("Empty argument");
System.out.println("Empty argument");
}
}
}