Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests config #1034

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
},
"extends": ["airbnb-base", "prettier"],
"globals": {
"run_spec": false
"runFormatTest": "readonly"
},
"parserOptions": {
"ecmaVersion": "latest",
Expand Down
4 changes: 2 additions & 2 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const TEST_STANDALONE = Boolean(process.env.TEST_STANDALONE);
const testMatch = [
'<rootDir>/tests/format/**/jsfmt.spec.js',
'<rootDir>/tests/format/**/format.test.js',

'<rootDir>/tests/unit/**/*.test.js'
];
Expand All @@ -11,7 +11,7 @@ if (TEST_STANDALONE) {

export default {
runner: 'jest-light-runner',
setupFiles: ['<rootDir>/tests/config/setup.js'],
setupFiles: ['<rootDir>/tests/config/format-test-setup.js'],
snapshotSerializers: [
'jest-snapshot-serializer-raw',
'jest-snapshot-serializer-ansi'
Expand Down
7 changes: 3 additions & 4 deletions tests/config/.prettierrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"singleQuote": false,
"trailingComma": "es5"
}

"singleQuote": false,
"trailingComma": "all"
}
3 changes: 3 additions & 0 deletions tests/config/format-test-setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import runFormatTest from "./run-format-test.js";

globalThis.runFormatTest = runFormatTest;
2 changes: 1 addition & 1 deletion tests/config/get-prettier.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
function getPrettierInternal() {
const entry = process.env.TEST_STANDALONE
? new URL("./require-standalone.cjs", import.meta.url)
: new URL("./require-prettier.cjs", import.meta.url);
: "prettier";

return import(entry).then((module) => module.default);
}
Expand Down
5 changes: 0 additions & 5 deletions tests/config/require-prettier.cjs

This file was deleted.

4 changes: 2 additions & 2 deletions tests/config/require-standalone.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ module.exports = {
};
prettier.formatWithCursor($$$input, options);
`,
{ $$$input: input, $$$options: options, ...sandbox }
{ $$$input: input, $$$options: options, ...sandbox },
);
},

Expand All @@ -57,7 +57,7 @@ module.exports = {
$$$options: options,
$$$devOptions: devOptions,
...sandbox,
}
},
);
},
},
Expand Down
44 changes: 23 additions & 21 deletions tests/config/format-test.js → tests/config/run-format-test.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import fs from "node:fs";
import path from "node:path";
import url from "node:url";

import createEsmUtils from "esm-utils";

import getPrettier from "./get-prettier.js";
import compileContract from "./utils/compile-contract.js";
import createSnapshot from "./utils/create-snapshot.js";
import visualizeEndOfLine from "./utils/visualize-end-of-line.js";
import consistentEndOfLine from "./utils/consistent-end-of-line.js";
import createSnapshot from "./utils/create-snapshot.js";
import stringifyOptionsForTitle from "./utils/stringify-options-for-title.js";
import visualizeEndOfLine from "./utils/visualize-end-of-line.js";

const { __dirname } = createEsmUtils(import.meta);

Expand All @@ -25,7 +27,7 @@ const unstableTests = new Map(
? fixture
: [fixture];
return [path.join(__dirname, "../format/", file), isUnstable];
})
}),
);

// Here we add files that will not have the same AST after being formatted.
Expand All @@ -35,7 +37,7 @@ const unstableAstTests = new Map(
? fixture
: [fixture];
return [path.join(__dirname, "../format/", file), isAstUnstable];
})
}),
);

const testsWithAstChanges = new Map(
Expand All @@ -59,7 +61,7 @@ const testsWithAstChanges = new Map(
? fixture
: [fixture];
return [path.join(__dirname, "../format/", file), compareBytecode];
})
}),
);

const isUnstable = (filename, options) => {
Expand Down Expand Up @@ -109,10 +111,10 @@ const shouldThrowOnFormat = (filename, options) => {

const isTestDirectory = (dirname, name) =>
(dirname + path.sep).startsWith(
path.join(__dirname, "../format", name) + path.sep
path.join(__dirname, "../format", name) + path.sep,
);

function runSpec(fixtures, parsers, options) {
function runFormatTest(fixtures, parsers, options) {
let { importMeta, snippets = [] } = fixtures.importMeta
? fixtures
: { importMeta: fixtures };
Expand All @@ -121,7 +123,7 @@ function runSpec(fixtures, parsers, options) {
// `IS_PARSER_INFERENCE_TESTS` mean to test `inferParser` on `standalone`
const IS_PARSER_INFERENCE_TESTS = isTestDirectory(
dirname,
"misc/parser-inference"
"misc/parser-inference",
);

// `IS_ERROR_TESTS` mean to watch errors like:
Expand Down Expand Up @@ -158,7 +160,7 @@ function runSpec(fixtures, parsers, options) {
path.extname(basename) === ".snap" ||
!file.isFile() ||
basename[0] === "." ||
basename === "jsfmt.spec.js" ||
basename === "format.test.js" ||
// VSCode creates this file sometime https://github.com/microsoft/vscode/issues/105191
basename === "debug.log"
) {
Expand Down Expand Up @@ -197,7 +199,7 @@ function runSpec(fixtures, parsers, options) {
};
const shouldThrowOnMainParserFormat = shouldThrowOnFormat(
name,
formatOptions
formatOptions,
);

let mainParserFormatResult;
Expand Down Expand Up @@ -266,13 +268,13 @@ async function runTest({

// Make sure output has consistent EOL
expect(formatResult.eolVisualizedOutput).toEqual(
visualizeEndOfLine(consistentEndOfLine(formatResult.outputWithCursor))
visualizeEndOfLine(consistentEndOfLine(formatResult.outputWithCursor)),
);

// The result is assert to equals to `output`
if (typeof output === "string") {
expect(formatResult.eolVisualizedOutput).toEqual(
visualizeEndOfLine(output)
visualizeEndOfLine(output),
);
return;
}
Expand All @@ -283,7 +285,7 @@ async function runTest({
parsers,
formatOptions,
CURSOR_PLACEHOLDER,
})
}),
).toMatchSnapshot();

if (!FULL_TEST) {
Expand All @@ -299,7 +301,7 @@ async function runTest({
const { eolVisualizedOutput: firstOutput, output } = formatResult;
const { eolVisualizedOutput: secondOutput } = await format(
output,
formatOptions
formatOptions,
);
if (isUnstableTest) {
// To keep eye on failed tests, this assert never supposed to pass,
Expand All @@ -326,15 +328,15 @@ async function runTest({
if (!shouldSkipEolTest(code, formatResult.options)) {
for (const eol of ["\r\n", "\r"]) {
const { eolVisualizedOutput: output } = await format(
code.replace(/\n/g, eol),
formatOptions
code.replace(/\n/gu, eol),
formatOptions,
);
// Only if `endOfLine: "auto"` the result will be different
const expected =
formatOptions.endOfLine === "auto"
? visualizeEndOfLine(
// All `code` use `LF`, so the `eol` of result is always `LF`
formatResult.outputWithCursor.replace(/\n/g, eol)
formatResult.outputWithCursor.replace(/\n/gu, eol),
)
: formatResult.eolVisualizedOutput;
expect(output).toEqual(expected);
Expand All @@ -344,7 +346,7 @@ async function runTest({
if (code.charAt(0) !== BOM) {
const { eolVisualizedOutput: output } = await format(
BOM + code,
formatOptions
formatOptions,
);
const expected = BOM + formatResult.eolVisualizedOutput;
expect(output).toEqual(expected);
Expand Down Expand Up @@ -427,14 +429,14 @@ const insertCursor = (text, cursorOffset) =>
async function format(originalText, originalOptions) {
const { text: input, options } = replacePlaceholders(
originalText,
originalOptions
originalOptions,
);
const inputWithCursor = insertCursor(input, options.cursorOffset);
const prettier = await getPrettier();

const { formatted: output, cursorOffset } = await prettier.formatWithCursor(
input,
options
options,
);
const outputWithCursor = insertCursor(output, cursorOffset);
const eolVisualizedOutput = visualizeEndOfLine(outputWithCursor);
Expand All @@ -452,4 +454,4 @@ async function format(originalText, originalOptions) {
};
}

export default runSpec;
export default runFormatTest;
3 changes: 0 additions & 3 deletions tests/config/setup.js

This file was deleted.

2 changes: 1 addition & 1 deletion tests/config/utils/compile-contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ async function compileContract(filename, content) {
...byteCodes,
[contractName]: compiledContracts[contractName].evm.bytecode.object,
}),
{}
{},
);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/config/utils/consistent-end-of-line.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
function consistentEndOfLine(text) {
let firstEndOfLine;
return text.replace(/\r\n?|\n/g, (endOfLine) => {
return text.replace(/\r\n?|\n/gu, (endOfLine) => {
firstEndOfLine = firstEndOfLine ?? endOfLine;
return firstEndOfLine;
});
Expand Down
9 changes: 5 additions & 4 deletions tests/config/utils/create-snapshot.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { wrap as raw } from "jest-snapshot-serializer-raw";
import visualizeRange from "./visualize-range.js";

import visualizeEndOfLine from "./visualize-end-of-line.js";
import visualizeRange from "./visualize-range.js";

const SEPARATOR_WIDTH = 80;
function printSeparator(description = "") {
Expand Down Expand Up @@ -48,7 +49,7 @@ function printWidthIndicator(printWidth, offset) {

function createSnapshot(
formatResult,
{ parsers, formatOptions, CURSOR_PLACEHOLDER }
{ parsers, formatOptions, CURSOR_PLACEHOLDER },
) {
let {
inputWithCursor: input,
Expand All @@ -69,7 +70,7 @@ function createSnapshot(
}

input = visualizeRange(input, { rangeStart, rangeEnd });
codeOffset = input.match(/^>?\s+1 \|/)[0].length + 1;
codeOffset = input.match(/^>?\s+1 \|/u)[0].length + 1;
}

if ("endOfLine" in formatOptions) {
Expand All @@ -89,7 +90,7 @@ function createSnapshot(
printSeparator("output"),
output,
printSeparator(),
].join("\n")
].join("\n"),
);
}

Expand Down
2 changes: 1 addition & 1 deletion tests/config/utils/stringify-options-for-title.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ function stringifyOptions(options) {
? undefined
: value === Number.POSITIVE_INFINITY
? "Infinity"
: value
: value,
);

return string === "{}" ? "" : string;
Expand Down
2 changes: 1 addition & 1 deletion tests/config/utils/visualize-end-of-line.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function visualizeEndOfLine(text) {
return text.replace(/\r\n?|\n/g, (endOfLine) => {
return text.replace(/\r\n?|\n/gu, (endOfLine) => {
switch (endOfLine) {
case "\n":
return "<LF>\n";
Expand Down
4 changes: 2 additions & 2 deletions tests/config/utils/visualize-range.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { LinesAndColumns } from "lines-and-columns";
import { codeFrameColumns } from "@babel/code-frame";
import { LinesAndColumns } from "lines-and-columns";
const codeFrameColumnsOptions = {
linesAbove: Number.POSITIVE_INFINITY,
linesBelow: Number.POSITIVE_INFINITY,
Expand Down Expand Up @@ -32,7 +32,7 @@ const visualizeRange = (text, { rangeStart = 0, rangeEnd = text.length }) =>
locationForRange(text, rangeStart, rangeEnd),
rangeStart > rangeEnd
? { ...codeFrameColumnsOptions, message: "[Reversed range]" }
: codeFrameColumnsOptions
: codeFrameColumnsOptions,
);

export default visualizeRange;
1 change: 1 addition & 0 deletions tests/format/AddressPayable/format.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
runFormatTest(import.meta, ['solidity-parse']);
1 change: 0 additions & 1 deletion tests/format/AddressPayable/jsfmt.spec.js

This file was deleted.

1 change: 1 addition & 0 deletions tests/format/AllSolidityFeatures/format.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
runFormatTest(import.meta, ['solidity-parse']);
1 change: 0 additions & 1 deletion tests/format/AllSolidityFeatures/jsfmt.spec.js

This file was deleted.

1 change: 1 addition & 0 deletions tests/format/AllSolidityFeaturesV0.4.26/format.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
runFormatTest(import.meta, ['solidity-parse'], { compiler: '0.4.26' });
1 change: 0 additions & 1 deletion tests/format/AllSolidityFeaturesV0.4.26/jsfmt.spec.js

This file was deleted.

1 change: 1 addition & 0 deletions tests/format/Arrays/format.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
runFormatTest(import.meta, ['solidity-parse']);
1 change: 0 additions & 1 deletion tests/format/Arrays/jsfmt.spec.js

This file was deleted.

1 change: 1 addition & 0 deletions tests/format/Assembly/format.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
runFormatTest(import.meta, ['solidity-parse']);
1 change: 0 additions & 1 deletion tests/format/Assembly/jsfmt.spec.js

This file was deleted.

1 change: 1 addition & 0 deletions tests/format/AssemblyV0.4.26/format.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
runFormatTest(import.meta, ['solidity-parse'], { compiler: '0.4.26' });
1 change: 0 additions & 1 deletion tests/format/AssemblyV0.4.26/jsfmt.spec.js

This file was deleted.

1 change: 1 addition & 0 deletions tests/format/BasicIterator/format.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
runFormatTest(import.meta, ['solidity-parse'], { compiler: '0.4.26' });
1 change: 0 additions & 1 deletion tests/format/BasicIterator/jsfmt.spec.js

This file was deleted.

1 change: 1 addition & 0 deletions tests/format/BinaryOperators/format.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
runFormatTest(import.meta, ['solidity-parse'], { compiler: '0.5.8' });
1 change: 0 additions & 1 deletion tests/format/BinaryOperators/jsfmt.spec.js

This file was deleted.

14 changes: 14 additions & 0 deletions tests/format/BreakingChangesV0.7.4/format.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
runFormatTest(import.meta, ['solidity-parse'], { compiler: '0.7.4' });
runFormatTest(import.meta, ['solidity-parse'], {
compiler: '0.7.4',
bracketSpacing: true
});
runFormatTest(import.meta, ['solidity-parse'], { compiler: '0.7.3' });
runFormatTest(import.meta, ['solidity-parse'], {
compiler: '0.7.3',
bracketSpacing: true
});
runFormatTest(import.meta, ['solidity-parse']);
runFormatTest(import.meta, ['solidity-parse'], {
bracketSpacing: true
});
14 changes: 0 additions & 14 deletions tests/format/BreakingChangesV0.7.4/jsfmt.spec.js

This file was deleted.

3 changes: 3 additions & 0 deletions tests/format/BreakingChangesV0.8.0/format.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
runFormatTest(import.meta, ['solidity-parse']);
runFormatTest(import.meta, ['solidity-parse'], { compiler: '0.8.0' });
runFormatTest(import.meta, ['solidity-parse'], { compiler: '0.7.0' });
Loading
Loading