stdio

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Apr 8, 2024 License: Apache-2.0 Imports: 4 Imported by: 0

README

godoc codecov Go Report Card

mock-stdio - utilities for mocking os.Stdin, os.Stdout and os.Stderr

Installation

> go get github.com/go-corelibs/mock-stdio@latest

Examples

NewStdin

func Test(t *testing.T) {
    inio := stdio.NewStdin("testing!\n")
    err := inio.Capture()
    So(err, ShouldEqual, nil)
    var data []byte
    data, err = io.ReadAll(os.Stdin)
    So(err, ShouldEqual, nil)
    So(data, ShouldEqual, []byte("testing!\n"))
}

NewStdout

func Test(t *testing.T) {
    outio := stdio.NewStdout()
    err := outio.Capture()
    So(err, ShouldEqual, nil)
    _, err = os.Stdout.WriteString("testing!\n")
    So(err, ShouldEqual, nil)
    So(outio.Data(), ShouldEqual, []byte("testing!\n"))
}

NewStderr

func Test(t *testing.T) {
    errio := stdio.NewStderr()
    err := errio.Capture()
    So(err, ShouldEqual, nil)
    _, err = os.Stderr.WriteString("testing!\n")
    So(err, ShouldEqual, nil)
    So(errio.Data(), ShouldEqual, []byte("testing!\n"))
}

Go-CoreLibs

Go-CoreLibs is a repository of shared code between the Go-Curses and Go-Enjin projects.

License

Copyright 2024 The Go-CoreLibs Authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use file except in compliance with the License.
You may obtain a copy of the license at

 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Documentation

Overview

Package stdio provides utilities for mocking os.Stdin, os.Stdout and os.Stderr for unit testing purposes

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrAlreadyCaptured = errors.New("already captured")
)
View Source
var (
	// StderrTempPattern is the pattern used with calling os.CreateTemp to make
	// the new temp file during os.Stderr Capture operations
	StderrTempPattern = "corelibs-mock-stdio.*.err"
)
View Source
var (
	// StdoutTempPattern is the pattern used with calling os.CreateTemp to make
	// the new temp file during os.Stdout Capture operations
	StdoutTempPattern = "corelibs-mock-stdio.*.out"
)

Functions

This section is empty.

Types

type Stdio

type Stdio interface {
	// Capture will replace the os file handle with a faked one that writes data
	// to a temporary file
	Capture() (err error)
	// Restore will replace the faked os file handle with the original one
	// captured
	Restore()
	// Data returns the contents of the temporary file
	Data() (data []byte)
	// Reader returns the *os.File instance for read operations
	Reader() (r *os.File)
	// Writer returns the *os.File instance for read operations
	Writer() (w *os.File)
	// Reset will Restore and then Capture the os file handle again
	Reset() (err error)
}

func NewStderr

func NewStderr() (s Stdio)

NewStderr creates a new Stdio instance that will Capture os.Stderr

func NewStdin

func NewStdin(data []byte) (s Stdio)

NewStdin creates a new Stdio instance that will Capture os.Stdin and immediately write the `data` given so that it's waiting for immediate reading from the captured os.Stdin instance

func NewStdout

func NewStdout() (s Stdio)

NewStdout creates a new Stdio instance that will Capture os.Stdout

Jump to

Keyboard shortcuts

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