mirror of
https://github.com/hashicorp/packer.git
synced 2026-09-24 00:41:41 -04:00
backport of commit 045b7b7365
This commit is contained in:
@@ -75,7 +75,8 @@ func (pc *packerCommand) Assert(t *testing.T, checks ...Checker) {
|
||||
for _, check := range checks {
|
||||
checkErr := check.Check(stdout, stderr, err)
|
||||
if checkErr != nil {
|
||||
t.Errorf("check %q failed: %s", check.Name(), checkErr)
|
||||
checkerName := InferName(check)
|
||||
t.Errorf("check %q failed: %s", checkerName, checkErr)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+27
-7
@@ -2,6 +2,7 @@ package test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"regexp"
|
||||
"testing"
|
||||
)
|
||||
@@ -32,7 +33,25 @@ func (s Stream) String() string {
|
||||
|
||||
type Checker interface {
|
||||
Check(stdout, stderr string, err error) error
|
||||
Name() string
|
||||
}
|
||||
|
||||
func InferName(c Checker) string {
|
||||
if c == nil {
|
||||
panic("nil checker - malformed test?")
|
||||
}
|
||||
|
||||
checkerType := reflect.TypeOf(c)
|
||||
_, ok := checkerType.MethodByName("Name")
|
||||
if !ok {
|
||||
return checkerType.String()
|
||||
}
|
||||
|
||||
retVals := reflect.ValueOf(c).MethodByName("Name").Call([]reflect.Value{})
|
||||
if len(retVals) != 1 {
|
||||
panic(fmt.Sprintf("Name function called - returned %d values. Must be one string only.", len(retVals)))
|
||||
}
|
||||
|
||||
return retVals[0].String()
|
||||
}
|
||||
|
||||
type MustSucceed struct{}
|
||||
@@ -41,8 +60,13 @@ func (_ MustSucceed) Check(stdout, stderr string, err error) error {
|
||||
return err
|
||||
}
|
||||
|
||||
func (_ MustSucceed) Name() string {
|
||||
return "Must succeed"
|
||||
type MustFail struct{}
|
||||
|
||||
func (_ MustFail) Check(stdout, stderr string, err error) error {
|
||||
if err == nil {
|
||||
return fmt.Errorf("unexpected command success")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Grep is essentially the equivalent to a normal grep -E on the command line.
|
||||
@@ -93,7 +117,3 @@ func (d Dump) Check(stdout, stderr string, err error) error {
|
||||
d.t.Logf("stderr: %s", stderr)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (_ Dump) Name() string {
|
||||
return "dump"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user