From 5f9478287ca49f2ce6f6a6db72ea4c51aeb91e97 Mon Sep 17 00:00:00 2001 From: Max Leske Date: Tue, 3 Oct 2023 20:13:42 +0200 Subject: [PATCH] feat: enable empty string as suffix replacement The logic for parsing suffix replacements did not allow for the empty string. This commit remedies that by using `""` as a special literal to mean the empty string. --- regex/parser/include_except_builder.go | 2 +- regex/parser/include_except_test.go | 28 ++++++++++++++++++++++++++ regex/parser/include_test.go | 24 +++++++++++++++++++++- 3 files changed, 52 insertions(+), 2 deletions(-) diff --git a/regex/parser/include_except_builder.go b/regex/parser/include_except_builder.go index 0693f42..8efa4cb 100644 --- a/regex/parser/include_except_builder.go +++ b/regex/parser/include_except_builder.go @@ -70,7 +70,7 @@ func replaceSuffixes(inputLines *bytes.Buffer, suffixReplacements map[string]str for match, replacement := range suffixReplacements { var found bool entry, found = strings.CutSuffix(entry, match) - if found { + if found && replacement != `""` { entry += replacement } } diff --git a/regex/parser/include_except_test.go b/regex/parser/include_except_test.go index d809309..2ab2598 100644 --- a/regex/parser/include_except_test.go +++ b/regex/parser/include_except_test.go @@ -211,6 +211,34 @@ no suffix 2 s.Equal(expected, actual.String()) } +func (s *parserIncludeExceptTestSuite) TestIncludeExcept_SuffixReplacements_WithEmptyString() { + includePath := s.writeFile(`no suffix1 +suffix with@ +suffix with~ +no suffix 2`, + s.includeDir) + excludePath := s.writeFile("no suffix1", s.excludeDir) + assemblyPath := s.writeFile( + fmt.Sprintf( + "##!> include-except %s %s -- %s %s %s %s", includePath, excludePath, + "@", `""`, + "~", `""`), + s.assemblyDir) + + assemblyFile, err := os.Open(assemblyPath) + s.Require().NoError(err) + defer assemblyFile.Close() + + parser := NewParser(s.ctx, assemblyFile) + actual, _ := parser.Parse(false) + expected := `suffix with +suffix with +no suffix 2 +` + + s.Equal(expected, actual.String()) +} + func (s *parserIncludeExceptTestSuite) TestIncludeExcept_MultipleExcludes() { includePath := s.writeFile(`\s*include1 leave me alone`, s.includeDir) diff --git a/regex/parser/include_test.go b/regex/parser/include_test.go index 02bbfe2..5c22270 100644 --- a/regex/parser/include_test.go +++ b/regex/parser/include_test.go @@ -155,7 +155,7 @@ no suffix 2 s.Equal(expected.String(), actual.String()) } -func (s *parserIncludeTestSuite) TestParserInclude_SuffixReplacements_WithBacSpacing() { +func (s *parserIncludeTestSuite) TestParserInclude_SuffixReplacements_WithBadSpacing() { _, err := s.includeFile.WriteString(`no suffix1 suffix with@ suffix with~ @@ -176,3 +176,25 @@ no suffix 2 s.Equal(expected.String(), actual.String()) } + +func (s *parserIncludeTestSuite) TestParserInclude_SuffixReplacements_WithEmptyString() { + _, err := s.includeFile.WriteString(`no suffix1 +suffix with@ +suffix with~ +no suffix 2`) + s.Require().NoError(err, "writing temp include file failed") + + s.reader = strings.NewReader(fmt.Sprintf( + "##!> include %s -- %s %s %s %s", s.includeFile.Name(), + "@", `""`, + "~", `""`)) + parser := NewParser(s.ctx, s.reader) + actual, _ := parser.Parse(false) + expected := bytes.NewBufferString(`no suffix1 +suffix with +suffix with +no suffix 2 +`) + + s.Equal(expected.String(), actual.String()) +}