# runtest

> 运行项目 airbuild.yaml 中某个具名测试脚本，并取回它的输出。

Source: https://air.build/cn/docs/cli/runtest/

`runtest` 运行项目 `airbuild.yaml` 中 `test:` 下的某个具名条目，并收集该条目列为输出的文件。它在团队的环境中运行。

| 团队 | 测试在哪里运行 |
| --- | --- |
| 云端团队 | `ios`：选定的源代码会上传到一台全新的 macOS 构建机。`android_beta`：Linux 构建机。`pocketjs_beta`：团队自己的主机。 |
| Mac 团队 | 在团队自己的 Mac 上。`--environment` 和 `--list-environments` 仅适用于云端团队。 |

`runtest` 从不签名，也从不请求 App 预览。

## 用法

```bash
runtest <project-id> [--platform <platform>] [--source <directory>] [--script <name>] [--additional-options '<flags>'] [--environment ID] [--no-cache]
runtest <project-id> --follow [--run ID]    # reattach; starts nothing
runtest --cancel [--run ID]
runtest --list-environments
runtest --help
```

## 选项

| 选项 | 取值 | 默认值 | 作用 |
| --- | --- | --- | --- |
| `<project-id>` | UUID | 必填 | 要测试的项目。 |
| `--script` | 测试名称 | 只有一个条目时即为该条目 | 要运行 `test:` 下的哪个条目。有多个条目时必填。 |
| `--additional-options` | 一行参数 | 空 | 原样作为环境变量 `additional_options` 传给脚本。请使用以空白分隔的参数；不要使用 shell 表达式或嵌套引号。 |
| `--platform` | `ios`、`pocketjs_beta`、`android_beta` | 默认清单 | 读取 `.airbuild/airbuild.<platform>.yaml` 而不是默认的 `airbuild.yaml`。 |
| `--source` | 目录 | 项目目录 | 测试该目录（例如一个 git worktree），有自己的 `airbuild.yaml` 就用它，否则用项目的。路径相对于当前目录。 |
| `--environment` | `--list-environments` 中的 ID | 默认环境 | 云端团队：在另一个构建机环境（macOS 和 Xcode 版本）上运行。 |
| `--no-cache` | — | 启用缓存 | 不使用构建机缓存（`airbuild-cache`、DerivedData，`android_beta` 则为 Gradle 的构建缓存）运行，也不保存任何缓存。 |
| `--follow` | — | — | 重新连接到正在进行的运行。如果运行已结束，会说明这一点并重放保存的输出。从不启动运行。 |
| `--run` | 运行 ID | 正在运行的那次，否则为最新一次 | 与 `--follow` 或 `--cancel` 同用。有多个运行在进行时必填。 |
| `--cancel` | — | — | 请求运行停止；最多等待 30 秒确认。 |
| `--list-environments` | — | — | 云端团队：列出构建机环境。 |

`runtest` 不接受 `--sign`、`--unsigned`、`--command` 和 `--output`。如果要运行没有清单的内联脚本，请使用 [runmacos](https://air.build/cn/docs/cli/runmacos/) 或 [runlinux](https://air.build/cn/docs/cli/runlinux/)。

## runtest 从 airbuild.yaml 读取的内容

`runtest` 需要 `version: 3` 或 `4`，不需要 `build:` 部分。它查找清单的方式与 [runbuild](https://air.build/cn/docs/cli/runbuild/) 相同。

```yaml title="airbuild.yaml"
version: 3
test:
  app:
    script: |
      airbuild-xcode test -project App.xcodeproj -scheme App \
        -resultBundlePath .airbuild/artifacts/Tests.xcresult $additional_options
    outputs:
      - .airbuild/artifacts/Tests.xcresult
  unit:
    script: swift test
```

| 键 | 说明 |
| --- | --- |
| `test.<name>` | 一个测试条目。名称只能使用 ASCII 字母、数字、点、下划线和连字符。每个条目只接受 `script` 和 `outputs`。 |
| `test.<name>.script` | Bash 脚本。它在源代码根目录以 `-e -o pipefail` 运行。 |
| `test.<name>.outputs` | 可选。脚本写出的文件或目录，相对于源代码根目录。 |

输出路径必须是具体的相对路径：不能是绝对路径，不能包含 `..`、通配符、符号链接或特殊文件，也不能有一个路径位于另一个路径之内。所有输出会保留各自的相对路径，收集到同一个目录中。

脚本的 `PATH` 中还有 `airbuild-cache`，可以在多次运行之间保留 `node_modules` 等文件；在 Mac 上，第一次运行 `airbuild-xcode install` 或 `test` 时会取回项目的 DerivedData。在 `android_beta` 上，`runtest` 会自行恢复和保存 Gradle 的构建缓存。

## 输出

```text
• Build started in the Environment. Run: <run-id>
• It runs to completion even if this command is interrupted (reattach with: runtest <project-id> --follow --run <run-id>).
Airbuild remote execution
… the test script's output …
✓ Command succeeded in <time>.

✓ Test finished.
  Build: <build-id>
  Artifact: <path to the collected outputs>
```

运行失败时仍会取回已存在的输出，`runtest` 以脚本的退出码退出：

```text
✗ Test failed.
  Reason: <why>
  Artifact: <path to the collected outputs>
```

没有声明输出时，会输出 `No outputs requested; skipping artifact download.`

## 同时运行多个

`runtest` 有自己的槽位，与 `runbuild`、`runmacos` 和 `runlinux` 分开。云端团队可以同时运行多个 `runtest`，每个使用一台全新的构建机；Mac 团队一次只运行一个。开始运行时会输出运行 ID；有多个正在运行时，`--follow` 和 `--cancel` 需要 `--run ID`。Ctrl-C 只会断开连接。

## 退出码

| 退出码 | 含义 |
| --- | --- |
| `0` | 脚本通过，且每个声明的输出都已写出。 |
| `1` | 运行无法启动或已丢失。 |
| `2` | 选项或参数有误，什么也没有运行。 |
| `65` | 脚本通过，但某个声明的输出缺失或无法导出。 |
| `130` | 已取消，或用 Ctrl-C 断开了连接。 |
| 其他 | 测试脚本自己的退出码。 |

## 错误

| 消息 | 怎么办 |
| --- | --- |
| `runtest requires airbuild.yaml with version: 3 or 4 and named test scripts.` | 添加一份带 `test:` 部分的清单。 |
| `runtest requires airbuild.yaml version: 3 or 4.` | 在清单中设置 `version: 3` 或 `version: 4`。 |
| `test must contain named scripts; names use only ASCII letters, digits, dots, underscores and hyphens.` | 修正 `test:` 部分。 |
| `Select a test with --script. Available scripts: …` | 用 `--script` 传入列出的某个名称。 |
| `--additional-options must be a single line.` | 去掉换行。 |
| `Outputs must be concrete relative paths inside the project, without parent traversal or glob patterns.` | 修正 `outputs`。 |
| `Output paths must not repeat or contain another declared output.` | 去掉重叠的路径。 |
| `Declared output was not produced: <path>` | 让脚本写出它，或从 `outputs` 中删除。 |
| `Cannot export a symbolic link: <path>` / `Output contains a symbolic link or special file: <path>` | 只导出普通文件和目录。 |
| `runtest does not accept build or signing options.` | 去掉 `--sign` / `--unsigned`。 |
| `A runtest execution is already running in this Team's Environment.` | 跟进或取消它。 |
| `N runtest executions are running; choose one with --run.` | 传入它列出的某个运行 ID。 |

## 示例

```bash
# The only test entry
runtest 3f6c1a2e-8b4d-4f0a-9c1e-2d7b5a6e9f10
```

```bash
# One XCTest class from the "app" entry
runtest 3f6c1a2e-8b4d-4f0a-9c1e-2d7b5a6e9f10 --script app \
  --additional-options '-only-testing:AppTests/ExampleTests'
```

```bash
# Android tests
runtest 3f6c1a2e-8b4d-4f0a-9c1e-2d7b5a6e9f10 --platform android_beta
```
