forked from jonschlinkert/npm-install-global
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
151 lines (129 loc) · 4.29 KB
/
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
144
145
146
147
148
149
150
151
'use strict';
require('mocha');
var fs = require('fs');
var path = require('path');
var gm = require('global-modules');
var assert = require('assert');
var npm = require('./');
function isInstalled(name) {
return fs.existsSync(path.resolve(gm, name));
}
describe('npm-install-global', function() {
this.timeout(20000);
before(function(cb) {
if (!isInstalled('mocha')) {
npm.install('mocha', cb);
} else {
cb();
}
});
describe('api', function() {
it('should export a function', function() {
assert.equal(typeof npm, 'function');
});
it('should expose an `install` method', function() {
assert(npm);
assert.equal(typeof npm.install, 'function');
});
it('should expose an `uninstall` method', function() {
assert(npm);
assert.equal(typeof npm.uninstall, 'function');
});
it('should throw an error when invalid args are passed', function(cb) {
try {
npm();
cb(new Error('expected an error'));
} catch (err) {
assert(err);
assert.equal(err.message, 'expected callback to be a function');
cb();
}
});
});
describe('.install', function() {
it('should install a global npm package', function(cb) {
npm.install('generate-foo', function(err) {
if (err) return cb(err);
assert(isInstalled('generate-foo'));
npm.uninstall('generate-foo', cb);
});
});
it('should install an array of global npm packages', function(cb) {
npm.install(['generate-foo', 'generate-bar'], function(err) {
if (err) return cb(err);
assert(isInstalled('generate-foo'));
assert(isInstalled('generate-bar'));
cb();
});
});
});
describe('.uninstall', function() {
it('should uninstall a global npm package', function(cb) {
npm.uninstall('generate-foo', function(err) {
if (err) return cb(err);
assert(!isInstalled('generate-foo'));
cb();
});
});
it('should uninstall a global npm package', function(cb) {
npm.uninstall(['generate-foo', 'generate-bar'], function(err) {
if (err) return cb(err);
assert(!isInstalled('generate-foo'));
assert(!isInstalled('generate-bar'));
cb();
});
});
});
describe('.maybeInstall', function() {
it('should not install a package that already exists', function(cb) {
npm.install('generate-foo', function(err) {
if (err) return cb(err);
npm.maybeInstall('generate-foo', function(err, names) {
if (err) return cb(err);
assert.equal(names.length, 0);
assert(isInstalled('generate-foo'));
cb();
});
});
});
it('should not install an array of packages that already exist', function(cb) {
npm.install(['generate-foo', 'generate-bar'], function(err) {
if (err) return cb(err);
npm.maybeInstall(['generate-foo', 'generate-bar'], function(err, names) {
if (err) return cb(err);
assert.equal(names.length, 0);
assert(isInstalled('generate-foo'));
assert(isInstalled('generate-bar'));
npm.uninstall(['generate-foo', 'generate-bar'], cb);
});
});
});
it('should install a package that does not exist', function(cb) {
npm.maybeInstall(['generate-foo', 'generate-bar'], function(err, names) {
if (err) return cb(err);
assert.equal(names.length, 2);
assert(isInstalled('generate-foo'));
assert(isInstalled('generate-bar'));
cb();
});
});
it('should only install packages in an array if they do not exist', function(cb) {
npm.uninstall(['generate-foo', 'generate-bar'], function(err) {
if (err) return cb(err);
npm.install(['generate-foo'], function(err) {
if (err) return cb(err);
assert(isInstalled('generate-foo'));
assert(!isInstalled('generate-bar'));
npm.maybeInstall(['generate-foo', 'generate-bar'], function(err, names) {
if (err) return cb(err);
assert.equal(names.length, 1);
assert.equal(names[0], 'generate-bar');
assert(isInstalled('generate-foo'));
assert(isInstalled('generate-bar'));
cb();
});
});
});
});
});
});