63 lines
1.7 KiB
Go
63 lines
1.7 KiB
Go
package chunkplan
|
|
|
|
import (
|
|
"go/parser"
|
|
"go/token"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strconv"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
const repositoryImportPrefix = "gitea.maximumdirect.net/eric/notarius/internal/"
|
|
|
|
func TestPlanStoreAndSourceImportBoundaries(t *testing.T) {
|
|
repositoryRoot := repositoryRoot(t)
|
|
for _, tc := range []struct {
|
|
name string
|
|
directory string
|
|
forbidden []string
|
|
}{
|
|
{name: "source is framework and module independent", directory: "internal/core/source", forbidden: []string{"framework/", "modules/"}},
|
|
{name: "plan store is module independent", directory: "internal/framework/chunkplan", forbidden: []string{"modules/"}},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
files, err := filepath.Glob(filepath.Join(repositoryRoot, tc.directory, "*.go"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, filename := range files {
|
|
if strings.HasSuffix(filename, "_test.go") {
|
|
continue
|
|
}
|
|
parsed, err := parser.ParseFile(token.NewFileSet(), filename, nil, parser.ImportsOnly)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, item := range parsed.Imports {
|
|
path, err := strconv.Unquote(item.Path.Value)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
path = strings.TrimPrefix(path, repositoryImportPrefix)
|
|
for _, prefix := range tc.forbidden {
|
|
if strings.HasPrefix(path, prefix) {
|
|
t.Fatalf("%s imports %q, forbidden by %s boundary", filepath.Base(filename), path, tc.name)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func repositoryRoot(t *testing.T) string {
|
|
t.Helper()
|
|
_, filename, _, ok := runtime.Caller(0)
|
|
if !ok {
|
|
t.Fatal("resolve test location")
|
|
}
|
|
return filepath.Clean(filepath.Join(filepath.Dir(filename), "..", "..", ".."))
|
|
}
|