precision-test

核心特性 (Features)

  • 三仓库统一:一套插件支持 vllm_ascend / sglang / pytorch,通过 repo-name 选择仓库适配器
  • 多粒度匹配:行级 + 函数级并行匹配,结果合并去重;文件级匹配作为重命名/删除场景的内部兜底(不可手动开关)
  • PR 源自动适配:三仓库均走 GitHub(github-pr,pytorch 检测上游 pytorch/pytorch);gitcode-pr 作为 GitCode PR 通用入口,与 github-pr 互斥
  • 基于覆盖率选择:利用覆盖率数据(SQLite 格式)精准识别受代码变更影响的测试用例
  • 安全加固:内置 DoS 防护、子进程环境变量白名单、路径与 PR 标识校验
  • 可配置:匹配粒度、阈值、虚拟环境等均可通过 action 输入参数调整

快速上手 (Quick Start)

前置条件

  • 仓库中已配置 .gitcode/workflows/ 目录
  • 已有覆盖率数据(coverage.py SQLite 格式),或使用 build-map 自动构建
  • 运行环境已安装 python3 / pip3(默认 skip-venv: true 直接使用系统 Python);如需隔离环境可设置 skip-venv: false 由插件创建 venv

基础用法

vllm_ascend / sglang(GitHub PR)

在 .gitcode/workflows/precision-test.yml 中创建以下工作流文件:

name: 精准测试

on:
  pull_request:
    branches: [main]

jobs:
  select-tests:
    runs-on: ubuntu-latest
    permissions:
      repository: read
      pull-requests: read
    steps:
      - name: 检出代码
        uses: actions/checkout@v4

      - name: 精准测试选择
        uses: openlibing/test-action/precision-test@v2.0.0
        with:
          repo-name: vllm_ascend
          github-pr: vllm-project/vllm-ascend#${{ github.event.pull_request.number }}
          source-dir: covstub
          min-affected: 1

      - name: 执行选中用例
        run: |
          if [ -f recommended_pytest_paths.txt ]; then
            pytest -n auto $(cat recommended_pytest_paths.txt)
          fi

pytorch(GitHub PR)

name: 精准测试

on:
  pull_request:
    branches: [main]

jobs:
  select-tests:
    runs-on: ubuntu-latest
    permissions:
      repository: read
      pull-requests: read
    steps:
      - name: 检出代码
        uses: actions/checkout@v4

      - name: 精准测试选择
        uses: openlibing/test-action/precision-test@v2.0.0
        with:
          repo-name: pytorch
          github-pr: pytorch/pytorch#${{ github.event.pull_request.number }}
          source-dir: covstub
          min-affected: 1

      - name: 执行选中用例
        run: |
          if [ -f recommended_pytest_paths.txt ]; then
            pytest -n auto $(cat recommended_pytest_paths.txt)
          fi

github-pr 与 gitcode-pr 互斥,必须且只能指定其中一个;同时指定会报错。

带用例映射的用法

如果需要重新构建测试用例映射关系,可以使用 map-file 和 build-map 参数(构建 map 时需提供 coverage-dir):

- name: 精准测试选择
  uses: openlibing/test-action/precision-test@v2.0.0
  with:
    repo-name: sglang
    github-pr: sgl-project/sglang#${{ github.event.pull_request.number }}
    map-file: test_case_map.json
    coverage-dir: sglang@20260908
    build-map: "true"

输入参数 (Inputs)

参数名 说明 必填 默认值
github-pr GitHub PR(vllm_ascend / sglang / pytorch),格式 owner/repo#pr_number 或仅 pr_number;与 gitcode-pr 互斥 二选一 -
gitcode-pr GitCode PR,格式 owner/repo#pr_number 或仅 pr_number;与 github-pr 互斥 二选一 -
repo-name 仓库适配器:vllm_ascend / sglang / pytorch 否 vllm_ascend
source-dir 源代码目录(函数级匹配与噪音过滤需要) 否 covstub
map-file 测试用例映射文件路径 否 test_case_map.json
coverage-dir 覆盖率数据目录;构建 map 时(build-map: true 或 map 文件不存在时)必填 否 coverage
build-map 是否强制重建测试用例映射 否 false
min-affected 最小受影响行数阈值 否 1
dedup 是否启用去重(相同覆盖行仅保留一个用例) 否 false
enable-line-match 是否启用行级匹配 否 true
disable-line-match 关闭行级匹配(优先级高于 enable-line-match) 否 false
enable-function-match 是否启用函数级匹配 否 true
disable-function-match 关闭函数级匹配(优先级高于 enable-function-match) 否 false
skip-imports 函数级匹配时是否跳过 import 语句行 否 false
skip-venv 跳过虚拟环境创建,直接使用系统 python3 / pip3 否 true

匹配粒度规则:disable-* 优先于 enable-*。即 disable-line-match: true 时无论 enable-line-match 取值如何,行级匹配均关闭。文件级匹配仅作为重命名/删除场景的内部兜底,不对外暴露开关。

输出参数 (Outputs)

无。本插件未在 action.yml 中定义输出参数,其产物为工作目录下的 recommended_pytest_paths.txt 文件,每行一个推荐执行的 pytest 测试路径。即使无推荐用例也会写入空文件,便于下游步骤统一 cat。可通过后续步骤读取该文件来执行选中的测试用例。

权限要求 (Permissions)

插件需读取 PR diff 信息,建议在 workflow 中声明以下权限:

permissions:
  repository: read
  pull-requests: read

如需提高 GitHub / GitCode API 速率上限,可配置 GITHUB_TOKEN / GITCODE_TOKEN 环境变量(详见下方"环境变量依赖"部分)。

仓库差异对照

维度 vllm_ascend sglang pytorch
PR 源 GitHub(github-pr) GitHub(github-pr) GitHub(github-pr)
产品代码前缀 vllm_ascend/ python/sglang/ torch/
全量触发变更 csrc/ 目录(非 .md) 无 无
测试目录识别 tests__ 前缀 / cpu-ut ____w__sglang__sglang__test__ 前缀 目录名含 __ 或以 test_ 开头

详细的测试用例名称转换、覆盖率目录布局探测、匹配逻辑说明见 src/README.md。

使用约束

  • 运行时环境为 node16
  • 需要预先准备覆盖率数据(coverage.py SQLite 格式),或通过 build-map: "true" 自动构建
  • github-pr / gitcode-pr 二选一,否则插件无法获取变更信息
  • 插件运行时会自动 pip install regex(diff_parser.py 依赖)
  • 覆盖率数据库大小、diff 文件大小等受安全限制约束(详见"环境变量依赖"部分)

环境变量依赖

DoS 防护

环境变量 默认值 说明
MAX_DATABASE_SIZE_MB 100 覆盖率数据库最大体积(MB)
DATABASE_QUERY_TIMEOUT 60 数据库查询超时时间(秒)
MAX_SOURCE_FILE_SIZE_MB 1 单个源文件最大解析体积(MB)
MAX_PARSE_RECURSION_DEPTH 1000 AST 解析最大递归深度
MAX_DIFF_SIZE_MB 50 diff 文件最大体积(MB)
REGEX_TIMEOUT_SECONDS 10 正则表达式匹配超时时间(秒)
PYTHON_EXEC_TIMEOUT_SECONDS 600 Python 子进程执行超时时间(秒)

数据完整性

环境变量 默认值 说明
MAX_VALID_LINE_NUMBER 1000000 最大有效行号

API 配置

环境变量 默认值 说明
GITHUB_TOKEN - GitHub API token(vllm_ascend / sglang 拉 PR diff,可选,提升速率)
GH_TOKEN - GitHub API token 备选(GITHUB_TOKEN 未设置时回退)
GITCODE_TOKEN - GitCode API token(gitcode-pr 拉 PR diff,可选,提升速率)

插件通过子进程环境变量白名单(SUBPROCESS_ENV_SAFE_NAMES)向 Python 子进程显式传递 PATH、HOME、代理、SSL 证书、GITHUB_TOKEN / GH_TOKEN / GITCODE_TOKEN 等安全变量,屏蔽 ACTIONS_* / ATOMGIT_* / INPUT_* / HUAWEICLOUD_* 等平台敏感前缀,避免密钥泄露到 Python 进程。

代理配置(内网环境)

GitHub / GitCode API 均需直连外网,内网环境必须配置代理:

- name: 精准测试选择
  uses: openlibing/test-action/precision-test@v2.0.0
  env:
    HTTP_PROXY: http://<user>:<pwd>@proxycn2.huawei.com:8080/
    HTTPS_PROXY: http://<user>:<pwd>@proxycn2.huawei.com:8080/
    GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
    PYTHON_EXEC_TIMEOUT_SECONDS: 900
  with:
    repo-name: vllm_ascend
    github-pr: vllm-project/vllm-ascend#${{ github.event.pull_request.number }}

自定义环境变量示例

- name: 精准测试选择
  uses: openlibing/test-action/precision-test@v2.0.0
  env:
    MAX_DATABASE_SIZE_MB: 200
    DATABASE_QUERY_TIMEOUT: 120
    PYTHON_EXEC_TIMEOUT_SECONDS: 900
    GITCODE_TOKEN: ${{ secrets.GITCODE_TOKEN }}
  with:
    repo-name: pytorch
    github-pr: pytorch/pytorch#${{ github.event.pull_request.number }}

数据格式

覆盖率数据采用 coverage.py 的 SQLite 格式存储(coverage.* 文件),包含以下两张表:

  • file:文件路径信息
  • arc:覆盖弧数据,含 fromno、tono 字段

覆盖率目录布局自动探测:测试目录下存在 covdata/ 子目录(vllm_ascend / pytorch)则从 covdata/ 读取 coverage.*;无 covdata/ 子目录(sglang)则直接读取测试目录下的 coverage.*。

上下游串联

本插件通常作为 CI 流水线中"测试选择"环节,与以下环节串联:

  1. 上游:代码变更(PR 触发)→ 本插件分析变更,筛选受影响用例
  2. 下游:读取 recommended_pytest_paths.txt,使用 pytest -n auto 并行执行选中的测试用例

典型串联流程:

PR 创建/更新 → 检出代码 → precision-test 筛选用例 → pytest 执行选中用例 → 上报结果

贡献与本地调试

本地开发

# 克隆仓库
git clone https://gitcode.com/openlibing/test-action.git
cd test-action/precision-test

# 安装依赖
npm install

# 构建(ncc 打包 index.js + 拷贝 test_selector Python 包到 dist/)
npm run build

构建产物结构:

dist/
├── index.js              # ncc 打包后的 Node.js 入口(含 @actions/core、@actions/exec)
├── licenses.txt          # 依赖许可证
└── test_selector/        # Python 包(运行时由 index.js 拷贝到临时目录并设置 PYTHONPATH)
    ├── cli.py / __main__.py / __init__.py
    ├── github.py / gitcode.py
    ├── coverage_selector.py / change_detector.py / diff_parser.py ...
    └── repos/            # 三仓库适配器
        ├── base.py / vllm_ascend.py / sglang.py / torch_npu.py

本地调试 Python 工具

Python 工具可独立于 Action 调试(详见 src/README.md):

cd src
python -m test_selector --repo vllm_ascend --github-pr "vllm-project/vllm-ascend#12379" --source-dir ./covstub

调试

可通过设置环境变量 ACTIONS_STEP_DEBUG 为 true 开启调试日志输出。

提交贡献

欢迎提交 Issue 和 Pull Request。请确保:

  • 代码通过所有现有测试
  • 新功能包含对应的测试用例
  • 遵循项目代码风格

开源协议 (License)

MIT