1var fs = require('graceful-fs') 2var path = require('path') 3 4var mkdirp = require('mkdirp') 5var rimraf = require('rimraf') 6var test = require('tap').test 7 8var common = require('../common-tap.js') 9 10var root = common.pkg 11var pkg = path.join(root, 'package') 12 13var EXEC_OPTS = { cwd: pkg } 14 15var json = { 16 name: 'install-save-local', 17 version: '0.0.0' 18} 19 20var localDependency = { 21 name: 'package-local-dependency', 22 version: '0.0.0' 23} 24 25var localDevDependency = { 26 name: 'package-local-dev-dependency', 27 version: '0.0.0' 28} 29 30test('setup deps in root', t => { 31 mkdirp.sync(path.join(root, 'package-local-dependency')) 32 fs.writeFileSync( 33 path.join(root, 'package-local-dependency', 'package.json'), 34 JSON.stringify(localDependency, null, 2) 35 ) 36 37 mkdirp.sync(path.join(root, 'package-local-dev-dependency')) 38 fs.writeFileSync( 39 path.join(root, 'package-local-dev-dependency', 'package.json'), 40 JSON.stringify(localDevDependency, null, 2) 41 ) 42 43 t.end() 44}) 45 46test('\'npm install --save ../local/path\' should save to package.json', function (t) { 47 t.plan(2) 48 t.test('setup', setup) 49 t.test('run test', t => common.npm( 50 [ 51 '--loglevel', 'silent', 52 '--save', 53 'install', '../package-local-dependency' 54 ], 55 EXEC_OPTS 56 ).then(([code]) => { 57 t.equal(code, 0, 'npm install exited with code 0') 58 59 var dependencyPackageJson = path.join( 60 pkg, 'node_modules', 'package-local-dependency', 'package.json' 61 ) 62 t.ok(JSON.parse(fs.readFileSync(dependencyPackageJson, 'utf8'))) 63 64 var pkgJson = JSON.parse(fs.readFileSync(pkg + '/package.json', 'utf8')) 65 t.is(Object.keys(pkgJson.dependencies).length, 1, 'only one dep') 66 t.ok( 67 /file:.*?[/]package-local-dependency$/.test(pkgJson.dependencies['package-local-dependency']), 68 'local package saved correctly' 69 ) 70 })) 71}) 72 73test('\'npm install --save local/path\' should save to package.json', function (t) { 74 t.plan(2) 75 t.test('setup', setup) 76 t.test('run test', t => common.npm( 77 [ 78 '--loglevel', 'silent', 79 '--save', 80 'install', 'package-local-dependency/' 81 ], 82 EXEC_OPTS 83 ).then(([code, out, err]) => { 84 t.equal(code, 0, 'npm install exited with code 0') 85 86 var dependencyPackageJson = path.join( 87 pkg, 'node_modules', 'package-local-dependency', 'package.json' 88 ) 89 t.ok(JSON.parse(fs.readFileSync(dependencyPackageJson, 'utf8'))) 90 91 var pkgJson = JSON.parse(fs.readFileSync(pkg + '/package.json', 'utf8')) 92 t.is(Object.keys(pkgJson.dependencies).length, 1, 'only one dep') 93 t.ok( 94 /file:package-local-dependency$/.test(pkgJson.dependencies['package-local-dependency']), 95 'local package saved correctly' 96 ) 97 })) 98}) 99 100test('\'npm install --save-dev ../local/path\' should save to package.json', function (t) { 101 t.plan(2) 102 t.test('setup', setup) 103 t.test('run test', t => common.npm( 104 [ 105 '--loglevel', 'silent', 106 '--save-dev', 107 'install', '../package-local-dev-dependency' 108 ], 109 EXEC_OPTS 110 ).then(([code]) => { 111 t.equal(code, 0, 'npm install exited with code 0') 112 113 var dependencyPackageJson = path.resolve( 114 pkg, 'node_modules', 'package-local-dev-dependency', 'package.json' 115 ) 116 t.ok(JSON.parse(fs.readFileSync(dependencyPackageJson, 'utf8'))) 117 118 var pkgJson = JSON.parse(fs.readFileSync(pkg + '/package.json', 'utf8')) 119 t.is(Object.keys(pkgJson.devDependencies).length, 1, 'only one dep') 120 t.ok( 121 /file:.*?[/\\]package-local-dev-dependency$/.test(pkgJson.devDependencies['package-local-dev-dependency']), 122 'local package saved correctly' 123 ) 124 125 t.end() 126 })) 127}) 128 129test('\'npm install --save-dev local/path\' should save to package.json', function (t) { 130 t.plan(2) 131 t.test('setup', setup) 132 t.test('run test', t => common.npm( 133 [ 134 '--loglevel', 'silent', 135 '--save-dev', 136 'install', 'package-local-dev-dependency/' 137 ], 138 EXEC_OPTS 139 ).then(([code]) => { 140 t.equal(code, 0, 'npm install exited with code 0') 141 142 var dependencyPackageJson = path.resolve( 143 pkg, 'node_modules', 'package-local-dev-dependency', 'package.json' 144 ) 145 t.ok(JSON.parse(fs.readFileSync(dependencyPackageJson, 'utf8'))) 146 147 var pkgJson = JSON.parse(fs.readFileSync(pkg + '/package.json', 'utf8')) 148 t.is(Object.keys(pkgJson.devDependencies).length, 1, 'only one dep') 149 t.ok( 150 /file:package-local-dev-dependency$/.test(pkgJson.devDependencies['package-local-dev-dependency']), 151 'local package saved correctly' 152 ) 153 154 t.end() 155 })) 156}) 157 158function setup (t) { 159 t.test('destroy', t => rimraf(pkg, t.end)) 160 t.test('create', t => { 161 mkdirp.sync(pkg) 162 fs.writeFileSync( 163 path.join(pkg, 'package.json'), 164 JSON.stringify(json, null, 2) 165 ) 166 167 mkdirp.sync(path.join(pkg, 'package-local-dependency')) 168 fs.writeFileSync( 169 path.join(pkg, 'package-local-dependency', 'package.json'), 170 JSON.stringify(localDependency, null, 2) 171 ) 172 173 mkdirp.sync(path.join(pkg, 'package-local-dev-dependency')) 174 fs.writeFileSync( 175 path.join(pkg, 'package-local-dev-dependency', 'package.json'), 176 JSON.stringify(localDevDependency, null, 2) 177 ) 178 t.end() 179 }) 180 t.end() 181} 182