diff --git a/__tests__/spec/install.js b/__tests__/spec/install.js index 453f91d241..e83a62ded7 100644 --- a/__tests__/spec/install.js +++ b/__tests__/spec/install.js @@ -1,10 +1,10 @@ /* eslint-env jest */ -const child_process = require('child_process') // eslint-disable-line camelcase const fs = require('fs') const path = require('path') const utils = require('../util') +const { exec } = require('../../lib/exec') describe('npm install', () => { const tmpDir = utils.mkdtempSync() @@ -13,9 +13,9 @@ describe('npm install', () => { const testDir = path.join(tmpDir, 'install-no-optional') await utils.mkPrototype(testDir) - child_process.execSync( + await exec( 'npm install', - { cwd: testDir, env: { LANG: process.env.LANG, PATH: process.env.PATH } } + { cwd: testDir } ) expect( diff --git a/__tests__/spec/sanity-checks.js b/__tests__/spec/sanity-checks.js index 15250dd7a4..482f085fdc 100644 --- a/__tests__/spec/sanity-checks.js +++ b/__tests__/spec/sanity-checks.js @@ -15,6 +15,8 @@ const { generateAssetsSync } = require('../../lib/build/tasks') const fse = require('fs-extra') const { projectDir } = require('../../lib/path-utils') +const createKitTimeout = parseInt(process.env.CREATE_KIT_TIMEOUT || '90000', 10) + function readFile (pathFromRoot) { return fs.readFileSync(path.join(__dirname, '../../' + pathFromRoot), 'utf8') } @@ -29,7 +31,7 @@ describe('The Prototype Kit', () => { jest.spyOn(fse, 'writeFileSync').mockImplementation(() => {}) jest.spyOn(sass, 'compile').mockImplementation((css, options) => ({ css })) generateAssetsSync() - }) + }, createKitTimeout) it('should call writeFileSync with result css from sass.compile', () => { expect(fse.writeFileSync).toHaveBeenCalledWith( diff --git a/__tests__/util/index.js b/__tests__/util/index.js index f0ad151bbc..34d0ad70dd 100644 --- a/__tests__/util/index.js +++ b/__tests__/util/index.js @@ -1,9 +1,10 @@ -const child_process = require('child_process') // eslint-disable-line camelcase - -const fs = require('fs-extra') const os = require('os') const path = require('path') +const fs = require('fs-extra') + +const { exec } = require('../../lib/exec') + /** * An ID that will be shared between all process in the same Jest test run, * this is useful for sharing fixture files. Normally sharing state across Jest @@ -71,6 +72,8 @@ async function mkPrototype (prototypePath, { } } + const startTime = Date.now() + try { // Remove previous test starter project await fs.remove(prototypePath) @@ -79,20 +82,21 @@ async function mkPrototype (prototypePath, { await fs.mkdirp(prototypePath) // Generate starter project and start - child_process.execSync( - 'npm i -g', - { stdio: 'inherit' } + await exec( + 'npm i -g' ) // Generate starter project and start - child_process.execSync( + await exec( 'govuk-prototype-kit create --version local', - { cwd: prototypePath, env: { ...process.env, env: 'test' }, stdio: 'inherit' } + { cwd: prototypePath, env: { ...process.env, env: 'test' } } ) if (allowTracking !== undefined) { - await fs.writeFile(path.join(prototypePath, 'usage-data-config.json'), `{ "collectUsageData": ${allowTracking}}`) + await fs.writeFile(path.join(prototypePath, 'usage-data-config.json'), `{ "collectUsageData": ${!!allowTracking}}`) } + + console.log(`Kit creation took [${Math.round((Date.now() - startTime) / 100) / 10}] seconds`) } catch (error) { console.error(error.message) console.error(error.stack) @@ -102,28 +106,28 @@ async function mkPrototype (prototypePath, { } } -function installExtensions (prototypePath, extensionNames) { +async function installExtensions (prototypePath, extensionNames) { let extensionNamesProcessed = extensionNames || [] if (!Array.isArray(extensionNames)) { extensionNamesProcessed = [extensionNames] } - child_process.execSync( + return exec( `npm install ${extensionNamesProcessed.join(' ')}`, - { cwd: prototypePath, env: { ...process.env, env: 'test' }, stdio: 'inherit' } + { cwd: prototypePath, env: { ...process.env, env: 'test' } } ) } -function npmInstall (pathToRunInstallIn) { - return child_process.exec( +async function npmInstall (pathToRunInstallIn) { + return exec( 'npm install', - { cwd: pathToRunInstallIn, env: { ...process.env, env: 'test' }, stdio: 'inherit' } + { cwd: pathToRunInstallIn, env: { ...process.env, env: 'test' } } ) } -function startPrototype (prototypePath) { - child_process.execSync( +async function startPrototype (prototypePath) { + return exec( 'npm start', - { cwd: prototypePath, env: { ...process.env, env: 'test' }, stdio: 'inherit' } + { cwd: prototypePath, env: { ...process.env, env: 'test' } } ) } diff --git a/cypress/plugins/index.js b/cypress/plugins/index.js index 37facbb403..b9da1d54e3 100644 --- a/cypress/plugins/index.js +++ b/cypress/plugins/index.js @@ -11,7 +11,6 @@ // This function is called when a project is opened or re-opened (e.g. due to // the project's config changing) -// const spawn = require('child_process').spawn const fs = require('fs') const fsp = fs.promises const path = require('path') diff --git a/lib/routes/prototype-admin-routes.js b/lib/routes/prototype-admin-routes.js index 965b969393..f1cb6423ac 100644 --- a/lib/routes/prototype-admin-routes.js +++ b/lib/routes/prototype-admin-routes.js @@ -11,7 +11,7 @@ const path = require('path') const extensions = require('../extensions/extensions') const config = require('../config').getConfig() const { requestHttpsJson } = require('../utils') -const { exec } = require('../upgradeToV13/exec') +const { exec } = require('../exec') const appViews = extensions.getAppViews([ path.join(projectDir, 'node_modules'), diff --git a/lib/upgradeToV13/exec.js b/lib/upgradeToV13/exec.js deleted file mode 100644 index 3f1675b585..0000000000 --- a/lib/upgradeToV13/exec.js +++ /dev/null @@ -1,48 +0,0 @@ -const { exec } = require('child_process') -const { verboseLog } = require('./fileHelpers') - -module.exports = { - exec: (command, options) => { - const errorOutput = [] - const optionsProcessed = Object.assign({}, options || {}) - - optionsProcessed.env = optionsProcessed.env || {} - optionsProcessed.env.LANG = optionsProcessed.env.LANG || process.env.LANG - optionsProcessed.env.PATH = optionsProcessed.env.PATH || process.env.PATH - - const child = exec(command, optionsProcessed) - - child.stdout.on('data', function (data) { - verboseLog('') - verboseLog(' ---- ') - verboseLog('') - verboseLog('install update:') - verboseLog(data) - verboseLog('') - verboseLog(' ---- ') - verboseLog('') - }) - child.stderr.on('data', function (data) { - errorOutput.push(data) - verboseLog('') - verboseLog(' ---- ') - verboseLog('') - verboseLog('install error:') - verboseLog(data) - verboseLog('') - verboseLog(' ---- ') - verboseLog('') - }) - - return new Promise(function (resolve, reject) { - child.on('close', function (code) { - if (code === 0) { - resolve(true) - } else { - console.error(errorOutput.join('\n')) - reject(new Error(`Exit code was ${code}`)) - } - }) - }) - } -} diff --git a/lib/upgradeToV13/fileHelpers.spec.js b/lib/upgradeToV13/fileHelpers.spec.js index a61180297a..ef198a2875 100644 --- a/lib/upgradeToV13/fileHelpers.spec.js +++ b/lib/upgradeToV13/fileHelpers.spec.js @@ -6,7 +6,7 @@ const { mockFileSystem } = require('../../__tests__/util/mockFileSystem') const { projectDir } = require('../path-utils') const { removeLineFromFile, replaceStartOfFile, deleteFile, deleteDirectory } = require('./fileHelpers') -describe.skip('file helpers', () => { +describe('file helpers', () => { const joinLines = arr => arr.join(os.EOL) const writeFileToMockFile = (filePathParts, contentStringOrArray) => testScope.fileSystem .writeFile(filePathParts, typeof contentStringOrArray === 'string' ? contentStringOrArray : joinLines(contentStringOrArray)) diff --git a/lib/upgradeToV13/index.js b/lib/upgradeToV13/index.js index 5c8a0a1b4e..f7d7c928f8 100644 --- a/lib/upgradeToV13/index.js +++ b/lib/upgradeToV13/index.js @@ -14,7 +14,7 @@ const { } = require('./fileHelpers') const path = require('path') const { projectDir, packageDir } = require('../path-utils') -const { exec } = require('./exec') +const { exec } = require('../exec') const fullReport = [] const reportSuccess = tag => fullReport.push(() => console.log(c.green(`Succeeded [${tag}]`))) diff --git a/package.json b/package.json index cf2b4e7984..dc8c054958 100644 --- a/package.json +++ b/package.json @@ -35,7 +35,7 @@ "test:acceptance:open": "KIT_TEST_DIR=tmp/test-prototype-package start-server-and-test 'node cypress/scripts/run-starter-prototype' 3000 'cypress open'", "test:smoke": "cypress run --spec \"cypress/integration/0-smoke-tests/*\"", "test:unit": "jest --detectOpenHandles lib bin", - "test:integration": "IS_INTEGRATION_TEST=true jest --detectOpenHandles --testTimeout=30000 __tests__", + "test:integration": "CREATE_KIT_TIMEOUT=90000 IS_INTEGRATION_TEST=true jest --detectOpenHandles --testTimeout=60000 __tests__", "test": "npm run test:unit && npm run test:integration && npm run lint" }, "dependencies": {