1
Fork 0

remove bytecounter

main
Motiejus Jakštys 2021-05-24 00:11:58 +03:00
parent d7fb5140e8
commit 8aada0e3e2
3 changed files with 0 additions and 79 deletions

View File

@ -1,18 +0,0 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
go_library(
name = "go_default_library",
srcs = ["bytecounter.go"],
importpath = "github.com/motiejus/code/undocker/internal/bytecounter",
visibility = ["//src/undocker:__subpackages__"],
)
go_test(
name = "go_default_test",
srcs = ["bytecounter_test.go"],
embed = [":go_default_library"],
deps = [
"@com_github_stretchr_testify//assert:go_default_library",
"@com_github_stretchr_testify//require:go_default_library",
],
)

View File

@ -1,20 +0,0 @@
package bytecounter
import "io"
// ByteCounter is an io.Writer that counts bytes written to it
type ByteCounter struct {
N int64
w io.Writer
}
// New returns a new ByteCounter
func New(w io.Writer) *ByteCounter {
return &ByteCounter{w: w}
}
// Write writes to the underlying io.Writer and counts total written bytes
func (b *ByteCounter) Write(data []byte) (n int, err error) {
defer func() { b.N += int64(n) }()
return b.w.Write(data)
}

View File

@ -1,41 +0,0 @@
package bytecounter
import (
"bytes"
"io"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestByteCounter(t *testing.T) {
r := bytes.NewBufferString("0123456789")
w := bytes.NewBuffer(nil)
bc := New(w)
_, err := io.Copy(bc, r)
require.NoError(t, err)
assert.Equal(t, []byte("0123456789"), w.Bytes())
assert.Equal(t, int64(10), bc.N)
}
func TestSingleByteCounter(t *testing.T) {
r := bytes.NewBufferString("0123456789")
w := bytes.NewBuffer(nil)
tw := &shortWriter{w}
bc := New(tw)
_, err := io.Copy(bc, r)
assert.EqualError(t, err, "short write")
assert.Len(t, w.Bytes(), 1)
assert.Equal(t, int64(1), bc.N)
}
// shortWriter writes only first byte
type shortWriter struct {
w io.Writer
}
// Write writes a byte to the underlying writer
func (f *shortWriter) Write(p []byte) (int, error) {
return f.w.Write(p[0:1])
}