iotest

package module
v0.0.0-...-9d159be Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 6, 2026 License: GPL-3.0 Imports: 2 Imported by: 0

README

Golang Utilities for IO Testing

GoDoc Build Status codecov

The iotest Go package contains code to test io types (e.g., io.Writer, io.Reader).

For example:

import (
	"errors"
	"fmt"

	"github.com/bassosimone/iotest"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

// Create a writer that always fails with a mocked error
w := &FuncWriter{
	WriteFunc: func(b []byte) (int, error) {
		return 0, errors.New("mocked error")
	},
}

// Use the writer with a function that will fail if it fails
count, err := fmt.Fprintf(w, "%s\n", "Hello, world!\n")

// Verify the test
require.Error(t, err)
assert.Equal(t, count, 0)

Installation

To add this package as a dependency to your module:

go get github.com/bassosimone/iotest

Development

To run the tests:

go test -v .

To measure test coverage:

go test -v -cover .

License

SPDX-License-Identifier: GPL-3.0-or-later

History

Adapted from ooni/probe-cli.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FuncReadCloser

type FuncReadCloser struct {
	ReadFunc  func(b []byte) (int, error)
	CloseFunc func() error
}

FuncReadCloser allows to mock any io.ReadCloser.

func (*FuncReadCloser) Close

func (r *FuncReadCloser) Close() error

Close implements io.Closer.

func (*FuncReadCloser) Read

func (r *FuncReadCloser) Read(b []byte) (int, error)

Read implements io.Reader.

type FuncReader

type FuncReader = FuncReadCloser

FuncReader allows to mock any io.Reader.

type FuncWriteCloser

type FuncWriteCloser struct {
	WriteFunc func(b []byte) (int, error)
	CloseFunc func() error
}

FuncWriteCloser allows to mock any io.WriteCloser.

func (*FuncWriteCloser) Close

func (w *FuncWriteCloser) Close() error

Close implements io.Closer.

func (*FuncWriteCloser) Write

func (w *FuncWriteCloser) Write(b []byte) (int, error)

Write implements io.Writer.

type FuncWriter

type FuncWriter = FuncWriteCloser

FuncWriter allows to mock any io.Writer.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL