Skip to content

Commit

Permalink
Test: Rename run_spec to runFormatTest (#15943)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Jan 19, 2024
1 parent ed23dac commit 6eda34d
Show file tree
Hide file tree
Showing 1,236 changed files with 1,548 additions and 1,485 deletions.
12 changes: 6 additions & 6 deletions CONTRIBUTING.md
Expand Up @@ -11,24 +11,24 @@ yarn test

The tests use [Jest snapshots](https://facebook.github.io/jest/docs/en/snapshot-testing.html). You can make changes and run `jest -u` (or `yarn test -u`) to update the snapshots. Then run `git diff` to take a look at what changed. Always update the snapshots when opening a PR.

Each test directory in `tests/format` has a `jsfmt.spec.js` file that controls how exactly the rest of the files in the directory are used for tests. This file must contain one or more calls to the `run_spec` global function. For example, in directories with JavaScript formatting tests, `jsfmt.spec.js` generally looks like this:
Each test directory in `tests/format` has a `jsfmt.spec.js` file that controls how exactly the rest of the files in the directory are used for tests. This file must contain one or more calls to the `runFormatTest` global function. For example, in directories with JavaScript formatting tests, `jsfmt.spec.js` generally looks like this:

```js
run_spec(import.meta, ["babel", "flow", "typescript"]);
runFormatTest(import.meta, ["babel", "flow", "typescript"]);
```
This verifies that for each file in the directory, the output matches the snapshot and is the same for each listed parser.
You can also pass options as the third argument:
```js
run_spec(import.meta, ["babel"], { trailingComma: "es5" });
runFormatTest(import.meta, ["babel"], { trailingComma: "es5" });
```
Signature:
```ts
function run_spec(
function runFormatTest(
fixtures:
| ImportMeta
| {
Expand All @@ -47,11 +47,11 @@ function run_spec(

Parameters:

- **`fixtures`**: Must be set to `import.meta` or to an object of the shape `{ importMeta: import.meta, ... }`. The object may have the `snippets` property to specify an array of extra input entries in addition to the files in the current directory. For each input entry (a file or a snippet), `run_spec` configures and runs a number of tests. The main check is that for a given input the output should match the snapshot (for snippets, the expected output can also be specified directly). [Additional checks](#deeper-testing) are controlled by options and environment variables.
- **`fixtures`**: Must be set to `import.meta` or to an object of the shape `{ importMeta: import.meta, ... }`. The object may have the `snippets` property to specify an array of extra input entries in addition to the files in the current directory. For each input entry (a file or a snippet), `runFormatTest` configures and runs a number of tests. The main check is that for a given input the output should match the snapshot (for snippets, the expected output can also be specified directly). [Additional checks](#deeper-testing) are controlled by options and environment variables.
- **`parsers`**: A list of parser names. The tests verify that the parsers in this list produce the same output. If the list includes `typescript`, then `babel-ts` is included implicitly. If the list includes `flow`, then `babel-flow` is included implicitly. If the list includes `babel`, and the current directory is inside `tests/format/js` or `tests/format/jsx`, then `acorn`, `espree`, and `meriyah` are included implicitly.
- **`options`**: In addition to Prettier's formatting options, can contain the `errors` property to specify that it's expected that the formatting shouldn't be successful and an error should be thrown for all (`errors: true`) or some combinations of input entries and parsers.
The implementation of `run_spec` can be found in [`tests/config/format-test.js`](tests/config/format-test.js).
The implementation of `runFormatTest` can be found in [`tests/config/run-format-test.js`](tests/config/run-format-test.js).
`tests/format/flow-repo/` contains the Flow test suite and is not supposed to be edited by hand. To update it, clone the Flow repo next to the Prettier repo and run: `node scripts/sync-flow-tests.cjs ../flow/tests/`.
Expand Down
4 changes: 2 additions & 2 deletions eslint.config.js
Expand Up @@ -320,7 +320,7 @@ export default [
{
files: ["tests/format/**/*.js"],
rules: {
"prettier-internal-rules/no-legacy-format-test-fixtures": "error",
"prettier-internal-rules/no-legacy-format-test": "error",
},
},
{
Expand All @@ -340,8 +340,8 @@ export default [
},
languageOptions: {
globals: {
run_spec: "readonly",
runCli: "readonly",
runFormatTest: "readonly",
},
},
},
Expand Down
2 changes: 1 addition & 1 deletion scripts/sync-flow-tests.cjs
Expand Up @@ -5,7 +5,7 @@ const path = require("path");
const flowParser = require("flow-parser");
const fastGlob = require("fast-glob");

const DEFAULT_SPEC_CONTENT = "run_spec(__dirname);\n";
const DEFAULT_SPEC_CONTENT = "runFormatTest(import.meta);\n";
const SPEC_FILE_NAME = "jsfmt.spec.js";
const FLOW_TESTS_DIR = path.join(__dirname, "../tests/format/flow-repo");

Expand Down
Expand Up @@ -11,7 +11,7 @@ module.exports = {
"no-doc-public-import": require("./no-doc-public-import.js"),
"no-empty-flat-contents-for-if-break": require("./no-empty-flat-contents-for-if-break.js"),
"no-identifier-n": require("./no-identifier-n.js"),
"no-legacy-format-test-fixtures": require("./no-legacy-format-test-fixtures.js"),
"no-legacy-format-test": require("./no-legacy-format-test.js"),
"no-node-comments": require("./no-node-comments.js"),
"no-unnecessary-ast-path-call": require("./no-unnecessary-ast-path-call.js"),
"prefer-ast-path-each": require("./prefer-ast-path-each.js"),
Expand Down
@@ -1,18 +1,26 @@
"use strict";

const dirnameArgumentSelector = [
const legacyRunFormatTestCall = [
"CallExpression",
'[callee.type="Identifier"]',
'[callee.name="run_spec"]',
].join("");

const runFormatTestCall = [
"CallExpression",
'[callee.type="Identifier"]',
'[callee.name="runFormatTest"]',
].join("");

const dirnameArgumentSelector = [
runFormatTestCall,
" > ",
"Identifier.arguments:first-child",
'[name="__dirname"]',
].join("");

const dirnamePropertySelector = [
"CallExpression",
'[callee.type="Identifier"]',
'[callee.name="run_spec"]',
runFormatTestCall,
" > ",
"ObjectExpression.arguments:first-child",
" > ",
Expand All @@ -23,16 +31,19 @@ const dirnamePropertySelector = [
'[value.name="__dirname"]',
].join("");

const MESSAGE_ID_LEGACY_FUNCTION_NAME = "legacy-function-name";
const MESSAGE_ID_ARGUMENT = "dirname-argument";
const MESSAGE_ID_PROPERTY = "dirname-property";

module.exports = {
meta: {
type: "suggestion",
docs: {
url: "https://github.com/prettier/prettier/blob/main/scripts/tools/eslint-plugin-prettier-internal-rules/no-legacy-format-test-fixtures.js",
url: "https://github.com/prettier/prettier/blob/main/scripts/tools/eslint-plugin-prettier-internal-rules/no-legacy-format-test.js",
},
messages: {
[MESSAGE_ID_LEGACY_FUNCTION_NAME]:
"Use `runFormatTest(…)` instead of `run_spec(…)`.",
[MESSAGE_ID_ARGUMENT]: "Use `import.meta` instead of `__dirname`.",
[MESSAGE_ID_PROPERTY]:
"Use `importMeta: import.meta` instead of `dirname: __dirname`.",
Expand All @@ -42,6 +53,14 @@ module.exports = {
},
create(context) {
return {
[legacyRunFormatTestCall](callExpression) {
context.report({
node: callExpression.callee,
messageId: MESSAGE_ID_LEGACY_FUNCTION_NAME,
fix: (fixer) =>
fixer.replaceText(callExpression.callee, "runFormatTest"),
});
},
[dirnameArgumentSelector](node) {
context.report({
node,
Expand Down
20 changes: 13 additions & 7 deletions scripts/tools/eslint-plugin-prettier-internal-rules/test.js
Expand Up @@ -271,26 +271,32 @@ test("no-identifier-n", {
],
});

test("no-legacy-format-test-fixtures", {
test("no-legacy-format-test", {
valid: [
"run_spec(import.meta, ['babel'])",
"run_spec({importMeta: import.meta}, ['babel'])",
"runFormatTest(import.meta, ['babel'])",
"runFormatTest({importMeta: import.meta}, ['babel'])",
].map((code) => ({ code, parserOptions: { sourceType: "module" } })),
invalid: [
{
code: "run_spec(__dirname, ['babel'])",
code: "run_spec(import.meta, ['babel'])",
errors: [{ message: "Use `runFormatTest(…)` instead of `run_spec(…)`." }],
output: "runFormatTest(import.meta, ['babel'])",
},
{
code: "runFormatTest(__dirname, ['babel'])",
errors: [{ message: "Use `import.meta` instead of `__dirname`." }],
output: "run_spec(import.meta, ['babel'])",
output: "runFormatTest(import.meta, ['babel'])",
},
{
code: "run_spec({snippets: ['x'], dirname: __dirname}, ['babel'])",
code: "runFormatTest({snippets: ['x'], dirname: __dirname}, ['babel'])",
errors: [
{
message:
"Use `importMeta: import.meta` instead of `dirname: __dirname`.",
},
],
output: "run_spec({snippets: ['x'], importMeta: import.meta}, ['babel'])",
output:
"runFormatTest({snippets: ['x'], importMeta: import.meta}, ['babel'])",
},
].map((test) => ({ ...test, parserOptions: { sourceType: "module" } })),
});
Expand Down
4 changes: 2 additions & 2 deletions tests/config/format-test-setup.js
@@ -1,3 +1,3 @@
import runSpec from "./format-test.js";
import runFormatTest from "./run-format-test.js";

globalThis.run_spec = runSpec;
globalThis.runFormatTest = runFormatTest;
Expand Up @@ -146,7 +146,7 @@ const ensurePromise = (value) => {
return value;
};

function runSpec(fixtures, parsers, options) {
function runFormatTest(fixtures, parsers, options) {
let { importMeta, snippets = [] } = fixtures.importMeta
? fixtures
: { importMeta: fixtures };
Expand Down Expand Up @@ -531,4 +531,4 @@ async function format(originalText, originalOptions) {
};
}

export default runSpec;
export default runFormatTest;
12 changes: 7 additions & 5 deletions tests/format/angular/angular/jsfmt.spec.js
@@ -1,5 +1,7 @@
run_spec(import.meta, ["angular"], { trailingComma: "none" });
run_spec(import.meta, ["angular"], { trailingComma: "es5" });
run_spec(import.meta, ["angular"], { printWidth: 1 });
run_spec(import.meta, ["angular"], { htmlWhitespaceSensitivity: "ignore" });
run_spec(import.meta, ["angular"], { bracketSpacing: false });
runFormatTest(import.meta, ["angular"], { trailingComma: "none" });
runFormatTest(import.meta, ["angular"], { trailingComma: "es5" });
runFormatTest(import.meta, ["angular"], { printWidth: 1 });
runFormatTest(import.meta, ["angular"], {
htmlWhitespaceSensitivity: "ignore",
});
runFormatTest(import.meta, ["angular"], { bracketSpacing: false });
4 changes: 2 additions & 2 deletions tests/format/angular/bracket-same-line/jsfmt.spec.js
@@ -1,2 +1,2 @@
run_spec(import.meta, ["angular"], { bracketSameLine: true });
run_spec(import.meta, ["angular"], { bracketSameLine: false });
runFormatTest(import.meta, ["angular"], { bracketSameLine: true });
runFormatTest(import.meta, ["angular"], { bracketSameLine: false });
@@ -1 +1 @@
run_spec(import.meta, ["angular"], { embeddedLanguageFormatting: "off" });
runFormatTest(import.meta, ["angular"], { embeddedLanguageFormatting: "off" });
2 changes: 1 addition & 1 deletion tests/format/angular/control-flow/jsfmt.spec.js
@@ -1 +1 @@
run_spec(import.meta, ["angular"]);
runFormatTest(import.meta, ["angular"]);
@@ -1 +1 @@
run_spec(import.meta, ["angular"], { printWidth: 20 });
runFormatTest(import.meta, ["angular"], { printWidth: 20 });
2 changes: 1 addition & 1 deletion tests/format/angular/icu-expression/jsfmt.spec.js
@@ -1 +1 @@
run_spec(import.meta, ["angular"]);
runFormatTest(import.meta, ["angular"]);
2 changes: 1 addition & 1 deletion tests/format/angular/interpolation/jsfmt.spec.js
@@ -1,2 +1,2 @@
// We always pass {trailingComma: "none"} when printing
run_spec(import.meta, ["__ng_interpolation"], { trailingComma: "none" });
runFormatTest(import.meta, ["__ng_interpolation"], { trailingComma: "none" });
2 changes: 1 addition & 1 deletion tests/format/angular/self-closing/jsfmt.spec.js
@@ -1 +1 @@
run_spec(import.meta, ["angular"]);
runFormatTest(import.meta, ["angular"]);
2 changes: 1 addition & 1 deletion tests/format/angular/shorthand/jsfmt.spec.js
@@ -1 +1 @@
run_spec(import.meta, ["angular"]);
runFormatTest(import.meta, ["angular"]);
2 changes: 1 addition & 1 deletion tests/format/angular/upper-case/jsfmt.spec.js
@@ -1 +1 @@
run_spec(import.meta, ["angular"]);
runFormatTest(import.meta, ["angular"]);
2 changes: 1 addition & 1 deletion tests/format/css/atrule/jsfmt.spec.js
@@ -1 +1 @@
run_spec(import.meta, ["css"]);
runFormatTest(import.meta, ["css"]);
4 changes: 2 additions & 2 deletions tests/format/css/attribute/jsfmt.spec.js
@@ -1,2 +1,2 @@
run_spec(import.meta, ["css"]);
run_spec(import.meta, ["css"], { singleQuote: true });
runFormatTest(import.meta, ["css"]);
runFormatTest(import.meta, ["css"], { singleQuote: true });
2 changes: 1 addition & 1 deletion tests/format/css/atword/jsfmt.spec.js
@@ -1 +1 @@
run_spec(import.meta, ["css"]);
runFormatTest(import.meta, ["css"]);
2 changes: 1 addition & 1 deletion tests/format/css/bom/jsfmt.spec.js
@@ -1 +1 @@
run_spec(import.meta, ["css"]);
runFormatTest(import.meta, ["css"]);
2 changes: 1 addition & 1 deletion tests/format/css/case/jsfmt.spec.js
@@ -1 +1 @@
run_spec(import.meta, ["css"]);
runFormatTest(import.meta, ["css"]);
2 changes: 1 addition & 1 deletion tests/format/css/character-escaping/jsfmt.spec.js
@@ -1 +1 @@
run_spec(import.meta, ["css"]);
runFormatTest(import.meta, ["css"]);
2 changes: 1 addition & 1 deletion tests/format/css/colon/jsfmt.spec.js
@@ -1 +1 @@
run_spec(import.meta, ["css"]);
runFormatTest(import.meta, ["css"]);
2 changes: 1 addition & 1 deletion tests/format/css/color/jsfmt.spec.js
@@ -1 +1 @@
run_spec(import.meta, ["css"]);
runFormatTest(import.meta, ["css"]);
2 changes: 1 addition & 1 deletion tests/format/css/combinator/jsfmt.spec.js
@@ -1 +1 @@
run_spec(import.meta, ["css"]);
runFormatTest(import.meta, ["css"]);
2 changes: 1 addition & 1 deletion tests/format/css/comments/jsfmt.spec.js
@@ -1 +1 @@
run_spec(import.meta, ["css"]);
runFormatTest(import.meta, ["css"]);
2 changes: 1 addition & 1 deletion tests/format/css/composes/jsfmt.spec.js
@@ -1 +1 @@
run_spec(import.meta, ["css"]);
runFormatTest(import.meta, ["css"]);
2 changes: 1 addition & 1 deletion tests/format/css/cursor/jsfmt.spec.js
@@ -1 +1 @@
run_spec(import.meta, ["css"]);
runFormatTest(import.meta, ["css"]);
2 changes: 1 addition & 1 deletion tests/format/css/empty/jsfmt.spec.js
@@ -1 +1 @@
run_spec(import.meta, ["css"]);
runFormatTest(import.meta, ["css"]);
2 changes: 1 addition & 1 deletion tests/format/css/fill-value/jsfmt.spec.js
@@ -1 +1 @@
run_spec(import.meta, ["css"]);
runFormatTest(import.meta, ["css"]);
2 changes: 1 addition & 1 deletion tests/format/css/front-matter/jsfmt.spec.js
@@ -1 +1 @@
run_spec(import.meta, ["css", "scss", "less"]);
runFormatTest(import.meta, ["css", "scss", "less"]);
2 changes: 1 addition & 1 deletion tests/format/css/grid/jsfmt.spec.js
@@ -1 +1 @@
run_spec(import.meta, ["css"]);
runFormatTest(import.meta, ["css"]);
2 changes: 1 addition & 1 deletion tests/format/css/important/jsfmt.spec.js
@@ -1 +1 @@
run_spec(import.meta, ["css"]);
runFormatTest(import.meta, ["css"]);
2 changes: 1 addition & 1 deletion tests/format/css/indent/jsfmt.spec.js
@@ -1 +1 @@
run_spec(import.meta, ["css"]);
runFormatTest(import.meta, ["css"]);
2 changes: 1 addition & 1 deletion tests/format/css/inline-url/jsfmt.spec.js
@@ -1 +1 @@
run_spec(import.meta, ["css"]);
runFormatTest(import.meta, ["css"]);
2 changes: 1 addition & 1 deletion tests/format/css/long-rule/jsfmt.spec.js
@@ -1 +1 @@
run_spec(import.meta, ["css"]);
runFormatTest(import.meta, ["css"]);
2 changes: 1 addition & 1 deletion tests/format/css/loose/jsfmt.spec.js
@@ -1 +1 @@
run_spec(import.meta, ["css"]);
runFormatTest(import.meta, ["css"]);
2 changes: 1 addition & 1 deletion tests/format/css/modules/jsfmt.spec.js
@@ -1 +1 @@
run_spec(import.meta, ["css"]);
runFormatTest(import.meta, ["css"]);
2 changes: 1 addition & 1 deletion tests/format/css/no-semicolon/jsfmt.spec.js
@@ -1 +1 @@
run_spec(import.meta, ["css"]);
runFormatTest(import.meta, ["css"]);
2 changes: 1 addition & 1 deletion tests/format/css/numbers/jsfmt.spec.js
@@ -1 +1 @@
run_spec(import.meta, ["css"]);
runFormatTest(import.meta, ["css"]);
2 changes: 1 addition & 1 deletion tests/format/css/params/jsfmt.spec.js
@@ -1 +1 @@
run_spec(import.meta, ["css"]);
runFormatTest(import.meta, ["css"]);
2 changes: 1 addition & 1 deletion tests/format/css/parens/jsfmt.spec.js
@@ -1 +1 @@
run_spec(import.meta, ["css"]);
runFormatTest(import.meta, ["css"]);
2 changes: 1 addition & 1 deletion tests/format/css/postcss-8-improment/jsfmt.spec.js
@@ -1 +1 @@
run_spec(import.meta, ["css"]);
runFormatTest(import.meta, ["css"]);
2 changes: 1 addition & 1 deletion tests/format/css/postcss-plugins/jsfmt.spec.js
@@ -1 +1 @@
run_spec(import.meta, ["css"]);
runFormatTest(import.meta, ["css"]);
2 changes: 1 addition & 1 deletion tests/format/css/prefix/jsfmt.spec.js
@@ -1 +1 @@
run_spec(import.meta, ["css"]);
runFormatTest(import.meta, ["css"]);
2 changes: 1 addition & 1 deletion tests/format/css/pseudo-call/jsfmt.spec.js
@@ -1 +1 @@
run_spec(import.meta, ["css"]);
runFormatTest(import.meta, ["css"]);
4 changes: 2 additions & 2 deletions tests/format/css/quotes/jsfmt.spec.js
@@ -1,2 +1,2 @@
run_spec(import.meta, ["css"]);
run_spec(import.meta, ["css"], { singleQuote: true });
runFormatTest(import.meta, ["css"]);
runFormatTest(import.meta, ["css"], { singleQuote: true });
2 changes: 1 addition & 1 deletion tests/format/css/range/jsfmt.spec.js
@@ -1 +1 @@
run_spec(import.meta, ["css"]);
runFormatTest(import.meta, ["css"]);
2 changes: 1 addition & 1 deletion tests/format/css/selector-call/jsfmt.spec.js
@@ -1 +1 @@
run_spec(import.meta, ["css"]);
runFormatTest(import.meta, ["css"]);
2 changes: 1 addition & 1 deletion tests/format/css/selector-list/jsfmt.spec.js
@@ -1 +1 @@
run_spec(import.meta, ["css"]);
runFormatTest(import.meta, ["css"]);
2 changes: 1 addition & 1 deletion tests/format/css/selector-string/jsfmt.spec.js
@@ -1 +1 @@
run_spec(import.meta, ["css"]);
runFormatTest(import.meta, ["css"]);
2 changes: 1 addition & 1 deletion tests/format/css/trailing-comma/jsfmt.spec.js
@@ -1 +1 @@
run_spec(import.meta, ["css"]);
runFormatTest(import.meta, ["css"]);
2 changes: 1 addition & 1 deletion tests/format/css/units/jsfmt.spec.js
@@ -1 +1 @@
run_spec(import.meta, ["css"]);
runFormatTest(import.meta, ["css"]);
2 changes: 1 addition & 1 deletion tests/format/css/url/jsfmt.spec.js
@@ -1 +1 @@
run_spec(import.meta, ["css"]);
runFormatTest(import.meta, ["css"]);
2 changes: 1 addition & 1 deletion tests/format/css/variables/jsfmt.spec.js
@@ -1 +1 @@
run_spec(import.meta, ["css"]);
runFormatTest(import.meta, ["css"]);
2 changes: 1 addition & 1 deletion tests/format/css/yaml/jsfmt.spec.js
@@ -1 +1 @@
run_spec(import.meta, ["css"]);
runFormatTest(import.meta, ["css"]);
2 changes: 1 addition & 1 deletion tests/format/flow-repo/abnormal/jsfmt.spec.js
@@ -1 +1 @@
run_spec(import.meta, ["flow"]);
runFormatTest(import.meta, ["flow"]);
2 changes: 1 addition & 1 deletion tests/format/flow-repo/annot/any/jsfmt.spec.js
@@ -1 +1 @@
run_spec(import.meta, ["flow"]);
runFormatTest(import.meta, ["flow"]);
2 changes: 1 addition & 1 deletion tests/format/flow-repo/annot/jsfmt.spec.js
@@ -1 +1 @@
run_spec(import.meta, ["flow"]);
runFormatTest(import.meta, ["flow"]);
2 changes: 1 addition & 1 deletion tests/format/flow-repo/annot2/jsfmt.spec.js
@@ -1 +1 @@
run_spec(import.meta, ["flow"]);
runFormatTest(import.meta, ["flow"]);
2 changes: 1 addition & 1 deletion tests/format/flow-repo/any/jsfmt.spec.js
@@ -1 +1 @@
run_spec(import.meta, ["flow"]);
runFormatTest(import.meta, ["flow"]);
2 changes: 1 addition & 1 deletion tests/format/flow-repo/arith/jsfmt.spec.js
@@ -1 +1 @@
run_spec(import.meta, ["flow"]);
runFormatTest(import.meta, ["flow"]);
2 changes: 1 addition & 1 deletion tests/format/flow-repo/array-filter/jsfmt.spec.js
@@ -1 +1 @@
run_spec(import.meta, ["flow"]);
runFormatTest(import.meta, ["flow"]);
2 changes: 1 addition & 1 deletion tests/format/flow-repo/array_spread/jsfmt.spec.js
@@ -1 +1 @@
run_spec(import.meta, ["flow"]);
runFormatTest(import.meta, ["flow"]);

0 comments on commit 6eda34d

Please sign in to comment.