Skip to content

Commit

Permalink
server: polyfill tty.getWindowSize close #1815 (#1828)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
bahmutov authored and brian-mann committed May 31, 2018
1 parent a4fa1b3 commit 27ed1dc
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 26 deletions.
11 changes: 0 additions & 11 deletions packages/server/lib/util/env.coffee

This file was deleted.

13 changes: 13 additions & 0 deletions packages/server/lib/util/env.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const set = (key, val) => {
return process.env[key] = val
}

const get = (key) => {
return process.env[key]
}

module.exports = {
set,

get,
}
15 changes: 0 additions & 15 deletions packages/server/lib/util/terminal-size.coffee

This file was deleted.

17 changes: 17 additions & 0 deletions packages/server/lib/util/terminal-size.js
Original file line number Diff line number Diff line change
@@ -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,
}
21 changes: 21 additions & 0 deletions packages/server/lib/util/tty.js
Original file line number Diff line number Diff line change
@@ -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]
Expand All @@ -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
Expand All @@ -36,4 +55,6 @@ const override = () => {

module.exports = {
override,

getWindowSize,
}
30 changes: 30 additions & 0 deletions packages/server/test/unit/tty_spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down

0 comments on commit 27ed1dc

Please sign in to comment.