1'use strict' 2 3const BB = require('bluebird') 4 5const common = require('../common-tap') 6const fs = require('fs') 7const mr = BB.promisify(require('npm-registry-mock')) 8const path = require('path') 9const test = require('tap').test 10 11const testDir = common.pkg 12 13function setup () { 14 fs.writeFileSync( 15 path.join(testDir, 'package.json'), 16 JSON.stringify({ 17 name: 'publish-organized', 18 version: '1.2.5' 19 }, null, 2), 20 'utf8' 21 ) 22 23 fs.writeFileSync( 24 path.join(testDir, 'index.js'), 25 'hello', 26 'utf8' 27 ) 28} 29 30let port = common.port 31function withServer (cb) { 32 return mr({port: port++, throwOnUnmatched: true}) 33 .tap(cb) 34 .then((server) => { 35 server.done() 36 return server.close() 37 }) 38} 39 40test('basic npm publish', (t) => { 41 setup() 42 return withServer((server) => { 43 server.filteringRequestBody(verify) 44 .put('/publish-organized', true) 45 .reply(201, {ok: true}) 46 47 return common.npm([ 48 'publish', 49 '--no-color', 50 '--cache', common.cache, 51 '--registry=' + common.registry.replace(common.port, server.port), 52 `--//localhost:${server.port}/:username=username`, 53 `--//localhost:${server.port}/:_password=` + Buffer.from('password').toString('base64'), 54 `--//localhost:${server.port}/:email=` + 'ogd@aoaioxxysz.net' 55 ], { 56 'cwd': testDir, 57 nodeExecPath: process.execPath 58 }) 59 .spread((code, stdout, stderr) => { 60 t.comment(stdout) 61 t.comment(stderr) 62 t.is(code, 0, 'published without error') 63 }) 64 65 function verify (body) { 66 t.doesNotThrow(() => { 67 const parsed = JSON.parse(body) 68 const current = parsed.versions['1.2.5'] 69 t.equal( 70 current._npmVersion, 71 require(path.resolve(__dirname, '../../package.json')).version, 72 'npm version is correct' 73 ) 74 75 t.equal( 76 current._nodeVersion, 77 process.versions.node, 78 'node version is correct' 79 ) 80 }, 'converted body back into object') 81 82 return true 83 } 84 }) 85}) 86 87test('npm publish --dry-run', (t) => { 88 setup() 89 return common.npm([ 90 'publish', 91 '--dry-run', 92 '--registry=https://example.registry/fake', 93 '--cache', common.cache, 94 '--loglevel=notice', 95 '--no-color' 96 ], {'cwd': testDir}) 97 .spread((code, stdout, stderr) => { 98 t.comment(stdout) 99 t.comment(stderr) 100 t.is(code, 0, 'published without error') 101 t.match(stderr, /notice\s+\d+[a-z]+\s+package\.json/gi, 'mentions package.json') 102 t.match(stderr, /notice\s+\d+[a-z]+\s+index\.js/gi, 'mentions index.js') 103 }) 104}) 105 106test('npm publish --json', (t) => { 107 setup() 108 return withServer((server) => { 109 server.filteringRequestBody(() => true) 110 .put('/publish-organized', true) 111 .reply(201, {ok: true}) 112 return common.npm([ 113 'publish', 114 '--json', 115 '--registry', common.registry.replace(common.port, server.port), 116 '--cache', common.cache 117 ], {'cwd': testDir}) 118 .spread((code, stdout, stderr) => { 119 t.comment(stdout) 120 t.comment(stderr) 121 t.is(code, 0, 'published without error') 122 t.similar(JSON.parse(stdout), { 123 name: 'publish-organized', 124 version: '1.2.5', 125 files: [ 126 {path: 'index.js'}, 127 {path: 'package.json'} 128 ], 129 entryCount: 2 130 }, 'JSON output reflects package contents') 131 t.equal(stderr.trim(), '', 'nothing on stderr') 132 }) 133 }) 134}) 135 136test('npm publish --dry-run --json', (t) => { 137 setup() 138 return common.npm([ 139 'publish', 140 '--dry-run', 141 '--json', 142 '--registry=https://example.registry/fake', 143 '--cache', common.cache, 144 '--loglevel=notice', 145 '--no-color' 146 ], {'cwd': testDir}) 147 .spread((code, stdout, stderr) => { 148 t.comment(stdout) 149 t.comment(stderr) 150 t.is(code, 0, 'published without error') 151 t.similar(JSON.parse(stdout), { 152 name: 'publish-organized', 153 version: '1.2.5', 154 files: [ 155 {path: 'index.js'}, 156 {path: 'package.json'} 157 ], 158 entryCount: 2 159 }, 'JSON output reflects package contents') 160 t.equal(stderr.trim(), '', 'nothing on stderr') 161 }) 162}) 163