Skip to content

Commit

Permalink
🎨 add warning when -d is provided instead of -D (#1813)
Browse files Browse the repository at this point in the history
  • Loading branch information
vikaspotluri123 authored Oct 16, 2024
1 parent 677af32 commit 0377086
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/command.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit 0377086

Please sign in to comment.