Appearance
配置文件
Anvil 使用 YAML 格式的 .anvil 配置文件。
配置文件查找优先级
| 优先级 | 位置 | 说明 |
|---|---|---|
| 1(最高) | 命令行 --config | 命令行指定的配置文件 |
| 2 | .anvil | 项目目录(当前工作目录) |
| 3 | ~/.anvil | 用户主目录 |
| 4(最低) | 内置默认值 | 无配置文件时使用 |
完整配置示例
yaml
# 编译器设置
compiler: gcc
compiler_flags:
- -Wno-attributes # 必须:抑制 anvil 属性警告
- -Wall
- -Wextra
# 目录设置
test_dir: tests
build_dir: build
# 测试执行设置
default_timeout: 30s
# 日志文件设置
compile_log: anvil-compile.log
test_log: anvil-test.log
# 输出设置
junit_file: ""
no_clean: false
no_check: false
# Include 路径设置(用于查找 anvil/mock.h)
anvil_include_paths:
- include
- ../include
- /usr/local/include
# 代码覆盖率设置
coverage:
enabled: false
format: lcov # gcov, lcov, html, cobertura
output_dir: coverage
exclude_patterns:
- "*/tests/*"
- "*/mocks/*"配置项详解
编译器设置
| 配置项 | 类型 | 默认值 | 说明 |
|---|---|---|---|
compiler | string | gcc | C 编译器路径或名称 |
compiler_flags | string[] | ["-Wno-attributes"] | 编译器标志列表 |
重要
-Wno-attributes 是必需的,用于抑制 Anvil 自定义属性的警告。
目录设置
| 配置项 | 类型 | 默认值 | 说明 |
|---|---|---|---|
test_dir | string | tests | 测试源文件目录 |
build_dir | string | build | 编译产物目录(.o 文件) |
执行设置
| 配置项 | 类型 | 默认值 | 说明 |
|---|---|---|---|
default_timeout | duration | 30s | 单个测试的超时时间 |
支持的时间格式:30s, 500ms, 1m, 2m30s
日志设置
| 配置项 | 类型 | 默认值 | 说明 |
|---|---|---|---|
compile_log | string | anvil-compile.log | 编译日志路径 |
test_log | string | anvil-test.log | 测试日志路径 |
输出设置
| 配置项 | 类型 | 默认值 | 说明 |
|---|---|---|---|
junit_file | string | "" | JUnit XML 报告路径 |
no_clean | bool | false | 保留生成文件 |
no_check | bool | false | 跳过构建目录验证 |
覆盖率设置
| 配置项 | 类型 | 默认值 | 说明 |
|---|---|---|---|
coverage.enabled | bool | false | 启用覆盖率收集 |
coverage.format | string | lcov | 输出格式 |
coverage.output_dir | string | coverage | 输出目录 |
coverage.exclude_patterns | string[] | [] | 排除模式 |
场景配置示例
CI/CD 环境
yaml
compiler: gcc
compiler_flags:
- -Wno-attributes
- -Wall
- -Werror # 将警告视为错误
test_dir: tests
build_dir: build
default_timeout: 30s
compile_log: ci-logs/compile.log
test_log: ci-logs/test.log
junit_file: ci-reports/test-results.xml
no_clean: false
no_check: false本地开发环境
yaml
compiler: gcc
compiler_flags:
- -Wno-attributes
- -g # 调试信息
- -O0 # 禁用优化
test_dir: tests
build_dir: build
default_timeout: 1m # 较长超时便于调试
no_clean: true # 保留生成文件便于调试嵌入式项目
yaml
compiler: arm-none-eabi-gcc
compiler_flags:
- -Wno-attributes
- -mcpu=cortex-m4
- -mthumb
- -Os
test_dir: tests/host
build_dir: build/host
default_timeout: 10s
anvil_include_paths:
- include
- hal/include启用覆盖率
yaml
compiler: gcc
compiler_flags:
- -Wno-attributes
- -fprofile-arcs
- -ftest-coverage
coverage:
enabled: true
format: html
output_dir: coverage
exclude_patterns:
- "*/tests/*"
- "*/mocks/*"
- "*/anvil-mocks/*"