Skip to content

Commit

Permalink
Fix panic getting terminal width in some CI environments
Browse files Browse the repository at this point in the history
In some CI environments like Codefresh, k6 detects that it's running in
a tty, the err returned by `terminal.GetSize()` is `nil`, yet
`termWidth` is 0. Strange scenario, and we should fix our TTY check if
possible, but it shouldn't panic with `divide by zero` anymore.

Closes #1579
  • Loading branch information
Ivan Mirić committed Jul 31, 2020
1 parent 8f4797d commit c479452
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions cmd/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,11 +254,15 @@ func showProgress(
}

var errTermGetSize bool
termWidth, _, err := terminal.GetSize(int(os.Stdout.Fd()))
if err != nil && stdoutTTY {
logger.WithError(err).Warn("error getting terminal size")
termWidth = defaultTermWidth
errTermGetSize = true
termWidth := defaultTermWidth
if stdoutTTY {
tw, _, err := terminal.GetSize(int(os.Stdout.Fd()))
if !(tw > 0) || err != nil {
errTermGetSize = true
logger.WithError(err).Warn("error getting terminal size")
} else {
termWidth = tw
}
}

// Get the longest left side string length, to align progress bars
Expand Down Expand Up @@ -335,16 +339,18 @@ func showProgress(
case <-winch:
if !errTermGetSize {
// More responsive progress bar resizing on platforms with SIGWINCH (*nix)
var err error
termWidth, _, err = terminal.GetSize(fd)
if err != nil {
if !(termWidth > 0) || err != nil {
termWidth = defaultTermWidth
}
}
case <-ticker.C:
// Default ticker-based progress bar resizing
if !errTermGetSize && winch == nil {
var err error
termWidth, _, err = terminal.GetSize(fd)
if err != nil {
if !(termWidth > 0) || err != nil {
termWidth = defaultTermWidth
}
}
Expand Down

0 comments on commit c479452

Please sign in to comment.