GeezerVison is showing github.com — feat(engine): add glob matching for operation names · majorcontext/keep@27ef218
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboard +15Lines changed: 15 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +// Package engine implements Keep's core policy evaluation. | | 2 | +package engine | | 3 | + | | 4 | +import "path" | | 5 | + | | 6 | +// GlobMatch returns true if the operation name matches the glob pattern. | | 7 | +// Supports * (any sequence) and ? (any single character). | | 8 | +// An empty pattern matches everything. | | 9 | +func GlobMatch(pattern, name string) bool { | | 10 | + if pattern == "" { | | 11 | + return true | | 12 | + } | | 13 | + matched, _ := path.Match(pattern, name) | | 14 | + return matched | | 15 | +} |
Collapse file Copy file name to clipboard +121Lines changed: 121 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +package engine | | 2 | + | | 3 | +import "testing" | | 4 | + | | 5 | +func TestGlob_Exact(t *testing.T) { | | 6 | + tests := []struct { | | 7 | + pattern string | | 8 | + name string | | 9 | + want bool | | 10 | + }{ | | 11 | + {"create_issue", "create_issue", true}, | | 12 | + {"create_issue", "delete_issue", false}, | | 13 | + } | | 14 | + for _, tt := range tests { | | 15 | + t.Run(tt.pattern+"/"+tt.name, func(t *testing.T) { | | 16 | + got := GlobMatch(tt.pattern, tt.name) | | 17 | + if got != tt.want { | | 18 | + t.Errorf("GlobMatch(%q, %q) = %v, want %v", tt.pattern, tt.name, got, tt.want) | | 19 | + } | | 20 | + }) | | 21 | + } | | 22 | +} | | 23 | + | | 24 | +func TestGlob_Star(t *testing.T) { | | 25 | + tests := []struct { | | 26 | + pattern string | | 27 | + name string | | 28 | + want bool | | 29 | + }{ | | 30 | + {"create_*", "create_issue", true}, | | 31 | + {"create_*", "create_comment", true}, | | 32 | + {"create_*", "delete_issue", false}, | | 33 | + } | | 34 | + for _, tt := range tests { | | 35 | + t.Run(tt.pattern+"/"+tt.name, func(t *testing.T) { | | 36 | + got := GlobMatch(tt.pattern, tt.name) | | 37 | + if got != tt.want { | | 38 | + t.Errorf("GlobMatch(%q, %q) = %v, want %v", tt.pattern, tt.name, got, tt.want) | | 39 | + } | | 40 | + }) | | 41 | + } | | 42 | +} | | 43 | + | | 44 | +func TestGlob_Question(t *testing.T) { | | 45 | + tests := []struct { | | 46 | + pattern string | | 47 | + name string | | 48 | + want bool | | 49 | + }{ | | 50 | + {"llm.tool_?", "llm.tool_a", true}, | | 51 | + {"llm.tool_?", "llm.tool_use", false}, | | 52 | + } | | 53 | + for _, tt := range tests { | | 54 | + t.Run(tt.pattern+"/"+tt.name, func(t *testing.T) { | | 55 | + got := GlobMatch(tt.pattern, tt.name) | | 56 | + if got != tt.want { | | 57 | + t.Errorf("GlobMatch(%q, %q) = %v, want %v", tt.pattern, tt.name, got, tt.want) | | 58 | + } | | 59 | + }) | | 60 | + } | | 61 | +} | | 62 | + | | 63 | +func TestGlob_StarAll(t *testing.T) { | | 64 | + tests := []struct { | | 65 | + pattern string | | 66 | + name string | | 67 | + want bool | | 68 | + }{ | | 69 | + {"*", "create_issue", true}, | | 70 | + {"*", "delete_comment", true}, | | 71 | + {"*", "llm.tool_use", true}, | | 72 | + } | | 73 | + for _, tt := range tests { | | 74 | + t.Run(tt.pattern+"/"+tt.name, func(t *testing.T) { | | 75 | + got := GlobMatch(tt.pattern, tt.name) | | 76 | + if got != tt.want { | | 77 | + t.Errorf("GlobMatch(%q, %q) = %v, want %v", tt.pattern, tt.name, got, tt.want) | | 78 | + } | | 79 | + }) | | 80 | + } | | 81 | +} | | 82 | + | | 83 | +func TestGlob_DotStar(t *testing.T) { | | 84 | + tests := []struct { | | 85 | + pattern string | | 86 | + name string | | 87 | + want bool | | 88 | + }{ | | 89 | + {"llm.*", "llm.request", true}, | | 90 | + {"llm.*", "llm.tool_use", true}, | | 91 | + {"llm.*", "create_issue", false}, | | 92 | + } | | 93 | + for _, tt := range tests { | | 94 | + t.Run(tt.pattern+"/"+tt.name, func(t *testing.T) { | | 95 | + got := GlobMatch(tt.pattern, tt.name) | | 96 | + if got != tt.want { | | 97 | + t.Errorf("GlobMatch(%q, %q) = %v, want %v", tt.pattern, tt.name, got, tt.want) | | 98 | + } | | 99 | + }) | | 100 | + } | | 101 | +} | | 102 | + | | 103 | +func TestGlob_Empty(t *testing.T) { | | 104 | + tests := []struct { | | 105 | + pattern string | | 106 | + name string | | 107 | + want bool | | 108 | + }{ | | 109 | + {"", "create_issue", true}, | | 110 | + {"", "llm.tool_use", true}, | | 111 | + {"", "anything", true}, | | 112 | + } | | 113 | + for _, tt := range tests { | | 114 | + t.Run(tt.pattern+"/"+tt.name, func(t *testing.T) { | | 115 | + got := GlobMatch(tt.pattern, tt.name) | | 116 | + if got != tt.want { | | 117 | + t.Errorf("GlobMatch(%q, %q) = %v, want %v", tt.pattern, tt.name, got, tt.want) | | 118 | + } | | 119 | + }) | | 120 | + } | | 121 | +} |
|