From 3858de1954dc6cc6d1e2cb28f0c50a4d5fe741c4 Mon Sep 17 00:00:00 2001 From: Michal Piechowiak Date: Thu, 1 Oct 2020 19:17:35 +0200 Subject: [PATCH] test(integration/gatsby-cli): use sandboxed directory to "globally" install gatsby-cli (#27056) * fix(e2e-test.sh): check if gatsby bin file exists before trying to chmod it * test(integration-tests/gatsby-cli): install cli to sandbox directory, so it can't use any accidental node_modules --- integration-tests/gatsby-cli/__tests__/new.js | 5 ++- integration-tests/gatsby-cli/jest.boot.js | 42 +++++++++++++++++++ integration-tests/gatsby-cli/jest.config.js | 18 ++++++++ integration-tests/gatsby-cli/package.json | 3 +- .../gatsby-cli/test-helpers/invoke-cli.js | 33 +++++++-------- scripts/e2e-test.sh | 19 ++++++--- 6 files changed, 93 insertions(+), 27 deletions(-) create mode 100644 integration-tests/gatsby-cli/jest.boot.js create mode 100644 integration-tests/gatsby-cli/jest.config.js diff --git a/integration-tests/gatsby-cli/__tests__/new.js b/integration-tests/gatsby-cli/__tests__/new.js index 570a94f5662b1..4465bd805d3a6 100644 --- a/integration-tests/gatsby-cli/__tests__/new.js +++ b/integration-tests/gatsby-cli/__tests__/new.js @@ -14,7 +14,8 @@ const clean = dir => execa(`yarn`, ["del-cli", dir]) describe(`gatsby new`, () => { // make folder for us to create sites into const dir = join(__dirname, "../execution-folder") - const originalPackageManager = getConfigStore().get("cli.packageManager") + const originalPackageManager = + getConfigStore().get("cli.packageManager") || `npm` beforeAll(async () => { await clean(dir) @@ -23,7 +24,7 @@ describe(`gatsby new`, () => { }) afterAll(async () => { - GatsbyCLI.from(cwd).invoke([`options`, `set`,`pm`, originalPackageManager]) + GatsbyCLI.from(cwd).invoke([`options`, `set`, `pm`, originalPackageManager]) await clean(dir) }) diff --git a/integration-tests/gatsby-cli/jest.boot.js b/integration-tests/gatsby-cli/jest.boot.js new file mode 100644 index 0000000000000..242e23ed3b65c --- /dev/null +++ b/integration-tests/gatsby-cli/jest.boot.js @@ -0,0 +1,42 @@ +const path = require(`path`) +const execa = require(`execa`) +const fs = require(`fs-extra`) + +module.exports = async () => { + console.log( + `Installing "gatsby-cli" in sandbox directory: "${process.env.GLOBAL_GATSBY_CLI_LOCATION}"` + ) + console.log( + `Tests will use "${process.env.GLOBAL_GATSBY_CLI_LOCATION}/node_modules/.bin/gatsby" CLI to invoke commands` + ) + + await fs.ensureDir(process.env.GLOBAL_GATSBY_CLI_LOCATION) + + await fs.outputJson( + path.join(process.env.GLOBAL_GATSBY_CLI_LOCATION, `package.json`), + { + dependencies: { + "gatsby-cli": "latest", + }, + } + ) + + const gatsbyDevLocation = path.join( + __dirname, + `..`, + `..`, + `packages`, + `gatsby-dev-cli`, + `dist`, + `index.js` + ) + + await execa.node(gatsbyDevLocation, [`--force-install`, `--scan-once`], { + cwd: process.env.GLOBAL_GATSBY_CLI_LOCATION, + stdio: `inherit`, + env: { + // we don't want to run gatsby-dev in with NODE_ENV=test + NODE_ENV: `production`, + }, + }) +} diff --git a/integration-tests/gatsby-cli/jest.config.js b/integration-tests/gatsby-cli/jest.config.js new file mode 100644 index 0000000000000..a561d40a50ecd --- /dev/null +++ b/integration-tests/gatsby-cli/jest.config.js @@ -0,0 +1,18 @@ +const fs = require(`fs`) +const path = require(`path`) +const os = require(`os`) +const baseConfig = require(`../jest.config.js`) + +// install global gatsby-cli to tmp dir to simulate sandbox +const GLOBAL_GATSBY_CLI_LOCATION = (process.env.GLOBAL_GATSBY_CLI_LOCATION = fs.mkdtempSync( + path.join(os.tmpdir(), `gatsby-cli-`) +)) + +module.exports = { + ...baseConfig, + globalSetup: "/integration-tests/gatsby-cli/jest.boot.js", + rootDir: `../../`, + globals: { + GLOBAL_GATSBY_CLI_LOCATION, + }, +} diff --git a/integration-tests/gatsby-cli/package.json b/integration-tests/gatsby-cli/package.json index 26aab2b690309..8a220609a218f 100644 --- a/integration-tests/gatsby-cli/package.json +++ b/integration-tests/gatsby-cli/package.json @@ -2,12 +2,11 @@ "name": "gatsby-cli-tests", "version": "1.0.0", "dependencies": { - "gatsby-cli": "^2.12.29", "strip-ansi": "^6.0.0" }, "license": "MIT", "scripts": { - "test": "jest --config=../jest.config.js gatsby-cli/" + "test": "jest --config=./jest.config.js gatsby-cli/" }, "devDependencies": { "del-cli": "^3.0.1", diff --git a/integration-tests/gatsby-cli/test-helpers/invoke-cli.js b/integration-tests/gatsby-cli/test-helpers/invoke-cli.js index 809fd82629317..cbf1e1f8cb86e 100644 --- a/integration-tests/gatsby-cli/test-helpers/invoke-cli.js +++ b/integration-tests/gatsby-cli/test-helpers/invoke-cli.js @@ -1,7 +1,14 @@ import execa, { sync } from "execa" -import { join, resolve } from "path" +import { join } from "path" import { createLogsMatcher } from "./matcher" +const gatsbyBinLocation = join( + GLOBAL_GATSBY_CLI_LOCATION, + `node_modules`, + `.bin`, + `gatsby` +) + // Use as `GatsbyCLI.cwd('execution-folder').invoke('new', 'foo')` export const GatsbyCLI = { from(relativeCwd) { @@ -9,14 +16,10 @@ export const GatsbyCLI = { invoke(args) { const NODE_ENV = args[0] === `develop` ? `development` : `production` try { - const results = sync( - resolve(`./node_modules/.bin/gatsby`), - [].concat(args), - { - cwd: join(__dirname, `../`, `./${relativeCwd}`), - env: { NODE_ENV, CI: 1, GATSBY_LOGGER: `ink` }, - } - ) + const results = sync(gatsbyBinLocation, [].concat(args), { + cwd: join(__dirname, `../`, `./${relativeCwd}`), + env: { NODE_ENV, CI: 1, GATSBY_LOGGER: `ink` }, + }) return [ results.exitCode, @@ -32,14 +35,10 @@ export const GatsbyCLI = { invokeAsync: (args, onExit) => { const NODE_ENV = args[0] === `develop` ? `development` : `production` - const res = execa( - resolve(`./node_modules/.bin/gatsby`), - [].concat(args), - { - cwd: join(__dirname, `../`, `./${relativeCwd}`), - env: { NODE_ENV, CI: 1, GATSBY_LOGGER: `ink` }, - } - ) + const res = execa(gatsbyBinLocation, [].concat(args), { + cwd: join(__dirname, `../`, `./${relativeCwd}`), + env: { NODE_ENV, CI: 1, GATSBY_LOGGER: `ink` }, + }) const logs = [] res.stdout.on("data", data => { diff --git a/scripts/e2e-test.sh b/scripts/e2e-test.sh index e7e4fed96b746..71dacef8c9ace 100755 --- a/scripts/e2e-test.sh +++ b/scripts/e2e-test.sh @@ -1,15 +1,22 @@ #!/bin/bash +set -e # bail on errors + SRC_PATH=$1 CUSTOM_COMMAND="${2:-yarn test}" GATSBY_PATH="${CIRCLE_WORKING_DIRECTORY:-../../}" # cypress docker does not support sudo and does not need it, but the default node executor does -command -v gatsby-dev || command -v sudo && sudo npm install -g gatsby-dev-cli || npm install -g gatsby-dev-cli && +command -v gatsby-dev || command -v sudo && sudo npm install -g gatsby-dev-cli || npm install -g gatsby-dev-cli # setting up child integration test link to gatsby packages -cd "$SRC_PATH" && -gatsby-dev --set-path-to-repo "$GATSBY_PATH" && -gatsby-dev --force-install --scan-once && # install _all_ files in gatsby/packages -chmod +x ./node_modules/.bin/gatsby && # this is sometimes necessary to ensure executable -sh -c "$CUSTOM_COMMAND" && +cd "$SRC_PATH" +gatsby-dev --set-path-to-repo "$GATSBY_PATH" +gatsby-dev --force-install --scan-once # install _all_ files in gatsby/packages +if test -f "./node_modules/.bin/gatsby"; then + chmod +x ./node_modules/.bin/gatsby # this is sometimes necessary to ensure executable + echo "Gatsby bin chmoded" +else + echo "Gatsby bin doesn't exist. Skipping chmod." +fi +sh -c "$CUSTOM_COMMAND" echo "e2e test run succeeded"