Skip to content

testsuite: added mocked serial monitor for integration tests #2379

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Allow use of the monitor in non-terminal envs
  • Loading branch information
cmaglie committed Oct 20, 2023
commit 20f4e2e0828854381cd2a738b3719a9da2d36785
20 changes: 13 additions & 7 deletions internal/cli/feedback/terminal.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,26 @@ func InteractiveStreams() (io.Reader, io.Writer, error) {
if format != Text {
return nil, nil, errors.New(tr("interactive terminal not supported for the '%s' output format", format))
}
if !isTerminal() {
return nil, nil, errors.New(tr("not running in a terminal"))
}
return os.Stdin, stdOut, nil
}

var oldStateStdin *term.State

// SetRawModeStdin sets the stdin stream in RAW mode (no buffering, echo disabled,
// no terminal escape codes nor signals interpreted)
func SetRawModeStdin() {
func SetRawModeStdin() error {
if oldStateStdin != nil {
panic("terminal already in RAW mode")
}
oldStateStdin, _ = term.MakeRaw(int(os.Stdin.Fd()))
if !IsTerminal() {
return errors.New(tr("not running in a terminal"))
}
old, err := term.MakeRaw(int(os.Stdin.Fd()))
if err != nil {
return err
}
oldStateStdin = old
return nil
}

// RestoreModeStdin restore the terminal settings to the normal non-RAW state. This
Expand All @@ -63,7 +68,8 @@ func RestoreModeStdin() {
oldStateStdin = nil
}

func isTerminal() bool {
// IsTerminal returns true if there is an interactive terminal
func IsTerminal() bool {
return term.IsTerminal(int(os.Stdin.Fd()))
}

Expand All @@ -72,7 +78,7 @@ func InputUserField(prompt string, secret bool) (string, error) {
if format != Text {
return "", errors.New(tr("user input not supported for the '%s' output format", format))
}
if !isTerminal() {
if !IsTerminal() {
return "", errors.New(tr("user input not supported in non interactive mode"))
}

Expand Down
10 changes: 6 additions & 4 deletions internal/cli/monitor/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,12 @@ func runMonitorCmd(portArgs *arguments.Port, fqbn *arguments.Fqbn, configs []str

ctx, cancel := cleanup.InterruptableContext(context.Background())
if raw {
feedback.SetRawModeStdin()
defer func() {
feedback.RestoreModeStdin()
}()
if feedback.IsTerminal() {
if err := feedback.SetRawModeStdin(); err != nil {
feedback.Warning(tr("Error setting raw mode: %s", err.Error()))
}
defer feedback.RestoreModeStdin()
}

// In RAW mode CTRL-C is not converted into an Interrupt by
// the terminal, we must intercept ASCII 3 (CTRL-C) on our own...
Expand Down