Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(command): fix allowEmpty and disable it by default (#352)
- Loading branch information
Showing
8 changed files
with
211 additions
and
154 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,35 @@ | ||
import { assertEquals, assertRejects } from "../../../dev_deps.ts"; | ||
import { Command } from "../../command.ts"; | ||
|
||
Deno.test("flags allowEmpty enabled", async () => { | ||
const { options, args } = await new Command() | ||
function cmd() { | ||
return new Command() | ||
.throwErrors() | ||
.allowEmpty(true) | ||
.option("-f, --flag [value:boolean]", "description ...") | ||
.action(() => {}) | ||
.parse(["-f"]); | ||
.option("-f, --flag [value:boolean]", "description ...", { | ||
required: true, | ||
}); | ||
} | ||
|
||
assertEquals(options, { flag: true }); | ||
assertEquals(args, []); | ||
Deno.test("[flags] should not allow empty by default", async () => { | ||
await assertRejects( | ||
() => cmd().parse([]), | ||
Error, | ||
'Missing required option "--flag".', | ||
); | ||
}); | ||
|
||
Deno.test("flags allowEmpty disabledNoFlags", async () => { | ||
const { options, args } = await new Command() | ||
.throwErrors() | ||
Deno.test("[flags] should not allow empty if disabled", async () => { | ||
await assertRejects( | ||
() => cmd().allowEmpty(false).parse([]), | ||
Error, | ||
'Missing required option "--flag".', | ||
); | ||
}); | ||
|
||
Deno.test("[flags] should allow empty if enabled", async () => { | ||
const { options, args } = await cmd() | ||
.allowEmpty(true) | ||
.action(() => {}) | ||
.parse([]); | ||
|
||
assertEquals(options, {}); | ||
assertEquals(args, []); | ||
}); | ||
|
||
Deno.test("flags allowEmpty disabled", async () => { | ||
const cmd = new Command() | ||
.throwErrors() | ||
.allowEmpty(false) | ||
.option("-f, --flag [value:boolean]", "description ...") | ||
.action(() => {}); | ||
|
||
await assertRejects( | ||
async () => { | ||
await cmd.parse([]); | ||
}, | ||
Error, | ||
"No arguments.", | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,44 @@ | ||
import { assertThrows } from "../../../dev_deps.ts"; | ||
import { assertEquals, assertThrows } from "../../../dev_deps.ts"; | ||
import { parseFlags } from "../../flags.ts"; | ||
|
||
Deno.test("flags allowEmpty enabled", () => { | ||
parseFlags([], { | ||
allowEmpty: true, | ||
flags: [{ | ||
name: "flag", | ||
aliases: ["f"], | ||
}], | ||
}); | ||
}); | ||
|
||
Deno.test("flags allowEmpty noFlags", () => { | ||
parseFlags([], { | ||
allowEmpty: true, | ||
flags: [], | ||
}); | ||
}); | ||
|
||
Deno.test("flags allowEmpty disabledNoFlags", () => { | ||
parseFlags([], { | ||
allowEmpty: false, | ||
flags: [], | ||
}); | ||
Deno.test("[flags] should not allow empty by default", () => { | ||
assertThrows( | ||
() => | ||
parseFlags([], { | ||
flags: [{ | ||
name: "flag", | ||
required: true, | ||
}], | ||
}), | ||
Error, | ||
'Missing required option "--flag".', | ||
); | ||
}); | ||
|
||
Deno.test("flags allowEmpty disabled", () => { | ||
Deno.test("[flags] should not allow empty if disabled", () => { | ||
assertThrows( | ||
() => | ||
parseFlags([], { | ||
allowEmpty: false, | ||
flags: [{ | ||
name: "flag", | ||
aliases: ["f"], | ||
required: true, | ||
}], | ||
}), | ||
Error, | ||
"No arguments.", | ||
'Missing required option "--flag".', | ||
); | ||
}); | ||
|
||
Deno.test("[flags] should allow empty if enabled", () => { | ||
const { flags, unknown } = parseFlags([], { | ||
allowEmpty: true, | ||
flags: [{ | ||
name: "flag", | ||
required: true, | ||
}], | ||
}); | ||
|
||
assertEquals(flags, {}); | ||
assertEquals(unknown, []); | ||
}); |
Oops, something went wrong.