1var fs = require('graceful-fs') 2var path = require('path') 3var existsSync = fs.existsSync || path.existsSync 4 5var mkdirp = require('mkdirp') 6var test = require('tap').test 7 8var common = require('../common-tap') 9 10var root = common.pkg 11var pkg = path.join(root, 'package-with-scoped-paths') 12var modules = path.join(pkg, 'node_modules') 13 14var EXEC_OPTS = { cwd: pkg } 15 16var scopedPaths = { 17 name: 'package-with-scoped-paths', 18 version: '0.0.0', 19 dependencies: { 20 'package-local-dependency': 'file:../package-local-dependency', 21 '@scoped/package-scoped-dependency': 'file:../package-scoped-dependency' 22 } 23} 24 25var localDependency = { 26 name: 'package-local-dependency', 27 version: '0.0.0', 28 description: 'Test for local installs' 29} 30 31var scopedDependency = { 32 name: '@scoped/package-scoped-dependency', 33 version: '0.0.0', 34 description: 'Test for local installs' 35} 36 37test('setup', function (t) { 38 mkdirp.sync(pkg) 39 fs.writeFileSync( 40 path.join(pkg, 'package.json'), 41 JSON.stringify(scopedPaths, null, 2) 42 ) 43 44 mkdirp.sync(path.join(root, 'package-local-dependency')) 45 fs.writeFileSync( 46 path.join(root, 'package-local-dependency', 'package.json'), 47 JSON.stringify(localDependency, null, 2) 48 ) 49 50 mkdirp.sync(path.join(root, 'package-scoped-dependency')) 51 fs.writeFileSync( 52 path.join(root, 'package-scoped-dependency', 'package.json'), 53 JSON.stringify(scopedDependency, null, 2) 54 ) 55 56 t.end() 57}) 58 59test('installing already installed local scoped package', function (t) { 60 common.npm( 61 [ 62 '--loglevel', 'silent', 63 '--parseable', 64 'install' 65 ], 66 EXEC_OPTS, 67 function (err, code, stdout) { 68 var installed = parseNpmInstallOutput(stdout) 69 t.ifError(err, 'install ran to completion without error') 70 t.notOk(code, 'npm install exited with code 0') 71 t.ok( 72 existsSync(path.join(modules, '@scoped', 'package-scoped-dependency', 'package.json')), 73 'package installed' 74 ) 75 t.ok( 76 contains(installed, 'node_modules/@scoped/package-scoped-dependency'), 77 'installed @scoped/package-scoped-dependency' 78 ) 79 t.ok( 80 contains(installed, 'node_modules/package-local-dependency'), 81 'installed package-local-dependency' 82 ) 83 84 common.npm( 85 [ 86 '--loglevel', 'silent', 87 '--parseable', 88 'install' 89 ], 90 EXEC_OPTS, 91 function (err, code, stdout) { 92 t.ifError(err, 'install ran to completion without error') 93 t.notOk(code, 'npm install raised no error code') 94 95 installed = parseNpmInstallOutput(stdout) 96 97 t.ok( 98 existsSync(path.join(modules, '@scoped', 'package-scoped-dependency', 'package.json')), 99 'package installed' 100 ) 101 102 t.notOk( 103 contains(installed, 'node_modules/@scoped/package-scoped-dependency'), 104 'did not reinstall @scoped/package-scoped-dependency' 105 ) 106 t.notOk( 107 contains(installed, 'node_modules/package-local-dependency'), 108 'did not reinstall package-local-dependency' 109 ) 110 t.end() 111 } 112 ) 113 } 114 ) 115}) 116 117function contains (list, element) { 118 var matcher = new RegExp(element.replace(/\//g, '[\\\\/]') + '$') 119 for (var i = 0; i < list.length; ++i) { 120 if (matcher.test(list[i])) { 121 return true 122 } 123 } 124 return false 125} 126 127function parseNpmInstallOutput (stdout) { 128 return stdout.trim().split(/\n\n|\s+/) 129} 130