From 71a754d97398e44bbcab46ded507ff045d605f55 Mon Sep 17 00:00:00 2001 From: romainmenke Date: Sat, 22 Jan 2022 17:56:45 +0100 Subject: [PATCH 1/2] add a shortcut to rewrite expect files --- packages/postcss-tape/src/index.ts | 5 +++++ plugin-packs/postcss-preset-env/package.json | 1 + plugins/css-blank-pseudo/package.json | 1 + plugins/postcss-base-plugin/package.json | 1 + plugins/postcss-custom-properties/package.json | 1 + plugins/postcss-hwb-function/package.json | 1 + plugins/postcss-image-set-function/package.json | 1 + plugins/postcss-nesting/package.json | 1 + plugins/postcss-pseudo-class-any-link/package.json | 1 + 9 files changed, 13 insertions(+) diff --git a/packages/postcss-tape/src/index.ts b/packages/postcss-tape/src/index.ts index 3bc49bf83..98937c337 100644 --- a/packages/postcss-tape/src/index.ts +++ b/packages/postcss-tape/src/index.ts @@ -223,6 +223,11 @@ export default function runner(currentPlugin: PluginCreator) { const resultString = result.css.toString(); await fsp.writeFile(resultFilePath, resultString, 'utf8'); + // Allow contributors to rewrite `.expect.css` files through postcss-tape. + if (process.env.REWRITE_EXPECTS) { + fsp.writeFile(expectFilePath, resultString, 'utf8'); + } + // Can't do further checks if "expect" is missing. if (expected === false) { continue; diff --git a/plugin-packs/postcss-preset-env/package.json b/plugin-packs/postcss-preset-env/package.json index 793336104..5fe1f2fe8 100644 --- a/plugin-packs/postcss-preset-env/package.json +++ b/plugin-packs/postcss-preset-env/package.json @@ -21,6 +21,7 @@ "prepublishOnly": "npm run clean && npm run build && npm run test", "stryker": "stryker run --logLevel error", "test": "node .tape.mjs && npm run test:exports", + "test:rewrite-expects": "REWRITE_EXPECTS=true node .tape.mjs", "test:exports": "node ./test/_import.mjs && node ./test/_require.cjs" }, "engines": { diff --git a/plugins/css-blank-pseudo/package.json b/plugins/css-blank-pseudo/package.json index 2ff793b93..bf7f15908 100644 --- a/plugins/css-blank-pseudo/package.json +++ b/plugins/css-blank-pseudo/package.json @@ -41,6 +41,7 @@ "prepublishOnly": "npm run clean && npm run build && npm run test", "stryker": "stryker run --logLevel error", "test": "node .tape.mjs && npm run test:exports", + "test:rewrite-expects": "REWRITE_EXPECTS=true node .tape.mjs", "cli": "css-blank-pseudo", "test:exports": "node ./test/_import.mjs && node ./test/_require.cjs" }, diff --git a/plugins/postcss-base-plugin/package.json b/plugins/postcss-base-plugin/package.json index 3c6798329..4059f2494 100644 --- a/plugins/postcss-base-plugin/package.json +++ b/plugins/postcss-base-plugin/package.json @@ -27,6 +27,7 @@ "prepublishOnly": "npm run clean && npm run build && npm run test", "stryker": "stryker run --logLevel error", "test": "node .tape.mjs && npm run test:exports", + "test:rewrite-expects": "REWRITE_EXPECTS=true node .tape.mjs", "test:cli": "bash ./test/cli/test.sh", "test:exports": "node ./test/_import.mjs && node ./test/_require.cjs" }, diff --git a/plugins/postcss-custom-properties/package.json b/plugins/postcss-custom-properties/package.json index bb2be7f4f..bcab84460 100644 --- a/plugins/postcss-custom-properties/package.json +++ b/plugins/postcss-custom-properties/package.json @@ -33,6 +33,7 @@ "prepublishOnly": "npm run clean && npm run build && npm run test", "stryker": "stryker run --logLevel error", "test": "node .tape.mjs && node .tape.cjs && npm run test:exports", + "test:rewrite-expects": "REWRITE_EXPECTS=true node .tape.mjs", "test:exports": "node ./test/_import.mjs && node ./test/_require.cjs" }, "engines": { diff --git a/plugins/postcss-hwb-function/package.json b/plugins/postcss-hwb-function/package.json index 2d1211201..0b9d1459d 100644 --- a/plugins/postcss-hwb-function/package.json +++ b/plugins/postcss-hwb-function/package.json @@ -30,6 +30,7 @@ "prepublishOnly": "npm run clean && npm run build && npm run test", "stryker": "stryker run --logLevel error", "test": "node .tape.mjs && npm run test:exports", + "test:rewrite-expects": "REWRITE_EXPECTS=true node .tape.mjs", "test:exports": "node ./test/_import.mjs && node ./test/_require.cjs" }, "engines": { diff --git a/plugins/postcss-image-set-function/package.json b/plugins/postcss-image-set-function/package.json index 8973da1ee..38f4e2541 100644 --- a/plugins/postcss-image-set-function/package.json +++ b/plugins/postcss-image-set-function/package.json @@ -22,6 +22,7 @@ "prepublishOnly": "npm run clean && npm run build && npm run test", "stryker": "stryker run --logLevel error", "test": "node .tape.mjs && npm run test:exports", + "test:rewrite-expects": "REWRITE_EXPECTS=true node .tape.mjs", "test:exports": "node ./test/_import.mjs && node ./test/_require.cjs" }, "engines": { diff --git a/plugins/postcss-nesting/package.json b/plugins/postcss-nesting/package.json index 54e656037..0d6c68f97 100644 --- a/plugins/postcss-nesting/package.json +++ b/plugins/postcss-nesting/package.json @@ -31,6 +31,7 @@ "prepublishOnly": "npm run clean && npm run build && npm run test", "stryker": "stryker run --logLevel error", "test": "node .tape.mjs && npm run test:exports", + "test:rewrite-expects": "REWRITE_EXPECTS=true node .tape.mjs", "test:deno": "deno run --unstable --allow-env --allow-read test/deno/test.js", "test:exports": "node ./test/_import.mjs && node ./test/_require.cjs" }, diff --git a/plugins/postcss-pseudo-class-any-link/package.json b/plugins/postcss-pseudo-class-any-link/package.json index 123a85650..3d5704866 100644 --- a/plugins/postcss-pseudo-class-any-link/package.json +++ b/plugins/postcss-pseudo-class-any-link/package.json @@ -21,6 +21,7 @@ "prepublishOnly": "npm run clean && npm run build && npm run test", "stryker": "stryker run --logLevel error", "test": "node .tape.mjs && npm run test:exports", + "test:rewrite-expects": "REWRITE_EXPECTS=true node .tape.mjs", "test:exports": "node ./test/_import.mjs && node ./test/_require.cjs" }, "engines": { From 50fc1dd96f2ddb2ede320d03266e3329e83cf5dc Mon Sep 17 00:00:00 2001 From: romainmenke Date: Sat, 22 Jan 2022 18:22:18 +0100 Subject: [PATCH 2/2] emit warnings if they don't match --- packages/postcss-tape/src/format-asserts.ts | 14 ++++++++++++-- packages/postcss-tape/src/index.ts | 6 +++--- .../test-self/basic.with-warnings.expect.log | 6 ++++++ 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/packages/postcss-tape/src/format-asserts.ts b/packages/postcss-tape/src/format-asserts.ts index 19f8ae6c6..e9194eb2a 100644 --- a/packages/postcss-tape/src/format-asserts.ts +++ b/packages/postcss-tape/src/format-asserts.ts @@ -1,3 +1,5 @@ +import type { Warning } from 'postcss'; + export const dashesSeparator = '----------------------------------------'; export function formatCSSAssertError(testCaseLabel, testCaseOptions, err, forGithubAnnotation = false) { @@ -25,7 +27,7 @@ export function formatCSSAssertError(testCaseLabel, testCaseOptions, err, forGit return formatted; } -export function formatWarningsAssertError(testCaseLabel, testCaseOptions, actual, expected, forGithubAnnotation = false) { +export function formatWarningsAssertError(testCaseLabel, testCaseOptions, actual: Array, expected: number, forGithubAnnotation = false) { let formatted = ''; formatted += `\n${testCaseLabel}\n\n`; @@ -41,9 +43,17 @@ export function formatWarningsAssertError(testCaseLabel, testCaseOptions, actual } } - formatted += `unexpected or missing warnings :\n+ actual ${actual}\n- expected ${expected}\n`; + formatted += `unexpected or missing warnings :\n+ actual ${actual.length}\n- expected ${expected}\n`; if (!forGithubAnnotation) { + actual.forEach((warning) => { + formatted += `\n[${warning.plugin}]: ${warning.text}`; + }); + + if (actual.length) { + formatted += '\n'; + } + formatted += '\n' + dashesSeparator; } diff --git a/packages/postcss-tape/src/index.ts b/packages/postcss-tape/src/index.ts index 98937c337..1dc0e334f 100644 --- a/packages/postcss-tape/src/index.ts +++ b/packages/postcss-tape/src/index.ts @@ -357,18 +357,18 @@ export default function runner(currentPlugin: PluginCreator) { if (result.warnings().length || testCaseOptions.warnings) { assert.strictEqual(result.warnings().length, testCaseOptions.warnings); } - } catch (err) { + } catch (_) { hasErrors = true; if (emitGitHubAnnotations) { console.log(formatGitHubActionAnnotation( - formatWarningsAssertError(testCaseLabel, testCaseOptions, result.warnings().length, testCaseOptions.warnings, true), + formatWarningsAssertError(testCaseLabel, testCaseOptions, result.warnings(), testCaseOptions.warnings, true), 'error', { file: normalizeFilePathForGithubAnnotation(expectFilePath), line: 1, col: 1 }, )); } else { failureSummary.add(testCaseLabel); - console.error(formatWarningsAssertError(testCaseLabel, testCaseOptions, result.warnings().length, testCaseOptions.warnings)); + console.error(formatWarningsAssertError(testCaseLabel, testCaseOptions, result.warnings(), testCaseOptions.warnings)); } } } diff --git a/packages/postcss-tape/test-self/basic.with-warnings.expect.log b/packages/postcss-tape/test-self/basic.with-warnings.expect.log index d30915f02..cccfbded7 100644 --- a/packages/postcss-tape/test-self/basic.with-warnings.expect.log +++ b/packages/postcss-tape/test-self/basic.with-warnings.expect.log @@ -19,6 +19,8 @@ unexpected or missing warnings : + actual 1 - expected 0 +[a-plugin]: a warning + ---------------------------------------- basic:with-multiple-warnings @@ -30,6 +32,10 @@ unexpected or missing warnings : + actual 3 - expected 7 +[a-plugin]: a warning +[a-plugin]: a warning +[a-plugin]: a warning + ---------------------------------------- unexpected failures: