From 03899071606866cc1b421715d79469aaedd6c998 Mon Sep 17 00:00:00 2001 From: Christopher Kaster Date: Sun, 22 Sep 2024 17:43:39 +0200 Subject: [PATCH] fix bug that caused some unwanted matches --- pkg/tmpl/tmpl.go | 3 +++ pkg/tmpl/tmpl_test.go | 1 + 2 files changed, 4 insertions(+) diff --git a/pkg/tmpl/tmpl.go b/pkg/tmpl/tmpl.go index 6c3a9c4..d38073c 100644 --- a/pkg/tmpl/tmpl.go +++ b/pkg/tmpl/tmpl.go @@ -110,6 +110,9 @@ func FindMatchingTemplates(filePath string, templateFiles []string) []string { } func MatchesFilename(fileName, templateName string) bool { + // escape "." + templateName = strings.ReplaceAll(templateName, ".", "\\.") + pattern := fmt.Sprintf("^%s$", regexp.MustCompile(`\[([^\]]+)\]`).ReplaceAllString(templateName, `([^/]+)`)) matched, err := regexp.MatchString(pattern, fileName) diff --git a/pkg/tmpl/tmpl_test.go b/pkg/tmpl/tmpl_test.go index 32a074c..52c8671 100644 --- a/pkg/tmpl/tmpl_test.go +++ b/pkg/tmpl/tmpl_test.go @@ -21,6 +21,7 @@ func TestMatchAndParseFilename(t *testing.T) { {"fooBarbaz.ml", "[var1]Bar[var2].ml", true, map[string]string{"var1": "foo", "var2": "baz"}}, {"example.txt", "[name].txt", true, map[string]string{"name": "example"}}, {"example.txt", "[name].c", false, nil}, + {"patch", "[name].h", false, nil}, } for _, tc := range testCases {