forked from mtusi2026/mtusi2026lab3
126 lines
4.1 KiB
Go
126 lines
4.1 KiB
Go
// Package quality checks source-level conventions required by the lab implementation.
|
|
package quality
|
|
|
|
import (
|
|
"go/ast"
|
|
"go/parser"
|
|
"go/token"
|
|
"io/fs"
|
|
"path/filepath"
|
|
"strconv"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// TestExportedDocumentation checks functions, types, constants, fields and interface methods.
|
|
func TestExportedDocumentation(t *testing.T) {
|
|
visitProductionFiles(t, func(path string, file *ast.File) {
|
|
ast.Inspect(file, func(node ast.Node) bool {
|
|
switch declaration := node.(type) {
|
|
case *ast.FuncDecl:
|
|
if declaration.Doc == nil || !strings.HasPrefix(strings.TrimSpace(declaration.Doc.Text()), declaration.Name.Name+" ") {
|
|
t.Errorf("%s: function %s needs a prefixed doc comment", path, declaration.Name.Name)
|
|
}
|
|
case *ast.GenDecl:
|
|
for _, spec := range declaration.Specs {
|
|
switch spec := spec.(type) {
|
|
case *ast.TypeSpec:
|
|
doc := spec.Doc
|
|
if doc == nil {
|
|
doc = declaration.Doc
|
|
}
|
|
requireDoc(t, path, spec.Name, doc)
|
|
case *ast.ValueSpec:
|
|
doc := spec.Doc
|
|
if doc == nil {
|
|
doc = declaration.Doc
|
|
}
|
|
for _, name := range spec.Names {
|
|
requireDoc(t, path, name, doc)
|
|
}
|
|
}
|
|
}
|
|
case *ast.StructType:
|
|
for _, field := range declaration.Fields.List {
|
|
for _, name := range field.Names {
|
|
requireDoc(t, path, name, field.Doc)
|
|
}
|
|
}
|
|
case *ast.InterfaceType:
|
|
for _, method := range declaration.Methods.List {
|
|
for _, name := range method.Names {
|
|
requireDoc(t, path, name, method.Doc)
|
|
}
|
|
}
|
|
}
|
|
return true
|
|
})
|
|
})
|
|
}
|
|
|
|
// requireDoc checks that an exported declaration has a correctly prefixed doc comment.
|
|
func requireDoc(t *testing.T, path string, name *ast.Ident, doc *ast.CommentGroup) {
|
|
t.Helper()
|
|
if !name.IsExported() {
|
|
return
|
|
}
|
|
if doc == nil || !strings.HasPrefix(strings.TrimSpace(doc.Text()), name.Name+" ") {
|
|
t.Errorf("%s: %s needs a Go doc comment starting with its name", path, name.Name)
|
|
}
|
|
}
|
|
|
|
// TestDependencyDirection prevents concrete infrastructure leaking into higher layers.
|
|
func TestDependencyDirection(t *testing.T) {
|
|
visitProductionFiles(t, func(path string, file *ast.File) {
|
|
normalized := filepath.ToSlash(path)
|
|
for _, dependency := range file.Imports {
|
|
imported, err := strconv.Unquote(dependency.Path.Value)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var forbidden []string
|
|
switch {
|
|
case strings.Contains(normalized, "internal/service/"):
|
|
forbidden = []string{"net/http", "github.com/gofiber/", "/internal/controller", "/internal/store/memory", "/internal/store/postgres", "/internal/valkey", "/internal/config", "go.lab260.ru/fielderr"}
|
|
case strings.Contains(normalized, "internal/admission/"), strings.Contains(normalized, "internal/strictjson/"):
|
|
forbidden = []string{"net/http", "github.com/gofiber/", "/internal/"}
|
|
case strings.Contains(normalized, "internal/cursor/"):
|
|
forbidden = []string{"net/http", "github.com/gofiber/", "/internal/service", "/internal/store", "/internal/controller", "/internal/model"}
|
|
case strings.Contains(normalized, "internal/store/"):
|
|
forbidden = []string{"net/http", "github.com/gofiber/", "/internal/controller", "/internal/service"}
|
|
case strings.Contains(normalized, "internal/controller/"):
|
|
forbidden = []string{"/internal/service/production", "/internal/store"}
|
|
}
|
|
for _, fragment := range forbidden {
|
|
if strings.Contains(imported, fragment) {
|
|
t.Errorf("%s: forbidden dependency %s", path, imported)
|
|
}
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
// visitProductionFiles parses application Go files and visits their syntax trees.
|
|
func visitProductionFiles(t *testing.T, visit func(string, *ast.File)) {
|
|
t.Helper()
|
|
for _, root := range []string{"../cmd", "../internal"} {
|
|
err := filepath.WalkDir(root, func(path string, entry fs.DirEntry, err error) error {
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if entry.IsDir() || !strings.HasSuffix(path, ".go") || strings.HasSuffix(path, "_test.go") {
|
|
return nil
|
|
}
|
|
file, err := parser.ParseFile(token.NewFileSet(), path, nil, parser.ParseComments)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
visit(path, file)
|
|
return nil
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
}
|