From 2d114a044a8dcc4910ee39503a5f624fd5c25d01 Mon Sep 17 00:00:00 2001 From: Lucas Bajolet Date: Fri, 10 May 2024 14:44:01 -0400 Subject: [PATCH] packer_test: add functions for managing pipechecks Instead of manually creating and populating a PipeChecker, we add functions to do this. --- packer_test/pipe_checker_test.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/packer_test/pipe_checker_test.go b/packer_test/pipe_checker_test.go index b34aa896b..2e395bff0 100644 --- a/packer_test/pipe_checker_test.go +++ b/packer_test/pipe_checker_test.go @@ -167,6 +167,17 @@ func IntCompare(op op, value int) Tester { }) } +// MkPipeCheck builds a new named PipeChecker +// +// Before it can be used, it will need a tester to be set, but this +// function is meant to make initialisation simpler. +func MkPipeCheck(name string, p ...Pipe) *PipeChecker { + return &PipeChecker{ + name: name, + pipers: p, + } +} + // PipeChecker is a kind of checker that essentially lets users write mini // gadgets that pipe inputs together, and compose those to end as a true/false // statement, which translates to an error. @@ -186,10 +197,29 @@ type PipeChecker struct { check Tester } +// SetTester sets the tester to use for a pipe checker +// +// This is required, otherwise running the pipe checker will fail +func (pc *PipeChecker) SetTester(t Tester) *PipeChecker { + pc.check = t + return pc +} + +// SetStream changes the stream(s) on which the PipeChecker will perform +// +// Defaults to BothStreams, i.e. stdout and stderr +func (pc *PipeChecker) SetStream(s Stream) *PipeChecker { + pc.stream = s + return pc +} + func (pc PipeChecker) Check(stdout, stderr string, _ error) error { if len(pc.pipers) == 0 { return fmt.Errorf("%s - empty pipeline", pc.Name()) } + if pc.check == nil { + return fmt.Errorf("%s - missing tester", pc.Name()) + } var pipeStr string switch pc.stream {