Skip to content

Commit

Permalink
Fix bug with non-tty checks and when. (#896)
Browse files Browse the repository at this point in the history
  • Loading branch information
jhorbulyk authored Mar 4, 2020
1 parent 65a4d59 commit ae97f39
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 31 deletions.
10 changes: 0 additions & 10 deletions packages/inquirer/lib/inquirer.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,6 @@ inquirer.ui = {
*/
inquirer.createPromptModule = function(opt) {
var promptModule = function(questions) {
// Check if prompt is being called in TTY environment
// If it isn't return a failed promise
if (!process.stdin.isTTY) {
const nonTtyError = new Error(
'Prompts can not be meaningfully rendered in non-TTY environments'
);
nonTtyError.isTtyError = true;
return Promise.reject(nonTtyError);
}

var ui = new inquirer.ui.Prompt(promptModule.prompts, opt);
var promise = ui.run(questions);

Expand Down
10 changes: 10 additions & 0 deletions packages/inquirer/lib/ui/prompt.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,16 @@ class PromptUI extends Base {
}

fetchAnswer(question) {
// Check if prompt is being called in TTY environment
// If it isn't return a failed promise
if (!process.stdin.isTTY) {
const nonTtyError = new Error(
'Prompts can not be meaningfully rendered in non-TTY environments'
);
nonTtyError.isTtyError = true;
return Promise.reject(nonTtyError);
}

var Prompt = this.prompts[question.type];
this.activePrompt = new Prompt(question, this.rl, this.answers);
return defer(() =>
Expand Down
69 changes: 48 additions & 21 deletions packages/inquirer/test/specs/inquirer.js
Original file line number Diff line number Diff line change
Expand Up @@ -674,31 +674,58 @@ describe('inquirer.prompt', function() {
});
});

it('Throw an exception when run in non-tty', function() {
var original = process.stdin.isTTY;
delete process.stdin.isTTY;
describe('Non-TTY checks', function() {
let original;

var prompt = inquirer.createPromptModule();
before(function() {
original = process.stdin.isTTY;
delete process.stdin.isTTY;
});

var prompts = [
{
type: 'confirm',
name: 'q1',
message: 'message'
}
];
after(function() {
process.stdin.isTTY = original;
});

var promise = prompt(prompts);
it('Throw an exception when run in non-tty', function() {
var prompt = inquirer.createPromptModule();

var prompts = [
{
type: 'confirm',
name: 'q1',
message: 'message'
}
];

var promise = prompt(prompts);

return promise
.then(() => {
// Failure
expect(true).to.equal(false);
})
.catch(error => {
expect(error.isTtyError).to.equal(true);
});
});

it('No exception when when flags non-tty', function() {
var prompt = inquirer.createPromptModule();

return promise
.then(() => {
// Failure
process.stdin.isTTY = original;
expect(true).to.equal(false);
})
.catch(error => {
process.stdin.isTTY = original;
expect(error.isTtyError).to.equal(true);
var prompts = [
{
name: 'name',
message: 'give a name to your app',
default: 'foo',
when: () => false
}
];

var promise = prompt(prompts);

return promise.then(answers => {
expect(answers).to.deep.equal({});
});
});
});
});

0 comments on commit ae97f39

Please sign in to comment.