From 2f8d3e1c33f77187c68893803018756d43daff15 Mon Sep 17 00:00:00 2001 From: Aleksa Sarai Date: Wed, 8 Nov 2017 03:29:49 +1100 Subject: [PATCH] internal: testutil: add DevZero helper This helper acts like /dev/zero (outputs \x00 indefinitely) in an OS-independent fashion. This ensures we don't need to special-case around Windows in tests that want to open /dev/zero. Signed-off-by: Aleksa Sarai --- internal/testutil/helpers.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/internal/testutil/helpers.go b/internal/testutil/helpers.go index a76056924e..287b3cb48a 100644 --- a/internal/testutil/helpers.go +++ b/internal/testutil/helpers.go @@ -1,6 +1,8 @@ package testutil import ( + "io" + "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -11,3 +13,15 @@ func ErrorContains(t require.TestingT, err error, expectedError string, msgAndAr require.Error(t, err, msgAndArgs...) assert.Contains(t, err.Error(), expectedError, msgAndArgs...) } + +// DevZero acts like /dev/zero but in an OS-independent fashion. +var DevZero io.Reader = devZero{} + +type devZero struct{} + +func (d devZero) Read(p []byte) (n int, err error) { + for i := 0; i < len(p); i++ { + p[i] = '\x00' + } + return len(p), nil +}