This commit is contained in:
Denis Mishin 2023-01-05 16:35:58 -05:00 committed by GitHub
parent 78fc4853db
commit 488bcd6f72
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
12 changed files with 447 additions and 67 deletions

View file

@ -1,8 +1,15 @@
package fileutil
import (
"bytes"
"fmt"
"os"
"path"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestIsReadableFile(t *testing.T) {
@ -45,3 +52,29 @@ func TestGetwd(t *testing.T) {
})
}
}
func TestReadFileUpTo(t *testing.T) {
d := t.TempDir()
input := []byte{1, 2, 3, 4, 5, 6, 7, 8, 9}
fname := path.Join(d, "test")
require.NoError(t, os.WriteFile(fname, input, 0600))
for _, tc := range []struct {
size int
expectError bool
}{
{len(input) - 1, true},
{len(input), false},
{len(input) + 1, false},
} {
t.Run(fmt.Sprint(tc), func(t *testing.T) {
out, err := ReadFileUpTo(fname, int64(tc.size))
if tc.expectError {
require.Error(t, err)
return
}
require.NoError(t, err)
assert.True(t, bytes.Equal(input, out))
})
}
}