From d912d16079866de39d1b8ae86b645503607f3ab3 Mon Sep 17 00:00:00 2001 From: Ryan Graham Date: Wed, 6 Sep 2017 04:18:18 -0700 Subject: [PATCH] fix(config): use npm.cmd on win32 and fix tests (#12) --- lib/config.js | 3 ++- test/lib/fixtureHelper.js | 12 +++++++++--- test/specs/index.js | 29 ++++++++++++++++------------- 3 files changed, 27 insertions(+), 17 deletions(-) diff --git a/lib/config.js b/lib/config.js index 4303a98..e0f73be 100644 --- a/lib/config.js +++ b/lib/config.js @@ -11,7 +11,8 @@ let _config function readConfig () { return new BB((resolve, reject) => { - const child = spawn('npm', ['config', 'ls', '--json'], { + const npmBin = process.platform === 'win32' ? 'npm.cmd' : 'npm' + const child = spawn(npmBin, ['config', 'ls', '--json'], { env: process.env, cwd: process.cwd(), stdio: [0, 'pipe', 2] diff --git a/test/lib/fixtureHelper.js b/test/lib/fixtureHelper.js index a4f5181..9243f62 100644 --- a/test/lib/fixtureHelper.js +++ b/test/lib/fixtureHelper.js @@ -4,10 +4,11 @@ const BB = require('bluebird') const fs = require('fs') const mkdirp = require('mkdirp') const resolve = require('path').resolve +const pathSep = require('path').sep const rimraf = require('rimraf') function getPath (dir) { - return resolve(__dirname, '..', 'fixtures', dir) + return resolve(__dirname, '..', 'fixtures', dir.replace(/\//g, pathSep)) } function writeFile (path, name, contents) { @@ -19,7 +20,8 @@ function writeFile (path, name, contents) { function writeFiles (path, fs) { Object.keys(fs).forEach(fileName => { - writeFile(path, fileName, fs[fileName]) + const filePath = fileName.replace(/\//g, pathSep) + writeFile(path, filePath, fs[fileName]) }) } @@ -44,6 +46,10 @@ module.exports = { return expected === fs.readFileSync(resolve(path, name)).toString() }, + read (dir, name) { + const path = getPath(dir) + return fs.readFileSync(resolve(path, name)).toString() + }, missing (dir, name) { const path = getPath(dir) try { @@ -59,7 +65,7 @@ module.exports = { const path = getPath(dir) return (_, child, childPath) => { mkdirp.sync(childPath) - const pathSuffix = childPath.replace(path, '') + const pathSuffix = childPath.replace(path, '').replace(/\\/g, '/') writeFiles(childPath, fs[pathSuffix]) return BB.resolve() } diff --git a/test/specs/index.js b/test/specs/index.js index 7d9b97e..dd9033b 100644 --- a/test/specs/index.js +++ b/test/specs/index.js @@ -1,5 +1,6 @@ 'use strict' +const path = require('path') const test = require('tap').test const requireInject = require('require-inject') const fixtureHelper = require('../lib/fixtureHelper.js') @@ -7,7 +8,9 @@ const fixtureHelper = require('../lib/fixtureHelper.js') let extract = () => {} const pkgName = 'hark-a-package' const pkgVersion = '1.0.0' -const writeEnvScript = 'node -e \'const fs = require("fs"); fs.writeFileSync(process.cwd() + "/" + process.env.npm_lifecycle_event, process.env.npm_lifecycle_event);\'' +const writeEnvScript = process.platform === 'win32' + ? 'echo %npm_lifecycle_event% > %npm_lifecycle_event%' + : 'echo $npm_lifecycle_event > $npm_lifecycle_event' const Installer = requireInject('../../index.js', { '../../lib/extract': { @@ -135,7 +138,7 @@ test('handles dependency list with only shallow subdeps', t => { new Installer({prefix}).run().then(details => { t.equal(details.pkgCount, 1) - t.ok(fixtureHelper.equals(prefix + '/node_modules/a', 'index.js', aContents)) + t.ok(fixtureHelper.equals(path.join(prefix, 'node_modules', 'a'), 'index.js', aContents)) fixtureHelper.teardown() t.end() @@ -182,8 +185,8 @@ test('handles dependency list with only deep subdeps', t => { new Installer({prefix}).run().then(details => { t.equal(details.pkgCount, 2) - t.ok(fixtureHelper.equals(prefix + '/node_modules/a', 'index.js', aContents)) - t.ok(fixtureHelper.equals(prefix + '/node_modules/a/node_modules/b', 'index.js', bContents)) + t.ok(fixtureHelper.equals(path.join(prefix, 'node_modules', 'a'), 'index.js', aContents)) + t.ok(fixtureHelper.equals(path.join(prefix, 'node_modules', 'a', 'node_modules', 'b'), 'index.js', bContents)) fixtureHelper.teardown() t.end() @@ -228,12 +231,12 @@ test('runs lifecycle hooks of packages with env variables', t => { new Installer({prefix}).run().then(details => { t.equal(details.pkgCount, 1) - t.ok(fixtureHelper.equals(prefix, 'preinstall', 'preinstall')) - t.ok(fixtureHelper.equals(prefix, 'install', 'install')) - t.ok(fixtureHelper.equals(prefix, 'postinstall', 'postinstall')) - t.ok(fixtureHelper.equals(prefix + '/node_modules/a', 'preinstall', 'preinstall')) - t.ok(fixtureHelper.equals(prefix + '/node_modules/a', 'install', 'install')) - t.ok(fixtureHelper.equals(prefix + '/node_modules/a', 'postinstall', 'postinstall')) + t.match(fixtureHelper.read(prefix, 'preinstall'), 'preinstall') + t.match(fixtureHelper.read(prefix, 'install'), 'install') + t.match(fixtureHelper.read(prefix, 'postinstall'), 'postinstall') + t.match(fixtureHelper.read(path.join(prefix, 'node_modules', 'a'), 'preinstall'), 'preinstall') + t.match(fixtureHelper.read(path.join(prefix, 'node_modules', 'a'), 'install'), 'install') + t.match(fixtureHelper.read(path.join(prefix, 'node_modules', 'a'), 'postinstall'), 'postinstall') fixtureHelper.teardown() console.log = originalConsoleLog @@ -286,9 +289,9 @@ test('skips lifecycle scripts with ignoreScripts is set', t => { t.ok(fixtureHelper.missing(prefix, 'preinstall')) t.ok(fixtureHelper.missing(prefix, 'install')) t.ok(fixtureHelper.missing(prefix, 'postinstall')) - t.ok(fixtureHelper.missing(prefix + '/node_modules/a', 'preinstall')) - t.ok(fixtureHelper.missing(prefix + '/node_modules/a', 'install')) - t.ok(fixtureHelper.missing(prefix + '/node_modules/a', 'postinstall')) + t.ok(fixtureHelper.missing(path.join(prefix, 'node_modules', 'a'), 'preinstall')) + t.ok(fixtureHelper.missing(path.join(prefix, 'node_modules', 'a'), 'install')) + t.ok(fixtureHelper.missing(path.join(prefix, 'node_modules', 'a'), 'postinstall')) fixtureHelper.teardown() console.log = originalConsoleLog