1'use strict' 2var fs = require('graceful-fs') 3var path = require('path') 4 5var mkdirp = require('mkdirp') 6var requireInject = require('require-inject') 7var test = require('tap').test 8 9var common = require('../common-tap.js') 10 11var pkg = common.pkg 12 13var json = { 14 name: 'gist-short-shortcut', 15 version: '0.0.0' 16} 17 18test('setup', function (t) { 19 setup() 20 t.end() 21}) 22 23test('gist-shortcut', function (t) { 24 var cloneUrls = [ 25 ['git://gist.github.com/deadbeef.git', 'GitHub gist shortcuts try git URLs first'], 26 ['https://gist.github.com/deadbeef.git', 'GitHub gist shortcuts try HTTPS URLs second'], 27 ['ssh://git@gist.github.com/deadbeef.git', 'GitHub gist shortcuts try SSH third'] 28 ] 29 var npm = requireInject.installGlobally('../../lib/npm.js', { 30 'child_process': { 31 'execFile': function (cmd, args, options, cb) { 32 process.nextTick(function () { 33 if (args.indexOf('clone') === -1) return cb(null, '', '') 34 var cloneUrl = cloneUrls.shift() 35 if (cloneUrl) { 36 t.is(args[args.length - 2], cloneUrl[0], cloneUrl[1]) 37 } else { 38 t.fail('too many attempts to clone') 39 } 40 cb(new Error('execFile mock fails on purpose')) 41 }) 42 } 43 } 44 }) 45 46 var opts = { 47 cache: common.cache, 48 prefix: pkg, 49 registry: common.registry, 50 loglevel: 'silent' 51 } 52 npm.load(opts, function (er) { 53 t.ifError(er, 'npm loaded without error') 54 npm.commands.install(['gist:deadbeef'], function (er, result) { 55 t.ok(er, 'mocked install failed as expected') 56 t.end() 57 }) 58 }) 59}) 60 61function setup () { 62 mkdirp.sync(pkg) 63 fs.writeFileSync( 64 path.join(pkg, 'package.json'), 65 JSON.stringify(json, null, 2) 66 ) 67 process.chdir(pkg) 68} 69