golang项目架构测试与分析工具插件arch-go的使用
Golang项目架构测试与分析工具插件arch-go的使用
Arch-Go简介
Arch-Go是一个用于Go项目的架构检查工具,可以帮助开发者定义和执行项目架构规则。
支持的规则
依赖检查
支持定义导入规则:
- 允许的依赖
- 内部依赖(同一模块)
- 标准库依赖
- 外部依赖(第三方包)
- 不允许的依赖
- 内部依赖(同一模块)
- 标准库依赖
- 外部依赖(第三方包)
包内容检查
允许定义一组包的内容,例如可以定义某个包应仅包含接口定义。支持的检查包括:
- shouldNotContainInterfaces
- shouldNotContainStructs
- shouldNotContainFunctions
- shouldNotContainMethods
- shouldOnlyContainInterfaces
- shouldOnlyContainStructs
- shouldOnlyContainFunctions
- shouldOnlyContainMethods
函数检查
检查一些函数属性,如:
- 最大参数数量
- 最大返回值数量
- 每个文件的最大公共函数数量
- 函数体的最大行数
命名规则检查
检查一些命名规则,例如:
- 如果一个结构体实现了匹配某些名称模式的接口,则其名称应以特定模式开头或结尾
配置
arch-go.yml文件
version: 1
threshold:
compliance: 100
coverage: 100
dependenciesRules:
- package: "**.impl.*"
shouldOnlyDependsOn:
internal:
- "**.foo.*"
- "*.bar.*"
shouldNotDependsOn:
internal: ["**.model.**"]
- package: "**.utils.**"
shouldOnlyDependsOn:
- "**.model.**"
- package: "**.foobar.**"
shouldOnlyDependsOn:
external:
- "gopkg.in/yaml.v3"
- package: "**.example.**"
shouldNotDependsOn:
external:
- "github.com/foobar/example-module"
contentsRules:
- package: "**.impl.model"
shouldNotContainInterfaces: true
- package: "**.impl.configuration"
shouldOnlyContainFunctions: true
- package: "**.impl.dependencies"
shouldNotContainStructs: true
shouldNotContainInterfaces: true
shouldNotContainMethods: true
shouldNotContainFunctions: true
functionsRules:
- package: "**.impl.**"
maxParameters: 3
maxReturnValues: 2
maxPublicFunctionPerFile: 1
maxLines: 50
namingRules:
- package: "**.arch-go.**"
interfaceImplementationNamingRule:
structsThatImplement: "*Connection"
shouldHaveSimpleNameEndingWith: "Connection"
包名模式
包名可以定义为固定值或使用*
特殊字符创建简单模式。
示例 | 描述 |
---|---|
*.name | 包应以name结尾,前面可以是任意内容,仅支持一级(例如foo/name,但不支持foo/bar/name) |
**.name | 包应以name结尾,前面可以是任意内容,支持多级(例如foo/name和foo/bar/name) |
**.*name | 包应以name后缀结尾,前面可以是任意内容,支持多级 |
*.name | 包应以name前缀开头,后面可以是任意内容,支持多级 |
name.* | 包应以name开头,后面可以是任意内容,仅支持一级 |
name.** | 包应以name开头,后面可以是任意内容,支持多级 |
.name. | 包应包含name,支持前后多级 |
.foo/bar. | 包应包含foo/bar,支持前后多级 |
foo.**.bar | 包应以foo开头,以bar结尾,中间可以是任意内容 |
foo.*.bar | 包应以foo开头,以bar结尾,中间只能有一级 |
阈值配置
当前版本支持合规率和覆盖率阈值的配置。默认情况下,两个限制都设置为100%。
使用方式
在命令行中使用Arch-Go
安装Arch-Go:
$ go install -v github.com/arch-go/arch-go@latest
进入模块路径:
$ cd [path-to-your-module]
执行Arch-Go工具:
$ arch-go [flags]
描述架构指南
Arch-Go包含一个命令来描述arch-go.yml
文件中的架构规则:
$ arch-go describe
输出示例:
Dependency Rules
* Packages that match pattern '**.cmd.*',
* Should only depends on packages that matches:
- '**.arch-go.**'
Function Rules
* Packages that match pattern '**.arch-go.**' should comply with the following rules:
* Functions should not have more than 50 lines
* Functions should not have more than 4 parameters
* Functions should not have more than 2 return values
* Files should not have more than 5 public functions
Content Rules
* Packages that match pattern '**.impl.model' should not contain functions or methods
* Packages that match pattern '**.impl.config' should only contain functions
Naming Rules
* Packages that match pattern '**.arch-go.**' should comply with:
* Structs that implement interfaces matching name '*Verification' should have simple name ending with 'Verification'
Threshold Rules
* The module must comply with at least 100% of the rules described above.
* The rules described above must cover at least 100% of the packages in this module.
Time: 0.000 seconds
支持的标志
标志 | 描述 |
---|---|
–color | 如果未设置(默认:auto),在tty中打印颜色。如果设置为yes或no,则覆盖默认值。这在CI中很有用 |
–verbose | 包括命令运行时的详细信息。简写为-v |
–html | 生成带有评估结果的HTML报告。HTML报告生成在.arch-go/report.html中 |
–json | 生成带有评估结果的JSON报告。JSON报告生成在.arch-go/report.json中 |
示例
$ arch-go
$ arch-go -v
$ arch-go --verbose
$ arch-go --html
$ arch-go --json
$ arch-go --color
$ arch-go describe
以编程方式使用Arch-Go
当前版本的Arch-Go允许我们将架构检查作为go test工具运行的测试的一部分。
将Arch-Go添加为项目依赖:
go get github.com/arch-go/arch-go@latest
创建一个架构测试示例:
package architecture_test
import (
"testing"
archgo "github.com/arch-go/arch-go/api"
config "github.com/arch-go/arch-go/api/configuration"
)
func TestArchitecture(t *testing.T) {
configuration := config.Config{
DependenciesRules: []*config.DependenciesRule{
{
Package: "**.cmd.**",
ShouldOnlyDependsOn: &config.Dependencies{
Internal: []string{
"**.cmd.**",
"**.internal.**",
},
},
},
},
}
moduleInfo := config.Load("github.com/arch-go/my-go-project")
result := archgo.CheckArchitecture(moduleInfo, configuration)
if !result.Pass {
t.Fatal("Project doesn't pass architecture tests")
}
}
result
变量将存储比验证结果更多的信息,包括每种规则类型和分析包的详细信息,因此您可以访问所有这些数据以根据需要创建断言。
贡献
欢迎贡献。
更多关于golang项目架构测试与分析工具插件arch-go的使用的实战教程也可以访问 https://www.itying.com/category-94-b0.html
更多关于golang项目架构测试与分析工具插件arch-go的使用的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html
arch-go:Golang项目架构分析与测试工具
arch-go 是一个用于分析和测试 Golang 项目架构的工具,它可以帮助开发者检查项目结构是否符合预定的架构规范,确保代码组织的一致性和可维护性。
主要功能
- 架构验证:检查项目是否符合分层架构规范
- 依赖分析:验证包之间的依赖关系是否正确
- 循环依赖检测:识别项目中的循环依赖问题
- 接口实现检查:确保接口实现符合预期
- 测试覆盖率分析:评估架构层面的测试覆盖率
安装
go install github.com/arch-go/arch-go@latest
基本使用
1. 初始化配置文件
arch-go init
这将生成一个默认的 arch-go.yml
配置文件,你可以根据项目需求进行修改。
2. 运行分析
arch-go analyze ./...
3. 生成报告
arch-go report -format=html
配置示例
以下是一个典型的 arch-go.yml
配置文件示例:
# arch-go.yml
layers:
- name: presentation
allowed_dependencies: ["domain", "application"]
path: "internal/presentation"
- name: application
allowed_dependencies: ["domain", "infrastructure"]
path: "internal/application"
- name: domain
allowed_dependencies: []
path: "internal/domain"
- name: infrastructure
allowed_dependencies: ["domain"]
path: "internal/infrastructure"
rules:
- name: no-domain-imports
description: "Domain layer should not import any other layers"
layer: "domain"
check: "no_dependencies"
- name: no-circular-deps
description: "No circular dependencies between packages"
check: "no_circular_deps"
- name: interface-implementation
description: "All interfaces should have at least one implementation"
check: "interface_implementation"
集成到CI/CD
可以将 arch-go 集成到你的 CI/CD 流程中,例如在 GitHub Actions 中:
# .github/workflows/arch-check.yml
name: Architecture Check
on: [push, pull_request]
jobs:
arch-check:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-go@v2
with:
go-version: '1.20'
- run: go install github.com/arch-go/arch-go@latest
- run: arch-go analyze ./...
高级用法
自定义规则
你可以通过 Go 代码扩展 arch-go 的功能:
package main
import (
"github.com/arch-go/arch-go/pkg/analyzer"
"github.com/arch-go/arch-go/pkg/core"
)
type CustomRule struct{}
func (r CustomRule) Name() string {
return "custom-rule"
}
func (r CustomRule) Description() string {
return "This is a custom rule"
}
func (r CustomRule) Check(ctx *core.Context) ([]core.Issue, error) {
// 实现你的自定义检查逻辑
return nil, nil
}
func main() {
analyzer := analyzer.NewAnalyzer()
analyzer.RegisterRule(CustomRule{})
// 运行分析器
results, err := analyzer.Analyze("./...")
if err != nil {
panic(err)
}
// 处理结果
for _, issue := range results.Issues {
println(issue.String())
}
}
忽略特定问题
可以在项目中创建 .arch-go-ignore
文件来忽略特定的架构问题:
# .arch-go-ignore
internal/presentation/controller/* no-domain-imports
internal/infrastructure/repository/* interface-implementation
常见问题解决方案
-
循环依赖问题:
- 引入接口进行解耦
- 创建新的中间层或共享内核
-
违反层依赖规则:
- 检查是否错误地放置了代码
- 考虑使用依赖倒置原则
-
接口缺少实现:
- 检查是否遗漏了实现
- 确认是否真的需要该接口
最佳实践
- 在项目早期引入 arch-go,确保架构一致性
- 将 arch-go 检查作为 CI 流程的一部分
- 定期审查架构规则,随着项目演进调整配置
- 结合其他工具如 go-callvis 进行更全面的架构分析
arch-go 是一个强大的工具,可以帮助团队维护良好的代码架构,特别是在大型项目中,它能有效防止架构腐化,保持代码组织的清晰和可维护性。