aboutsummaryrefslogtreecommitdiff
path: root/internal/behavior
diff options
context:
space:
mode:
Diffstat (limited to 'internal/behavior')
-rw-r--r--internal/behavior/store.go27
-rw-r--r--internal/behavior/yml_test.go29
2 files changed, 52 insertions, 4 deletions
diff --git a/internal/behavior/store.go b/internal/behavior/store.go
index f2e9350..d31fe84 100644
--- a/internal/behavior/store.go
+++ b/internal/behavior/store.go
@@ -84,7 +84,11 @@ func WalkYAMLDir(dir string, fn func(path, id string, data []byte) error) error
}
return err
}
- if d.IsDir() || !strings.HasSuffix(d.Name(), ".yaml") {
+ if d.IsDir() {
+ return nil
+ }
+ id, ok := yamlID(d.Name())
+ if !ok {
return nil
}
if excludedContentPath(dir, path) {
@@ -97,11 +101,23 @@ func WalkYAMLDir(dir string, fn func(path, id string, data []byte) error) error
}
return readErr
}
- id := strings.TrimSuffix(d.Name(), ".yaml")
return fn(path, id, data)
})
}
+// yamlID returns the id (filename without extension) for a .yaml or .yml file.
+// The runtime trigger loader accepts .yml, so validation discovery must too,
+// otherwise such a file loads and fires while every check ignores it.
+func yamlID(name string) (string, bool) {
+ if strings.HasSuffix(name, ".yaml") {
+ return strings.TrimSuffix(name, ".yaml"), true
+ }
+ if strings.HasSuffix(name, ".yml") {
+ return strings.TrimSuffix(name, ".yml"), true
+ }
+ return "", false
+}
+
type Duplicate struct {
ID string
Paths []string
@@ -110,13 +126,16 @@ type Duplicate struct {
func CheckDuplicateIDs(dir string) []Duplicate {
seen := make(map[string][]string)
filepath.WalkDir(dir, func(path string, d os.DirEntry, err error) error {
- if err != nil || d.IsDir() || !strings.HasSuffix(d.Name(), ".yaml") {
+ if err != nil || d.IsDir() {
+ return nil
+ }
+ id, ok := yamlID(d.Name())
+ if !ok {
return nil
}
if excludedContentPath(dir, path) {
return nil
}
- id := strings.TrimSuffix(d.Name(), ".yaml")
seen[id] = append(seen[id], path)
return nil
})
diff --git a/internal/behavior/yml_test.go b/internal/behavior/yml_test.go
new file mode 100644
index 0000000..9452e9e
--- /dev/null
+++ b/internal/behavior/yml_test.go
@@ -0,0 +1,29 @@
+package behavior
+
+import (
+ "os"
+ "path/filepath"
+ "testing"
+)
+
+// Regression: WalkYAMLDir only matched .yaml, so a .yml trigger file loaded
+// and fired at runtime while every validation pass ignored it.
+func TestWalkYAMLDirIncludesYml(t *testing.T) {
+ dir := t.TempDir()
+ if err := os.WriteFile(filepath.Join(dir, "a.yaml"), []byte("x: 1"), 0644); err != nil {
+ t.Fatal(err)
+ }
+ if err := os.WriteFile(filepath.Join(dir, "b.yml"), []byte("x: 2"), 0644); err != nil {
+ t.Fatal(err)
+ }
+ ids := map[string]bool{}
+ if err := WalkYAMLDir(dir, func(path, id string, data []byte) error {
+ ids[id] = true
+ return nil
+ }); err != nil {
+ t.Fatal(err)
+ }
+ if !ids["a"] || !ids["b"] {
+ t.Fatalf("WalkYAMLDir ids = %v, want both a and b", ids)
+ }
+}