Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not print logs if print command is used. #5728

Merged
merged 4 commits into from Jan 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/Serverless.js
Expand Up @@ -81,6 +81,7 @@ class Serverless {
if (this.cli.displayHelp(this.processedInput)) { if (this.cli.displayHelp(this.processedInput)) {
return BbPromise.resolve(); return BbPromise.resolve();
} }
this.cli.suppressLogIfPrintCommand(this.processedInput);


// make sure the command exists before doing anything else // make sure the command exists before doing anything else
this.pluginManager.validateCommand(this.processedInput.commands); this.pluginManager.validateCommand(this.processedInput.commands);
Expand Down
24 changes: 24 additions & 0 deletions lib/classes/CLI.js
Expand Up @@ -79,6 +79,30 @@ class CLI {
return { commands, options }; return { commands, options };
} }


suppressLogIfPrintCommand(processedInput) {
const commands = processedInput.commands;
const options = processedInput.options;

// if "-help" or "-h" was entered
if (options.help || options.h) {
return;
}

// if "print" was NOT entered
if (commands.indexOf('print') === -1) {
return;
}

// if other command was combined with "print"
if (commands.length !== 1) {
return;
}

// Make "log" no-op to suppress warnings.
// But preserve "consoleLog" which "print" command use to print config.
this.log = function () {};
}

displayHelp(processedInput) { displayHelp(processedInput) {
const commands = processedInput.commands; const commands = processedInput.commands;
const options = processedInput.options; const options = processedInput.options;
Expand Down
80 changes: 80 additions & 0 deletions lib/classes/CLI.test.js
Expand Up @@ -72,6 +72,86 @@ describe('CLI', () => {
}); });
}); });


describe('#suppressLogIfPrintCommand()', () => {
let logStub;
let consoleLogStub;

beforeEach(() => {
logStub = sinon.stub();
consoleLogStub = sinon.stub();
});

it('should do nothing when no command is given', () => {
cli = new CLI(serverless, []);
cli.log = logStub;
cli.consoleLog = consoleLogStub;

const processedInput = cli.processInput();
cli.suppressLogIfPrintCommand(processedInput);
cli.log('logged');
cli.consoleLog('logged');

expect(logStub.calledOnce).to.equal(true);
expect(consoleLogStub.calledOnce).to.equal(true);
});

it('should do nothing when "print" is given with "-h"', () => {
cli = new CLI(serverless, ['print', '-h']);
cli.log = logStub;
cli.consoleLog = consoleLogStub;

const processedInput = cli.processInput();
cli.suppressLogIfPrintCommand(processedInput);
cli.log('logged');
cli.consoleLog('logged');

expect(logStub.calledOnce).to.equal(true);
expect(consoleLogStub.calledOnce).to.equal(true);
});

it('should do nothing when "print" is given with "-help"', () => {
cli = new CLI(serverless, ['print', '-help']);
cli.log = logStub;
cli.consoleLog = consoleLogStub;

const processedInput = cli.processInput();
cli.suppressLogIfPrintCommand(processedInput);
cli.log('logged');
cli.consoleLog('logged');

expect(logStub.calledOnce).to.equal(true);
expect(consoleLogStub.calledOnce).to.equal(true);
});

it('should do nothing when "print" is combined with other command.', () => {
cli = new CLI(serverless, ['other', 'print']);
cli.log = logStub;
cli.consoleLog = consoleLogStub;

const processedInput = cli.processInput();
cli.suppressLogIfPrintCommand(processedInput);
cli.log('logged');
cli.consoleLog('logged');

expect(logStub.calledOnce).to.equal(true);
expect(consoleLogStub.calledOnce).to.equal(true);
});

it('should suppress log when "print" is given', () => {
cli = new CLI(serverless, ['print', '-myvar', '123']);
cli.log = logStub;
cli.consoleLog = consoleLogStub;

const processedInput = cli.processInput();
cli.suppressLogIfPrintCommand(processedInput);
cli.log('NOT LOGGED');
cli.consoleLog('logged');

expect(logStub.calledOnce).to.equal(false);
expect(consoleLogStub.calledOnce).to.equal(true);
});
});

describe('#displayHelp()', () => { describe('#displayHelp()', () => {
it('should return true when no command is given', () => { it('should return true when no command is given', () => {
cli = new CLI(serverless, []); cli = new CLI(serverless, []);
Expand Down