/
index.js
125 lines (111 loc) · 4.42 KB
/
index.js
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
'use strict';
/*jshint asi: true */
var debug// = true;
var test = debug ? function () {} : require('tap').test
var test_ = !debug ? function () {} : require('tap').test
var browserify = require('browserify')
, vm = require('vm')
, path = require('path')
, entry = require.resolve('./fixtures/')
, dedupe = require('../')
function inspect(obj, depth) {
console.log(require('util').inspect(obj, false, depth || 5, true));
}
function setupVm () {
var buf = [];
var ctx = { console: { log: function (s, arg) { buf.push(s + (arg ? ' ' + arg : '')) } } };
return { ctx: ctx, buf: buf };
}
function check (t, criteria, output, dedupes) {
var bfy = browserify();
var setup = setupVm();
var deduped = [];
dedupe(bfy, criteria)
.require(entry, { entry: true })
.on('deduping', function (d) { deduped.push(d.split(path.sep).slice(-4).join(path.sep)) })
.bundle(function (err, src) {
if (err) return t.fail(err);
vm.runInNewContext(src, setup.ctx);
t.deepEqual(setup.buf, output, 'loads correct versions')
t.equal(deduped.length, dedupes.length, 'correct number of dedupes')
deduped.forEach(function (d) { t.ok(~dedupes.indexOf(d), 'correct dedupe') })
/*inspect(setup.buf);
inspect(deduped);*/
t.end()
});
}
test('\nexact - loads all ( 0.1.0, 0.1.5, 0.2.0, 1.0.0 ) common versions and dedupes one that is found twice with exact same version', function (t) {
check(t
, 'patch'
, [ 'loading common 0.1.0',
'loading common 0.1.5',
'loading common 0.2.0',
'loading common 1.0.0',
'depends-0.1.0 - common common-0.1.0',
'also-depends-0.1.0 - common common-0.1.0',
'depends-0.1.5 - common dep - common-0.1.5',
'depends-0.2.0 - common common-0.2.0',
'depends-1.0.0 - common common-1.0.0' ]
, [ 'also-depends-0.1.0/node_modules/common/common.js' ]
)
})
test('\npatch- loads all ( 0.1.0, 0.1.5, 0.2.0, 1.0.0 ) common versions and dedupes one that is found twice with exact same version', function (t) {
check(t
, 'patch'
, [ 'loading common 0.1.0',
'loading common 0.1.5',
'loading common 0.2.0',
'loading common 1.0.0',
'depends-0.1.0 - common common-0.1.0',
'also-depends-0.1.0 - common common-0.1.0',
'depends-0.1.5 - common dep - common-0.1.5',
'depends-0.2.0 - common common-0.2.0',
'depends-1.0.0 - common common-1.0.0' ]
, [ 'also-depends-0.1.0/node_modules/common/common.js' ]
)
})
test('\nminor - loads 0.1.5, 0.2.0, 1.0.0 - not 0.1.0 and dedupes 0.1.0 wherever it was referenced with 0.1.5', function (t) {
check(t
, 'minor'
, [ 'loading common 0.1.5',
'loading common 0.2.0',
'loading common 1.0.0',
'depends-0.1.0 - common dep - common-0.1.5',
'also-depends-0.1.0 - common dep - common-0.1.5',
'depends-0.1.5 - common dep - common-0.1.5',
'depends-0.2.0 - common common-0.2.0',
'depends-1.0.0 - common common-1.0.0' ]
, [ 'depends-0.1.0/node_modules/common/common.js',
'also-depends-0.1.0/node_modules/common/common.js' ]
)
})
test('\nmajor - loads 0.2.0, 1.0.0 - not 0.1.0, 0.1.5 and dedupes 0.1.0 and 0.1.5 wherever referenced with 0.2.0 - does not dedupe dep.js', function (t) {
check(t
, 'major'
, [ 'loading common 0.2.0',
'loading common 1.0.0',
'depends-0.1.0 - common common-0.2.0',
'also-depends-0.1.0 - common common-0.2.0',
'depends-0.1.5 - common common-0.2.0',
'depends-0.2.0 - common common-0.2.0',
'depends-1.0.0 - common common-1.0.0' ]
, [ 'depends-0.1.0/node_modules/common/common.js',
'also-depends-0.1.0/node_modules/common/common.js',
'depends-0.1.5/node_modules/common/common.js' ]
)
})
test('\nany - loads 1.0.0 only and dedupes 0.1.0, 0.1.5, 0.2.0 wherever referenced with 1.0.0 - does not dedupe dep.js', function (t) {
check(t
, 'any'
, [ 'loading common 1.0.0',
'depends-0.1.0 - common common-1.0.0',
'also-depends-0.1.0 - common common-1.0.0',
'depends-0.1.5 - common common-1.0.0',
'depends-0.2.0 - common common-1.0.0',
'depends-1.0.0 - common common-1.0.0' ]
, [ 'depends-0.1.0/node_modules/common/common.js',
'depends-0.2.0/node_modules/common/common.js',
'also-depends-0.1.0/node_modules/common/common.js',
'depends-0.1.5/node_modules/common/common.js' ]
)
})