1var fs = require('fs') 2var path = require('path') 3 4var mr = require('npm-registry-mock') 5var rimraf = require('rimraf') 6var test = require('tap').test 7 8var common = require('../common-tap.js') 9 10var pkg = common.pkg 11 12var EXEC_OPTS = { cwd: pkg } 13 14var json = { 15 name: 'install-save-prefix', 16 version: '0.0.1' 17} 18 19test('start mock reg', function (t) { 20 mr({ port: common.port }, function (er, s) { 21 t.ifError(er, 'started mock registry') 22 t.parent.teardown(() => s.close()) 23 t.end() 24 }) 25}) 26 27test('install --save with \'^\' save prefix should accept minor updates', function (t) { 28 t.plan(2) 29 t.test('setup', setup) 30 t.test('run test', t => common.npm( 31 [ 32 '--registry', common.registry, 33 '--loglevel', 'silent', 34 '--save-prefix', '^', 35 '--save', 36 'install', 'underscore@latest' 37 ], 38 EXEC_OPTS 39 ).then(([code]) => { 40 console.error('back from install!', code) 41 t.equal(code, 0, 'npm install exited with code 0') 42 43 var p = path.join(pkg, 'node_modules', 'underscore', 'package.json') 44 t.ok(JSON.parse(fs.readFileSync(p))) 45 46 var pkgJson = JSON.parse(fs.readFileSync( 47 path.join(pkg, 'package.json'), 48 'utf8' 49 )) 50 t.deepEqual( 51 pkgJson.dependencies, 52 { 'underscore': '^1.5.1' }, 53 'got expected save prefix and version of 1.5.1' 54 ) 55 })) 56}) 57 58test('install --save-dev with \'^\' save prefix should accept minor dev updates', function (t) { 59 t.plan(2) 60 t.test('setup', setup) 61 t.test('run test', t => common.npm( 62 [ 63 '--registry', common.registry, 64 '--loglevel', 'silent', 65 '--save-prefix', '^', 66 '--save-dev', 67 'install', 'underscore@1.3.1' 68 ], 69 EXEC_OPTS 70 ).then(([code]) => { 71 t.equal(code, 0, 'npm install exited with code 0') 72 73 var p = path.join(pkg, 'node_modules', 'underscore', 'package.json') 74 t.ok(JSON.parse(fs.readFileSync(p))) 75 76 var pkgJson = JSON.parse(fs.readFileSync( 77 path.join(pkg, 'package.json'), 78 'utf8' 79 )) 80 t.deepEqual( 81 pkgJson.devDependencies, 82 { 'underscore': '^1.3.1' }, 83 'got expected save prefix and version of 1.3.1' 84 ) 85 t.end() 86 })) 87}) 88 89test('install --save with \'~\' save prefix should accept patch updates', function (t) { 90 t.plan(2) 91 t.test('setup', setup) 92 t.test('run test', t => common.npm( 93 [ 94 '--registry', common.registry, 95 '--loglevel', 'silent', 96 '--save-prefix', '~', 97 '--save', 98 'install', 'underscore@1.3.1' 99 ], 100 EXEC_OPTS 101 ).then(([code]) => { 102 t.equal(code, 0, 'npm install exited with code 0') 103 104 var p = path.join(pkg, 'node_modules', 'underscore', 'package.json') 105 t.ok(JSON.parse(fs.readFileSync(p))) 106 107 var pkgJson = JSON.parse(fs.readFileSync( 108 path.join(pkg, 'package.json'), 109 'utf8' 110 )) 111 t.deepEqual( 112 pkgJson.dependencies, 113 { 'underscore': '~1.3.1' }, 114 'got expected save prefix and version of 1.3.1' 115 ) 116 })) 117}) 118 119test('install --save-dev with \'~\' save prefix should accept patch updates', function (t) { 120 t.plan(2) 121 t.test('setup', setup) 122 t.test('run test', t => common.npm( 123 [ 124 '--registry', common.registry, 125 '--loglevel', 'silent', 126 '--save-prefix', '~', 127 '--save-dev', 128 'install', 'underscore@1.3.1' 129 ], 130 EXEC_OPTS 131 ).then(([code]) => { 132 t.notOk(code, 'npm install exited with code 0') 133 134 var p = path.join(pkg, 'node_modules', 'underscore', 'package.json') 135 t.ok(JSON.parse(fs.readFileSync(p))) 136 137 var pkgJson = JSON.parse(fs.readFileSync( 138 path.join(pkg, 'package.json'), 139 'utf8' 140 )) 141 t.deepEqual( 142 pkgJson.devDependencies, 143 { 'underscore': '~1.3.1' }, 144 'got expected save prefix and version of 1.3.1' 145 ) 146 })) 147}) 148 149function setup (t) { 150 t.test('destroy', t => { 151 t.plan(2) 152 rimraf(path.resolve(pkg, 'node_modules'), () => t.pass('node_modules')) 153 rimraf(path.resolve(pkg, 'pacakage-lock.json'), () => t.pass('lock file')) 154 }) 155 t.test('create', t => { 156 fs.writeFileSync( 157 path.join(pkg, 'package.json'), 158 JSON.stringify(json, null, 2) 159 ) 160 t.end() 161 }) 162 163 t.end() 164} 165