• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict'
2var test = require('tap').test
3var npm = require('../../lib/npm')
4
5test('setup', function (t) {
6  npm.load({}, t.done)
7})
8
9test('replaceModuleByName', function (t) {
10  var replaceModuleByName = require('../../lib/install/deps')._replaceModuleByName
11  var mods = []
12  for (var ii = 0; ii < 10; ++ii) {
13    mods.push({package: {name: String(ii)}, path: '/path/to/' + ii})
14  }
15
16  var test = {}
17  test.A = mods.slice(0, 4)
18  replaceModuleByName(test, 'A', mods[2])
19  t.isDeeply(test.A, mods.slice(0, 4), 'replacing an existing module leaves the order alone')
20  replaceModuleByName(test, 'A', mods[7])
21  t.isDeeply(test.A, mods.slice(0, 4).concat(mods[7]), 'replacing a new module appends')
22
23  test.B = mods.slice(0, 4)
24  var replacement = {package: {name: '1'}, isReplacement: true}
25  replaceModuleByName(test, 'B', replacement)
26  t.isDeeply(test.B, [mods[0], replacement, mods[2], mods[3]], 'replacing existing module swaps out for the new version')
27
28  replaceModuleByName(test, 'C', mods[7])
29  t.isDeeply(test.C, [mods[7]], 'replacing when the key does not exist yet, causes its creation')
30
31  test.D = mods.slice(0, 4)
32  var duplicateByPath = {package: {name: 'dup'}, path: test.D[0].path}
33  replaceModuleByName(test, 'D', duplicateByPath)
34  t.isDeeply(test.D, mods.slice(0, 4).concat(duplicateByPath), 'replacing with a duplicate path but diff names appends')
35  t.end()
36})
37
38test('replaceModuleByPath', function (t) {
39  var replaceModuleByPath = require('../../lib/install/deps')._replaceModuleByPath
40  var mods = []
41  for (var ii = 0; ii < 10; ++ii) {
42    mods.push({package: {name: String(ii)}, path: '/path/to/' + ii})
43  }
44
45  var test = {}
46  test.A = mods.slice(0, 4)
47  replaceModuleByPath(test, 'A', mods[2])
48  t.isDeeply(test.A, mods.slice(0, 4), 'replacing an existing module leaves the order alone')
49  replaceModuleByPath(test, 'A', mods[7])
50  t.isDeeply(test.A, mods.slice(0, 4).concat(mods[7]), 'replacing a new module appends')
51
52  test.B = mods.slice(0, 4)
53  var replacement = {package: {name: '1'}, isReplacement: true, path: '/path/to/1'}
54  replaceModuleByPath(test, 'B', replacement)
55  t.isDeeply(test.B, [mods[0], replacement, mods[2], mods[3]], 'replacing existing module swaps out for the new version')
56
57  replaceModuleByPath(test, 'C', mods[7])
58  t.isDeeply(test.C, [mods[7]], 'replacing when the key does not exist yet, causes its creation')
59
60  test.D = mods.slice(0, 4)
61  var duplicateByPath = {package: {name: 'dup'}, path: test.D[0].path}
62  replaceModuleByPath(test, 'D', duplicateByPath)
63  t.isDeeply(test.D, [duplicateByPath].concat(mods.slice(1, 4)), 'replacing with a duplicate path but diff names replaces')
64  t.end()
65})
66