Files
Reims-Go/internal/archtest/macos_boundary_test.go
T

238 lines
7.8 KiB
Go

package archtest
import (
"go/parser"
"go/token"
"io/fs"
"os"
"path/filepath"
"sort"
"strconv"
"strings"
"testing"
)
const macosImportPrefix = "github.com/Nwflower/reims-go/internal/macos"
// macosImporter is a package outside internal/macos that may still import
// macOS code: debt to pay down before macOS leaves this repository.
type macosImporter struct {
src, test bool // which of its files may import internal/macos
why string
}
// macosImporters is the ratchet. The test fails when a package not listed
// here starts importing internal/macos, and when a listed package (or its
// source or test files) stops, so the list can only shrink.
var macosImporters = map[string]macosImporter{
"internal/app": {test: true, why: "GUI composition root tests may exercise native macOS flows"},
"internal/datamigrate": {src: true, test: true, why: "data layout v3 invokes and verifies the macOS-owned state and ledger migration"},
"internal/plugin/builtin": {src: true, why: "host Guest Tools bridge still uses reims argument helpers; guest registration moved to internal/macos/guest"},
"internal/guesttools": {src: true, test: true, why: "mixed: host transport and clipboard next to the macOS guest agent image"},
"internal/plugin": {test: true, why: "sidecar manager test builds a reims-ctl stand-in"},
}
// macosSourceFiles is the file-scoped native macOS boundary. plugins_flow.go
// and cli/plugin.go are the composition files; every other entry is a native
// macOS page or flow mounted through that registration. New files may not be added.
var macosSourceFiles = map[string][]string{
"internal/app": {
"internal/app/create_flow.go",
"internal/app/extras_flow.go",
"internal/app/guesttools_flow.go",
"internal/app/install_flow.go",
"internal/app/install_guide.go",
"internal/app/installer_bindings.go",
"internal/app/installer_coordinator.go",
"internal/app/macos_client.go",
"internal/app/macos_detail.go",
"internal/app/macos_lifecycle_native.go",
"internal/app/macos_settings.go",
"internal/app/macos_window.go",
"internal/app/opencore_flow.go",
"internal/app/plugins_flow.go",
},
"internal/cli": {
"internal/cli/installer.go",
"internal/cli/macos_native.go",
"internal/cli/plugin.go",
},
}
func TestMacOSBoundary(t *testing.T) {
root, err := filepath.Abs(filepath.Join("..", ".."))
if err != nil {
t.Fatal(err)
}
type use struct{ src, test []string }
found := map[string]*use{}
skipDirs := map[string]bool{".git": true, ".debug": true, "build": true, "frontend": true, "node_modules": true}
err = filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
rel, _ := filepath.Rel(root, path)
rel = filepath.ToSlash(rel)
if d.IsDir() {
if skipDirs[d.Name()] || rel == "internal/macos" {
return filepath.SkipDir
}
return nil
}
if !strings.HasSuffix(path, ".go") {
return nil
}
f, err := parser.ParseFile(token.NewFileSet(), path, nil, parser.ImportsOnly)
if err != nil {
return err
}
for _, imp := range f.Imports {
p, _ := strconv.Unquote(imp.Path.Value)
if p != macosImportPrefix && !strings.HasPrefix(p, macosImportPrefix+"/") {
continue
}
dir := filepath.ToSlash(filepath.Dir(rel))
u := found[dir]
if u == nil {
u = &use{}
found[dir] = u
}
if strings.HasSuffix(path, "_test.go") {
u.test = append(u.test, rel)
} else {
u.src = append(u.src, rel)
}
break
}
return nil
})
if err != nil {
t.Fatal(err)
}
var problems []string
for dir, u := range found {
if files, scoped := macosSourceFiles[dir]; scoped {
allowed := make(map[string]bool, len(files))
for _, file := range files {
allowed[file] = true
}
for _, file := range u.src {
if !allowed[file] {
problems = append(problems, "host code imports internal/macos outside the native file list: "+file)
}
}
continue
}
allowed := macosImporters[dir]
if len(u.src) > 0 && !allowed.src {
problems = append(problems, "host code imports internal/macos (go through the driver layer or a host service instead): "+strings.Join(u.src, ", "))
}
if len(u.test) > 0 && !allowed.test {
problems = append(problems, "host tests import internal/macos: "+strings.Join(u.test, ", "))
}
}
for dir, files := range macosSourceFiles {
u := found[dir]
present := map[string]bool{}
if u != nil {
for _, file := range u.src {
present[file] = true
}
}
for _, file := range files {
if !present[file] {
problems = append(problems, file+" no longer imports internal/macos: remove it from macosSourceFiles")
}
}
}
for dir, allowed := range macosImporters {
u := found[dir]
if allowed.src && (u == nil || len(u.src) == 0) {
problems = append(problems, dir+" no longer imports internal/macos from source files: set src to false (or drop the entry) in macosImporters")
}
if allowed.test && (u == nil || len(u.test) == 0) {
problems = append(problems, dir+" no longer imports internal/macos from tests: set test to false (or drop the entry) in macosImporters")
}
}
sort.Strings(problems)
for _, p := range problems {
t.Error(p)
}
}
// The ratchet only means something if internal/macos exists where the test
// looks; guard against the walk silently finding nothing after a move.
func TestMacOSBoundaryRootExists(t *testing.T) {
if st, err := os.Stat(filepath.Join("..", "macos")); err != nil || !st.IsDir() {
t.Fatalf("internal/macos not found next to internal/archtest: %v", err)
}
}
// macosHostImports is the reverse ratchet: dependencies of internal/macos on
// private host packages. External repositories cannot import these, so the
// list must shrink to zero before macOS becomes a separate Go module.
var macosHostImports = map[string]string{
"config": "configuration paths and persisted host settings",
"downloader": "pluggable download provider contract not yet moved to pkg",
"driver": "public driver contracts not yet moved to pkg",
"filelock": "shared host file locking",
"host": "host inspection and process services",
"i18n": "host localization and coded errors",
"netproxy": "host HTTP proxy resolution",
"plugin": "plugin manifests, hooks and ledger contracts",
"plugin/builtin": "built-in callback wiring during the transition",
"qmp": "host QMP helpers",
"runtime": "host runtime kinds and child process environment",
"version": "host application version",
"vmstart": "mixed host/macOS start preparation pending step 2",
}
func TestMacOSDoesNotGainPrivateHostDependencies(t *testing.T) {
root, err := filepath.Abs(filepath.Join("..", "macos"))
if err != nil {
t.Fatal(err)
}
found := map[string][]string{}
err = filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() || !strings.HasSuffix(path, ".go") {
return nil
}
f, err := parser.ParseFile(token.NewFileSet(), path, nil, parser.ImportsOnly)
if err != nil {
return err
}
for _, imp := range f.Imports {
p, _ := strconv.Unquote(imp.Path.Value)
const prefix = "github.com/Nwflower/reims-go/internal/"
if !strings.HasPrefix(p, prefix) {
continue
}
name := strings.TrimPrefix(p, prefix)
if strings.HasPrefix(name, "macos") {
continue
}
rel, _ := filepath.Rel(root, path)
found[name] = append(found[name], filepath.ToSlash(rel))
}
return nil
})
if err != nil {
t.Fatal(err)
}
for name, files := range found {
if _, ok := macosHostImports[name]; !ok {
t.Errorf("internal/macos gained private host dependency internal/%s (%v)", name, files)
}
}
for name := range macosHostImports {
if len(found[name]) == 0 {
t.Errorf("internal/macos no longer imports internal/%s; remove it from macosHostImports", name)
}
}
}