From 44760767d105ba7b18c032e4b3ec48e6687768ec Mon Sep 17 00:00:00 2001 From: Brandon Roberts Date: Wed, 28 Aug 2024 14:09:53 -0500 Subject: [PATCH 1/3] fix: escape non-standard characters in the filename path Closes #100 --- cmd/generate/codeowners/output.go | 15 ++++++++++++-- cmd/generate/codeowners/output_test.go | 27 ++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 cmd/generate/codeowners/output_test.go diff --git a/cmd/generate/codeowners/output.go b/cmd/generate/codeowners/output.go index 921ccac..ed131c7 100644 --- a/cmd/generate/codeowners/output.go +++ b/cmd/generate/codeowners/output.go @@ -5,6 +5,7 @@ import ( "os" "sort" "strings" + "regexp" "github.com/open-sauced/pizza-cli/pkg/config" ) @@ -58,13 +59,13 @@ func writeGitHubCodeownersChunk(authorStats AuthorStats, config *config.Spec, fi } if len(topContributors) > 0 { - _, err := fmt.Fprintf(file, "%s @%s\n", srcFilename, strings.Join(resultSlice, " @")) + _, err := fmt.Fprintf(file, "%s @%s\n", cleanFilename(srcFilename), strings.Join(resultSlice, " @")) if err != nil { return nil, fmt.Errorf("error writing to %s file: %w", outputPath, err) } } else { // no code owners to attribute to file - _, err := fmt.Fprintf(file, "%s\n", srcFilename) + _, err := fmt.Fprintf(file, "%s\n", cleanFilename(srcFilename)) if err != nil { return nil, fmt.Errorf("error writing to %s file: %w", outputPath, err) } @@ -116,3 +117,13 @@ func getTopContributorAttributions(authorStats AuthorStats, n int, config *confi return topContributors } + +func cleanFilename(filename string) string { + // Split the filename in case its rename, see https://github.com/open-sauced/pizza-cli/issues/101 + parsedFilename := strings.Split(filename, " ")[0] + // Replace anything that is not a word, period, single quote, dash, space, forward slash, or backslash with an escaped version + re := regexp.MustCompile(`([^\w\.\'\-\s\/\\])`) + escapedFilename := re.ReplaceAllString(parsedFilename, "\\$0") + + return escapedFilename +} diff --git a/cmd/generate/codeowners/output_test.go b/cmd/generate/codeowners/output_test.go new file mode 100644 index 0000000..305771e --- /dev/null +++ b/cmd/generate/codeowners/output_test.go @@ -0,0 +1,27 @@ +package codeowners + +import ( + "testing" +) + +func TestCleanFilename(testRunner *testing.T) { + var tests = []struct { + name string + input string + expected string + }{ + {"path/to/(home).go", "path/to/(home).go", `path/to/\(home\).go`}, + {"path/to/[home].go", "path/to/[home].go", `path/to/\[home\].go`}, + {"path/to/+page.go", "path/to/+page.go", `path/to/\+page.go`}, + {"path/to/go-home.go", "path/to/go-home.go", `path/to/go-home.go`}, + } + + for _, testItem := range tests { + testRunner.Run(testItem.name, func(tester *testing.T) { + ans := cleanFilename(testItem.input) + if ans != testItem.expected { + tester.Errorf("got %s, expected %s", ans, testItem.expected) + } + }) + } +} From 9c640b8d90b2b92f48c0cd56f6155dc4afaf6829 Mon Sep 17 00:00:00 2001 From: Brandon Roberts Date: Wed, 28 Aug 2024 15:07:27 -0500 Subject: [PATCH 2/3] chore: fix formatting --- cmd/generate/codeowners/output.go | 2 +- cmd/generate/codeowners/output_test.go | 18 ++++++++---------- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/cmd/generate/codeowners/output.go b/cmd/generate/codeowners/output.go index ed131c7..6e58b71 100644 --- a/cmd/generate/codeowners/output.go +++ b/cmd/generate/codeowners/output.go @@ -3,9 +3,9 @@ package codeowners import ( "fmt" "os" + "regexp" "sort" "strings" - "regexp" "github.com/open-sauced/pizza-cli/pkg/config" ) diff --git a/cmd/generate/codeowners/output_test.go b/cmd/generate/codeowners/output_test.go index 305771e..462de3f 100644 --- a/cmd/generate/codeowners/output_test.go +++ b/cmd/generate/codeowners/output_test.go @@ -1,19 +1,17 @@ package codeowners -import ( - "testing" -) +import "testing" func TestCleanFilename(testRunner *testing.T) { var tests = []struct { - name string - input string - expected string + name string + input string + expected string }{ - {"path/to/(home).go", "path/to/(home).go", `path/to/\(home\).go`}, - {"path/to/[home].go", "path/to/[home].go", `path/to/\[home\].go`}, - {"path/to/+page.go", "path/to/+page.go", `path/to/\+page.go`}, - {"path/to/go-home.go", "path/to/go-home.go", `path/to/go-home.go`}, + {"path/to/(home).go", "path/to/(home).go", `path/to/\(home\).go`}, + {"path/to/[home].go", "path/to/[home].go", `path/to/\[home\].go`}, + {"path/to/+page.go", "path/to/+page.go", `path/to/\+page.go`}, + {"path/to/go-home.go", "path/to/go-home.go", `path/to/go-home.go`}, } for _, testItem := range tests { From da0e1727dbb0dc3ccdb15e13f5a9b7a351379399 Mon Sep 17 00:00:00 2001 From: Brandon Roberts Date: Wed, 28 Aug 2024 15:11:29 -0500 Subject: [PATCH 3/3] chore: add more test cases --- cmd/generate/codeowners/output_test.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/cmd/generate/codeowners/output_test.go b/cmd/generate/codeowners/output_test.go index 462de3f..8703665 100644 --- a/cmd/generate/codeowners/output_test.go +++ b/cmd/generate/codeowners/output_test.go @@ -12,6 +12,10 @@ func TestCleanFilename(testRunner *testing.T) { {"path/to/[home].go", "path/to/[home].go", `path/to/\[home\].go`}, {"path/to/+page.go", "path/to/+page.go", `path/to/\+page.go`}, {"path/to/go-home.go", "path/to/go-home.go", `path/to/go-home.go`}, + {`path\to\(home).go`, `path\to\(home).go`, `path\to\\(home\).go`}, + {`path\to\[home].go`, `path\to\[home].go`, `path\to\\[home\].go`}, + {`path\to\+page.go`, `path\to\+page.go`, `path\to\\+page.go`}, + {`path\to\go-home.go`, `path\to\go-home.go`, `path\to\go-home.go`}, } for _, testItem := range tests {