Move some bundle test code into tests/ (#12577)

This commit is contained in:
fisker Cheung authored and GitHub committed 2022-04-02 16:14:32 +08:00
1 parent 32480b621b
commit afd0220dea
4 files changed
+59 -40

No files matched your search

-12
View File
@@ -63,8 +63,6 @@ const clear = () => {
};
async function createBundle(bundleConfig, options) {
const { target } = bundleConfig;
try {
for await (const {
name,
@@ -89,16 +87,6 @@ async function createBundle(bundleConfig, options) {
continue;
}
// Files including U+FFEE can't load in Chrome Extension
// `prettier-chrome-extension` https://github.com/prettier/prettier-chrome-extension
// details https://github.com/prettier/prettier/pull/8534
if (target === "universal") {
const content = await fs.readFile(absolutePath, "utf8");
if (content.includes("\ufffe")) {
throw new Error("Bundled umd file should not have U+FFFE character.");
}
}
const sizeMessages = [];
if (options.printSize) {
+5 -28
View File
@@ -1,37 +1,14 @@
"use strict";
const fs = require("fs");
const vm = require("vm");
const fastGlob = require("fast-glob");
const createSandBox = require("./utils/create-sandbox.js");
const sandbox = vm.createContext();
const source = fastGlob
.sync(["standalone.js", "parser-*.js"], {
const sandbox = createSandBox({
files: fastGlob.sync(["standalone.js", "parser-*.js"], {
cwd: process.env.PRETTIER_DIR,
absolute: true,
})
.map((file) => fs.readFileSync(file, "utf8"))
.join(";");
vm.runInContext(source, sandbox);
const allowedGlobalObjects = new Set(["prettier", "prettierPlugins"]);
const globalObjects = Object.keys(sandbox).filter(
(property) => !allowedGlobalObjects.has(property)
);
if (globalObjects.length > 0) {
throw new Error(
`Global ${globalObjects
.map(
(property) =>
`"${property}"(${Object.prototype.toString
.call(sandbox[property])
.slice(8, -1)})`
)
.join(", ")} should not be exposed.`
);
}
}),
});
// TODO: maybe expose (and write tests) for `format`, `utils`, and
// `__debug` methods
+14
View File
@@ -0,0 +1,14 @@
"use strict";
const vm = require("vm");
const fs = require("fs");
function createSandBox({ files }) {
const source = files.map((file) => fs.readFileSync(file, "utf8")).join(";");
const sandbox = vm.createContext();
vm.runInContext(source, sandbox);
return sandbox;
}
module.exports = createSandBox;
+40
View File
@@ -1,8 +1,10 @@
"use strict";
const path = require("path");
const fs = require("fs");
const fastGlob = require("fast-glob");
const { projectRoot } = require("../env.js");
const createSandBox = require("../../config/utils/create-sandbox.js");
const coreOptions = require("../../../src/main/core-options.js");
const codeSamples =
require("../../../website/playground/codeSamples.js").default;
@@ -12,6 +14,21 @@ const parserNames = coreOptions.options.parser.choices.map(
);
const distDirectory = path.join(projectRoot, "dist");
// Files including U+FFEE can't load in Chrome Extension
// `prettier-chrome-extension` https://github.com/prettier/prettier-chrome-extension
// details https://github.com/prettier/prettier/pull/8534
test("code", async () => {
const files = await fastGlob(["**/*"], {
cwd: distDirectory,
absolute: true,
});
for (const file of files) {
const text = await fs.promises.readFile(file, "utf8");
expect(text.includes("\ufffe")).toBe(false);
}
});
describe("standalone", () => {
const standalone = require(path.join(distDirectory, "standalone.js"));
const plugins = fastGlob
@@ -47,3 +64,26 @@ describe("standalone", () => {
});
}
});
test("global objects", async () => {
const files = await fastGlob(["standalone.js", "parser-*.js"], {
cwd: distDirectory,
absolute: true,
});
const allowedGlobalObjects = new Set(["prettier", "prettierPlugins"]);
const getGlobalObjects = (file) => {
const sandbox = createSandBox({ files: [file] });
return Object.fromEntries(
Object.entries(sandbox).filter(
([property]) => !allowedGlobalObjects.has(property)
)
);
};
for (const file of files) {
const globalObjects = getGlobalObjects(file);
expect(globalObjects).toStrictEqual({});
}
});