Skip to content

Commit e197fee

Browse files
committed
test(integration-tests/gatsby-cli): install cli to sandbox directory, so it can't use any accidental node_modules
1 parent 414648d commit e197fee

File tree

5 files changed

+80
-21
lines changed

5 files changed

+80
-21
lines changed

integration-tests/gatsby-cli/__tests__/new.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ const clean = dir => execa(`yarn`, ["del-cli", dir])
1414
describe(`gatsby new`, () => {
1515
// make folder for us to create sites into
1616
const dir = join(__dirname, "../execution-folder")
17-
const originalPackageManager = getConfigStore().get("cli.packageManager")
17+
const originalPackageManager =
18+
getConfigStore().get("cli.packageManager") || `npm`
1819

1920
beforeAll(async () => {
2021
await clean(dir)
@@ -23,7 +24,7 @@ describe(`gatsby new`, () => {
2324
})
2425

2526
afterAll(async () => {
26-
GatsbyCLI.from(cwd).invoke([`options`, `set`,`pm`, originalPackageManager])
27+
GatsbyCLI.from(cwd).invoke([`options`, `set`, `pm`, originalPackageManager])
2728
await clean(dir)
2829
})
2930

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
const path = require(`path`)
2+
const execa = require(`execa`)
3+
const fs = require(`fs-extra`)
4+
5+
module.exports = async () => {
6+
console.log(
7+
`Installing "gatsby-cli" in sandbox directory: "${process.env.GLOBAL_GATSBY_CLI_LOCATION}"`
8+
)
9+
console.log(
10+
`Tests will use "${process.env.GLOBAL_GATSBY_CLI_LOCATION}/node_modules/.bin/gatsby" CLI to invoke commands`
11+
)
12+
13+
await fs.ensureDir(process.env.GLOBAL_GATSBY_CLI_LOCATION)
14+
15+
await fs.outputJson(
16+
path.join(process.env.GLOBAL_GATSBY_CLI_LOCATION, `package.json`),
17+
{
18+
dependencies: {
19+
"gatsby-cli": "latest",
20+
},
21+
}
22+
)
23+
24+
const gatsbyDevLocation = path.join(
25+
__dirname,
26+
`..`,
27+
`..`,
28+
`packages`,
29+
`gatsby-dev-cli`,
30+
`dist`,
31+
`index.js`
32+
)
33+
34+
await execa.node(gatsbyDevLocation, [`--force-install`, `--scan-once`], {
35+
cwd: process.env.GLOBAL_GATSBY_CLI_LOCATION,
36+
stdio: `inherit`,
37+
env: {
38+
// we don't want to run gatsby-dev in with NODE_ENV=test
39+
NODE_ENV: `production`,
40+
},
41+
})
42+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
const fs = require(`fs`)
2+
const path = require(`path`)
3+
const os = require(`os`)
4+
const baseConfig = require(`../jest.config.js`)
5+
6+
// install global gatsby-cli to tmp dir to simulate sandbox
7+
const GLOBAL_GATSBY_CLI_LOCATION = (process.env.GLOBAL_GATSBY_CLI_LOCATION = fs.mkdtempSync(
8+
path.join(os.tmpdir(), `gatsby-cli-`)
9+
))
10+
11+
module.exports = {
12+
...baseConfig,
13+
globalSetup: "<rootDir>/integration-tests/gatsby-cli/jest.boot.js",
14+
rootDir: `../../`,
15+
globals: {
16+
GLOBAL_GATSBY_CLI_LOCATION,
17+
},
18+
}

integration-tests/gatsby-cli/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@
22
"name": "gatsby-cli-tests",
33
"version": "1.0.0",
44
"dependencies": {
5-
"gatsby-cli": "^2.12.29",
65
"strip-ansi": "^6.0.0"
76
},
87
"license": "MIT",
98
"scripts": {
10-
"test": "jest --config=../jest.config.js gatsby-cli/"
9+
"test": "jest --config=./jest.config.js gatsby-cli/"
1110
},
1211
"devDependencies": {
1312
"del-cli": "^3.0.1",

integration-tests/gatsby-cli/test-helpers/invoke-cli.js

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
11
import execa, { sync } from "execa"
2-
import { join, resolve } from "path"
2+
import { join } from "path"
33
import { createLogsMatcher } from "./matcher"
44

5+
const gatsbyBinLocation = join(
6+
GLOBAL_GATSBY_CLI_LOCATION,
7+
`node_modules`,
8+
`.bin`,
9+
`gatsby`
10+
)
11+
512
// Use as `GatsbyCLI.cwd('execution-folder').invoke('new', 'foo')`
613
export const GatsbyCLI = {
714
from(relativeCwd) {
815
return {
916
invoke(args) {
1017
const NODE_ENV = args[0] === `develop` ? `development` : `production`
1118
try {
12-
const results = sync(
13-
resolve(`./node_modules/.bin/gatsby`),
14-
[].concat(args),
15-
{
16-
cwd: join(__dirname, `../`, `./${relativeCwd}`),
17-
env: { NODE_ENV, CI: 1, GATSBY_LOGGER: `ink` },
18-
}
19-
)
19+
const results = sync(gatsbyBinLocation, [].concat(args), {
20+
cwd: join(__dirname, `../`, `./${relativeCwd}`),
21+
env: { NODE_ENV, CI: 1, GATSBY_LOGGER: `ink` },
22+
})
2023

2124
return [
2225
results.exitCode,
@@ -32,14 +35,10 @@ export const GatsbyCLI = {
3235

3336
invokeAsync: (args, onExit) => {
3437
const NODE_ENV = args[0] === `develop` ? `development` : `production`
35-
const res = execa(
36-
resolve(`./node_modules/.bin/gatsby`),
37-
[].concat(args),
38-
{
39-
cwd: join(__dirname, `../`, `./${relativeCwd}`),
40-
env: { NODE_ENV, CI: 1, GATSBY_LOGGER: `ink` },
41-
}
42-
)
38+
const res = execa(gatsbyBinLocation, [].concat(args), {
39+
cwd: join(__dirname, `../`, `./${relativeCwd}`),
40+
env: { NODE_ENV, CI: 1, GATSBY_LOGGER: `ink` },
41+
})
4342

4443
const logs = []
4544
res.stdout.on("data", data => {

0 commit comments

Comments
 (0)