Move Notarius path and executable resolution into its adapter

This commit is contained in:
2026-12-02 21:37:50 +00:00
parent 106036cf5b
commit d2f8b21c97
10 changed files with 106 additions and 58 deletions

View File

@@ -0,0 +1,37 @@
package notarius
import (
"fmt"
"os/exec"
"path/filepath"
"regexp"
"strings"
)
var unsafeLaneCharacters = regexp.MustCompile(`[^a-zA-Z0-9._-]`)
// LaneRelativePath maps a native lane identifier to its bundle-relative JSON path.
func LaneRelativePath(laneID string) (string, error) {
name := unsafeLaneCharacters.ReplaceAllString(strings.TrimSpace(laneID), "_")
for strings.Contains(name, "..") {
name = strings.ReplaceAll(name, "..", "__")
}
name = strings.Trim(name, "._")
if name == "" {
return "", fmt.Errorf("notarius lane ID %q cannot produce a filename", laneID)
}
return "lanes/" + name + ".json", nil
}
// ResolveExecutable resolves an explicit binary or PATH entry to an absolute path.
func ResolveExecutable(value string) (string, error) {
resolved, err := exec.LookPath(strings.TrimSpace(value))
if err != nil {
return "", err
}
resolved, err = filepath.Abs(strings.TrimSpace(resolved))
if err != nil {
return "", err
}
return filepath.Clean(resolved), nil
}

View File

@@ -0,0 +1,38 @@
package notarius
import (
"os"
"path/filepath"
"testing"
)
func TestLaneRelativePath(t *testing.T) {
for _, test := range []struct{ id, want string }{
{"npc-registry", "lanes/npc-registry.json"},
{" a/b..c ", "lanes/a_b__c.json"},
{"._...", ""},
} {
got, err := LaneRelativePath(test.id)
if got != test.want || (err != nil) != (test.want == "") {
t.Errorf("LaneRelativePath(%q) = %q, %v", test.id, got, err)
}
}
}
func TestResolveExecutableUsesExplicitPathAndSearchPath(t *testing.T) {
dir := t.TempDir()
binary := filepath.Join(dir, "notarius-test")
if err := os.WriteFile(binary, []byte("#!/bin/sh\nexit 0\n"), 0755); err != nil {
t.Fatal(err)
}
t.Setenv("PATH", dir)
for _, value := range []string{binary, "notarius-test"} {
got, err := ResolveExecutable(value)
if err != nil || got != binary {
t.Fatalf("resolve %q = %q, %v", value, got, err)
}
}
if _, err := ResolveExecutable("missing-notarius"); err == nil {
t.Fatal("missing binary was accepted")
}
}