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

64 lines
1.5 KiB
Go

package archtest
import (
"io/fs"
"os"
"path/filepath"
"strings"
"testing"
)
// externalDriverIDs are guest drivers that live in separate plugin
// repositories. The host reaches them only through the generic driver
// contract, so no host source file may name one.
var externalDriverIDs = []string{
"reims.driver.wsl",
"reims.driver.hyperv-linux",
"reims.driver.hyperv-windows",
"reims.driver.sandbox",
"reims.driver.avd",
}
func TestHostCodeNamesNoExternalDriver(t *testing.T) {
root, err := filepath.Abs(filepath.Join("..", ".."))
if err != nil {
t.Fatal(err)
}
skipDirs := map[string]bool{".git": true, ".debug": true, "build": true, "frontend": true, "node_modules": true}
scanned := 0
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/archtest" {
return filepath.SkipDir
}
return nil
}
if !strings.HasSuffix(path, ".go") || strings.HasSuffix(path, "_test.go") {
return nil
}
raw, err := os.ReadFile(path)
if err != nil {
return err
}
scanned++
text := string(raw)
for _, id := range externalDriverIDs {
if strings.Contains(text, id) {
t.Errorf("%s names the external driver %q: host code must stay driver-agnostic", rel, id)
}
}
return nil
})
if err != nil {
t.Fatal(err)
}
if scanned < 50 {
t.Fatalf("scanned only %d Go files: the walk no longer finds the host sources", scanned)
}
}