Skip to content

test: 实时 API 测试仅在 integration build tag 下编译 - #60

Merged
HoneyBBQ merged 2 commits into
mainfrom
ci/test-isolation
Sep 29, 2026
Merged

HoneyBBQ merged 2 commits into
mainfrom
ci/test-isolation

Conversation

@HoneyBBQ

@HoneyBBQ HoneyBBQ commented Sep 28, 2026 •

Copy link
Copy Markdown
Collaborator

问题

调用真实 provider API 的测试写在普通的 _test.go 文件里,只要环境中有凭据就会执行。这些包大多在 TestMain 里加载 .env。开发者本地有 .env 时,每次 go test ./... 都会发出计费请求,加 -short 也拦不住。Copilot 的单元测试还会读取 GITHUB_COPILOT_TOKEN,并把它发给本地测试服务器。

CI 只测一个 Go 版本,也不检查 go.mod 和 go.sum 是否 tidy。

改动

  • 实时 API 测试、它们的辅助函数和加载 .env 的 TestMain,都移到带 //go:build integration 的 *_integration_test.go 文件里。涉及六个包:alibabacloud/images、anthropic/messages、github/copilot、google/generativeai、openai/completions、openai/responses。其中 alibabacloud/images 的 TestMain 是新加的。opencode/go 的实时测试原本就在单独的文件里,只补上 tag 并按同样的规则改名。Edge TTS 包原本就用这个 tag。不加 tag 时,读取凭据的代码不会编译进测试二进制。
  • 加 tag 后,缺少凭据的测试仍然跳过。阿里云图片测试保留 DASHSCOPE_IMAGE_LIVE=1 开关。
  • Copilot 单元测试改用固定的假 token。
  • CI 在 go.mod 声明的 Go 版本和最新稳定版上分别跑 build、vet 和单元测试,并跑 go vet -tags integration,保证带 tag 的文件能编译。go mod tidy 改动了 go.mod 或 go.sum 时,lint job 失败。集成测试 job 加上 -tags integration,触发条件和 secrets 不变。
  • mise.toml 和 docs/getting-started.md 写明新的命令:go test -tags integration -count=1 -run '^TestIntegration_' ./...。

验证

  • 在 Go 1.25.7 和 Go 1.27.1 上,go build、go vet、go vet -tags integration、go test ./... -short -count=1 -race 和 go test ./... -count=1 均通过。golangci-lint run 加不加 --build-tags integration 都没有告警。actionlint 通过。
  • 把测试读取的所有凭据变量设为假值,在 .env 里写同样的值,再把 HTTPS_PROXY/HTTP_PROXY 指向一个关闭的端口。此时 go test ./... 加不加 -short 都通过。同样设置下加 -tags integration,实时测试会执行并在代理上失败。这说明它们默认是被编译排除的,不是碰巧跳过。
  • 本分支没有跑过集成测试 job。

⚠️ No human QA

@HoneyBBQ
HoneyBBQ added this pull request to stack #62 September 28, 2026 10:10
@HoneyBBQ
HoneyBBQ marked this pull request as ready for review September 28, 2026 12:45
@HoneyBBQ HoneyBBQ changed the title test: run live API tests only under the integration build tag test: 实时 API 测试仅在 integration build tag 下编译 Sep 28, 2026
Live provider tests ran whenever an API key was present in the
environment or a .env file, so a plain `go test -short ./...` could send
billed requests. Move every live test, its helpers and the .env-loading
TestMain into *_integration_test.go files guarded by
`//go:build integration`, matching the existing Edge TTS test. The
default build no longer reads any credential variables; the Copilot unit
tests use a fixed token for their local server.

Run live tests with: go test -tags integration -run '^TestIntegration_' ./...
Run the test job against the Go version declared in go.mod and the
current stable release, fail when go mod tidy changes go.mod or go.sum,
vet the integration-tagged tests on every run, and select live tests in
the integration job with -tags integration.
@HoneyBBQ
HoneyBBQ merged commit f3872ad into main Sep 29, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant