forked from prettier/prettier-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: migrate
plugin-options-string
tests
Copies the tests from prettier. Notable changes: - A failed plugin load throws an uncaught exception before this change. This catches it and logs an error to stderr - A `snapshot-diff` package was being used to create a `git diff` like diff as a jest snapshot. We can pick the unique lines out instead of using this Skipped tests: - `--help {option}` is not supported in prettier CLI - `--config-path` with stdin is not yet supported (see prettier#21) Notable hiccups: - Parsing argv seems to fall over on things like `--plugin=--foo--`, so the test has been changed for now to use `--plugin=totally-invalid`
- Loading branch information
Showing
6 changed files
with
152 additions
and
4 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
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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"plugins": ["./plugin.cjs"], | ||
"fooString": "baz" | ||
} |
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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
"use strict"; | ||
|
||
module.exports = { | ||
languages: [ | ||
{ | ||
name: "foo", | ||
parsers: ["foo-parser"], | ||
extensions: [".foo"], | ||
since: "1.0.0", | ||
}, | ||
], | ||
options: { | ||
fooString: { | ||
type: "string", | ||
default: "bar", | ||
description: "foo description", | ||
}, | ||
}, | ||
parsers: { | ||
"foo-parser": { | ||
parse: (text) => ({ text }), | ||
astFormat: "foo-ast", | ||
}, | ||
}, | ||
printers: { | ||
"foo-ast": { | ||
print: (path, options) => | ||
options.fooString ? `foo:${options.fooString}` : path.getValue().text, | ||
}, | ||
}, | ||
}; |
18 changes: 18 additions & 0 deletions
18
test/__tests__/__snapshots__/plugin-options-string.js.snap
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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`show detailed external option with \`--help foo-string\` (stderr) 1`] = `""`; | ||
|
||
exports[`show detailed external option with \`--help foo-string\` (stdout) 1`] = ` | ||
"--foo-string <string> | ||
foo description | ||
Default: bar" | ||
`; | ||
exports[`show detailed external option with \`--help foo-string\` (write) 1`] = `[]`; | ||
exports[`show external options with \`--help\` 1`] = ` | ||
" --foo-string <value> foo description | ||
Defaults to "bar"" | ||
`; |
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 |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { runCli } from "../utils"; | ||
|
||
test("show external options with `--help`", async () => { | ||
const originalStdout = await runCli("plugin-options-string", ["--help"]) | ||
.stdout; | ||
const pluggedStdout = await runCli("plugin-options-string", [ | ||
"--help", | ||
"--plugin=./plugin.cjs", | ||
]).stdout; | ||
const originalLines = originalStdout.split("\n"); | ||
const pluggedLines = pluggedStdout.split("\n"); | ||
const differentLines = pluggedLines.filter((line) => | ||
!originalLines.includes(line)); | ||
expect(differentLines.join("\n")).toMatchSnapshot(); | ||
}); | ||
|
||
// Note (43081j); we don't currently support `--help {optionName}` | ||
describe.skip("show detailed external option with `--help foo-string`", () => { | ||
runCli("plugin-options-string", [ | ||
"--plugin=./plugin.cjs", | ||
"--help", | ||
"foo-string", | ||
]).test({ | ||
status: 0, | ||
}); | ||
}); | ||
|
||
describe("external options from CLI should work", () => { | ||
runCli( | ||
"plugin-options-string", | ||
[ | ||
"--plugin=./plugin.cjs", | ||
"--stdin-filepath", | ||
"example.foo", | ||
"--foo-string", | ||
"baz", | ||
], | ||
{ input: "hello-world" }, | ||
).test({ | ||
stdout: "foo:baz", | ||
stderr: "", | ||
status: 0, | ||
write: [], | ||
}); | ||
}); | ||
|
||
// TODO (43081j): this won't work until we fix #21 | ||
describe.skip("external options from config file should work", () => { | ||
runCli( | ||
"plugin-options-string", | ||
["--config-path=./config.json", "--stdin-filepath", "example.foo"], | ||
{ input: "hello-world" }, | ||
).test({ | ||
stdout: "foo:baz", | ||
stderr: "", | ||
status: 0, | ||
write: [], | ||
}); | ||
}); | ||
|
||
describe("Non exists plugin", () => { | ||
runCli( | ||
"plugin-options-string", | ||
["--plugin=totally-invalid", "--stdin-filepath", "example.foo"], | ||
{ input: "hello-world" }, | ||
).test({ | ||
stdout: "", | ||
stderr: expect.stringMatching(/Cannot find package 'totally-invalid'/u), | ||
status: 1, | ||
write: [], | ||
}); | ||
}); |