From 27ed1dc3818a960196b35b07ecac915e3ee905b4 Mon Sep 17 00:00:00 2001 From: Gleb Bahmutov Date: Thu, 31 May 2018 02:06:34 -0400 Subject: [PATCH] server: polyfill tty.getWindowSize close #1815 (#1828) * server: polyfill tty.getWindowSize close #1815 * correctly polyfill getWindowSize for stdout and stderr, dont touch tty - use our terminal-size wrapper * fix not having coffeescript available yet --- packages/server/lib/util/env.coffee | 11 ------- packages/server/lib/util/env.js | 13 ++++++++ packages/server/lib/util/terminal-size.coffee | 15 ---------- packages/server/lib/util/terminal-size.js | 17 +++++++++++ packages/server/lib/util/tty.js | 21 +++++++++++++ packages/server/test/unit/tty_spec.coffee | 30 +++++++++++++++++++ 6 files changed, 81 insertions(+), 26 deletions(-) delete mode 100644 packages/server/lib/util/env.coffee create mode 100644 packages/server/lib/util/env.js delete mode 100644 packages/server/lib/util/terminal-size.coffee create mode 100644 packages/server/lib/util/terminal-size.js diff --git a/packages/server/lib/util/env.coffee b/packages/server/lib/util/env.coffee deleted file mode 100644 index b1f6b8588c30..000000000000 --- a/packages/server/lib/util/env.coffee +++ /dev/null @@ -1,11 +0,0 @@ -set = (key, val) -> - process.env[key] = val - -get = (key) -> - process.env[key] - -module.exports = { - set - - get -} diff --git a/packages/server/lib/util/env.js b/packages/server/lib/util/env.js new file mode 100644 index 000000000000..7a2a44c60427 --- /dev/null +++ b/packages/server/lib/util/env.js @@ -0,0 +1,13 @@ +const set = (key, val) => { + return process.env[key] = val +} + +const get = (key) => { + return process.env[key] +} + +module.exports = { + set, + + get, +} diff --git a/packages/server/lib/util/terminal-size.coffee b/packages/server/lib/util/terminal-size.coffee deleted file mode 100644 index 9349a3f49790..000000000000 --- a/packages/server/lib/util/terminal-size.coffee +++ /dev/null @@ -1,15 +0,0 @@ -termSize = require("term-size") -env = require("./env") - -get = -> - obj = termSize() - - if env.get("CI") - ## reset to 100 - obj.columns = 100 - - obj - -module.exports = { - get -} diff --git a/packages/server/lib/util/terminal-size.js b/packages/server/lib/util/terminal-size.js new file mode 100644 index 000000000000..b32f4cdd28e0 --- /dev/null +++ b/packages/server/lib/util/terminal-size.js @@ -0,0 +1,17 @@ +const termSize = require('term-size') +const env = require('./env') + +const get = () => { + const obj = termSize() + + if (env.get('CI')) { + // reset to 100 + obj.columns = 100 + } + + return obj +} + +module.exports = { + get, +} diff --git a/packages/server/lib/util/tty.js b/packages/server/lib/util/tty.js index a7e68ec9d6af..2305278f7d39 100644 --- a/packages/server/lib/util/tty.js +++ b/packages/server/lib/util/tty.js @@ -1,4 +1,13 @@ const tty = require('tty') +const terminalSize = require('./terminal-size') + +// polyfills node's getWindowSize +// by returning an array of columns/rows +function getWindowSize () { + const { columns, rows } = terminalSize.get() + + return [columns, rows] +} function patchStream (patched, name) { const stream = process[name] @@ -17,6 +26,16 @@ const override = () => { 2: false, } + // polyfill in node's getWindowSize + // if it doesn't exist on stdout and stdin + // (if we are a piped process) or we are + // in windows on electron + ;['stdout', 'stderr'].forEach((fn) => { + if (!process[fn].getWindowSize) { + process[fn].getWindowSize = getWindowSize + } + }) + tty.isatty = function (fd) { if (patched[fd]) { // force stderr to return true @@ -36,4 +55,6 @@ const override = () => { module.exports = { override, + + getWindowSize, } diff --git a/packages/server/test/unit/tty_spec.coffee b/packages/server/test/unit/tty_spec.coffee index 2c39a7a624fb..d45eb4517a3d 100644 --- a/packages/server/test/unit/tty_spec.coffee +++ b/packages/server/test/unit/tty_spec.coffee @@ -2,10 +2,40 @@ require("../spec_helper") tty = require("tty") ttyUtil = require("#{root}lib/util/tty") +terminalSize = require("#{root}lib/util/terminal-size") ttys = [process.stdin.isTTY, process.stdout.isTTY, process.stderr.isTTY] describe "lib/util/tty", -> + context "getWindowSize", -> + ## https://github.com/cypress-io/cypress/issues/1815 + ## windows has undefined process.stdout.getWindowSize + ## when running in electron, even when stdio inherited + beforeEach -> + ## need to delete both the initial module and the + ## "base.js" module with problematic tty.getWindowSize call + delete require.cache[require.resolve("mocha/lib/reporters/base")] + delete require.cache[require.resolve("mocha/lib/reporters")] + + it "polyfills stdout and stderr getWindowSize", -> + sinon.stub(tty, "isatty").returns(true) + sinon.stub(terminalSize, "get").returns({ columns: 10, rows: 20 }) + + sinon.stub(process.stdout, "getWindowSize").value(undefined) + sinon.stub(process.stderr, "getWindowSize").value(undefined) + + ttyUtil.override() + + ## forces mocha reporters base to use tty.getWindowSize() + ## check the terminal width - should be the ttyUtil hardcoded + require("mocha/lib/reporters") + base = require("mocha/lib/reporters/base") + + expect(process.stdout.getWindowSize()).to.deep.eq([10, 20]) + expect(process.stderr.getWindowSize()).to.deep.eq([10, 20]) + + expect(base.window.width).to.equal(10) + context ".override", -> beforeEach -> process.env.FORCE_STDIN_TTY = '1'