From 3c76e2a403d56c346ad3d44eddc274fc545a435f Mon Sep 17 00:00:00 2001 From: zkochan Date: Sat, 15 Oct 2016 19:36:50 +0300 Subject: [PATCH] fix(lifecycle): npm scripts can find bin files of dependencies --- src/runScript.ts | 2 + test/install.ts | 50 ++++++++++++-------- test/packages/pkg-that-uses-plugins/index.js | 2 +- test/support/prepare.ts | 2 + 4 files changed, 35 insertions(+), 21 deletions(-) diff --git a/src/runScript.ts b/src/runScript.ts index 724ca6742c..fb2e4e519b 100644 --- a/src/runScript.ts +++ b/src/runScript.ts @@ -13,6 +13,8 @@ if (process.platform === 'win32') { PATH = e } }) +} else { + PATH = 'PATH' } export type RunScriptOptions = { diff --git a/test/install.ts b/test/install.ts index 751dd91550..04b11845eb 100644 --- a/test/install.ts +++ b/test/install.ts @@ -147,7 +147,7 @@ test('overwriting (is-positive@3.0.0 with is-positive@latest)', async function ( test('forcing', async function (t) { prepare() - await installPkgs(['magic-hook@2.0.0']) + await installPkgs(['magic-hook@2.0.0'], testDefaults()) const distPath = path.join(process.cwd(), 'node_modules/.store/magic-hook@2.0.0/_/dist') await rimraf(distPath) @@ -458,28 +458,47 @@ test('packages should find the plugins they use when symlinks are preserved', as t.skip('this test only for NodeJS with --preserve-symlinks support') return } - prepare() - await installPkgs([local('pkg-that-uses-plugins'), local('plugin-example')], testDefaults({ save: true })) - const result = spawnSync('pkg-that-uses-plugins', [], { - env: extendPathWithLocalBin() + prepare({ + scripts: { + test: 'pkg-that-uses-plugins' + } }) - t.equal(result.stdout.toString(), 'plugin-example\n', 'package executable have found its plugin') + await installPkgs([local('pkg-that-uses-plugins'), local('plugin-example')], testDefaults({ save: true })) + const result = spawnSync('npm', ['test']) + t.ok(result.stdout.toString().indexOf('My plugin is plugin-example') !== -1, 'package executable have found its plugin') t.equal(result.status, 0, 'executable exited with success') }) test('run js bin file', async function (t) { - prepare() + prepare({ + scripts: { + test: 'hello-world-js-bin' + } + }) await installPkgs([local('hello-world-js-bin')], testDefaults({ save: true })) - const result = spawnSync('hello-world-js-bin', [], { - env: extendPathWithLocalBin() - }) - t.equal(result.stdout.toString(), 'Hello world!\n', 'package executable printed its message') + const result = spawnSync('npm', ['test']) + t.ok(result.stdout.toString().indexOf('Hello world!') !== -1, 'package executable printed its message') t.equal(result.status, 0, 'executable exited with success') }) const pnpmBin = path.join(__dirname, '../src/bin/pnpm.ts') +test('bin files are found by lifecycle scripts', t => { + prepare({ + scripts: { + postinstall: 'hello-world-js-bin' + } + }) + + const result = spawnSync('ts-node', [pnpmBin, 'install', local('hello-world-js-bin')]) + + t.equal(result.status, 0, 'installation was successfull') + t.ok(result.stdout.toString().indexOf('Hello world!') !== -1, 'postinstall script was executed') + + t.end() +}) + test('installation via the CLI', t => { prepare() const result = spawnSync('ts-node', [pnpmBin, 'install', 'rimraf@2.5.1']) @@ -607,12 +626,3 @@ test('should update subdep on second install', async function (t) { t.ok(await exists('node_modules/.store/dep-of-pkg-with-1-dep@1.1.0'), 'should update to dep-of-pkg-with-1-dep@1.1.0') }) - -function extendPathWithLocalBin () { - return { - PATH: [ - path.join(process.cwd(), 'node_modules', '.bin'), - process.env.PATH - ].join(path.delimiter) - } -} diff --git a/test/packages/pkg-that-uses-plugins/index.js b/test/packages/pkg-that-uses-plugins/index.js index 30f75ef93c..6dc64724e9 100644 --- a/test/packages/pkg-that-uses-plugins/index.js +++ b/test/packages/pkg-that-uses-plugins/index.js @@ -1,3 +1,3 @@ #!/usr/bin/env node 'use strict' -console.log(require('../plugin-example/package.json').name) +console.log('My plugin is', require('../plugin-example/package.json').name) diff --git a/test/support/prepare.ts b/test/support/prepare.ts index 1541fd93a4..014f4b4112 100644 --- a/test/support/prepare.ts +++ b/test/support/prepare.ts @@ -2,6 +2,7 @@ import mkdirp = require('mkdirp') import fs = require('fs') import path = require('path') import {stripIndent} from 'common-tags' +import globalPath from './globalPath' const root = process.cwd() process.env.ROOT = root @@ -14,6 +15,7 @@ const npmrc = stripIndent` fetch-retry-maxtimeout = 180000 registry = http://localhost:4873/ quiet = true + global-path = ${globalPath} ` fs.writeFileSync(path.join(tmpPath, '.npmrc'), npmrc, 'utf-8')