-
Notifications
You must be signed in to change notification settings - Fork 13
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
fix issue 26 #27
fix issue 26 #27
Changes from 1 commit
80a0229
b841c18
0bdc91a
11ea917
3e00f49
fc91f26
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ package kaitai | |
|
||
import ( | ||
"bytes" | ||
"errors" | ||
"io" | ||
"reflect" | ||
"testing" | ||
|
@@ -59,6 +60,7 @@ func TestStream_Size(t *testing.T) { | |
wantErr bool | ||
}{ | ||
{"Size", NewStream(bytes.NewReader([]byte("test"))), 4, false}, | ||
{"Zero size", NewStream(bytes.NewReader([]byte{})), 0, false}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
|
@@ -74,6 +76,42 @@ func TestStream_Size(t *testing.T) { | |
} | ||
} | ||
|
||
const initialPosition = 5 | ||
generalmimon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
type failingReader struct { | ||
pos int64 | ||
} | ||
|
||
func (fr *failingReader) Read(p []byte) (n int, err error) { return 0, nil } | ||
func (fr *failingReader) Seek(offset int64, whence int) (int64, error) { | ||
switch { | ||
case whence == io.SeekCurrent && fr.pos != initialPosition: | ||
return 0, errors.New("not allowed to seek to the current pos") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure if I fully understand this case. I guess that you're creating the error returned to the first And What about this: switch {
case whence == io.SeekCurrent && fr.pos == -1:
return 0, errors.New("artificial error when seeking with io.SeekCurrent after seeking to end")
// ...
default: // whence == io.SeekEnd
fr.pos = -1
} It would help me if I saw the code for the first time. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I agree. Done. |
||
case whence == io.SeekCurrent: | ||
return fr.pos, nil | ||
case whence == io.SeekStart: | ||
fr.pos = offset | ||
default: | ||
chavacava marked this conversation as resolved.
Show resolved
Hide resolved
|
||
fr.pos++ | ||
} | ||
return fr.pos, nil | ||
} | ||
|
||
// No regression test for issue #26 | ||
func TestErrorHandlingInStream_Size(t *testing.T) { | ||
fr := &failingReader{initialPosition} | ||
s := NewStream(fr) | ||
_, err := s.Size() | ||
|
||
if err == nil { | ||
t.Fatal("Expected error, got nothing") | ||
} | ||
generalmimon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
chavacava marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if fr.pos != initialPosition { | ||
t.Fatalf("Expected position to be %v, got %v", initialPosition, fr.pos) | ||
} | ||
} | ||
|
||
func TestStream_Pos(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The tool https://github.com/golangci/golangci-lint noticed this problem:
This might potentially lead to a bug that this call fails, but because its error return value is ignored,
k.Size()
will happily returnerr == nil
(despite the fact that the stream pointer failed to return to the original position for some reason and we're clearly in a situation of failure).I think the code should be like this:
See https://yourbasic.org/golang/defer/#use-func-to-return-a-value and https://blog.learngoprogramming.com/5-gotchas-of-defer-in-go-golang-part-iii-36a1ab3d6ef1#afb5.
Could you please adjust the tests to check this case as well?
Perhaps we can make the
failingReader
more generic by restoring its normal behavior (i.e.io.SeekEnd
behaving as normal, not settingpos
to-1
) and instead passing a param in it (e.g.numSeeksLimit
) that limits the number of succeedingSeek()
calls. Then we can just go iterate0..(numSeeksThatSizeMethodDoes - 1)
and check for each limit that theSize()
function always returns an error and restores the stream position to its original state.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added two test cases: