-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
test.js
213 lines (186 loc) · 5.67 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
'use strict';
require('mocha');
var fs = require('fs');
var path = require('path');
var assert = require('assert');
var generate = require('generate');
var npm = require('npm-install-global');
var gm = require('global-modules');
var del = require('delete');
var pkg = require('./package');
var generator = require('./');
var app;
var actual = path.resolve.bind(path, __dirname, 'actual');
function hasValue(str, val) {
return str.indexOf(val) !== -1;
}
function hasValues(str, arr) {
arr = Array.isArray(arr) ? arr : [arr];
var len = arr.length;
var idx = -1;
while (++idx < len) {
if (!hasValue(str, arr[idx])) {
return false;
}
}
return true;
}
function exists(name, fn, cb) {
if (arguments.length === 2) {
cb = fn;
fn = function() {
return true;
};
}
return function(err) {
if (err) return cb(err);
var filepath = actual(name);
fs.stat(filepath, function(err, stat) {
if (err) return cb(err);
assert(stat);
assert(fn(fs.readFileSync(filepath, 'utf8')));
del(actual(), cb);
});
};
}
function symlink(dir, cb) {
var src = path.resolve(dir);
var name = path.basename(src);
var dest = path.resolve(gm, name);
fs.stat(dest, function(err, stat) {
if (err) {
fs.symlink(src, dest, cb);
} else {
cb();
}
});
}
describe('generate-readme', function() {
if (!process.env.CI && !process.env.TRAVIS) {
before(function(cb) {
npm.maybeInstall('generate', cb);
});
}
beforeEach(function() {
app = generate({silent: true});
app.cwd = actual();
app.option('dest', actual());
app.option('askWhen', 'not-answered');
// provide template data to avoid prompts
app.data(pkg);
app.data({
author: {
name: 'Jon Schlinkert',
username: 'jonschlnkert',
url: 'https://github.com/jonschlinkert'
}
});
});
afterEach(function(cb) {
del(actual(), cb);
});
describe('plugin', function() {
it('should only register the plugin once', function(cb) {
var count = 0;
app.on('plugin', function(name) {
if (name === 'generate-readme') {
count++;
}
});
app.use(generator);
app.use(generator);
app.use(generator);
assert.equal(count, 1);
cb();
});
it('should extend tasks onto the instance', function() {
app.use(generator);
assert(app.tasks.hasOwnProperty('default'));
assert(app.tasks.hasOwnProperty('readme'));
});
it('should run the `default` task with .build', function(cb) {
app.use(generator);
app.build('default', exists('README.md', function(str) {
return hasValues(str, [pkg.name, pkg.repository]);
}, cb));
});
it('should run the `default` task with .generate', function(cb) {
app.use(generator);
app.generate('default', exists('README.md', function(str) {
return hasValues(str, [pkg.name, pkg.repository]);
}, cb));
});
});
if (!process.env.CI && !process.env.TRAVIS) {
describe('generator (CLI)', function() {
before(function(cb) {
symlink(__dirname, cb);
});
it('should run the default task using the `generate-readme` name', function(cb) {
app.use(generator);
app.generate('generate-readme', exists('README.md', function(str) {
return hasValues(str, [pkg.name, pkg.repository]);
}, cb));
});
it('should run the default task using the `readme` generator alias', function(cb) {
app.use(generator);
app.generate('readme', exists('README.md', function(str) {
return hasValues(str, [pkg.name, pkg.repository]);
}, cb));
});
});
}
describe('generator (API)', function() {
it('should run the default task on the generator', function(cb) {
app.register('readme', generator);
app.generate('readme', exists('README.md', cb));
});
it('should run the `readme` task', function(cb) {
app.register('readme', generator);
app.generate('readme:readme', exists('README.md', cb));
});
it('should run the `default` task when defined explicitly', function(cb) {
app.register('readme', generator);
app.generate('readme:default', exists('README.md', cb));
});
});
describe('sub-generator', function() {
it('should work as a sub-generator', function(cb) {
app.register('foo', function(foo) {
foo.register('readme', generator);
});
app.generate('foo.readme', exists('README.md', cb));
});
it('should run the `generator:default` task when defined explicitly', function(cb) {
app.register('foo', function(foo) {
foo.register('readme', generator);
});
app.generate('foo.readme:default', exists('README.md', cb));
});
it('should run the `generator:node` task', function(cb) {
app.register('foo', function(foo) {
foo.register('readme', generator);
});
app.generate('foo.readme:node', exists('README.md', cb));
});
it('should run the `generator:minimal` task', function(cb) {
app.register('foo', function(foo) {
foo.register('readme', generator);
});
app.generate('foo.readme:minimal', exists('README.md', cb));
});
it('should run the `generator:readme` task', function(cb) {
app.register('foo', function(foo) {
foo.register('readme', generator);
});
app.generate('foo.readme:readme', exists('README.md', cb));
});
it('should work with nested sub-generators', function(cb) {
app
.register('foo', generator)
.register('bar', generator)
.register('baz', generator);
app.generate('foo.bar.baz', exists('README.md', cb));
});
});
});