猛虎下山是什么生肖| 贡菜是什么做的| 一九八六年属什么生肖| 商字五行属什么| 手肿是什么原因| 尿里带血是什么原因| 什么食物含钾| 山楂可以和什么一起泡水喝| 吃什么止咳| 中药学是干什么的| 什么样的小鸟| 上将相当于什么级别| 叶酸片有什么功效| 附件炎吃什么药| 拉拉秧学名叫什么| 白细胞计数高是什么原因| 羊水偏多是什么原因| 吃太烫的东西有什么坏处| 6月16日是什么星座| 拉油便是什么原因| 口水臭吃什么药| 包茎是什么意思| 提手旁的字有什么| 清白是什么意思| 甘油三酯高应该注意什么| 什么原因导致性功能减退| 婴儿为什么喜欢趴着睡| 疖肿什么意思| 儿童口腔溃疡用什么药| 阴唇肿是什么原因| 一岁半宝宝反复发烧是什么原因| 甲钴胺不能和什么药一起服用| lf是什么意思| 宝宝拉肚子有粘液是什么原因| 送巧克力代表什么意思| 身体发凉是什么原因| by是什么意思| 青筋暴起是什么原因| 经常放响屁是什么原因| 位图是什么意思| 烤乳猪用的是什么猪| 1997年属牛的是什么命| 焦糖色裤子配什么颜色上衣| 炼蜜是什么| 山楂片泡水喝有什么好处| 山竹不能和什么水果一起吃| 特应性皮炎是什么意思| 婴儿吃手是什么原因| 奥美拉唑与雷贝拉唑有什么区别| 感冒头痛吃什么药| 肺不好吃什么| 女性胆囊炎有什么症状| 一个胸大一个胸小是什么原因| canon什么牌子| 眩晕呕吐是什么病| 吃什么补气血| 八方来财是什么意思| 餐巾纸属于什么垃圾| 呃逆吃什么药| 参苓白术散治什么病| lanvin是什么牌子| 女人为什么要少吃鳝鱼| 胡人是什么民族| 养成系是什么意思| 杀鸡给猴看什么意思| 尿维生素c阳性是什么意思| 114是什么意思| 哥弟属于什么档次| 腱鞘炎是什么原因| 吃什么有助于骨头恢复| 床头朝什么方向是正确的| 血脂高是什么原因引起的| 什么颜色属木| 圣诞节适合送什么礼物| 地盆是一种什么病| 圆脸适合什么短发| 宾馆和酒店有什么区别| 什么是钙化结节| 唐朝以后是什么朝代| 善存什么时间吃比较好| 什么是鸡奸| 神机妙算是什么生肖| 小儿流鼻涕吃什么药好| 脚腕筋疼是什么原因| 手指起水泡是什么原因| 动物奶油是什么做的| 特工是什么意思| 幻觉是什么意思| 耍小聪明是什么意思| 高岗为什么自杀| 春捂秋冻指的是什么意思| 没经验开什么店最简单| 内痔是什么样的图片| 脊髓病变是什么病| 美丽的邂逅是什么意思| 陈皮和橘子皮有什么区别| 路上行人匆匆过是什么歌| 颈椎问题挂什么科| 电风扇什么牌子好| 生活老师是做什么的| 血氧仪是干什么用的| 红肉是什么肉| 西加一横读什么| 青岛是鲁什么| 少帅是什么军衔| 碎银子是什么茶| ict是什么意思| 什么叫智齿牙| 不良反应是什么意思| 晚上睡不着觉什么原因| 浓鼻涕吃什么药| 功成名就是什么意思| 什么人容易得骨髓瘤| 蒙古族不吃什么肉| 经常抠鼻子有什么危害| 八仙茶属于什么茶| 宫颈糜烂用什么药好得快| 做肠镜要做什么准备| 畈是什么意思| movefree是什么药| a-l-岩藻糖苷酶偏高是什么原因| 去火吃什么| 甲状腺功能亢进症是什么病| 阳光明媚下一句接什么| 吃什么生发效果最好| 有什么汤菜谱大全| 细菌性阴道炎是什么原因引起的| 喝水多尿少是什么原因| 克隆恩病是什么| 什么叫有氧运动和无氧运动| 悲戚是什么意思| 1953年属什么生肖| 灰指甲不治疗有什么后果| 抗病毒什么药效果好| 透析病人磷高了吃什么降磷| 女人十个簸箕是什么命| 朗朗原名叫什么| 除湿气吃什么| 黄鼻涕是什么原因| 微创人流和无痛人流有什么区别| 领导喜欢什么样的员工| 五毒为什么没有蜘蛛| vintage是什么牌子| 优衣库属于什么档次| 胆囊炎用什么药| 乳腺癌ki67是什么意思| 饮什么止渴| 米面是什么| 米田共是什么意思| 1981年是什么年| 暧昧什么意思| 反式脂肪是什么意思| 酱油什么时候发明的| 上环是什么意思| 腋下黑是什么原因| 嘴巴干苦是什么原因| 低血糖是什么原因| 自行车什么牌子好| 运动后出汗多是什么原因| 早醒是什么原因造成的| 手上长小水泡是什么原因| 桑蚕丝被有什么好处| 拼音b像什么| npv是什么病毒| 瞳孔放大意味着什么| 卷柏属于什么植物| 人间仙境是什么意思| 立秋抓秋膘吃什么| 五指毛桃不能和什么一起吃| 子宫颈肥大有什么危害| 10月27日什么星座| 红肉指的是什么肉| 脂肪瘤去医院挂什么科| 月经准时来说明什么| 口干口渴是什么原因| 夏天补什么| 吃一个海参相当于吃了什么| pml是什么意思| 癫痫是什么| 十二生肖代表什么花| 女性下面长什么样| 5月17日是什么星座| 拉屎肛门疼是什么原因| 鸟字旁与什么有关| becky是什么意思| 慢性咽喉炎吃什么药好| 嘛哩嘛哩哄是什么意思| 心梗是什么意思| 非营运车辆是什么意思| 妤是什么意思| 为什么小鸟站在电线上不会触电| 惊弓之鸟告诉我们什么道理| 为什么庙里不让孕妇去| 脚心发热是什么原因| 铁蛋白低吃什么可以补| 盲点是什么意思| 什么是人乳头瘤病毒| 脚气吃什么药| 什么叫做凤凰男| 什么护肤品好用| 陪嫁一般陪些什么东西| 大便秘结是什么意思| 胃疼吃什么食物最养胃| 看喉咙挂什么科| 冷暖自知上一句是什么| 女性胆固醇高吃什么| 一比吊糟什么意思| 吃什么能补蛋白| 一个骨一个宽是什么字| 腿浮肿是什么原因| 我是小姨的什么人| 6月13号是什么星座| 肝是干什么用的| 识大体是什么意思| 丹毒不能吃什么| 一什么火焰| 精囊腺囊肿是什么意思| 为什么不| 夕阳朝乾是什么意思| 七点到九点是什么时辰| 四川有什么好玩的| 4级手术是什么意思| 8朵玫瑰花代表什么意思| 苦瓜干泡水喝有什么功效| 土地出让金什么意思| 弛张热常见于什么病| 丁是什么生肖| playboy什么意思| 乳房发烫胀痛什么原因| 附件炎吃什么药| 眩晕挂号挂什么科| 发情是什么意思| 喉咙嘶哑是什么原因| ca724是什么意思| 蓝灰色配什么颜色好看| 输卵管堵塞有什么样症状| 下体有异味是什么原因| 头陀是什么意思| 糖类抗原125偏高是什么原因| 胃炎能吃什么| 扁平疣是什么病| 七夕节的含义是什么| 吃中药不能吃什么| 纳豆什么味道| 鼻子里面痒是什么原因| 肚子胀气放屁吃什么药| 蛋白肉是什么东西做的| 男人什么时候精子最强| 决堤是什么意思| 18岁属什么| 花圃是什么意思| 镉是什么东西| 潮喷是什么感觉| 远视眼是什么意思| 马赛克什么意思| 丝瓜络是什么| 身份证更换需要带什么| 胃酸是什么酸| 二网是什么意思| 属马的生什么属相的宝宝好| 脾肾气虚的症状是什么| 熬夜眼睛红血丝用什么眼药水| 苏联什么时候解体| 新生儿黄疸高有什么危害| 百度
Skip to main content

筑梦路上:我们都是奋斗者

百度 波音公司称,777-9X的驾驶舱将比A350-1000宽40厘米,经济舱座位宽度达到46厘米。

You can set up tests for your CodeQL queries to ensure that they continue to return the expected results with new releases of the CodeQL CLI.

Who can use this feature?

CodeQL is available for the following repository types:

About testing custom queries

CodeQL provides a simple test framework for automated regression testing of queries. Test your queries to ensure that they behave as expected.

During a query test, CodeQL compares the results the user expects the query to produce with those actually produced. If the expected and actual results differ, the query test fails. To fix the test, you should iterate on the query and the expected results until the actual results and the expected results exactly match. This topic shows you how to create test files and execute tests on them using the test run subcommand.

Setting up a test CodeQL pack for custom queries

All CodeQL tests must be stored in a special "test" CodeQL pack. That is, a directory for test files with a qlpack.yml file that defines:

name: <name-of-test-pack>
version: 0.0.0
dependencies:
  <codeql-libraries-and-queries-to-test>: "*"
extractor: <language-of-code-to-test>

The dependencies value specifies the CodeQL packs containing queries to test. Typically, these packs will be resolved from source, and so it is not necessary to specify a fixed version of the pack. The extractor defines which language the CLI will use to create test databases from the code files stored in this CodeQL pack. For more information, see Customizing analysis with CodeQL packs.

You may find it useful to look at the way query tests are organized in the CodeQL repository. Each language has a src directory, ql/<language>/ql/src, that contains libraries and queries for analyzing codebases. Alongside the src directory, there is a test directory with tests for these libraries and queries.

Each test directory is configured as a test CodeQL pack with two subdirectories:

  • 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 CodeQL pack. Then add the following files to the subdirectory before you run the test command:

  • A query reference file (.qlref file) defining the location of the query to test. The location is defined relative to the root of the CodeQL pack that contains the query. Usually, this is a CodeQL pack specified in the dependencies block of the test pack. For more information, see Query reference files.

    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.

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.

For an example showing how to create and test a query, see the example below.

Note

Your .ql, .qlref, and .expected files must have consistent names:

  • If you want to directly specify the .ql file itself in the test command, it must have the same base name as the corresponding .expected file. For example, if the query is MyJavaQuery.ql, the expected results file must be MyJavaQuery.expected.
  • If you want to specify a .qlref file in the command, it must have the same base name as the corresponding .expected file, but the query itself may have a different name.
  • The names of the example code files don’t have to be consistent with the other test files. All example code files found next to the .qlref (or .ql) file and in any subdirectories will be used to create a test database. Therefore, for simplicity, we recommend you don’t save test files in directories that are ancestors of each other.

Running codeql test run

CodeQL query tests are executed by running the following command:

codeql test run <test|dir>

The <test|dir> argument can be one or more of the following:

  • Path to a .ql file.
  • Path to a .qlref file that references a .ql file.
  • Path to a directory that will be searched recursively for .ql and .qlref files.

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 test run.

Example

The following example shows you how to set up a test for a query that searches Java code for if statements that have empty then blocks. It includes steps to add the custom query and corresponding test files to separate CodeQL packs outside your checkout of the CodeQL repository. This ensures when you update the CodeQL libraries, or check out a different branch, you won’t overwrite your custom queries and tests.

Prepare a query and test files

  1. Develop the query. For example, the following simple query finds empty then blocks in Java code:

    import java
    
    from IfStmt ifstmt
    where ifstmt.getThen() instanceof EmptyStmt
    select ifstmt, "This if statement has an empty then."
    
  2. Save the query to a file named EmptyThen.ql in a directory with your other custom queries. For example, custom-queries/java/queries/EmptyThen.ql.

  3. If you haven’t already added your custom queries to a CodeQL pack, create a CodeQL pack now. For example, if your custom Java queries are stored in custom-queries/java/queries, add a qlpack.yml file with the following contents to custom-queries/java/queries:

    name: my-custom-queries
    dependencies:
      codeql/java-queries: "*"
    

    For more information about CodeQL packs, see Customizing analysis with CodeQL packs.

  4. Create a CodeQL pack for your Java tests by adding a qlpack.yml file with the following contents to custom-queries/java/tests, updating the dependencies to match the name of your CodeQL pack of custom queries:

    The following qlpack.yml file states that my-github-user/my-query-tests depends on my-github-user/my-custom-queries at a version greater than or equal to 1.2.3 and less than 2.0.0. It also declares that the CLI should use the Java extractor when creating test databases. The tests: . line declares that all .ql files in the pack should be run as tests when codeql test run is run with the --strict-test-discovery option. Typically, test packs do not contain a version property. This prevents you from accidentally publishing them.

    name: my-github-user/my-query-tests
    dependencies:
      my-github-user/my-custom-queries: ^1.2.3
    extractor: java-kotlin
    tests: .
    
  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.

  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 CodeQL pack that contains the query. In this case, the query is in the top level directory of the CodeQL pack named my-custom-queries, which is declared as a dependency for my-query-tests. Therefore, EmptyThen.qlref should simply contain EmptyThen.ql.

  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.

    class Test {
      public void problem(String arg) {
        if (arg.isEmpty())
          ;
        {
          System.out.println("Empty argument");
        }
      }
    
      public void good(String arg) {
        if (arg.isEmpty()) {
          System.out.println("Empty argument");
        }
      }
    }
    

Execute the test

To execute the test, move into the custom-queries directory and run codeql test run java/tests/EmptyThen.

When the test runs, it:

  1. Finds one test in the EmptyThen directory.

  2. Extracts a CodeQL database from the .java files stored in the EmptyThen directory.

  3. Compiles the query referenced by the EmptyThen.qlref file.

    If this step fails, it’s because the CLI can’t find your custom CodeQL pack. Re-run the command and specify the location of your custom CodeQL pack, for example:

    codeql test run --search-path=java java/tests/EmptyThen

    For information about saving the search path as part of your configuration, see Specifying command options in a CodeQL configuration file.

  4. Executes the test by running the query and generating an EmptyThen.actual results file.

  5. Checks for an EmptyThen.expected file to compare with the .actual results file.

  6. Reports the results of the test — in this case, a failure: 0 tests passed; 1 tests failed:. The test failed because we haven’t yet added a file with the expected results of the query.

View the query test output

CodeQL generates the following files in the EmptyThen directory:

  • EmptyThen.actual, a file that contains the actual results generated by the query.
  • EmptyThen.testproj, a test database that you can load into VS Code and use to debug failing tests. When tests complete successfully, this database is deleted in a housekeeping step. You can override this step by running test run with the --keep-databases option.

In this case, the failure was expected and is easy to fix. If you open the EmptyThen.actual file, you can see the results of the test:


| Test.java:3:5:3:22 | stmt | This if statement has an empty then. |

This file contains a table, with a column for the location of the result, along with separate columns for each part of the select clause the query outputs. Since the results are what we expected, we can update the file extension to define this as the expected result for this test (EmptyThen.expected).

If you rerun the test now, the output will be similar but it will finish by reporting: All 1 tests passed..

If the results of the query change, for example, if you revise the select statement for the query, the test will fail. For failed results, the CLI output includes a unified diff of the EmptyThen.expected and EmptyThen.actual files. This information may be sufficient to debug trivial test failures.

For failures that are harder to debug, you can import EmptyThen.testproj into CodeQL for VS Code, execute EmptyThen.ql, and view the results in the Test.java example code. For more information, see Managing CodeQL databases.

Further reading

定心丸什么意思 女人为什么会怀孕 霍金什么时候去世的 什么是断掌 吃什么东西补血最快最有效
息怒是什么意思 10月15是什么星座 尿蛋白低是什么原因 bc是什么牌子 封印是什么意思
建档需要做什么检查 阴道炎是什么 msgm是什么品牌 乌鸦反哺是什么意思 m是什么牌子
江诗丹顿属于什么档次 刘邦字什么 十月23日是什么星座 点痣后需要注意什么事项 人什么什么什么
藏青色是什么颜色xscnpatent.com 借鉴是什么意思bysq.com 长裙配什么鞋子好看hcv7jop9ns7r.cn 态生两靥之愁中靥指什么hcv8jop2ns7r.cn 什么是白噪音hcv7jop4ns7r.cn
药物过敏用什么药hcv8jop5ns2r.cn 酸菜吃多了有什么危害hcv9jop4ns4r.cn ig什么意思hcv8jop7ns7r.cn 舌头上有黑苔是什么原因hcv8jop7ns8r.cn 右手大拇指抖动是什么原因xianpinbao.com
眉梢有痣代表什么1949doufunao.com 小腿肚疼是什么原因hcv8jop1ns2r.cn 孕妇为什么会水肿hcv9jop1ns0r.cn 早上不晨勃是什么原因hcv8jop8ns0r.cn hpv感染什么症状hcv8jop4ns8r.cn
吃头发的虫子叫什么hcv7jop9ns6r.cn 手抖是什么病的前兆hcv9jop6ns6r.cn 便秘吃什么最快排便dajiketang.com 20年是什么年hcv8jop2ns4r.cn 脾虚湿盛吃什么药hcv8jop2ns3r.cn
百度