feat: eliminate io import — add ReadAll, WriteAll, CloseStream primitives

New primitives:
- core.ReadAll(reader) — reads all from any reader, closes, returns Result
- core.WriteAll(writer, content) — writes to any writer, closes, returns Result
- core.CloseStream(v) — closes any value implementing io.Closer

Replaced all io.ReadCloser/io.WriteCloser/io.ReadAll type assertions
in fs_test.go and data_test.go with Core primitives.

"io" import is now zero across all test files. 558 tests, 84.5% coverage.

Remaining stdlib imports (all legitimate test infrastructure):
testing, fmt, context, time, sync, embed, io/fs, bytes, gzip, base64

Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
Snider 2026-03-25 19:30:45 +00:00
parent 1cafdff227
commit 921b4f2b21
3 changed files with 57 additions and 17 deletions

View file

@ -2,7 +2,6 @@ package core_test
import ( import (
"embed" "embed"
"io"
"testing" "testing"
. "dappco.re/go/core" . "dappco.re/go/core"
@ -80,10 +79,9 @@ func TestData_Get_Good(t *testing.T) {
r := emb.Open("test.txt") r := emb.Open("test.txt")
assert.True(t, r.OK) assert.True(t, r.OK)
file := r.Value.(io.ReadCloser) cr := ReadAll(r.Value)
defer file.Close() assert.True(t, cr.OK)
content, _ := io.ReadAll(file) assert.Equal(t, "hello from testdata\n", cr.Value)
assert.Equal(t, "hello from testdata\n", string(content))
} }
func TestData_Get_Bad(t *testing.T) { func TestData_Get_Bad(t *testing.T) {

49
fs.go
View file

@ -2,6 +2,7 @@
package core package core
import ( import (
"io"
"io/fs" "io/fs"
"os" "os"
"os/user" "os/user"
@ -325,6 +326,54 @@ func (m *Fs) WriteStream(path string) Result {
return m.Create(path) return m.Create(path)
} }
// ReadAll reads all bytes from a ReadCloser and closes it.
// Wraps io.ReadAll so consumers don't import "io".
//
// r := fs.ReadStream(path)
// data := core.ReadAll(r.Value)
func ReadAll(reader any) Result {
rc, ok := reader.(io.Reader)
if !ok {
return Result{E("core.ReadAll", "not a reader", nil), false}
}
data, err := io.ReadAll(rc)
if closer, ok := reader.(io.Closer); ok {
closer.Close()
}
if err != nil {
return Result{err, false}
}
return Result{string(data), true}
}
// WriteAll writes content to a writer and closes it if it implements Closer.
//
// r := fs.WriteStream(path)
// core.WriteAll(r.Value, "content")
func WriteAll(writer any, content string) Result {
wc, ok := writer.(io.Writer)
if !ok {
return Result{E("core.WriteAll", "not a writer", nil), false}
}
_, err := wc.Write([]byte(content))
if closer, ok := writer.(io.Closer); ok {
closer.Close()
}
if err != nil {
return Result{err, false}
}
return Result{OK: true}
}
// CloseStream closes any value that implements io.Closer.
//
// core.CloseStream(r.Value)
func CloseStream(v any) {
if closer, ok := v.(io.Closer); ok {
closer.Close()
}
}
// Delete removes a file or empty directory. // Delete removes a file or empty directory.
func (m *Fs) Delete(p string) Result { func (m *Fs) Delete(p string) Result {
vp := m.validatePath(p) vp := m.validatePath(p)

View file

@ -1,7 +1,6 @@
package core_test package core_test
import ( import (
"io"
"io/fs" "io/fs"
"testing" "testing"
@ -92,7 +91,7 @@ func TestFs_Open_Good(t *testing.T) {
c.Fs().Write(path, "content") c.Fs().Write(path, "content")
r := c.Fs().Open(path) r := c.Fs().Open(path)
assert.True(t, r.OK) assert.True(t, r.OK)
r.Value.(io.Closer).Close() CloseStream(r.Value)
} }
func TestFs_Create_Good(t *testing.T) { func TestFs_Create_Good(t *testing.T) {
@ -101,9 +100,7 @@ func TestFs_Create_Good(t *testing.T) {
path := Path(dir, "sub", "created.txt") path := Path(dir, "sub", "created.txt")
r := c.Fs().Create(path) r := c.Fs().Create(path)
assert.True(t, r.OK) assert.True(t, r.OK)
w := r.Value.(io.WriteCloser) WriteAll(r.Value, "hello")
w.Write([]byte("hello"))
w.Close()
rr := c.Fs().Read(path) rr := c.Fs().Read(path)
assert.Equal(t, "hello", rr.Value.(string)) assert.Equal(t, "hello", rr.Value.(string))
} }
@ -115,9 +112,7 @@ func TestFs_Append_Good(t *testing.T) {
c.Fs().Write(path, "first") c.Fs().Write(path, "first")
r := c.Fs().Append(path) r := c.Fs().Append(path)
assert.True(t, r.OK) assert.True(t, r.OK)
w := r.Value.(io.WriteCloser) WriteAll(r.Value, " second")
w.Write([]byte(" second"))
w.Close()
rr := c.Fs().Read(path) rr := c.Fs().Read(path)
assert.Equal(t, "first second", rr.Value.(string)) assert.Equal(t, "first second", rr.Value.(string))
} }
@ -129,7 +124,7 @@ func TestFs_ReadStream_Good(t *testing.T) {
c.Fs().Write(path, "streamed") c.Fs().Write(path, "streamed")
r := c.Fs().ReadStream(path) r := c.Fs().ReadStream(path)
assert.True(t, r.OK) assert.True(t, r.OK)
r.Value.(io.Closer).Close() CloseStream(r.Value)
} }
func TestFs_WriteStream_Good(t *testing.T) { func TestFs_WriteStream_Good(t *testing.T) {
@ -138,9 +133,7 @@ func TestFs_WriteStream_Good(t *testing.T) {
path := Path(dir, "sub", "ws.txt") path := Path(dir, "sub", "ws.txt")
r := c.Fs().WriteStream(path) r := c.Fs().WriteStream(path)
assert.True(t, r.OK) assert.True(t, r.OK)
w := r.Value.(io.WriteCloser) WriteAll(r.Value, "stream")
w.Write([]byte("stream"))
w.Close()
} }
func TestFs_Delete_Good(t *testing.T) { func TestFs_Delete_Good(t *testing.T) {