From bd79b37aa7ffd8f0dccdfe44b620a80b43c63cd3 Mon Sep 17 00:00:00 2001 From: Lucas Bajolet Date: Mon, 6 May 2024 16:40:30 -0400 Subject: [PATCH] test: add PipeGrep to grep an input --- test/pipe_checker_test.go | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/test/pipe_checker_test.go b/test/pipe_checker_test.go index 6e63a845d..2a9608047 100644 --- a/test/pipe_checker_test.go +++ b/test/pipe_checker_test.go @@ -1,6 +1,10 @@ package test -import "fmt" +import ( + "fmt" + "regexp" + "strings" +) // Pipe is any command that allows piping two gadgets together // @@ -17,6 +21,16 @@ func (c CustomPipe) Process(input string) (string, error) { return c(input) } +// PipeGrep performs a grep on an input and returns the matches, one-per-line. +// +// The expression passed as parameter will be compiled to a POSIX extended regexp. +func PipeGrep(expression string) Pipe { + re := regexp.MustCompilePOSIX(expression) + return CustomPipe(func(input string) (string, error) { + return strings.Join(re.FindAllString(input, -1), "\n"), nil + }) +} + // Tester is the end of a pipe for testing purposes. // // Once multiple commands have been piped together in a pipeline, we can