Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update regex patterns to support multi-line mode in CLI and add tests. #82

Merged
merged 1 commit into from
Nov 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion internal/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,9 @@ func compileRegexps(rawPatterns []string, ignoreCase bool) ([]*regexp.Regexp, er
regexps := make([]*regexp.Regexp, 0, len(rawPatterns))
for _, pattern := range rawPatterns {
if ignoreCase {
pattern = "(?i)" + pattern
pattern = "(?im)" + pattern
} else {
pattern = "(?m)" + pattern
}
re, err := regexp.Compile(pattern)
if err != nil {
Expand Down
65 changes: 65 additions & 0 deletions internal/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -877,3 +877,68 @@ func TestRun_ExtractWithFail(t *testing.T) {
})
}
}

func TestRun_MultiLineMode(t *testing.T) {
tests := map[string]struct {
args []string
input string
expected string
}{
// -exclude
"exclude empty lines": {
args: []string{"purl", "-exclude", "^\n$"},
input: "line1\n\nline2\nline3\n\n",
expected: "line1\nline2\nline3\n",
},
"exclude lines with only spaces": {
args: []string{"purl", "-exclude", "^\\s+$"},
input: "line1\n \nline2\nline3\n\t\n",
expected: "line1\nline2\nline3\n",
},
"exclude lines starting with #": {
args: []string{"purl", "-exclude", "^#"},
input: "#comment1\nline1\n#comment2\nline2\n",
expected: "line1\nline2\n",
},
// -filter
"filter lines starting with specific word": {
args: []string{"purl", "-filter", "^START"},
input: "START line1\nline2\nSTART line3\nline4\n",
expected: "START line1\nSTART line3\n",
},
"filter lines containing word": {
args: []string{"purl", "-filter", "word"},
input: "this is a word\nno match\nanother word here\n",
expected: "this is a word\nanother word here\n",
},
// -replace
"replace specific lines": {
args: []string{"purl", "-replace", "@^line1@REPLACED@"},
input: "line1\nline2\nline1 again\n",
expected: "REPLACED\nline2\nREPLACED again\n",
},
"replace word in multiple lines": {
args: []string{"purl", "-replace", "@line@LINE@"},
input: "line1\nline2\nnot this\nline3\n",
expected: "LINE1\nLINE2\nnot this\nLINE3\n",
},
}

for name, test := range tests {
test := test
t.Run(name, func(t *testing.T) {
t.Parallel()
outStream, errStream, inputStream := new(bytes.Buffer), new(bytes.Buffer), new(bytes.Buffer)
cl := cli.NewCLI(outStream, errStream, inputStream, false, false)
inputStream.WriteString(test.input)

if got, expected := cl.Run(test.args), 0; got != expected {
t.Fatalf("Expected exit code %d, but got %d; error: %q", expected, got, errStream.String())
}

if outStream.String() != test.expected {
t.Errorf("Output=%q, want %q; error: %q", outStream.String(), test.expected, errStream.String())
}
})
}
}