Skip to content

Commit

Permalink
Move some bundle test code into tests/ (#12577)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Apr 2, 2022
1 parent 32480b6 commit afd0220
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 40 deletions.
12 changes: 0 additions & 12 deletions scripts/build/build.mjs
Expand Up @@ -63,8 +63,6 @@ const clear = () => {
};

async function createBundle(bundleConfig, options) {
const { target } = bundleConfig;

try {
for await (const {
name,
Expand All @@ -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) {
Expand Down
33 changes: 5 additions & 28 deletions tests/config/require-standalone.js
@@ -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
Expand Down
14 changes: 14 additions & 0 deletions tests/config/utils/create-sandbox.js
@@ -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 changes: 40 additions & 0 deletions tests/integration/__tests__/bundle.js
@@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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({});
}
});

0 comments on commit afd0220

Please sign in to comment.