• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict'
2
3const BB = require('bluebird')
4
5var fs = require('graceful-fs')
6var path = require('path')
7
8var mkdirp = require('mkdirp')
9var requireInject = require('require-inject')
10var test = require('tap').test
11
12var common = require('../common-tap.js')
13
14var pkg = common.pkg
15
16var json = {
17  name: 'bitbucket-shortcut',
18  version: '0.0.0'
19}
20
21test('setup', function (t) {
22  setup()
23  t.end()
24})
25
26test('bitbucket-shortcut', function (t) {
27  var cloneUrls = [
28    ['https://bitbucket.org/foo/private.git', 'Bitbucket shortcuts try HTTPS URLs first'],
29    ['ssh://git@bitbucket.org/foo/private.git', 'Bitbucket shortcuts try SSH second']
30  ]
31
32  var npm = requireInject.installGlobally('../../lib/npm.js', {
33    'pacote/lib/util/git': {
34      'revs': (repo, opts) => {
35        return BB.resolve().then(() => {
36          var cloneUrl = cloneUrls.shift()
37          if (cloneUrl) {
38            t.is(repo, cloneUrl[0], cloneUrl[1])
39          } else {
40            t.fail('too many attempts to clone')
41          }
42          throw new Error('git.revs mock fails on purpose')
43        })
44      }
45    }
46  })
47
48  var opts = {
49    cache: common.cache,
50    prefix: pkg,
51    registry: common.registry,
52    loglevel: 'silent'
53  }
54  npm.load(opts, function (er) {
55    t.ifError(er, 'npm loaded without error')
56    npm.commands.install(['bitbucket:foo/private'], function (err) {
57      t.match(err.message, /mock fails on purpose/, 'mocked install failed as expected')
58      t.end()
59    })
60  })
61})
62
63function setup () {
64  mkdirp.sync(pkg)
65  fs.writeFileSync(
66    path.join(pkg, 'package.json'),
67    JSON.stringify(json, null, 2)
68  )
69  process.chdir(pkg)
70}
71