Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions .github/workflows/fitness.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: Fitness Functions

on:
pull_request:

jobs:
fitness:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- uses: actions/setup-go@v5
with:
go-version-file: go.mod

- name: Run fitness tests
run: go test ./fitness/...
39 changes: 39 additions & 0 deletions fitness/dependency_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package fitness

import (
"encoding/json"
"os/exec"
"strings"
"testing"
)

func TestDependencyDirection(t *testing.T) {
hierarchy := []string{
"listener",
"proxy",
"router",
"backend",
}

for i, pkg := range hierarchy {
imports := getImports("load-balancer/" + pkg)
for _, imp := range imports {
for j := 0; j < i; j++ {
if strings.Contains(imp, "load-balancer/"+hierarchy[j]) {
t.Errorf(
"%s imports %s — violates downward dependency rule",
pkg, hierarchy[j],
)
}
}
}
}
}

func getImports(pkg string) []string {
cmd := exec.Command("go", "list", "-json", pkg)
out, _ := cmd.Output()
var info struct{ Imports []string }
json.Unmarshal(out, &info)
return info.Imports
}
66 changes: 66 additions & 0 deletions fitness/interface_size_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package fitness

import (
"go/ast"
"go/parser"
"go/token"
"os"
"path/filepath"
"strings"
"testing"
)

func TestInterfaceSizes(t *testing.T) {
maxMethods := 5
projectRoot := ".."

filepath.Walk(projectRoot, func(path string, info os.FileInfo, err error) error {
if err != nil {
return nil
}
if info.IsDir() {
name := info.Name()
if name == "vendor" || name == "fitness" || name == ".git" {
return filepath.SkipDir
}
return nil
}
if !strings.HasSuffix(path, ".go") {
return nil
}
if strings.HasSuffix(path, "_test.go") {
return nil
}

fset := token.NewFileSet()
file, err := parser.ParseFile(fset, path, nil, 0)
if err != nil {
return nil
}

for _, decl := range file.Decls {
gd, ok := decl.(*ast.GenDecl)
if !ok || gd.Tok != token.TYPE {
continue
}
for _, spec := range gd.Specs {
ts := spec.(*ast.TypeSpec)
iface, ok := ts.Type.(*ast.InterfaceType)
if !ok {
continue
}

methodCount := len(iface.Methods.List)
name := file.Name.Name + "." + ts.Name.Name

if methodCount > maxMethods {
t.Errorf(
"%s (%s) has %d methods (max %d)",
name, path, methodCount, maxMethods,
)
}
}
}
return nil
})
}
Loading