From 75e4a476869eb6ed02ddc7b0eebc2673c802dc66 Mon Sep 17 00:00:00 2001 From: Lucas Bajolet Date: Mon, 6 May 2024 15:38:34 -0400 Subject: [PATCH] test: add generic gadget for testing When writing tests, one may need to write a one-off checker for a packer command that ran, without having to completely implement the Checker interface. This commit introduces a generic CustomChecker implementation (i.e. a function) that can be one-off implemented by developers if their test doesn't fit the existing gadgets, and the need is not generic/reusable enough to justify introducing a new gadget for other users. --- test/gadgets_test.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/test/gadgets_test.go b/test/gadgets_test.go index 9ad4819d8..03ee4c35e 100644 --- a/test/gadgets_test.go +++ b/test/gadgets_test.go @@ -132,3 +132,20 @@ func (_ PanicCheck) Check(stdout, stderr string, _ error) error { } return nil } + +// CustomCheck is meant to be a one-off checker with a user-provided function. +// +// Use this if none of the existing checkers match your use case, and it is not +// reusable/generic enough for use in other tests. +type CustomCheck struct { + name string + checkFunc func(stdout, stderr string, err error) error +} + +func (c CustomCheck) Check(stdout, stderr string, err error) error { + return c.checkFunc(stdout, stderr, err) +} + +func (c CustomCheck) Name() string { + return fmt.Sprintf("custom check - %s", c.name) +}