1/* eslint-disable standard/no-callback-literal */ 2var test = require('tap').test 3var requireInject = require('require-inject') 4 5var npm = require('../../lib/npm.js') 6 7test('npm init <name>', function (t) { 8 var initJsonMock = function () { 9 t.ok(false, 'should not run initJson()') 10 } 11 initJsonMock.yes = function () { 12 t.ok(false, 'should not run initJson.yes()') 13 return false 14 } 15 var libnpxMock = function () { 16 return Promise.resolve() 17 } 18 libnpxMock.parseArgs = function (argv, defaultNpm) { 19 t.ok(argv[0].includes('node'), 'node is the first arg') 20 t.equals(argv[2], '--always-spawn', 'set npx opts.alwaysSpawn') 21 t.equals(argv[3], 'create-name', 'expands name') 22 t.ok(defaultNpm.endsWith('npm-cli.js'), 'passes npm bin path') 23 } 24 25 npm.load({ loglevel: 'silent' }, function () { 26 var init = requireInject('../../lib/init', { 27 'init-package-json': initJsonMock, 28 'libnpx': libnpxMock 29 }) 30 31 init(['name'], function () {}) 32 33 t.end() 34 }) 35}) 36 37test('npm init expands scopes', function (t) { 38 var libnpxMock = function () { 39 return Promise.resolve() 40 } 41 42 npm.load({ loglevel: 'silent' }, function () { 43 var init = requireInject('../../lib/init', { 44 'libnpx': libnpxMock 45 }) 46 47 libnpxMock.parseArgs = function (argv) { 48 t.equals(argv[3], '@scope/create', 'expands @scope') 49 } 50 51 init(['@scope'], function () {}) 52 53 libnpxMock.parseArgs = function (argv) { 54 t.equals(argv[3], '@scope/create-name', 'expands @scope/name') 55 } 56 57 init(['@scope/name'], function () {}) 58 59 t.end() 60 }) 61}) 62 63test('npm init expands version names', function (t) { 64 var libnpxMock = function () { 65 return Promise.resolve() 66 } 67 68 npm.load({ loglevel: 'silent' }, function () { 69 var init = requireInject('../../lib/init', { 70 'libnpx': libnpxMock 71 }) 72 73 libnpxMock.parseArgs = function (argv) { 74 t.equals(argv[3], 'create-name@1.2.3', 'expands name@1.2.3') 75 } 76 77 init(['name@1.2.3'], function () {}) 78 79 libnpxMock.parseArgs = function (argv) { 80 t.equals(argv[3], 'create-name@^1.2.3', 'expands name@^1.2.3') 81 } 82 83 init(['name@^1.2.3'], function () {}) 84 85 t.end() 86 }) 87}) 88 89test('npm init expands git names', function (t) { 90 var libnpxMock = function () { 91 return Promise.resolve() 92 } 93 94 npm.load({ loglevel: 'silent' }, function () { 95 var init = requireInject('../../lib/init', { 96 'libnpx': libnpxMock 97 }) 98 99 libnpxMock.parseArgs = function (argv) { 100 t.equals(argv[3], 'user/create-foo', 'expands git repo') 101 } 102 103 init(['user/foo'], function () {}) 104 105 libnpxMock.parseArgs = function (argv) { 106 t.equals(argv[3], 'git+https://github.com/user/create-foo', 'expands git url') 107 } 108 109 init(['git+https://github.com/user/foo'], function () {}) 110 111 t.end() 112 }) 113}) 114 115test('npm init errors on folder and tarballs', function (t) { 116 npm.load({ loglevel: 'silent' }, function () { 117 var init = require('../../lib/init') 118 119 try { 120 init(['../foo/bar/'], function () {}) 121 } catch (e) { 122 t.equals(e.code, 'EUNSUPPORTED') 123 } 124 125 t.throws( 126 () => init(['../foo/bar/'], function () {}), 127 /Unrecognized initializer: \.\.\/foo\/bar\// 128 ) 129 130 t.throws( 131 () => init(['file:foo.tar.gz'], function () {}), 132 /Unrecognized initializer: file:foo\.tar\.gz/ 133 ) 134 135 t.throws( 136 () => init(['http://x.com/foo.tgz'], function () {}), 137 /Unrecognized initializer: http:\/\/x\.com\/foo\.tgz/ 138 ) 139 140 t.end() 141 }) 142}) 143 144test('npm init forwards arguments', function (t) { 145 var libnpxMock = function () { 146 return Promise.resolve() 147 } 148 149 npm.load({ loglevel: 'silent' }, function () { 150 var origArgv = process.argv 151 var init = requireInject('../../lib/init', { 152 'libnpx': libnpxMock 153 }) 154 155 libnpxMock.parseArgs = function (argv) { 156 process.argv = origArgv 157 t.same(argv.slice(4), ['a', 'b', 'c']) 158 } 159 process.argv = [ 160 process.argv0, 161 'NPM_CLI_PATH', 162 'init', 163 'name', 164 'a', 'b', 'c' 165 ] 166 167 init(['name'], function () {}) 168 169 t.end() 170 }) 171}) 172