Skip to content

Commit

Permalink
test: add a new case for UnwrapNopCloser (#725)
Browse files Browse the repository at this point in the history
Add a new unit test case by the change to `UnwrapNopCloser` introduced
by #715

---------

Signed-off-by: Shiwei Zhang <shizh@microsoft.com>
  • Loading branch information
shizhMSFT committed Mar 19, 2024
1 parent bf0d637 commit 8d139f0
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 16 deletions.
26 changes: 13 additions & 13 deletions internal/ioutil/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,23 +44,23 @@ func CopyBuffer(dst io.Writer, src io.Reader, buf []byte, desc ocispec.Descripto
return vr.Verify()
}

// nopCloserType is the type of `io.NopCloser()`.
var nopCloserType = reflect.TypeOf(io.NopCloser(nil))
// nopCloserWriterToType is the type of `io.nopCloserWriterTo`
var nopCloserWriterToType = reflect.TypeOf(io.NopCloser(struct {
io.Reader
io.WriterTo
}{}))
// Types returned by `io.NopCloser()`.
var (
nopCloserType = reflect.TypeOf(io.NopCloser(nil))
nopCloserWriterToType = reflect.TypeOf(io.NopCloser(struct {
io.Reader
io.WriterTo
}{}))
)

// UnwrapNopCloser returns the underlying reader if rc is a NopCloser
// else it simply returns rc.
// UnwrapNopCloser unwraps the reader wrapped by `io.NopCloser()`.
// Similar implementation can be found in the built-in package `net/http`.
// Reference: https://github.com/golang/go/blob/go1.22.1/src/net/http/transfer.go#L1090-L1105
func UnwrapNopCloser(rc io.Reader) io.Reader {
switch reflect.TypeOf(rc) {
func UnwrapNopCloser(r io.Reader) io.Reader {
switch reflect.TypeOf(r) {
case nopCloserType, nopCloserWriterToType:
return reflect.ValueOf(rc).Field(0).Interface().(io.Reader)
return reflect.ValueOf(r).Field(0).Interface().(io.Reader)
default:
return rc
return r
}
}
19 changes: 16 additions & 3 deletions internal/ioutil/io_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ import (
)

func TestUnwrapNopCloser(t *testing.T) {
var reader struct {
io.Reader
}
var readerWithWriterTo struct {
io.Reader
io.WriterTo
}

tests := []struct {
name string
rc io.Reader
Expand All @@ -37,9 +45,14 @@ func TestUnwrapNopCloser(t *testing.T) {
name: "nil",
},
{
name: "no-op closer",
rc: io.NopCloser(os.Stdin),
want: os.Stdin,
name: "no-op closer with plain io.Reader",
rc: io.NopCloser(reader),
want: reader,
},
{
name: "no-op closer with io.WriteTo",
rc: io.NopCloser(readerWithWriterTo),
want: readerWithWriterTo,
},
{
name: "any ReadCloser",
Expand Down

0 comments on commit 8d139f0

Please sign in to comment.