Skip to content

Commit e8bfafe

Browse files
authored
Merge pull request #102 from setlog/quality-remove-unnecessary-code
Remove own code by relying on bytes.Buffer instead
2 parents eb9c1dc + a722dec commit e8bfafe

File tree

4 files changed

+14
-40
lines changed

4 files changed

+14
-40
lines changed

pkg/dummy/io.go

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,11 @@
11
package dummy
22

3-
import "io"
3+
import (
4+
"bytes"
5+
)
46

5-
type ReadCloser struct {
6-
Data []byte
7-
pos int
7+
type ByteReadCloser struct {
8+
*bytes.Buffer
89
}
910

10-
func (rc *ReadCloser) Read(p []byte) (n int, err error) {
11-
if rc.Data == nil {
12-
return 0, nil
13-
}
14-
var bytesWritten int
15-
if len(p) < len(rc.Data)-rc.pos {
16-
bytesWritten = rc.write(p, len(p))
17-
} else {
18-
bytesWritten = rc.write(p, len(rc.Data)-rc.pos)
19-
}
20-
if rc.pos == len(rc.Data) {
21-
return bytesWritten, io.EOF
22-
}
23-
return bytesWritten, nil
24-
}
25-
26-
func (rc *ReadCloser) write(p []byte, count int) int {
27-
x := 0
28-
for i := rc.pos; i < rc.pos+count; i++ {
29-
p[x] = rc.Data[i]
30-
x++
31-
}
32-
rc.pos += x
33-
return x
34-
}
35-
36-
func (rc *ReadCloser) Close() error {
37-
return nil
38-
}
11+
func (rc *ByteReadCloser) Close() error { return nil }

pkg/dummy/io_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111

1212
func TestReadCloser(t *testing.T) {
1313
data := []byte("super amazing data")
14-
rc := &dummy.ReadCloser{Data: data}
14+
rc := &dummy.ByteReadCloser{Buffer: bytes.NewBuffer(data)}
1515
readData, err := ioutil.ReadAll(rc)
1616
if err != nil {
1717
t.Fatalf("Error: %v", err)
@@ -23,7 +23,7 @@ func TestReadCloser(t *testing.T) {
2323

2424
func TestReadCloserBigFile(t *testing.T) {
2525
data := []byte("hyper amazing data" + misc.MustGetRandomHexString(10000))
26-
rc := &dummy.ReadCloser{Data: data}
26+
rc := &dummy.ByteReadCloser{Buffer: bytes.NewBuffer(data)}
2727
readData, err := ioutil.ReadAll(rc)
2828
if err != nil {
2929
t.Fatalf("Error: %v", err)

pkg/fetching/download_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func TestHttpClientDoFuncFailure(t *testing.T) {
6464
func TestBadHttpResponse(t *testing.T) {
6565
de := fetching.CreateDummyEnvironment(t, 1000, -1)
6666
fetching.DoForClientFunc = func(client *http.Client, req *http.Request) (*http.Response, error) {
67-
return &http.Response{StatusCode: http.StatusInternalServerError, Body: &dummy.ReadCloser{}}, nil
67+
return &http.Response{StatusCode: http.StatusInternalServerError, Body: &dummy.ByteReadCloser{}}, nil
6868
}
6969
de.TestDownloadRetries(t, "http://example.com")
7070
de.OmitContentLength = true

pkg/launcher/hashing/scan_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package hashing
22

33
import (
4+
"bytes"
45
"context"
56
"fmt"
67
"io"
@@ -49,13 +50,13 @@ func dummyListDirectory(dirPath string) ([]os.FileInfo, error) {
4950
func dummyReadFile(filePath string) (io.ReadCloser, error) {
5051
switch filePath {
5152
case filepath.FromSlash("x/foo"):
52-
return &dummy.ReadCloser{Data: []byte("abc")}, nil
53+
return &dummy.ByteReadCloser{Buffer: bytes.NewBuffer([]byte("abc"))}, nil
5354
case filepath.FromSlash("x/foo/bar"):
54-
return &dummy.ReadCloser{Data: []byte("def")}, nil
55+
return &dummy.ByteReadCloser{Buffer: bytes.NewBuffer([]byte("def"))}, nil
5556
case filepath.FromSlash("x/fuu/baaar"):
56-
return &dummy.ReadCloser{Data: []byte("ghi")}, nil
57+
return &dummy.ByteReadCloser{Buffer: bytes.NewBuffer([]byte("ghi"))}, nil
5758
case filepath.FromSlash("x/fuu/moo/meow/bla"):
58-
return &dummy.ReadCloser{Data: []byte("jkl")}, nil
59+
return &dummy.ByteReadCloser{Buffer: bytes.NewBuffer([]byte("jkl"))}, nil
5960
}
6061
return nil, fmt.Errorf("File not found")
6162
}

0 commit comments

Comments
 (0)