oficina / mapa Public

Clone
git clone https://amadan.net/oficina/mapa

Plain git — no account needed to clone.

Download

Download this file

1package mapatest
2
3import (
4 "go/ast"
5 "go/parser"
6 "go/token"
7 "io/fs"
8 "os/exec"
9 "path/filepath"
10 "strconv"
11 "strings"
12 "testing"
13)
14
15// I8, egress (Notion import spec §5, D2). Mapa's own tree has no egress
16// exemption at all: no client, transport, request constructor, dialler
17// or resolver anywhere in internal/mapa or cmd. Only the notion module
18// dials; only internal/mapa's import_*.go files import it; notionmd,
19// the pure mapper, never does; and no production file imports the
20// fake. rastrillo's sign-in mail lives in rastrillo.
21//
22// Mutations (make mutate, first red 2026-11-17): an http.Get in a
23// handler; the module imported from notionmd.
24
25const (
26 notionModule = "amadan.net/oficina/notion.git"
27 notionFake = notionModule + "/notiontest"
28)
29
30// dials is, by package path, every name that dials, resolves, or builds
31// what dials; "*" is the whole package. Selectors are matched through
32// each file's own import names, so an alias does not hide one, and a
33// dot import of any of these packages is refused outright (Fable L12).
34var dials = map[string]map[string]bool{
35 "net/http": {"Client": true, "Transport": true, "Get": true,
36 "Post": true, "Head": true, "PostForm": true, "NewRequest": true,
37 "NewRequestWithContext": true, "DefaultClient": true,
38 "DefaultTransport": true},
39 "net": {"Dial": true, "DialTimeout": true, "DialTCP": true,
40 "DialUDP": true, "DialIP": true, "DialUnix": true, "Dialer": true,
41 "Resolver": true, "DefaultResolver": true, "Lookup*": true},
42 "crypto/tls": {"Dial": true, "DialWithDialer": true, "Dialer": true},
43 "net/smtp": {"*": true},
44 "os/exec": {"*": true},
45}
46
47// dialling is whether name from the package at path dials.
48func dialling(path, name string) bool {
49 names, ok := dials[path]
50 if !ok {
51 return false
52 }
53 return names["*"] || names[name] ||
54 (names["Lookup*"] && strings.HasPrefix(name, "Lookup"))
55}
56
57// productionFiles is every non-test Go file under the repository's
58// internal/mapa and cmd, relative to the repository.
59func productionFiles(t *testing.T) []string {
60 t.Helper()
61 var files []string
62 for _, root := range []string{"../mapa", "../../cmd"} {
63 err := filepath.WalkDir(root, func(path string, entry fs.DirEntry,
64 err error) error {
65 if err == nil && !entry.IsDir() &&
66 strings.HasSuffix(path, ".go") &&
67 !strings.HasSuffix(path, "_test.go") {
68 files = append(files, path)
69 }
70 return err
71 })
72 if err != nil {
73 t.Fatal(err)
74 }
75 }
76 return files
77}
78
79// platformEgress is Mapa's one other egress, by file and by name: Mapa
80// images' writes to CARLOS's own bucket, the platform's object store,
81// over a presigned URL (images spec §1). Images landed after the Notion
82// spec said no directory is exempt; these two files may build an
83// http.Client and a request, and nothing else that dials, and the
84// import rules hold in them as everywhere (Fable round 3, L6).
85var platformEgress = map[string]map[string]bool{
86 "config_images.go": {"net/http.Client": true},
87 "images_bucket.go": {"net/http.Client": true,
88 "net/http.NewRequestWithContext": true},
89}
90
91func TestI8MapaHasNoEgressOfItsOwn(t *testing.T) {
92 for _, path := range productionFiles(t) {
93 var allowed map[string]bool
94 if filepath.Dir(path) == "../mapa" {
95 allowed = platformEgress[filepath.Base(path)]
96 }
97 file, err := parser.ParseFile(token.NewFileSet(), path, nil, 0)
98 if err != nil {
99 t.Fatal(err)
100 }
101 names := importNames(t, path, file)
102 ast.Inspect(file, func(node ast.Node) bool {
103 selector, ok := node.(*ast.SelectorExpr)
104 if !ok {
105 return true
106 }
107 ident, ok := selector.X.(*ast.Ident)
108 name := ""
109 if ok {
110 name = names[ident.Name] + "." + selector.Sel.Name
111 }
112 if ok && !allowed[name] &&
113 dialling(names[ident.Name], selector.Sel.Name) {
114 t.Errorf("%s uses %s.%s: Mapa dials only through the "+
115 "notion module", path, names[ident.Name],
116 selector.Sel.Name)
117 }
118 return true
119 })
120 checkImports(t, path, file)
121 }
122}
123
124// importNames maps each name a file uses for an import to its path,
125// and refuses a dot import of a package that dials.
126func importNames(t *testing.T, path string,
127 file *ast.File) map[string]string {
128 names := map[string]string{}
129 for _, spec := range file.Imports {
130 imported, _ := strconv.Unquote(spec.Path.Value)
131 name := imported[strings.LastIndex(imported, "/")+1:]
132 if spec.Name != nil {
133 name = spec.Name.Name
134 }
135 if name == "." && dials[imported] != nil {
136 t.Errorf("%s dot-imports %s", path, imported)
137 }
138 names[name] = imported
139 }
140 return names
141}
142
143// checkImports holds one file's imports to the rule.
144func checkImports(t *testing.T, path string, file *ast.File) {
145 name := filepath.Base(path)
146 importer := filepath.Base(filepath.Dir(path)) == "mapa" &&
147 strings.HasPrefix(name, "import_")
148 for _, spec := range file.Imports {
149 imported, _ := strconv.Unquote(spec.Path.Value)
150 switch {
151 case strings.HasPrefix(imported, notionFake):
152 t.Errorf("%s imports the fake; only tests may", path)
153 case strings.HasPrefix(imported, notionModule) && !importer:
154 t.Errorf("%s imports the notion module; only "+
155 "internal/mapa/import_*.go may", path)
156 }
157 }
158}
159
160// The package half: of every package the binary links, only
161// internal/mapa imports the module.
162func TestI8OnlyTheImportLinksTheModule(t *testing.T) {
163 command := exec.Command("go", "list", "-deps", "-f",
164 `{{.ImportPath}} {{join .Imports " "}}`, "../../cmd/...")
165 output, err := command.CombinedOutput()
166 if err != nil {
167 t.Fatalf("go list: %v\n%s", err, output)
168 }
169 for _, line := range strings.Split(string(output), "\n") {
170 fields := strings.Fields(line)
171 if len(fields) < 2 || strings.HasPrefix(fields[0], notionModule) {
172 continue
173 }
174 for _, imported := range fields[1:] {
175 if strings.HasPrefix(imported, notionModule) &&
176 fields[0] != "amadan.net/oficina/mapa/internal/mapa" {
177 t.Errorf("%s imports %s", fields[0], imported)
178 }
179 }
180 }
181}
182