Extract testUI func

This commit is contained in:
Paul Meyer 2020-03-25 18:08:09 +00:00
parent 9ed6fab8c4
commit b3e361a139
2 changed files with 23 additions and 8 deletions

View File

@ -0,0 +1,19 @@
package chroot
import (
"io/ioutil"
"strings"
"github.com/hashicorp/packer/packer"
)
// testUI returns a test ui plus a function to retrieve the errors written to the ui
func testUI() (packer.Ui, func() string) {
errorBuffer := &strings.Builder{}
ui := &packer.BasicUi{
Reader: strings.NewReader(""),
Writer: ioutil.Discard,
ErrorWriter: errorBuffer,
}
return ui, errorBuffer.String
}

View File

@ -129,12 +129,7 @@ func Test_StepVerifySourceDisk_Run(t *testing.T) {
}, nil
})
errorBuffer := &strings.Builder{}
ui := &packer.BasicUi{
Reader: strings.NewReader(""),
Writer: ioutil.Discard,
ErrorWriter: errorBuffer,
}
ui, getErr := testUI()
state := new(multistep.BasicStateBag)
state.Put("azureclient", &client.AzureClientSetMock{
@ -148,8 +143,9 @@ func Test_StepVerifySourceDisk_Run(t *testing.T) {
}
if tt.errormatch != "" {
if !regexp.MustCompile(tt.errormatch).MatchString(errorBuffer.String()) {
t.Errorf("Expected the error output (%q) to match %q", errorBuffer.String(), tt.errormatch)
errs := getErr()
if !regexp.MustCompile(tt.errormatch).MatchString(errs) {
t.Errorf("Expected the error output (%q) to match %q", errs, tt.errormatch)
}
}