Skip to content

Commit 624f1cf

Browse files
committed
Adding data SaveToWriter
Signed-off-by: apostasie <[email protected]>
1 parent 8251646 commit 624f1cf

File tree

3 files changed

+43
-7
lines changed

3 files changed

+43
-7
lines changed

mod/tigron/expect/doc.md

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ func TestMyThing(t *testing.T) {
131131
myTest.Command = test.Custom("ls")
132132

133133
// Set your expectations
134-
myTest.Expected = test.Expects(0, nil, func(stdout, info string, t tig.T){
134+
myTest.Expected = test.Expects(0, nil, func(stdout string, t tig.T){
135135
t.Helper()
136136
// Bla bla, do whatever advanced stuff and some asserts
137137
})
@@ -143,7 +143,7 @@ func TestMyThing(t *testing.T) {
143143
// You can of course generalize your comparator into a generator if it is going to be useful repeatedly
144144

145145
func MyComparatorGenerator(param1, param2 any) test.Comparator {
146-
return func(stdout, info string, t tig.T) {
146+
return func(stdout string, t tig.T) {
147147
t.Helper()
148148
// Do your thing...
149149
// ...
@@ -155,10 +155,6 @@ func MyComparatorGenerator(param1, param2 any) test.Comparator {
155155
You can now pass along `MyComparator(comparisonString)` as the third parameter of `test.Expects`, or compose it with
156156
other comparators using `expect.All(MyComparator(comparisonString), OtherComparator(somethingElse))`
157157

158-
Note that you have access to an opaque `info` string, that provides a brief formatted header message that assert
159-
will use in case of failure to provide context on the error.
160-
You may of course ignore it and write your own message.
161-
162158
### Advanced expectations
163159

164160
You may want to have expectations that contain a certain piece of data that is being used in the command or at
@@ -180,6 +176,7 @@ import (
180176

181177
"gotest.tools/v3/assert"
182178

179+
"github.com/containerd/nerdctl/mod/tigron/tig"
183180
"github.com/containerd/nerdctl/mod/tigron/test"
184181
)
185182

mod/tigron/test/data.go

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package test
1919
import (
2020
"crypto/sha256"
2121
"fmt"
22+
"io"
2223
"os"
2324
"path/filepath"
2425
"regexp"
@@ -110,7 +111,42 @@ func (tp *temp) Save(value string, key ...string) string {
110111
assertive.ErrorIsNil(
111112
assertive.WithSilentSuccess(tp.t),
112113
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),
114150
)
115151

116152
return pth

mod/tigron/test/interfaces.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ type DataTemp interface {
4040
// Save will store the content in the file, ensuring parent dir exists, and return the path.
4141
// Asserts on failure.
4242
Save(data string, key ...string) string
43+
// SaveWithFunc allows to directly manipulate the fd.
44+
// This is particularly useful for encoding functions (pem.Encode) that expect a file-descriptor
45+
SaveToWriter(writer func(file io.Writer) error, key ...string) string
4346
// Path will return the absolute path for the asset, whether it exists or not.
4447
Path(key ...string) string
4548
// Exists asserts that the object exist.

0 commit comments

Comments
 (0)