From 3a713ec1b70b4c79eee6897b78d6dfd7ab659ce5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ivan=20Miri=C4=87?= Date: Fri, 31 Jul 2020 11:30:48 +0200 Subject: [PATCH] Fix panic getting terminal width in some CI environments 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 --- cmd/ui.go | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/cmd/ui.go b/cmd/ui.go index 93a59a71913..e0042cf51ce 100644 --- a/cmd/ui.go +++ b/cmd/ui.go @@ -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 @@ -333,19 +337,19 @@ func showProgress( outMutex.Unlock() return case <-winch: - if !errTermGetSize { + if stdoutTTY && !errTermGetSize { // More responsive progress bar resizing on platforms with SIGWINCH (*nix) - termWidth, _, err = terminal.GetSize(fd) - if err != nil { - termWidth = defaultTermWidth + tw, _, err := terminal.GetSize(fd) + if tw > 0 && err == nil { + termWidth = tw } } case <-ticker.C: // Default ticker-based progress bar resizing - if !errTermGetSize && winch == nil { - termWidth, _, err = terminal.GetSize(fd) - if err != nil { - termWidth = defaultTermWidth + if stdoutTTY && !errTermGetSize && winch == nil { + tw, _, err := terminal.GetSize(fd) + if tw > 0 && err == nil { + termWidth = tw } } }