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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

馃帹 add warning when -d is provided instead of -D #1813

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
10 changes: 10 additions & 0 deletions lib/command.js
Expand Up @@ -119,6 +119,16 @@ class Command {

// This needs to run before the installation check
if (argv.dir) {
if (argv.dir === true) {
// CASE: the short-form dir flag was provided, and a development flag was not provided in any form
// --> This is probably a typo
const help = process.argv.includes('-d') && !('development' in argv)
? '. Did you mean -D?'
: '';
ui.log('Invalid directory provided' + help, 'red', true);
process.exit(1);
}

debug('Directory specified, attempting to update');
const path = require('path');
const dir = path.resolve(argv.dir);
Expand Down
48 changes: 48 additions & 0 deletions test/unit/command-spec.js
Expand Up @@ -265,6 +265,54 @@ describe('Unit: Command', function () {
}
});

it('Errors when a boolean directory is provided', async function () {
sinon.stub(process, 'exit').throws(new Error('exit_stub'));
const outStub = sinon.stub(process.stderr, 'write');
const Command = require(modulePath);
class TestCommand extends Command {}

let errorCount = 0;

const fail = () => {
throw new Error('Should have errored');
};

const assertError = (error, expectHelp, assertionContext) => {
errorCount += 1;
expect(error).to.be.ok;
expect(error.message, assertionContext).to.equal('exit_stub');
expect(outStub.callCount, assertionContext).to.equal(errorCount);

let assertion = expect(outStub.args[errorCount - 1][0], assertionContext).to;

if (!expectHelp) {
assertion = assertion.not;
}

assertion.contain('Did you mean -D');
};

const originalProcessArgv = process.argv;

try {
// Don't expect a help message when the long-form flag is used
await TestCommand._run('test', {dir: true})
.then(fail)
.catch(e => assertError(e, false, 'ghost test --dir'));

process.argv.push('-d');
await TestCommand._run('test', {dir: true, development: true})
.then(fail)
.catch(e => assertError(e, false, 'ghost test -d --development'));

await TestCommand._run('test', {dir: true})
.then(fail)
.catch(e => assertError(e, true, 'ghost test -d'));
} finally {
process.argv = originalProcessArgv;
}
});

it('Changes directory if needed', async function () {
sinon.stub(process, 'exit').throws(new Error('exit_stub'));
const outStub = sinon.stub(process.stderr, 'write');
Expand Down