Skip to content

add support for postcss-html in postcss-tape #602

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

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
193 changes: 191 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion packages/postcss-tape/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
],
"dependencies": {
"postcss": "~8.4",
"postcss-8.4": "npm:postcss@~8.4"
"postcss-8.4": "npm:postcss@~8.4",
"postcss-html": "^1.5.0"
},
"scripts": {
"build": "rollup -c ../../rollup/default.js",
Expand Down
41 changes: 32 additions & 9 deletions packages/postcss-tape/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
/* eslint-disable @typescript-eslint/no-var-requires */

import postcss from 'postcss';
import postcssOldestSupported, { AcceptedPlugin } from 'postcss-8.4';
import path from 'path';
import fs, { promises as fsp } from 'fs';
import { strict as assert } from 'assert';

import postcss from 'postcss';
import type { PluginCreator, Plugin, Result } from 'postcss';

import postcssOldestSupported, { AcceptedPlugin } from 'postcss-8.4';

import syntaxHTML from 'postcss-html';

import { formatGitHubActionAnnotation } from './github-annotations';
import { dashesSeparator, formatCSSAssertError, formatWarningsAssertError } from './format-asserts';
import noopPlugin from './noop-plugin';
Expand Down Expand Up @@ -34,7 +36,18 @@ type TestCaseOptions = {
// Do something before the test is run.
before?: () => void,
// Do something after the test is run.
after?: () => void|Promise<void>,
after?: () => void | Promise<void>,

// Process the test cases with "postcss-html" as the syntax
postcssSyntaxHTML?: boolean,
}

function postcssSyntax(options: TestCaseOptions) {
if (options.postcssSyntaxHTML) {
return syntaxHTML();
}

return null;
}

export default function runner(currentPlugin: PluginCreator<unknown>) {
Expand Down Expand Up @@ -145,9 +158,14 @@ export default function runner(currentPlugin: PluginCreator<unknown>) {
const testSourceFilePathWithoutExtension = path.join('.', 'test', testCaseLabel.split(':')[0]);
const testFilePathWithoutExtension = path.join('.', 'test', testCaseLabel.replace(/:/g, '.'));

const testFilePath = `${testSourceFilePathWithoutExtension}.css`;
let expectFilePath = `${testFilePathWithoutExtension}.expect.css`;
let resultFilePath = `${testFilePathWithoutExtension}.result.css`;
let extension = 'css';
if (testCaseOptions.postcssSyntaxHTML) {
extension = 'html';
}

const testFilePath = `${testSourceFilePathWithoutExtension}.${extension}`;
let expectFilePath = `${testFilePathWithoutExtension}.expect.${extension}`;
let resultFilePath = `${testFilePathWithoutExtension}.result.${extension}`;

if (testCaseOptions.expect) {
expectFilePath = path.join('.', 'test', testCaseOptions.expect);
Expand Down Expand Up @@ -191,6 +209,7 @@ export default function runner(currentPlugin: PluginCreator<unknown>) {
inline: false,
annotation: false,
},
syntax: postcssSyntax(testCaseOptions),
});
} catch (err) {
sawException = true;
Expand Down Expand Up @@ -260,7 +279,10 @@ export default function runner(currentPlugin: PluginCreator<unknown>) {
// Assert result sourcemaps with recent PostCSS.
{
try {
if (result.map.toJSON().sources.includes('<no source>')) {
if (
!testCaseOptions.postcssSyntaxHTML &&
result.map.toJSON().sources.includes('<no source>')
) {
throw new Error('Sourcemap is broken');
}
} catch (err) {
Expand Down Expand Up @@ -296,6 +318,7 @@ export default function runner(currentPlugin: PluginCreator<unknown>) {
inline: false,
annotation: false,
},
syntax: postcssSyntax(testCaseOptions),
});

if (secondPassResult.warnings().length) {
Expand Down
1 change: 1 addition & 0 deletions packages/postcss-tape/test-self/document.expect.code
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0
1 change: 1 addition & 0 deletions packages/postcss-tape/test-self/document.expect.log
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pass b-plugin
13 changes: 13 additions & 0 deletions packages/postcss-tape/test-self/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,16 @@ set -e
git --no-pager diff --no-index --word-diff ./test-self/basic.break-css.expect.log ./test-self/basic.break-css.result.log
git --no-pager diff --no-index --word-diff ./test-self/basic.break-css.expect.code ./test-self/basic.break-css.result.code
# endregion:Basic with broken result CSS

# region:Document
set +e

echo "" > ./test-self/document.result.log

node ./test/document.mjs > ./test-self/document.result.log 2>&1
echo "$?" > ./test-self/document.result.code

set -e
git --no-pager diff --no-index --word-diff ./test-self/document.expect.log ./test-self/document.result.log
git --no-pager diff --no-index --word-diff ./test-self/document.expect.code ./test-self/document.result.code
# endregion:Document
Loading