check-safety/plugin_imports_test.go
Alex Dunmow cd88c808b0 initial: standalone check-safety module hoisted from CMS
Static safety/lint runner for the BlockNinja codebase. ~25 invariant
checks across Go and frontend sources. Was at git.dev.alexdunmow.com:block/ninja
in backend/cmd/check-safety/ until the 2026-06-06 consolidation moved
the BlockNinja repos under a shared ~/src/blockninja/ parent.

This repo is the standalone extraction:
- Own go.mod (git.dev.alexdunmow.com/block/check-safety, go 1.26.4)
- Vendored internal/{helpers,theme} from CMS (Go's internal/ rule
  blocks cross-module imports; vendoring is the workaround)
- CLI contract unchanged: `check-safety <target-dir> [--flags]`
- CMS Makefile shells into ../check-safety for safety-check /
  install-safety-checker targets

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-06-06 13:04:02 +08:00

77 lines
2.4 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/core/plugin"
"git.dev.alexdunmow.com/block/ninja/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/ninja/internal/helpers" {
t.Fatalf("importPath = %q, want forbidden BlockNinja import", violations[0].importPath)
}
}
func TestCheckStandalonePluginImportsAllowsCoreSDKImports(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) != 0 {
t.Fatalf("checkStandalonePluginImports() returned %d violations, want 0: %#v", len(violations), violations)
}
}
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/ninja/internal/templates"
)
templ Page() {
<div></div>
}
`, 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/ninja/internal/templates" {
t.Fatalf("importPath = %q, want forbidden BlockNinja templ import", violations[0].importPath)
}
if violations[0].line != 5 {
t.Fatalf("line = %d, want 5", violations[0].line)
}
}