Skip to content

[Tigron]: DataTemp.SaveToWriter method #4137

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion mod/tigron/test/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package test
import (
"crypto/sha256"
"fmt"
"io"
"os"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions mod/tigron/test/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down