mirror of
https://github.com/hashicorp/packer.git
synced 2026-06-09 00:32:09 -04:00
test: add inverse mode for Grep gadget
Since sometimes we want to check for matches, and sometimes we want to check for a lack of match, we add one more option for the Grep gadget: inverse. This essentially replicates `grep -v`, and will succeed only if the regex provided did NOT match on the requested streams.
This commit is contained in:
parent
ad4631fb67
commit
54a1d28525
1 changed files with 7 additions and 2 deletions
|
|
@ -1,6 +1,7 @@
|
|||
package test
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"regexp"
|
||||
|
|
@ -77,6 +78,7 @@ func (_ MustFail) Check(stdout, stderr string, err error) error {
|
|||
type Grep struct {
|
||||
streams Stream
|
||||
expect string
|
||||
inverse bool
|
||||
}
|
||||
|
||||
func (g Grep) Check(stdout, stderr string, err error) error {
|
||||
|
|
@ -98,8 +100,11 @@ func (g Grep) Check(stdout, stderr string, err error) error {
|
|||
found = found || re.MatchString(stream)
|
||||
}
|
||||
|
||||
if !found {
|
||||
return fmt.Errorf("streams %q did not match the expected regexp %q", g.streams, g.expect)
|
||||
if g.inverse && found {
|
||||
return errors.New("unexpectedly matched the regexp")
|
||||
}
|
||||
if !g.inverse && !found {
|
||||
return errors.New("did not match the regexp")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue