> ## Documentation Index
> Fetch the complete documentation index at: https://docs.oasm.io/llms.txt
> Use this file to discover all available pages before exploring further.

# 构建与测试

> Task 命令、测试规则以及连接器的发布方式

每个 task 都是对 `go` 的轻量封装。在单个模块内工作时，可直接运行对应的命令。

## 命令

```bash theme={null}
task test        # go test per module
task vet         # go vet per module
task fmt         # gofmt per module
task tidy        # go mod tidy per module with GOWORK=off
task manifest    # regenerate manifest.json
```

在单个连接器内工作时很有用：

```bash theme={null}
cd vulnerabilities/example
go test ./... -count=1
go vet ./...
go build ./...
```

> [!NOTE]
>
> 模块范围的命令必须在模块目录内运行。除非 `go.work` 将各模块统一起来，否则在仓库根目录运行 `go test ./...` 不会覆盖到连接器。

## 测试规则

测试**不需要 Docker、网络或凭据**，这是首要门槛。CI 会以 `GOWORK=off` 对每个模块运行相同的 `go test`。

连接器的适配器必须覆盖以下内容才能合并：

* **解析**：工具原始输出 → `Finding`。
* **严重性映射**：工具输出的每个值都能映射到枚举。
* **错误映射**：`fatal:` 与 `retryable:` 前缀。
* **取消**：`ctx.Done()` 关闭时 `Execute` 返回。

测试适配器，而不是 SDK。注入一个假的工具二进制文件或客户端，使测试保持封闭（hermetic）。

```go theme={null}
func TestExecuteCancellation(t *testing.T) {
    ctx, cancel := context.WithCancel(context.Background())
    out := make(chan connector.Finding)
    done := make(chan error, 1)

    go func() { done <- (&ExampleAdapter{}).Execute(ctx, map[string]any{"target": "x"}, out) }()
    cancel()

    select {
    case err := <-done:
        if !errors.Is(err, context.Canceled) {
            t.Fatalf("expected context.Canceled, got %v", err)
        }
    case <-time.After(time.Second):
        t.Fatal("Execute did not return after cancellation")
    }
}
```

## 构建镜像

每个连接器都有自己的多阶段 `Dockerfile`，以仓库根目录作为构建上下文。直接构建即可：

```bash theme={null}
docker build -f vulnerabilities/example/Dockerfile -t connector-example:1.0.0 .
```

保持镜像精简：静态构建（`CGO_ENABLED=0`），将单个二进制文件放到最小化基础镜像上，创建非特权用户并通过 `USER` 切换到该用户。

## CI 与发布

连接器工作流按路径过滤，仅在 `sdk/**`、`ports_scanner/**`、`vulnerabilities/**`、`url_discovery/**`、`scripts/**` 以及工作流文件本身变更时触发。

* **推送 / 拉取请求时**：发现每个 `<category>/<slug>/manifest.yaml`，根据 `slug` 和 `version` 生成构建矩阵，运行完整测试套件，然后以 `push: false` 构建每个镜像。不会发布任何内容。
* **手动触发时**：执行相同的流水线，外加一个推送任务，发布 `connector-<slug>:<version>` 和 `:latest`。`dry_run` 输入只构建和测试而不发布，`image_tag` 输入可覆盖标签。

由于发布是手动且由标签驱动的，`manifest.yaml` 中的 `version` 字段是有意义的：提升版本号会生成新的不可变镜像标签。

## 设计约束

* **SDK 是唯一的共享代码。** 任何向上泄漏的工具特定逻辑都会让每个连接器变得更重、契约更模糊。
* **`manifest.json` 是派生产物。** 重新生成它，切勿手动编辑或修补。
* **容器即边界。** 连接器以非特权方式运行，不暴露任何入站网络接口。


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.