Skip to content

Commit

Permalink
Fix failure with isMultiple when isRequired function returns `fal…
Browse files Browse the repository at this point in the history
…se` (#194)
  • Loading branch information
softwarewright committed Jul 29, 2021
1 parent 5d3bb31 commit e1f0e24
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
2 changes: 1 addition & 1 deletion index.js
Expand Up @@ -26,7 +26,7 @@ const isFlagMissing = (flagName, definedFlags, receivedFlags, input) => {
return isFlagRequired;
}

return flag.isMultiple && receivedFlags[flagName].length === 0;
return flag.isMultiple && receivedFlags[flagName].length === 0 && isFlagRequired;
};

const getMissingRequiredFlags = (flags, receivedFlags, input) => {
Expand Down
21 changes: 21 additions & 0 deletions test/fixtures/fixture-conditional-required-multiple.js
@@ -0,0 +1,21 @@
#!/usr/bin/env node
import meow from '../../index.js';

const cli = meow({
importMeta: import.meta,
description: 'Custom description',
help: `
Usage
foo <input>
`,
flags: {
test: {
type: 'number',
alias: 't',
isRequired: () => false,
isMultiple: true,
},
},
});

console.log(cli.flags.test);
10 changes: 10 additions & 0 deletions test/is-required-flag.js
Expand Up @@ -7,6 +7,11 @@ const __dirname = path.dirname(fileURLToPath(import.meta.url));
const fixtureRequiredPath = path.join(__dirname, 'fixtures', 'fixture-required.js');
const fixtureRequiredFunctionPath = path.join(__dirname, 'fixtures', 'fixture-required-function.js');
const fixtureRequiredMultiplePath = path.join(__dirname, 'fixtures', 'fixture-required-multiple.js');
const fixtureConditionalRequiredMultiplePath = path.join(
__dirname,
'fixtures',
'fixture-conditional-required-multiple.js',
);

test('spawn cli and test not specifying required flags', async t => {
try {
Expand Down Expand Up @@ -118,3 +123,8 @@ test('spawn cli and test isRequired with isMultiple giving no values, but flag i
t.regex(stderr, /--test/);
}
});

test('spawn cli and test isRequire function that returns false with isMultiple given no values, but flag is not given', async t => {
const {stdout} = await execa(fixtureConditionalRequiredMultiplePath, []);
t.is(stdout, '[]');
});

0 comments on commit e1f0e24

Please sign in to comment.