-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from BenoitZugmeyer/saner-parse-arguments-parsing
improve: saner arguments parsing
- Loading branch information
Showing
9 changed files
with
193 additions
and
188 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,68 +1,89 @@ | ||
import { test, mock } from "node:test"; | ||
import { test, describe } from "node:test"; | ||
import type { TestContext } from "node:test"; | ||
|
||
import parseArguments from "../parseArguments.ts"; | ||
import CLIError from "../CLIError.ts"; | ||
|
||
test("parseArguments", (t: TestContext) => { | ||
{ | ||
const { stdout, exit } = withStdout(() => { | ||
parseArguments(`x x`.split(" ")); | ||
describe("parseArguments", () => { | ||
test("empty arguments", (t: TestContext) => { | ||
t.assert.throws( | ||
() => parseArguments([]), | ||
new CLIError( | ||
"Missing positional argument URL:LINE:COLUMN. Use --help for documentation.", | ||
), | ||
); | ||
}); | ||
|
||
test("--version", (t: TestContext) => { | ||
t.assert.deepStrictEqual(parseArguments(["--version"]), { | ||
command: "version", | ||
}); | ||
t.assert.snapshot(stdout); | ||
t.assert.deepStrictEqual(exit, { | ||
status: 1, | ||
t.assert.deepStrictEqual(parseArguments(["-V"]), { | ||
command: "version", | ||
}); | ||
} | ||
}); | ||
|
||
{ | ||
const { stdout, exit } = withStdout(() => { | ||
parseArguments(`--version`.split(" ")); | ||
test("--help", (t: TestContext) => { | ||
t.assert.deepStrictEqual(parseArguments(["--help"]), { | ||
command: "help", | ||
}); | ||
t.assert.deepStrictEqual(parseArguments(["--help", "toto"]), { | ||
command: "help", | ||
}); | ||
t.assert.deepStrictEqual(exit, { status: 0 }); | ||
t.assert.match(stdout, /^\d+\.\d+\.\d+.*\n$/); | ||
} | ||
t.assert.deepStrictEqual(parseArguments(["toto", "--help"]), { | ||
command: "help", | ||
}); | ||
t.assert.deepStrictEqual(parseArguments(["-h"]), { | ||
command: "help", | ||
}); | ||
}); | ||
|
||
t.assert.deepStrictEqual(parseArguments(`https://foo.com:1:1`.split(" ")), { | ||
debug: false, | ||
sourceURL: "https://foo.com", | ||
position: { line: 1, column: 1 }, | ||
beforeContext: 5, | ||
afterContext: 5, | ||
useSourceMap: true, | ||
test("defaults", (t: TestContext) => { | ||
t.assert.deepStrictEqual(parseArguments([`https://foo.com:1:1`]), { | ||
command: "context", | ||
configuration: { | ||
debug: false, | ||
sourceURL: "https://foo.com", | ||
position: { line: 1, column: 1 }, | ||
useSourceMap: true, | ||
beforeContext: 5, | ||
afterContext: 5, | ||
}, | ||
}); | ||
}); | ||
|
||
const parsedArguments = parseArguments(`-C 2 https://foo.com:1:1`.split(" ")); | ||
t.assert.strictEqual(parsedArguments.beforeContext, 2); | ||
t.assert.strictEqual(parsedArguments.afterContext, 2); | ||
test("URL and position", (t: TestContext) => { | ||
const parsedArguments = parseArguments([`https://foo.com:42:12`]); | ||
t.assert.strictEqual(parsedArguments.command, "context"); | ||
t.assert.strictEqual( | ||
parsedArguments.configuration.sourceURL, | ||
"https://foo.com", | ||
); | ||
t.assert.deepStrictEqual(parsedArguments.configuration.position, { | ||
line: 42, | ||
column: 12, | ||
}); | ||
}); | ||
|
||
t.assert.strictEqual( | ||
parseArguments(`-d https://foo.com:1:1`.split(" ")).debug, | ||
true, | ||
); | ||
}); | ||
test("use source maps", (t: TestContext) => { | ||
const parsedArguments = parseArguments([ | ||
`--no-source-map`, | ||
`https://foo.com:1:1`, | ||
]); | ||
t.assert.strictEqual(parsedArguments.command, "context"); | ||
t.assert.strictEqual(parsedArguments.configuration.useSourceMap, false); | ||
}); | ||
|
||
function withStdout(fn: () => void) { | ||
// Make sure to restore the mocks as soon as possible so nodejs test runner can actually write to | ||
// stdout. | ||
const writeMock = mock.method(process.stdout, "write", () => true); | ||
const exitMock = mock.method(process, "exit", () => { | ||
throw new Error("exit"); | ||
test("context", (t: TestContext) => { | ||
const parsedArguments = parseArguments([`-C`, `2`, `https://foo.com:1:1`]); | ||
t.assert.strictEqual(parsedArguments.command, "context"); | ||
t.assert.strictEqual(parsedArguments.configuration.beforeContext, 2); | ||
t.assert.strictEqual(parsedArguments.configuration.afterContext, 2); | ||
}); | ||
let result; | ||
try { | ||
fn(); | ||
} catch { | ||
// Ignore | ||
} finally { | ||
result = { | ||
stdout: writeMock.mock.calls.map((call) => call.arguments[0]).join(""), | ||
exit: | ||
exitMock.mock.calls.length > 0 | ||
? { status: exitMock.mock.calls[0].arguments[0] } | ||
: undefined, | ||
}; | ||
exitMock.mock.restore(); | ||
writeMock.mock.restore(); | ||
} | ||
return result; | ||
} | ||
|
||
test("--debug", (t: TestContext) => { | ||
const parsedArguments = parseArguments([`--debug`, `https://foo.com:1:1`]); | ||
t.assert.strictEqual(parsedArguments.command, "context"); | ||
t.assert.strictEqual(parsedArguments.configuration.debug, true); | ||
}); | ||
}); |
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.