summaryrefslogtreecommitdiff
path: root/deps/npm/test/tap/dedupe-git-semver.js
blob: 21e372e24b0d0e234b1b447b8bdcc9ad169decc1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
'use strict'
const fs = require('fs')
const path = require('path')
const test = require('tap').test
const requireInject = require('require-inject')
const Tacks = require('tacks')
const File = Tacks.File
const Dir = Tacks.Dir

const manifests = {
  'git-wrap': {
    name: 'git-wrap',
    version: '1.0.0',
    dependencies: {
      git: 'git+https://example.com/git#semver:1.0'
    }
  },
  git: {
    name: 'git',
    version: '1.0.0'
  }
}

const npm = requireInject.installGlobally('../../lib/npm.js', {
  pacote: {
    manifest: function (spec) {
      const manifest = manifests[spec.name]
      manifest._requested = spec
      manifest._resolved = spec.saveSpec.replace(/^file:/, '').replace(/(:?#.*)$/, '#0000000000000000000000000000000000000000')
      manifest._from = spec.rawSpec
      return Promise.resolve(manifest)
    }
  },
  '../../lib/utils/output.js': function () {
    // do not output to stdout
  }
})

const basedir = path.join(__dirname, path.basename(__filename, '.js'))
const testdir = path.join(basedir, 'testdir')
const cachedir = path.join(basedir, 'cache')
const tmpdir = path.join(basedir, 'tmp')

const cwd = process.cwd()

const conf = {
  cache: cachedir,
  tmp: tmpdir,
  loglevel: 'silent',
  'package-lock-only': true
}

const fixture = new Tacks(Dir({
  cache: Dir(),
  global: Dir(),
  tmp: Dir(),
  testdir: Dir({
    'package.json': File({
      name: 'fixture',
      version: '1.0.0',
      dependencies: {
        git: 'git+https://example.com/git#semver:1',
        'git-wrap': 'file:git-wrap-1.0.0.tgz'
      }
    }),
    // Tarball source:
    // 'git-wrap': Dir({
    //   'package.json': File({
    //     name: 'git-wrap',
    //     version: '1.0.0',
    //     dependencies: {
    //       git: 'git+https://example.com/git#semver:1.0'
    //     }
    //   })
    // }),
    'git-wrap-1.0.0.tgz': File(Buffer.from(
      '1f8b0800000000000003ed8fcd0ac23010843df729423caaf9c13642df26' +
      'b44bad9a3434f107a4efeec68aa7de2c8898ef32cb0c3bec3a5d1d7503dc' +
      '8dca0ebeb38b991142a83c27537e44ee30db164a48a994c01987a210a873' +
      '1f32c5d907dde3299ff68cbf90b7fe08f78c106ab5015a12dab46173edb5' +
      'a3ebe85ea0f76d676320996062746b70606bb0550b1ea3b84f9e9baf82d5' +
      '3e04e74bcee1a68d3b01ab3ac3d15f7a30d8586215c59d211bb26fff9e48' +
      '2412ffcc034458283d00080000',
      'hex'
    ))
  })
}))

function setup () {
  cleanup()
  fixture.create(basedir)
}

function cleanup () {
  fixture.remove(basedir)
}

test('setup', function (t) {
  setup()
  process.chdir(testdir)
  npm.load(conf, function (err) {
    if (err) throw err
    t.done()
  })
})

test('dedupe matching git semver ranges', function (t) {
  npm.commands.install(function (err) {
    if (err) throw err
    const packageLock = JSON.parse(fs.readFileSync('package-lock.json'))
    t.same(packageLock, {
      name: 'fixture',
      version: '1.0.0',
      lockfileVersion: 1,
      requires: true,
      dependencies: {
        git: {
          from: 'git+https://example.com/git#semver:1',
          version: 'git+https://example.com/git#0000000000000000000000000000000000000000'
        },
        'git-wrap': {
          version: 'file:git-wrap-1.0.0.tgz',
          requires: {
            git: 'git+https://example.com/git#semver:1'
          }
        }
      }
    })
    t.done()
  })
})

test('cleanup', function (t) {
  process.chdir(cwd)
  cleanup()
  t.done()
})