File tree 2 files changed +40
-1
lines changed
2 files changed +40
-1
lines changed Original file line number Diff line number Diff line change @@ -19,6 +19,7 @@ package test
19
19
import (
20
20
"crypto/sha256"
21
21
"fmt"
22
+ "io"
22
23
"os"
23
24
"path/filepath"
24
25
"regexp"
@@ -110,7 +111,42 @@ func (tp *temp) Save(value string, key ...string) string {
110
111
assertive .ErrorIsNil (
111
112
assertive .WithSilentSuccess (tp .t ),
112
113
err ,
113
- fmt .Sprintf ("Saving file %q must succeed" , filepath .Join (key ... )),
114
+ fmt .Sprintf ("Saving file %q must succeed" , pth ),
115
+ )
116
+
117
+ return pth
118
+ }
119
+
120
+ func (tp * temp ) SaveToWriter (writer func (file io.Writer ) error , key ... string ) string {
121
+ tp .t .Helper ()
122
+
123
+ tp .Dir (key [:len (key )- 1 ]... )
124
+
125
+ pth := filepath .Join (append ([]string {tp .tempDir }, key ... )... )
126
+ silentT := assertive .WithSilentSuccess (tp .t )
127
+
128
+ //nolint:gosec // it is fine
129
+ file , err := os .OpenFile (pth , os .O_CREATE , FilePermissionsDefault )
130
+ assertive .ErrorIsNil (
131
+ silentT ,
132
+ err ,
133
+ fmt .Sprintf ("Opening file %q must succeed" , pth ),
134
+ )
135
+
136
+ defer func () {
137
+ err = file .Close ()
138
+ assertive .ErrorIsNil (
139
+ silentT ,
140
+ err ,
141
+ fmt .Sprintf ("Closing file %q must succeed" , pth ),
142
+ )
143
+ }()
144
+
145
+ err = writer (file )
146
+ assertive .ErrorIsNil (
147
+ silentT ,
148
+ err ,
149
+ fmt .Sprintf ("Filewriter failed while attempting to write to %q" , pth ),
114
150
)
115
151
116
152
return pth
Original file line number Diff line number Diff line change @@ -40,6 +40,9 @@ type DataTemp interface {
40
40
// Save will store the content in the file, ensuring parent dir exists, and return the path.
41
41
// Asserts on failure.
42
42
Save (data string , key ... string ) string
43
+ // SaveToWriter allows to write to the file as a writer.
44
+ // This is particularly useful for encoding functions like pem.Encode.
45
+ SaveToWriter (writer func (file io.Writer ) error , key ... string ) string
43
46
// Path will return the absolute path for the asset, whether it exists or not.
44
47
Path (key ... string ) string
45
48
// Exists asserts that the object exist.
You can’t perform that action at this time.
0 commit comments