serialtest

package
v0.9.1 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package serialtest provides fakes for testing serial device handling without hardware.

FakePort carries data over a real pty pair, so the read/write path under test is the one a real tty takes. Line settings and control lines are recorded rather than applied, because a pty cannot express them: TIOCMGET on a pty returns ENOTTY, which is the whole reason moat speaks RFC2217 rather than relaying through a pty. Tests assert on the recorded values.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewFailingEnumerator

func NewFailingEnumerator(err error) serialdev.Enumerator

NewFailingEnumerator returns an Enumerator that always fails, for testing how callers report an unreadable device list.

func NewFakeEnumerator

func NewFakeEnumerator(devs ...serialdev.Device) serialdev.Enumerator

NewFakeEnumerator returns an Enumerator yielding a fixed device list.

Types

type FakePort

type FakePort struct {
	// contains filtered or unexported fields
}

FakePort implements serialport.Port over a pty pair.

func NewFakePort

func NewFakePort(t *testing.T) *FakePort

NewFakePort returns a fake port and registers its cleanup.

func (*FakePort) ApplySettings

func (p *FakePort) ApplySettings(s serialport.Settings) error

ApplySettings records the requested line settings. SetApplySettingsError, when set, is returned instead — for testing how callers handle a device that rejects a line change.

func (*FakePort) BreakCount

func (p *FakePort) BreakCount() int

BreakCount returns how many breaks were sent.

func (*FakePort) Close

func (p *FakePort) Close() error

Close closes both ends. It is safe to call more than once, since both the test cleanup and the code under test may close the port.

func (*FakePort) FlushBuffers

func (p *FakePort) FlushBuffers() error

FlushBuffers records a purge. A pty has no kernel receive queue to flush that would matter to assertions, so recording is enough to prove the broker forwards PURGE_DATA to the port. FlushErr, when set, is returned instead — for testing how callers handle a device that cannot flush.

func (*FakePort) LastModem

func (p *FakePort) LastModem() serialport.Modem

LastModem returns the most recently applied control lines.

func (*FakePort) LastSettings

func (p *FakePort) LastSettings() serialport.Settings

LastSettings returns the most recently applied line settings.

func (*FakePort) ModemSequence

func (p *FakePort) ModemSequence() []serialport.Modem

ModemSequence returns every control-line change in order.

The sequence matters, not just the final state: driving an ESP32 into its bootloader is a specific series of DTR/RTS transitions, so a relay that coalesced them would leave the final state correct and the board unflashed.

func (*FakePort) ModemStatus

func (p *FakePort) ModemStatus() (serialport.ModemStatus, error)

ModemStatus reports the scripted input lines (Status field). A pty cannot express them, so the value is whatever the test set.

func (*FakePort) Name

func (p *FakePort) Name() string

func (*FakePort) Peer

func (p *FakePort) Peer() io.ReadWriter

Peer is the far end of the wire — what a device would read and write.

func (*FakePort) PurgeCount

func (p *FakePort) PurgeCount() int

PurgeCount returns how many buffer flushes were requested.

func (*FakePort) Read

func (p *FakePort) Read(b []byte) (int, error)

func (*FakePort) SendBreak

func (p *FakePort) SendBreak() error

SendBreak records a break.

func (*FakePort) SetApplySettingsError

func (p *FakePort) SetApplySettingsError(err error)

SetApplySettingsError makes subsequent ApplySettings calls fail, or clears it with nil.

func (*FakePort) SetFlushError

func (p *FakePort) SetFlushError(err error)

SetFlushError makes subsequent FlushBuffers calls fail, or clears it with nil.

func (*FakePort) SetModem

func (p *FakePort) SetModem(m serialport.Modem) error

SetModem records the requested control lines. SetModemError, when set, is returned instead — for testing how callers handle a device that cannot drive its control lines.

func (*FakePort) SetModemError

func (p *FakePort) SetModemError(err error)

SetModemError makes subsequent SetModem calls fail, or clears it with nil.

func (*FakePort) SetModemStatus

func (p *FakePort) SetModemStatus(s serialport.ModemStatus)

SetModemStatus scripts what ModemStatus reports.

func (*FakePort) SetModemStatusError

func (p *FakePort) SetModemStatusError(err error)

SetModemStatusError makes subsequent ModemStatus calls fail, or clears it with nil — for testing how callers handle a device that cannot report its input lines.

func (*FakePort) SetReadError

func (p *FakePort) SetReadError(err error)

SetReadError makes subsequent Read calls fail, or clears it with nil — for testing how callers handle a device that vanishes mid-session (EIO on unplug, ENXIO on disconnect).

func (*FakePort) SettingsSequence

func (p *FakePort) SettingsSequence() []serialport.Settings

SettingsSequence returns every line-settings change in order.

func (*FakePort) Write

func (p *FakePort) Write(b []byte) (int, error)

Jump to

Keyboard shortcuts

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