nop

package
v0.18.4 Latest Latest
Warning

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

Go to latest
Published: May 9, 2022 License: MIT Imports: 10 Imported by: 2

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var EOFChan = make(chan interface{})

EOFChan can be closed to simulate reading the end of a file. The MockLine value will be returned with simulated end-of-file behavior.

View Source
var MockLine = []byte("mock line\n")

MockLine can be used for setting the return value of a call to Read() or ReadLine(). The set value will be returned unless Reader is initialized with a MockReader value that indicates returning an error.

View Source
var MsgChan = make(chan []byte)

MsgChan can be used to set a read message value. The call to Read or ReadLine will block until a mock message is send on MsgChan. This way both the value and timing of a read call can be controlled. This can be useful when testing.

The reader only listens on MsgChan if len(MockLine) is 0.

If EOFChan is closed then and a message is written to MsgChan the reader may behave as if it reached an end-of-file or it may return the MsgChan message. If it is desired to return a message with end-of-file behavior then make sure MockLine is set with the desired return value.

Functions

func ListFiles

func ListFiles(pth string) ([]stat.Stats, error)

ListFiles will return a single stat.Stats record whose path is the same as pth.

if pth has 'err' in the first part of the path then no stats will be returned along with an instance of error.

func Stat added in v0.5.0

func Stat(pth string) (stat.Stats, error)

Stat can be used as a mock stat for testing. The following are supported mocks error, err, stat_error or stat_err - return an error when called stat_dir - identifies the path as a directory

Types

type Reader

type Reader struct {
	MockReadMode string
	// contains filtered or unexported fields
}

func NewReader

func NewReader(pth string) (*Reader, error)
Example
// showing:
// - new reader

r, err := NewReader("nop://file.txt")
if r == nil {
	return
}
fmt.Println(err)        // output: <nil>
fmt.Println(r.sts.Path) // output: nop://file.txt
Output:

<nil>
nop://file.txt

func (*Reader) Close

func (r *Reader) Close() (err error)
Example
// showing:
// - Reader.ReadLine() returning an error

r, _ := NewReader("nop://file.txt")
if r == nil {
	return
}
r.sts.ByteCnt = 10
err := r.Close()
fmt.Println(err)        // output: <nil>
fmt.Println(r.sts.Size) // output: 10
Output:

<nil>
10

func (*Reader) Read

func (r *Reader) Read(p []byte) (n int, err error)

Read will return n as len(MockLine) or length of MsgChan bytes.

Example
// showing:
// - Reader.Read() happy path

r, _ := NewReader("nop://file.txt")
if r == nil {
	return
}
buf := make([]byte, 100)
n, err := r.Read(buf)
fmt.Println(n)             // output: 10
fmt.Println(err)           // output: <nil>
fmt.Println(r.sts.ByteCnt) // output: 10
Output:

10
<nil>
10

func (*Reader) ReadLine

func (r *Reader) ReadLine() (ln []byte, err error)
Example
// showing:
// - Reader.ReadLine() happy path

r, _ := NewReader("nop://file.txt")
if r == nil {
	return
}
ln, err := r.ReadLine()
fmt.Print(string(ln))      // output: mock line
fmt.Println(err)           // output: <nil>
fmt.Println(r.sts.ByteCnt) // output: 10
fmt.Println(r.sts.LineCnt) // output: 1
Output:

mock line
<nil>
10
1

func (*Reader) Stats

func (r *Reader) Stats() stat.Stats
Example
// showing:
// - Reader.Stats() happy path

r, _ := NewReader("nop://file.txt")
if r == nil {
	return
}
r.sts.LineCnt = 10
r.sts.ByteCnt = 100
r.sts.Created = "created date"
r.sts.Size = 200
r.sts.Checksum = "checksum"

sts := r.Stats()
fmt.Println(sts.Path)     // output: nop://file.txt
fmt.Println(sts.LineCnt)  // output: 10
fmt.Println(sts.ByteCnt)  // output: 100
fmt.Println(sts.Created)  // output: created date
fmt.Println(sts.Size)     // output: 200
fmt.Println(sts.Checksum) // output: checksum
Output:

nop://file.txt
10
100
created date
200
checksum

type Writer

type Writer struct {
	MockWriteMode string
	// contains filtered or unexported fields
}

Writer is a no-operation writer useful for testing.

func NewWriter

func NewWriter(pth string) (*Writer, error)
Example
// showing:
// - new writer happy path

w, err := NewWriter("nop://file.txt")
if w == nil {
	return
}
fmt.Println(err)        // output: <nil>
fmt.Println(w.sts.Path) // output: nop://file.txt
Output:

<nil>
nop://file.txt

func (*Writer) Abort

func (w *Writer) Abort() error
Example
// showing:
// - writer.Abort happy path

w, _ := NewWriter("nop://file.txt")
if w == nil {
	return
}

err := w.Abort()
fmt.Println(err) // output: <nil>
Output:

<nil>

func (*Writer) Close

func (w *Writer) Close() error
Example
// showing:
// - writer.Close happy path

w, _ := NewWriter("nop://file.txt")
if w == nil {
	return
}
w.sts.ByteCnt = 10 // Size is set from final byte count
err := w.Close()
isCreated := w.sts.Created != "" // close sets sts.Created
fmt.Println(err)                 // output: <nil>
fmt.Println(w.sts.Size)          // output: 10
fmt.Println(isCreated)           // output: true
Output:

<nil>
10
true

func (*Writer) Stats

func (w *Writer) Stats() stat.Stats
Example
// showing:
// - writer.WriteLine happy path

w, _ := NewWriter("nop://file.txt")
if w == nil {
	return
}

sts := w.Stats()
fmt.Println(sts.Path) // output: nop://file.txt
Output:

nop://file.txt

func (*Writer) Write

func (w *Writer) Write(p []byte) (n int, err error)
Example
// showing:
// - writer.Write happy path

w, _ := NewWriter("nop://file.txt")
if w == nil {
	return
}

n, err := w.Write([]byte("test line"))
fmt.Println(n)             // output: 9
fmt.Println(err)           // output: <nil>
fmt.Println(w.sts.ByteCnt) // output: 9
fmt.Println(w.sts.LineCnt) // output: 0
Output:

9
<nil>
9
0

func (*Writer) WriteLine

func (w *Writer) WriteLine(ln []byte) (err error)
Example
// showing:
// - writer.WriteLine happy path

w, _ := NewWriter("nop://file.txt")
if w == nil {
	return
}

err := w.WriteLine([]byte("test line"))
fmt.Println(err)           // output: <nil>
fmt.Println(w.sts.ByteCnt) // output: 10
fmt.Println(w.sts.LineCnt) // output: 1
Output:

<nil>
10
1

Jump to

Keyboard shortcuts

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