Skip to content
This repository has been archived by the owner on Aug 17, 2019. It is now read-only.

Commit

Permalink
fix(config): use npm.cmd on win32 and fix tests (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
rmg authored and zkat committed Sep 6, 2017
1 parent 14a9a5f commit d912d16
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 17 deletions.
3 changes: 2 additions & 1 deletion lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
12 changes: 9 additions & 3 deletions test/lib/fixtureHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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])
})
}

Expand All @@ -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 {
Expand All @@ -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()
}
Expand Down
29 changes: 16 additions & 13 deletions test/specs/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
'use strict'

const path = require('path')
const test = require('tap').test
const requireInject = require('require-inject')
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': {
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit d912d16

Please sign in to comment.