From 0f3d178da85822a1f133f1e1f531caa83c108b73 Mon Sep 17 00:00:00 2001 From: James Garbutt <43081j@users.noreply.github.com> Date: Tue, 18 Feb 2025 14:42:16 +0000 Subject: [PATCH 1/2] test: migrate `plugin-resolution` tests Copies the `plugin-resolution` tests from prettier. --- .../plugins/absolute-path/prettier.config.mjs | 7 ++ test/__fixtures__/plugins/automatic/file.txt | 1 + .../@prettier/plugin-foo/index.js | 24 ++++++ .../@prettier/plugin-foo/package.json | 1 + .../@user/prettier-plugin-foobar/index.js | 24 ++++++ .../@user/prettier-plugin-foobar/package.json | 1 + .../node_modules/prettier-plugin-bar/index.js | 26 +++++++ .../prettier-plugin-bar/package.json | 1 + .../prettier-plugin-baz.js/index.js | 30 +++++++ .../prettier-plugin-baz.js/package.json | 1 + .../plugins/automatic/prettier-plugin-baz.js | 9 +++ .../@company/prettier-plugin-bespoke/main.js | 23 ++++++ .../prettier-plugin-bespoke/package.json | 3 + .../plugins/defaultOptions/config.json | 3 + .../plugins/defaultOptions/plugin.cjs | 30 +++++++ test/__fixtures__/plugins/extensions/file.foo | 1 + .../plugins/extensions/plugin.cjs | 24 ++++++ .../plugins/extensions2/plugin.cjs | 24 ++++++ .../plugins/flushLineSuffix/file.foo | 1 + .../plugins/flushLineSuffix/plugin.cjs | 25 ++++++ .../plugins/options-string/config.json | 4 + .../plugins/options-string/plugin.cjs | 31 ++++++++ test/__fixtures__/plugins/options/config.json | 4 + test/__fixtures__/plugins/options/plugin.cjs | 41 ++++++++++ test/__fixtures__/plugins/preprocess/file.foo | 1 + .../plugins/preprocess/plugin.cjs | 24 ++++++ .../virtualDirectory/node_modules/.gitignore | 0 test/__tests__/plugin-resolution.js | 78 +++++++++++++++++++ 28 files changed, 442 insertions(+) create mode 100644 test/__fixtures__/plugins/absolute-path/prettier.config.mjs create mode 100644 test/__fixtures__/plugins/automatic/file.txt create mode 100644 test/__fixtures__/plugins/automatic/node_modules/@prettier/plugin-foo/index.js create mode 100644 test/__fixtures__/plugins/automatic/node_modules/@prettier/plugin-foo/package.json create mode 100644 test/__fixtures__/plugins/automatic/node_modules/@user/prettier-plugin-foobar/index.js create mode 100644 test/__fixtures__/plugins/automatic/node_modules/@user/prettier-plugin-foobar/package.json create mode 100644 test/__fixtures__/plugins/automatic/node_modules/prettier-plugin-bar/index.js create mode 100644 test/__fixtures__/plugins/automatic/node_modules/prettier-plugin-bar/package.json create mode 100644 test/__fixtures__/plugins/automatic/node_modules/prettier-plugin-baz.js/index.js create mode 100644 test/__fixtures__/plugins/automatic/node_modules/prettier-plugin-baz.js/package.json create mode 100644 test/__fixtures__/plugins/automatic/prettier-plugin-baz.js create mode 100644 test/__fixtures__/plugins/bespoke/node_modules/@company/prettier-plugin-bespoke/main.js create mode 100644 test/__fixtures__/plugins/bespoke/node_modules/@company/prettier-plugin-bespoke/package.json create mode 100644 test/__fixtures__/plugins/defaultOptions/config.json create mode 100644 test/__fixtures__/plugins/defaultOptions/plugin.cjs create mode 100644 test/__fixtures__/plugins/extensions/file.foo create mode 100644 test/__fixtures__/plugins/extensions/plugin.cjs create mode 100644 test/__fixtures__/plugins/extensions2/plugin.cjs create mode 100644 test/__fixtures__/plugins/flushLineSuffix/file.foo create mode 100644 test/__fixtures__/plugins/flushLineSuffix/plugin.cjs create mode 100644 test/__fixtures__/plugins/options-string/config.json create mode 100644 test/__fixtures__/plugins/options-string/plugin.cjs create mode 100644 test/__fixtures__/plugins/options/config.json create mode 100644 test/__fixtures__/plugins/options/plugin.cjs create mode 100644 test/__fixtures__/plugins/preprocess/file.foo create mode 100644 test/__fixtures__/plugins/preprocess/plugin.cjs create mode 100644 test/__fixtures__/plugins/virtualDirectory/node_modules/.gitignore create mode 100644 test/__tests__/plugin-resolution.js diff --git a/test/__fixtures__/plugins/absolute-path/prettier.config.mjs b/test/__fixtures__/plugins/absolute-path/prettier.config.mjs new file mode 100644 index 0000000..da9d1a4 --- /dev/null +++ b/test/__fixtures__/plugins/absolute-path/prettier.config.mjs @@ -0,0 +1,7 @@ +import path from 'node:path' + +export default { + plugins: [ + path.resolve('../../../../../config/prettier-plugins/prettier-plugin-uppercase-rocks/index.js') + ] +} diff --git a/test/__fixtures__/plugins/automatic/file.txt b/test/__fixtures__/plugins/automatic/file.txt new file mode 100644 index 0000000..12f00e9 --- /dev/null +++ b/test/__fixtures__/plugins/automatic/file.txt @@ -0,0 +1 @@ +contents diff --git a/test/__fixtures__/plugins/automatic/node_modules/@prettier/plugin-foo/index.js b/test/__fixtures__/plugins/automatic/node_modules/@prettier/plugin-foo/index.js new file mode 100644 index 0000000..6fd8b0c --- /dev/null +++ b/test/__fixtures__/plugins/automatic/node_modules/@prettier/plugin-foo/index.js @@ -0,0 +1,24 @@ +"use strict"; + +module.exports = { + languages: [ + { + name: "foo", + parsers: ["foo"], + extensions: [".foo"], + }, + ], + parsers: { + foo: { + parse: text => ({ text }), + astFormat: "foo", + }, + }, + printers: { + foo: { + print(path) { + return ["foo+", path.getValue().text]; + }, + }, + }, +}; diff --git a/test/__fixtures__/plugins/automatic/node_modules/@prettier/plugin-foo/package.json b/test/__fixtures__/plugins/automatic/node_modules/@prettier/plugin-foo/package.json new file mode 100644 index 0000000..b731bd6 --- /dev/null +++ b/test/__fixtures__/plugins/automatic/node_modules/@prettier/plugin-foo/package.json @@ -0,0 +1 @@ +{"type": "commonjs"} diff --git a/test/__fixtures__/plugins/automatic/node_modules/@user/prettier-plugin-foobar/index.js b/test/__fixtures__/plugins/automatic/node_modules/@user/prettier-plugin-foobar/index.js new file mode 100644 index 0000000..bd7f142 --- /dev/null +++ b/test/__fixtures__/plugins/automatic/node_modules/@user/prettier-plugin-foobar/index.js @@ -0,0 +1,24 @@ +"use strict"; + +module.exports = { + languages: [ + { + name: "foobar", + parsers: ["foobar"], + extensions: [".foobar"], + }, + ], + parsers: { + foobar: { + parse: text => ({ text }), + astFormat: "foobar", + }, + }, + printers: { + foobar: { + print(path) { + return ["foobar+", path.getValue().text]; + }, + }, + }, +}; diff --git a/test/__fixtures__/plugins/automatic/node_modules/@user/prettier-plugin-foobar/package.json b/test/__fixtures__/plugins/automatic/node_modules/@user/prettier-plugin-foobar/package.json new file mode 100644 index 0000000..b731bd6 --- /dev/null +++ b/test/__fixtures__/plugins/automatic/node_modules/@user/prettier-plugin-foobar/package.json @@ -0,0 +1 @@ +{"type": "commonjs"} diff --git a/test/__fixtures__/plugins/automatic/node_modules/prettier-plugin-bar/index.js b/test/__fixtures__/plugins/automatic/node_modules/prettier-plugin-bar/index.js new file mode 100644 index 0000000..e4bf5c9 --- /dev/null +++ b/test/__fixtures__/plugins/automatic/node_modules/prettier-plugin-bar/index.js @@ -0,0 +1,26 @@ +"use strict"; + +module.exports = { + languages: [ + { + name: "bar", + parsers: ["bar"], + }, + ], + parsers: { + bar: { + parse: text => ({ text }), + astFormat: "bar", + }, + }, + printers: { + bar: { + print(path) { + return ["content from `prettier-plugin-bar` package + ", path.getValue().text]; + }, + }, + }, + defaultOptions: { + tabWidth: 4, + }, +}; diff --git a/test/__fixtures__/plugins/automatic/node_modules/prettier-plugin-bar/package.json b/test/__fixtures__/plugins/automatic/node_modules/prettier-plugin-bar/package.json new file mode 100644 index 0000000..b731bd6 --- /dev/null +++ b/test/__fixtures__/plugins/automatic/node_modules/prettier-plugin-bar/package.json @@ -0,0 +1 @@ +{"type": "commonjs"} diff --git a/test/__fixtures__/plugins/automatic/node_modules/prettier-plugin-baz.js/index.js b/test/__fixtures__/plugins/automatic/node_modules/prettier-plugin-baz.js/index.js new file mode 100644 index 0000000..04dbc89 --- /dev/null +++ b/test/__fixtures__/plugins/automatic/node_modules/prettier-plugin-baz.js/index.js @@ -0,0 +1,30 @@ +"use strict"; + +module.exports = { + languages: [ + { + name: "baz", + parsers: ["baz"], + }, + ], + parsers: { + baz: { + parse: text => ({ text }), + astFormat: "baz", + }, + }, + printers: { + baz: { + async print(path) { + const { default: prettier } = await import( + "../../../../../config/prettier-entry.js" + ); + const { concat } = prettier.doc.builders; + return concat(["content from `prettier-plugin-baz.js` package + ", path.getValue().text]); + }, + }, + }, + defaultOptions: { + tabWidth: 4, + }, +}; diff --git a/test/__fixtures__/plugins/automatic/node_modules/prettier-plugin-baz.js/package.json b/test/__fixtures__/plugins/automatic/node_modules/prettier-plugin-baz.js/package.json new file mode 100644 index 0000000..b731bd6 --- /dev/null +++ b/test/__fixtures__/plugins/automatic/node_modules/prettier-plugin-baz.js/package.json @@ -0,0 +1 @@ +{"type": "commonjs"} diff --git a/test/__fixtures__/plugins/automatic/prettier-plugin-baz.js b/test/__fixtures__/plugins/automatic/prettier-plugin-baz.js new file mode 100644 index 0000000..61fa15f --- /dev/null +++ b/test/__fixtures__/plugins/automatic/prettier-plugin-baz.js @@ -0,0 +1,9 @@ +import createPlugin from "../../../config/utils/create-plugin.cjs"; + +const plugin = createPlugin({ + name: "baz", + print: (text) => `content from \`prettier-plugin-baz.js\` file + ${text}`, + finalNewLine: false, +}); + +export default plugin; diff --git a/test/__fixtures__/plugins/bespoke/node_modules/@company/prettier-plugin-bespoke/main.js b/test/__fixtures__/plugins/bespoke/node_modules/@company/prettier-plugin-bespoke/main.js new file mode 100644 index 0000000..7af4f02 --- /dev/null +++ b/test/__fixtures__/plugins/bespoke/node_modules/@company/prettier-plugin-bespoke/main.js @@ -0,0 +1,23 @@ +"use strict"; + +module.exports = { + languages: [ + { + name: "bespoke", + parsers: ["bespoke"], + }, + ], + parsers: { + bespoke: { + parse: text => ({ text }), + astFormat: "bespoke" + }, + }, + printers: { + bespoke: { + print(path) { + return ["bespoke+", path.getValue().text]; + }, + }, + }, +}; diff --git a/test/__fixtures__/plugins/bespoke/node_modules/@company/prettier-plugin-bespoke/package.json b/test/__fixtures__/plugins/bespoke/node_modules/@company/prettier-plugin-bespoke/package.json new file mode 100644 index 0000000..2af9e0d --- /dev/null +++ b/test/__fixtures__/plugins/bespoke/node_modules/@company/prettier-plugin-bespoke/package.json @@ -0,0 +1,3 @@ +{ + "main": "./main.js" +} diff --git a/test/__fixtures__/plugins/defaultOptions/config.json b/test/__fixtures__/plugins/defaultOptions/config.json new file mode 100644 index 0000000..37ed1cf --- /dev/null +++ b/test/__fixtures__/plugins/defaultOptions/config.json @@ -0,0 +1,3 @@ +{ + "plugins": ["./plugin"] +} diff --git a/test/__fixtures__/plugins/defaultOptions/plugin.cjs b/test/__fixtures__/plugins/defaultOptions/plugin.cjs new file mode 100644 index 0000000..3f08e6f --- /dev/null +++ b/test/__fixtures__/plugins/defaultOptions/plugin.cjs @@ -0,0 +1,30 @@ +"use strict"; + +module.exports = { + languages: [ + { + name: "foo", + parsers: ["foo-parser"], + extensions: [".foo"], + }, + ], + defaultOptions: { + tabWidth: 8, + bracketSpacing: false, + }, + parsers: { + "foo-parser": { + parse: (text) => ({ text }), + astFormat: "foo-ast", + }, + }, + printers: { + "foo-ast": { + print: (path, options) => + JSON.stringify({ + tabWidth: options.tabWidth, + bracketSpacing: options.bracketSpacing, + }), + }, + }, +}; diff --git a/test/__fixtures__/plugins/extensions/file.foo b/test/__fixtures__/plugins/extensions/file.foo new file mode 100644 index 0000000..12f00e9 --- /dev/null +++ b/test/__fixtures__/plugins/extensions/file.foo @@ -0,0 +1 @@ +contents diff --git a/test/__fixtures__/plugins/extensions/plugin.cjs b/test/__fixtures__/plugins/extensions/plugin.cjs new file mode 100644 index 0000000..be9f6c9 --- /dev/null +++ b/test/__fixtures__/plugins/extensions/plugin.cjs @@ -0,0 +1,24 @@ +"use strict"; + +module.exports = { + languages: [ + { + name: "foo", + parsers: ["foo-parser"], + extensions: [".foo"], + }, + ], + parsers: { + "foo-parser": { + parse: (text) => ({ text }), + astFormat: "foo-ast", + }, + }, + printers: { + "foo-ast": { + print(path) { + return ["!", path.getValue().text]; + }, + }, + }, +}; diff --git a/test/__fixtures__/plugins/extensions2/plugin.cjs b/test/__fixtures__/plugins/extensions2/plugin.cjs new file mode 100644 index 0000000..50dfd3a --- /dev/null +++ b/test/__fixtures__/plugins/extensions2/plugin.cjs @@ -0,0 +1,24 @@ +"use strict"; + +module.exports = { + languages: [ + { + name: "bar", + parsers: ["bar-parser"], + extensions: [".bar"], + }, + ], + parsers: { + "bar-parser": { + parse: (text) => ({ text }), + astFormat: "bar-ast", + }, + }, + printers: { + "bar-ast": { + print(path) { + return ["!", path.getValue().text]; + }, + }, + }, +}; diff --git a/test/__fixtures__/plugins/flushLineSuffix/file.foo b/test/__fixtures__/plugins/flushLineSuffix/file.foo new file mode 100644 index 0000000..12f00e9 --- /dev/null +++ b/test/__fixtures__/plugins/flushLineSuffix/file.foo @@ -0,0 +1 @@ +contents diff --git a/test/__fixtures__/plugins/flushLineSuffix/plugin.cjs b/test/__fixtures__/plugins/flushLineSuffix/plugin.cjs new file mode 100644 index 0000000..059e347 --- /dev/null +++ b/test/__fixtures__/plugins/flushLineSuffix/plugin.cjs @@ -0,0 +1,25 @@ +"use strict"; + +module.exports = { + languages: [ + { + name: "foo", + parsers: ["foo-parser"], + extensions: [".foo"], + }, + ], + parsers: { + "foo-parser": { + parse: (text) => ({ text }), + astFormat: "foo-ast", + }, + }, + printers: { + "foo-ast": { + print(path) { + // TODO[@fisker]: Use `lineSuffix` after we support ESM plugin + return { type: "line-suffix", contents: path.getValue().text.trim() }; + }, + }, + }, +}; diff --git a/test/__fixtures__/plugins/options-string/config.json b/test/__fixtures__/plugins/options-string/config.json new file mode 100644 index 0000000..a1d5192 --- /dev/null +++ b/test/__fixtures__/plugins/options-string/config.json @@ -0,0 +1,4 @@ +{ + "plugins": ["./plugin.cjs"], + "fooString": "baz" +} diff --git a/test/__fixtures__/plugins/options-string/plugin.cjs b/test/__fixtures__/plugins/options-string/plugin.cjs new file mode 100644 index 0000000..85f5cc5 --- /dev/null +++ b/test/__fixtures__/plugins/options-string/plugin.cjs @@ -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, + }, + }, +}; diff --git a/test/__fixtures__/plugins/options/config.json b/test/__fixtures__/plugins/options/config.json new file mode 100644 index 0000000..9eb8fce --- /dev/null +++ b/test/__fixtures__/plugins/options/config.json @@ -0,0 +1,4 @@ +{ + "plugins": ["./plugin.cjs"], + "fooOption": "baz" +} diff --git a/test/__fixtures__/plugins/options/plugin.cjs b/test/__fixtures__/plugins/options/plugin.cjs new file mode 100644 index 0000000..2191f69 --- /dev/null +++ b/test/__fixtures__/plugins/options/plugin.cjs @@ -0,0 +1,41 @@ +"use strict"; + +module.exports = { + languages: [ + { + name: "foo", + parsers: ["foo-parser"], + extensions: [".foo"], + since: "1.0.0", + }, + ], + options: { + fooOption: { + type: "choice", + default: "bar", + description: "foo description", + choices: [ + { + value: "bar", + description: "bar description", + }, + { + value: "baz", + description: "baz description", + }, + ], + }, + }, + parsers: { + "foo-parser": { + parse: (text) => ({ text }), + astFormat: "foo-ast", + }, + }, + printers: { + "foo-ast": { + print: (path, options) => + options.fooOption ? `foo:${options.fooOption}` : path.getValue().text, + }, + }, +}; diff --git a/test/__fixtures__/plugins/preprocess/file.foo b/test/__fixtures__/plugins/preprocess/file.foo new file mode 100644 index 0000000..12f00e9 --- /dev/null +++ b/test/__fixtures__/plugins/preprocess/file.foo @@ -0,0 +1 @@ +contents diff --git a/test/__fixtures__/plugins/preprocess/plugin.cjs b/test/__fixtures__/plugins/preprocess/plugin.cjs new file mode 100644 index 0000000..0e11e21 --- /dev/null +++ b/test/__fixtures__/plugins/preprocess/plugin.cjs @@ -0,0 +1,24 @@ +"use strict"; + +module.exports = { + languages: [ + { + name: "foo", + parsers: ["foo-parser"], + extensions: [".foo"], + since: "1.0.0", + }, + ], + parsers: { + "foo-parser": { + preprocess: (text) => `preprocessed:${text}`, + parse: (text) => ({ text }), + astFormat: "foo-ast", + }, + }, + printers: { + "foo-ast": { + print: (path) => path.getValue().text, + }, + }, +}; diff --git a/test/__fixtures__/plugins/virtualDirectory/node_modules/.gitignore b/test/__fixtures__/plugins/virtualDirectory/node_modules/.gitignore new file mode 100644 index 0000000..e69de29 diff --git a/test/__tests__/plugin-resolution.js b/test/__tests__/plugin-resolution.js new file mode 100644 index 0000000..362067b --- /dev/null +++ b/test/__tests__/plugin-resolution.js @@ -0,0 +1,78 @@ +import { runCli } from "../utils"; + +describe("loads --plugin by its relative path", () => { + runCli("plugins", [ + "automatic/file.txt", + "--parser=bar", + "--plugin=./automatic/node_modules/prettier-plugin-bar/index.js", + ]).test({ + stdout: "content from `prettier-plugin-bar` package + contents", + stderr: "", + status: 0, + write: [], + }); +}); + +describe("loads --plugin by its relative path without leading ./", () => { + runCli("plugins", [ + "automatic/file.txt", + "--parser=bar", + "--plugin=automatic/node_modules/prettier-plugin-bar/index.js", + ]).test({ + stdout: "content from `prettier-plugin-bar` package + contents", + stderr: "", + status: 0, + write: [], + }); +}); + +describe("loads --plugin by package name", () => { + runCli("plugins/automatic", [ + "file.txt", + "--parser=foobar", + "--plugin=@user/prettier-plugin-foobar", + ]).test({ + stdout: "foobar+contents", + stderr: "", + status: 0, + write: [], + }); +}); + +describe("loads --plugin by filename without leading ./, should resolve to file, not package", () => { + runCli("plugins/automatic", [ + "file.txt", + "--parser=baz", + "--plugin=prettier-plugin-baz.js", + ]).test({ + stdout: "content from `prettier-plugin-baz.js` file + contents", + stderr: "", + status: 0, + write: [], + }); +}); + +describe("loads --plugin by bespoke plugin name (assuming it is installed in cwd)", () => { + runCli("plugins/bespoke", [ + "../automatic/file.txt", + "--parser=bespoke", + "--plugin=@company/prettier-plugin-bespoke/main.js", + ]).test({ + stdout: "bespoke+contents", + stderr: "", + status: 0, + write: [], + }); +}); + +// TODO (43081j): re-enable this once #21 is fixed +describe.skip("support absolute path in config file", () => { + runCli("plugins/absolute-path", ["--parser=uppercase-rocks"], { + input: "prettier should be uppercase", + }).test({ + stdout: "PRETTIER SHOULD BE UPPERCASE", + stderr: "", + status: 0, + write: [], + }); +}); From beb5749f524b3f49cf5a464cb60495139795a18d Mon Sep 17 00:00:00 2001 From: James Garbutt <43081j@users.noreply.github.com> Date: Wed, 26 Feb 2025 21:19:02 +0000 Subject: [PATCH 2/2] test: skip non-relative relative path tests --- test/__tests__/plugin-resolution.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/test/__tests__/plugin-resolution.js b/test/__tests__/plugin-resolution.js index 362067b..9d84ca0 100644 --- a/test/__tests__/plugin-resolution.js +++ b/test/__tests__/plugin-resolution.js @@ -13,7 +13,9 @@ describe("loads --plugin by its relative path", () => { }); }); -describe("loads --plugin by its relative path without leading ./", () => { +// TODO (43081j): we don't currently support loading relative paths without +// the leading `./` +describe.skip("loads --plugin by its relative path without leading ./", () => { runCli("plugins", [ "automatic/file.txt", "--parser=bar", @@ -39,7 +41,9 @@ describe("loads --plugin by package name", () => { }); }); -describe("loads --plugin by filename without leading ./, should resolve to file, not package", () => { +// TODO (43081j): we don't currently support loading relative paths without +// the leading `./` +describe.skip("loads --plugin by filename without leading ./, should resolve to file, not package", () => { runCli("plugins/automatic", [ "file.txt", "--parser=baz",