Skip to content

Commit b08cfa7

Browse files
committed
fix(ui): disable allowPrompt if stdout is not a TTY
1 parent 69b9624 commit b08cfa7

File tree

2 files changed

+45
-6
lines changed

2 files changed

+45
-6
lines changed

lib/ui/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class UI {
4242
this.stdout = this.options.stdout;
4343
this.stderr = this.options.stderr;
4444
this.verbose = this.options.verbose;
45-
this.allowPrompt = this.options.allowPrompt;
45+
this.allowPrompt = this.options.allowPrompt && Boolean(this.stdout.isTTY);
4646

4747
// Add custom prompt module that uses the
4848
// specified streams

test/unit/ui/index-spec.js

+44-5
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,48 @@ describe('Unit: UI', function () {
2121
sandbox.restore();
2222
});
2323

24-
it('can be created successfully', function () {
24+
describe('constructor', function () {
2525
const UI = require(modulePath);
26-
const ui = new UI();
2726

28-
expect(ui).to.be.ok;
27+
it('works with defaults', function () {
28+
const ui = new UI();
29+
expect(ui.stdin).to.equal(process.stdin);
30+
expect(ui.stdout).to.equal(process.stdout);
31+
expect(ui.stderr).to.equal(process.stderr);
32+
expect(ui.verbose).to.be.false;
33+
expect(ui.allowPrompt).to.be.true;
34+
});
35+
36+
it('works with custom options', function () {
37+
const stdin = {stdin: true};
38+
const stdout = {stdout: true};
39+
const stderr = {stderr: true};
40+
41+
const ui = new UI({
42+
stdin: stdin,
43+
stdout: stdout,
44+
stderr: stderr,
45+
verbose: true,
46+
allowPrompt: false
47+
});
48+
49+
expect(ui.stdin).to.equal(stdin);
50+
expect(ui.stdout).to.equal(stdout);
51+
expect(ui.stderr).to.equal(stderr);
52+
expect(ui.verbose).to.be.true;
53+
expect(ui.allowPrompt).to.be.false;
54+
});
55+
56+
it('sets allowPrompt to false if process.stdout is not a TTY', function () {
57+
const stdout = {stdout: true, isTTY: false};
58+
const ui = new UI({
59+
allowPrompt: true,
60+
stdout: stdout
61+
});
62+
63+
expect(ui.stdout).to.equal(stdout);
64+
expect(ui.allowPrompt).to.be.false;
65+
});
2966
});
3067

3168
describe('run', function () {
@@ -171,7 +208,8 @@ describe('Unit: UI', function () {
171208
const UI = require(modulePath);
172209

173210
it('fails when prompting is disabled', function (done) {
174-
const ui = new UI({allowPrompt: false});
211+
const ui = new UI();
212+
ui.allowPrompt = false;
175213
const noSpinStub = sandbox.stub(ui, 'noSpin');
176214

177215
try {
@@ -189,7 +227,8 @@ describe('Unit: UI', function () {
189227
});
190228

191229
it('passes options to prompt method', function () {
192-
const ui = new UI({allowPrompt: true});
230+
const ui = new UI();
231+
ui.allowPrompt = true;
193232
const noSpinStub = sandbox.stub(ui, 'noSpin').callsFake((fn) => fn());
194233
const inquirerStub = sandbox.stub(ui, 'inquirer').resolves();
195234

0 commit comments

Comments
 (0)