From 889b7dab792de2afbbd69cced06dd6e5ba7ed1cc Mon Sep 17 00:00:00 2001 From: Lucas Duailibe Date: Tue, 12 Jun 2018 15:05:40 -0300 Subject: [PATCH] Run tests using the standalone build in an empty context (#4668) Validate that our build is stripping all the "native" dependencies (native modules and env vars). --- .circleci/config.yml | 14 +++++++++ jest.config.js | 3 +- scripts/build/shims/assert.js | 6 ++-- scripts/build/shims/os.js | 1 + scripts/test-dist.js | 6 +++- tests_config/require_standalone.js | 48 ++++++++++++++++++++++++++++++ tests_config/run_spec.js | 17 ++++++++++- 7 files changed, 88 insertions(+), 7 deletions(-) create mode 100644 scripts/build/shims/os.js create mode 100644 tests_config/require_standalone.js diff --git a/.circleci/config.yml b/.circleci/config.yml index c403f56f1..f3b490922 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -80,6 +80,17 @@ jobs: at: ~/prettier - run: yarn test:dist + # Run tests using the standalone build + test_prod_standalone: + <<: *defaults + steps: + - attach_workspace: + at: ~/prettier + - run: + command: yarn test:dist + environment: + TEST_STANDALONE: 1 + workflows: version: 2 prod: @@ -94,3 +105,6 @@ workflows: - test_prod_node9: requires: - build_prod + - test_prod_standalone: + requires: + - build_prod diff --git a/jest.config.js b/jest.config.js index ed53031b7..94e425a6d 100644 --- a/jest.config.js +++ b/jest.config.js @@ -31,7 +31,8 @@ module.exports = { // If this is removed, see also scripts/build/build.js. "graceful-fs": "/tests_config/fs.js", - "prettier/local": "/tests_config/require_prettier.js" + "prettier/local": "/tests_config/require_prettier.js", + "prettier/standalone": "/tests_config/require_standalone.js" }, transform: {} }; diff --git a/scripts/build/shims/assert.js b/scripts/build/shims/assert.js index 77aefbd98..9e553f1ce 100644 --- a/scripts/build/shims/assert.js +++ b/scripts/build/shims/assert.js @@ -1,4 +1,2 @@ -module.exports = { - ok() {}, - strictEqual() {} -}; +export function ok() {} +export function strictEqual() {} diff --git a/scripts/build/shims/os.js b/scripts/build/shims/os.js new file mode 100644 index 000000000..e4a6d208b --- /dev/null +++ b/scripts/build/shims/os.js @@ -0,0 +1 @@ +export const EOL = "\n"; diff --git a/scripts/test-dist.js b/scripts/test-dist.js index 78e03c7da..38ec9f4ed 100644 --- a/scripts/test-dist.js +++ b/scripts/test-dist.js @@ -20,7 +20,11 @@ shell.exec("npm init -y", { cwd: tmpDir }); shell.exec(`npm install "${tarPath}"`, { cwd: tmpDir }); shell.config.silent = false; -const code = shell.exec("yarn test --color --runInBand", { +const cmd = `yarn test --color --runInBand ${ + process.env.TEST_STANDALONE ? "tests/" : "" +}`; + +const code = shell.exec(cmd, { cwd: rootDir, env: Object.assign({}, process.env, { NODE_ENV: "production", diff --git a/tests_config/require_standalone.js b/tests_config/require_standalone.js new file mode 100644 index 000000000..deef3c51d --- /dev/null +++ b/tests_config/require_standalone.js @@ -0,0 +1,48 @@ +"use strict"; + +const fs = require("fs"); +const path = require("path"); +const vm = require("vm"); + +const sources = [ + "standalone.js", + "parser-babylon.js", + "parser-flow.js", + "parser-typescript.js", + "parser-postcss.js", + "parser-graphql.js", + "parser-markdown.js", + "parser-vue.js" +].map(filename => + fs.readFileSync(path.join(process.env.PRETTIER_DIR, filename), "utf-8") +); + +const sandbox = vm.createContext(); +vm.runInContext(sources.join(";"), sandbox); + +// TODO: maybe expose (and write tests) for `format`, `utils`, and +// `__debug` methods +module.exports = { + formatWithCursor(input, options) { + return vm.runInNewContext( + `prettier.formatWithCursor( + $$$input, + Object.assign({ plugins: prettierPlugins }, $$$options) + );`, + Object.assign({ $$$input: input, $$$options: options }, sandbox) + ); + }, + + __debug: { + parse(input, options, massage) { + return vm.runInNewContext( + `prettier.__debug.parse( + $$$input, + Object.assign({ plugins: prettierPlugins }, $$$options), + ${JSON.stringify(massage)} + );`, + Object.assign({ $$$input: input, $$$options: options }, sandbox) + ); + } + } +}; diff --git a/tests_config/run_spec.js b/tests_config/run_spec.js index 9df76d714..57010aa1f 100644 --- a/tests_config/run_spec.js +++ b/tests_config/run_spec.js @@ -2,9 +2,13 @@ const fs = require("fs"); const extname = require("path").extname; -const prettier = require("prettier/local"); const AST_COMPARE = process.env["AST_COMPARE"]; +const TEST_STANDALONE = process.env["TEST_STANDALONE"]; + +const prettier = !TEST_STANDALONE + ? require("prettier/local") + : require("prettier/standalone"); function run_spec(dirname, parsers, options) { /* instabul ignore if */ @@ -13,6 +17,13 @@ function run_spec(dirname, parsers, options) { } fs.readdirSync(dirname).forEach(filename => { + // We need to have a skipped test with the same name of the snapshots, + // so Jest doesn't mark them as obsolete. + if (TEST_STANDALONE && parsers.some(skipStandalone)) { + test.skip(filename); + return; + } + const path = dirname + "/" + filename; if ( extname(filename) !== ".snap" && @@ -116,6 +127,10 @@ function read(filename) { return fs.readFileSync(filename, "utf8"); } +function skipStandalone(parser) { + return new Set(["parse5", "glimmer"]).has(parser); +} + /** * Wraps a string in a marker object that is used by `./raw-serializer.js` to * directly print that string in a snapshot without escaping all double quotes.