1var fs = require('graceful-fs') 2var path = require('path') 3 4var mkdirp = require('mkdirp') 5var rimraf = require('rimraf') 6const t = require('tap') 7 8var common = require('../common-tap.js') 9common.skipIfWindows('links are weird on windows') 10 11var pkg = common.pkg 12var tmp = path.join(pkg, 'tmp') 13var dep = path.join(pkg, 'dep') 14 15var json = { 16 name: 'install-link-scripts', 17 version: '1.0.0', 18 description: 'a test', 19 repository: 'git://github.com/npm/npm.git', 20 license: 'ISC' 21} 22 23var dependency = { 24 name: 'dep', 25 version: '1.0.0', 26 scripts: { 27 install: 'node ./bin/foo' 28 } 29} 30 31var foo = function () { /* 32#!/usr/bin/env node 33 34console.log('hey sup') 35*/ }.toString().split('\n').slice(1, -1).join('\n') 36 37process.env.npm_config_prefix = tmp 38 39t.beforeEach(cb => { 40 rimraf(pkg, er => { 41 if (er) { 42 return cb(er) 43 } 44 mkdirp.sync(tmp) 45 fs.writeFileSync( 46 path.join(pkg, 'package.json'), 47 JSON.stringify(json, null, 2) 48 ) 49 50 mkdirp.sync(path.join(dep, 'bin')) 51 fs.writeFileSync( 52 path.join(dep, 'package.json'), 53 JSON.stringify(dependency, null, 2) 54 ) 55 fs.writeFileSync(path.join(dep, 'bin', 'foo'), foo) 56 fs.chmod(path.join(dep, 'bin', 'foo'), '0755') 57 cb() 58 }) 59}) 60 61t.test('plain install', function (t) { 62 common.npm( 63 [ 64 'install', dep, 65 '--tmp', tmp 66 ], 67 { cwd: pkg }, 68 function (err, code, stdout, stderr) { 69 t.ifErr(err, 'npm install ' + dep + ' finished without error') 70 t.equal(code, 0, 'exited ok') 71 t.notOk(stderr, 'no output stderr') 72 t.match(stdout, /hey sup/, 'postinstall script for dep ran') 73 t.end() 74 } 75 ) 76}) 77 78t.test('link', function (t) { 79 common.npm( 80 [ 81 'link', 82 '--tmp', tmp 83 ], 84 { cwd: dep }, 85 function (err, code, stdout, stderr) { 86 t.ifErr(err, 'npm link finished without error') 87 t.equal(code, 0, 'exited ok') 88 t.notOk(stderr, 'no output stderr') 89 t.match(stdout, /hey sup/, 'script ran') 90 t.end() 91 } 92 ) 93}) 94 95t.test('install --link', function (t) { 96 common.npm( 97 [ 98 'link', 99 '--tmp', tmp 100 ], 101 { cwd: dep }, 102 function (err, code, stdout, stderr) { 103 t.ifErr(err, 'npm link finished without error') 104 105 common.npm( 106 [ 107 'install', '--link', dependency.name, 108 '--tmp', tmp 109 ], 110 { cwd: pkg }, 111 function (err, code, stdout, stderr) { 112 t.ifErr(err, 'npm install --link finished without error') 113 t.equal(code, 0, 'exited ok') 114 t.notOk(stderr, 'no output stderr') 115 t.notMatch(stdout, /hey sup/, "script didn't run") 116 t.end() 117 } 118 ) 119 } 120 ) 121}) 122