1// verify that prepublishOnly runs _only_ on publish 2var join = require('path').join 3 4var mr = require('npm-registry-mock') 5var test = require('tap').test 6var Tacks = require('tacks') 7var File = Tacks.File 8var Dir = Tacks.Dir 9var path = require('path') 10 11var common = require('../common-tap') 12 13var pkg = common.pkg 14var cachedir = common.cache 15var tmpdir = join(pkg, 'tmp') 16 17var env = { 18 'npm_config_cache': cachedir, 19 'npm_config_tmp': tmpdir, 20 'npm_config_global': 'false' 21} 22 23for (var i in process.env) { 24 if (!/^npm_config_/.test(i)) { 25 env[i] = process.env[i] 26 } 27} 28 29var server 30 31var fixture = new Tacks(Dir({ 32 cache: Dir(), 33 tmp: Dir(), 34 '.npmrc': File([ 35 'progress=false', 36 'registry=' + common.registry, 37 '//localhost:1337/:username=username', 38 '//localhost:1337/:_authToken=deadbeeffeed' 39 ].join('\n') + '\n'), 40 helper: Dir({ 41 'script.js': File([ 42 '#!/usr/bin/env node\n', 43 'console.log("ok")\n' 44 ].join('\n') + '\n' 45 ), 46 'package.json': File({ 47 name: 'helper', 48 version: '6.6.6', 49 bin: './script.js' 50 }) 51 }), 52 'package.json': File({ 53 name: 'npm-test-prepublish-only', 54 version: '1.2.5', 55 dependencies: { 56 'helper': 'file:./helper' 57 }, 58 scripts: { 59 build: 'helper', 60 prepublishOnly: 'node ' + path.resolve(__dirname, '../../') + ' run build' 61 } 62 }) 63})) 64 65test('setup', function (t) { 66 fixture.create(pkg) 67 mr({port: common.port, throwOnUnmatched: true}, function (err, s) { 68 t.ifError(err, 'registry mocked successfully') 69 server = s 70 common.npm( 71 [ 72 'install', 73 '--loglevel', 'error', 74 '--cache', cachedir, 75 '--tmp', tmpdir 76 ], 77 { 78 cwd: pkg, 79 env: env 80 }, 81 function (err, code, stdout, stderr) { 82 t.equal(code, 0, 'install exited OK') 83 t.ifErr(err, 'installed successfully') 84 85 t.notOk(stderr, 'got stderr data:' + JSON.stringify('' + stderr)) 86 87 t.end() 88 } 89 ) 90 }) 91}) 92 93test('test', function (t) { 94 server.filteringRequestBody(function () { return true }) 95 .put('/npm-test-prepublish-only', true) 96 .reply(201, {ok: true}) 97 98 common.npm( 99 [ 100 'publish', 101 '--loglevel', 'warn', 102 '--scripts-prepend-node-path' 103 ], 104 { 105 cwd: pkg, 106 env: env 107 }, 108 function (err, code, stdout, stderr) { 109 t.equal(code, 0, 'publish ran without error') 110 t.ifErr(err, 'published successfully') 111 112 t.notOk(stderr, 'got stderr data:' + JSON.stringify('' + stderr)) 113 var c = stdout.trim() 114 var regex = new RegExp( 115 '> npm-test-prepublish-only@1.2.5 prepublishOnly [^\\r\\n]+\\r?\\n' + 116 '> .* run build\\r?\\n' + 117 '\\r?\\n' + 118 '\\r?\\n' + 119 '> npm-test-prepublish-only@1.2.5 build [^\\r\\n]+\\r?\\n' + 120 '> helper\\r?\\n' + 121 '\\r?\\n' + 122 'ok\\r?\\n' + 123 '\\+ npm-test-prepublish-only@1.2.5', 'ig' 124 ) 125 126 t.match(c, regex) 127 t.end() 128 } 129 ) 130}) 131 132test('cleanup', function (t) { 133 server.close() 134 t.pass('cleaned up') 135 t.end() 136}) 137