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