check-safety/plugin_imports_test.go
Alex Dunmow 0749c22d30 feat(2c): forbid all first-party imports in standalone plugins; fix --plugin-dir-only scans
Import boundary now covers the whole first-party tree, not just block/cms:
block/core and block/orchestrator imports are violations too, with an
explicit carve-out for the packages that deliberately stayed core after
the pluginsdk extraction (core/captcha, core/backup — calcomblock uses
captcha today). The .templ import scan gets the same rule.

defaultScanTargetDir: --plugin-dir with no positional target used to
leave targetDir at cwd, so the checker scanned its own repo and
self-reported failures while never scanning the plugin. It now targets
the first plugin root, matching the positional form.

Verified green across cms + all 11 v0.2.1 fleet repos (bidmasters still
fails the version anchor as expected — it pins v0.2.0 on a detached
HEAD, unrelated to these changes).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-07 21:48:01 +08:00

130 lines
4.3 KiB
Go

package main
import (
"path/filepath"
"testing"
)
func TestCheckStandalonePluginImportsFlagsBlockNinjaCMSImports(t *testing.T) {
root := t.TempDir()
writeTestFile(t, filepath.Join(root, "plugin.mod"), "[plugin]\nname = \"example\"\nversion = \"1.0.0\"\n", 0644)
writeTestFile(t, filepath.Join(root, "main.go"), `package example
import (
"git.dev.alexdunmow.com/block/pluginsdk/plugin"
"git.dev.alexdunmow.com/block/cms/internal/helpers"
)
func Example() {
_ = plugin.PluginRegistration{}
_ = helpers.Slugify("x")
}
`, 0644)
violations := checkStandalonePluginImports(root)
if len(violations) != 1 {
t.Fatalf("checkStandalonePluginImports() returned %d violations, want 1: %#v", len(violations), violations)
}
if violations[0].importPath != "git.dev.alexdunmow.com/block/cms/internal/helpers" {
t.Fatalf("importPath = %q, want forbidden BlockNinja import", violations[0].importPath)
}
}
func TestCheckStandalonePluginImportsFlagsCoreImports(t *testing.T) {
root := t.TempDir()
writeTestFile(t, filepath.Join(root, "plugin.mod"), "[plugin]\nname = \"example\"\nversion = \"1.0.0\"\n", 0644)
writeTestFile(t, filepath.Join(root, "main.go"), `package example
import "git.dev.alexdunmow.com/block/core/plugin"
func Example() {
_ = plugin.PluginRegistration{}
}
`, 0644)
violations := checkStandalonePluginImports(root)
if len(violations) != 1 {
t.Fatalf("checkStandalonePluginImports() returned %d violations, want 1: %#v", len(violations), violations)
}
if violations[0].importPath != "git.dev.alexdunmow.com/block/core/plugin" {
t.Fatalf("importPath = %q, want forbidden core import", violations[0].importPath)
}
}
func TestCheckStandalonePluginImportsAllowsSDKAndKeptCoreImports(t *testing.T) {
root := t.TempDir()
writeTestFile(t, filepath.Join(root, "plugin.mod"), "[plugin]\nname = \"example\"\nversion = \"1.0.0\"\n", 0644)
writeTestFile(t, filepath.Join(root, "main.go"), `package example
import (
"git.dev.alexdunmow.com/block/core/backup"
"git.dev.alexdunmow.com/block/core/captcha"
"git.dev.alexdunmow.com/block/pluginsdk/plugin"
"git.dev.alexdunmow.com/block/pluginsdk/render"
)
func Example() {
_ = plugin.PluginRegistration{}
_ = captcha.Config{}
_ = backup.Config{}
_ = render.BlockNoteToHTML
}
`, 0644)
violations := checkStandalonePluginImports(root)
if len(violations) != 0 {
t.Fatalf("checkStandalonePluginImports() returned %d violations, want 0: %#v", len(violations), violations)
}
}
func TestCheckStandalonePluginImportsFlagsOrchestratorImports(t *testing.T) {
root := t.TempDir()
writeTestFile(t, filepath.Join(root, "plugin.mod"), "[plugin]\nname = \"example\"\nversion = \"1.0.0\"\n", 0644)
writeTestFile(t, filepath.Join(root, "main.go"), `package example
import "git.dev.alexdunmow.com/block/orchestrator/backend/internal/services"
func Example() {
_ = services.InstanceService{}
}
`, 0644)
violations := checkStandalonePluginImports(root)
if len(violations) != 1 {
t.Fatalf("checkStandalonePluginImports() returned %d violations, want 1: %#v", len(violations), violations)
}
if violations[0].importPath != "git.dev.alexdunmow.com/block/orchestrator/backend/internal/services" {
t.Fatalf("importPath = %q, want forbidden orchestrator import", violations[0].importPath)
}
}
func TestCheckStandalonePluginImportsFlagsBlockNinjaCMSImportsInTemplFiles(t *testing.T) {
root := t.TempDir()
writeTestFile(t, filepath.Join(root, "plugin.mod"), "[plugin]\nname = \"example\"\nversion = \"1.0.0\"\n", 0644)
writeTestFile(t, filepath.Join(root, "page.templ"), `package example
import (
"git.dev.alexdunmow.com/block/core/templates/bn"
"git.dev.alexdunmow.com/block/cms/internal/templates"
)
templ Page() {
<div></div>
}
`, 0644)
violations := checkStandalonePluginImports(root)
if len(violations) != 2 {
t.Fatalf("checkStandalonePluginImports() returned %d violations, want 2: %#v", len(violations), violations)
}
if violations[0].importPath != "git.dev.alexdunmow.com/block/core/templates/bn" {
t.Fatalf("importPath[0] = %q, want forbidden core templ import", violations[0].importPath)
}
if violations[1].importPath != "git.dev.alexdunmow.com/block/cms/internal/templates" {
t.Fatalf("importPath[1] = %q, want forbidden BlockNinja templ import", violations[1].importPath)
}
if violations[1].line != 5 {
t.Fatalf("line = %d, want 5", violations[1].line)
}
}