# runtest

> 執行專案 airbuild.yaml 中其中一個具名測試腳本，並帶回它的輸出檔案。

Source: https://air.build/tw/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/tw/docs/cli/runmacos/) 或 [runlinux](https://air.build/tw/docs/cli/runlinux/)。

## runtest 從 airbuild.yaml 讀取的內容

`runtest` 需要 `version: 3` 或 `4`，不需要 `build:` 區段。它尋找設定檔的方式與 [runbuild](https://air.build/tw/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
```
