-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.test.js
143 lines (120 loc) · 3.27 KB
/
index.test.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
var test = require('tap').test;
var proxyquire = require('proxyquire');
var Promise = require('es6-promise').Promise; // jshint ignore:line
var path = require('path');
var False = function () { return false; };
var True = function () { return true; };
var suceed = False;
var fs = {
statSync: function (filename) {
return {
isFile: function () {
return suceed(filename);
},
isFIFO: function () {
return suceed(filename);
},
};
},
stat: function (filename) {
if (!suceed(filename)) {
return Promise.reject(new Error('not a file'));
}
return Promise.resolve({
isFile: function () {
return true;
}
});
}
};
var resolve = proxyquire('../lib/index', {
'promise-fs': fs,
});
test('resolve immediately', function (t) {
t.plan(2);
suceed = True;
resolve('foo').then(function () {
t.pass('foo found');
}).catch(function (e) {
t.fail(e.stack);
});
t.ok(resolve.sync('foo'), 'sync should find immediately');
});
test('resolve never', function (t) {
t.plan(2);
suceed = False;
resolve('foo').then(function () {
t.fail('should result in NO_PACKAGE_FOUND');
}).catch(function (e) {
if (e.code === 'NO_PACKAGE_FOUND') {
t.pass('not found');
} else {
t.fail(e.stack);
}
});
t.throws(function () {
resolve.sync('foo');
}, /package not found foo/, 'sync should throw saying not found');
});
test('resolve if not found', function (t) {
var statSync = fs.statSync;
t.plan(3);
t.test('setup', function (t) {
fs.statSync = function () {
var e = new Error('not found');
e.code = 'ENOENT';
throw e;
};
t.pass('setup complete');
t.end();
});
t.test('resolve if not found (test)', function (t) {
t.plan(1);
t.throws(function () {
resolve.sync('foo');
}, /package not found foo/, 'sync should throw saying not found');
});
t.test('teardown', function (t) {
fs.statSync = statSync;
t.pass('teardown complete');
t.end();
});
});
test('resolve at root', function (t) {
t.plan(2);
var target = '/node_modules/foo';
var base = __dirname + '/fixtures/bar';
suceed = function (filename) {
// console.log('%s ===? %s', filename, filename === target + '/package.json');
return filename === target + '/package.json';
};
resolve('foo', base).then(function (dir) {
t.equal(dir, target);
}).catch(function (e) {
if (e.code === 'NO_PACKAGE_FOUND') {
t.fail('not found at root');
} else {
t.fail(e.stack, 'unknown error on search at root');
}
});
t.equal(resolve.sync('foo', base), target, 'found module at root');
});
test('resolve works with scoped packages', function (t) {
t.plan(2);
var target = __dirname + '/node_modules/@remy/foo/package.json';
suceed = function (filename) {
return filename === target;
};
var base = __dirname + '/node_modules/@remy/foo/node_modules/semver/node_modules/bar';
resolve('@remy/foo', base).then(function (dir) {
t.equal(dir, path.dirname(target));
}).catch(function (e) {
console.log(e.stack);
if (e.code === 'NO_PACKAGE_FOUND') {
t.fail('not found');
} else {
t.fail(e.stack);
}
});
t.equal(resolve.sync('@remy/foo', base), path.dirname(target), 'sync scoped matches');
});