diff --git a/mod/tigron/test/data.go b/mod/tigron/test/data.go index d5583d60b2d..9400b88ec03 100644 --- a/mod/tigron/test/data.go +++ b/mod/tigron/test/data.go @@ -19,6 +19,7 @@ package test import ( "crypto/sha256" "fmt" + "io" "os" "path/filepath" "regexp" @@ -110,7 +111,42 @@ func (tp *temp) Save(value string, key ...string) string { assertive.ErrorIsNil( assertive.WithSilentSuccess(tp.t), err, - fmt.Sprintf("Saving file %q must succeed", filepath.Join(key...)), + fmt.Sprintf("Saving file %q must succeed", pth), + ) + + return pth +} + +func (tp *temp) SaveToWriter(writer func(file io.Writer) error, key ...string) string { + tp.t.Helper() + + tp.Dir(key[:len(key)-1]...) + + pth := filepath.Join(append([]string{tp.tempDir}, key...)...) + silentT := assertive.WithSilentSuccess(tp.t) + + //nolint:gosec // it is fine + file, err := os.OpenFile(pth, os.O_CREATE, FilePermissionsDefault) + assertive.ErrorIsNil( + silentT, + err, + fmt.Sprintf("Opening file %q must succeed", pth), + ) + + defer func() { + err = file.Close() + assertive.ErrorIsNil( + silentT, + err, + fmt.Sprintf("Closing file %q must succeed", pth), + ) + }() + + err = writer(file) + assertive.ErrorIsNil( + silentT, + err, + fmt.Sprintf("Filewriter failed while attempting to write to %q", pth), ) return pth diff --git a/mod/tigron/test/interfaces.go b/mod/tigron/test/interfaces.go index afa52013f31..12df876747a 100644 --- a/mod/tigron/test/interfaces.go +++ b/mod/tigron/test/interfaces.go @@ -40,6 +40,9 @@ type DataTemp interface { // Save will store the content in the file, ensuring parent dir exists, and return the path. // Asserts on failure. Save(data string, key ...string) string + // SaveToWriter allows to write to the file as a writer. + // This is particularly useful for encoding functions like pem.Encode. + SaveToWriter(writer func(file io.Writer) error, key ...string) string // Path will return the absolute path for the asset, whether it exists or not. Path(key ...string) string // Exists asserts that the object exist.