-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
181 lines (152 loc) · 5.56 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
'use strict';
process.env.GENERATE_CLI = true;
require('mocha');
var fs = require('fs');
var path = require('path');
var assert = require('assert');
var generate = require('generate');
var existsSync = require('fs-exists-sync');
var del = require('delete');
var generator = require('./');
var app;
function exists(name, cb) {
var filepath = path.resolve(__dirname, 'actual', name);
return function(err) {
if (err) return cb(err);
assert(existsSync(filepath));
del(filepath, cb);
}
}
function generic(gen) {
gen.task('default', function() {
console.log(gen.namespace, 'task >', this.name);
cb();
});
}
describe('generate-file', function() {
beforeEach(function() {
app = generate({cli: true, silent: true});
// temporary
app.generator('defaults', function() {});
app.option('renameKey', function(key, view) {
return view ? view.stem : path.basename(key, path.extname(key));
});
});
after(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-file') {
count++;
}
});
app.use(generator());
app.use(generator());
app.use(generator());
assert.equal(count, 1);
cb();
});
});
describe('generator', function() {
it('should work as a plugin', function() {
app.use(generator());
assert(app.tasks.hasOwnProperty('default'));
});
it('should create tasks with the name of each view on the `templates` collection', function() {
app.template('foo.md', {content: 'this is foo'});
app.template('bar.md', {content: 'this is bar'});
app.template('baz.md', {content: 'this is baz'});
app.use(generator());
assert(app.tasks.hasOwnProperty('foo'));
assert(app.tasks.hasOwnProperty('bar'));
assert(app.tasks.hasOwnProperty('baz'));
assert(!app.tasks.hasOwnProperty('qux'));
});
it('should create tasks with the name of each view on the given collection', function() {
app.create('pages');
app.pages('one.md', {content: 'this is one'});
app.pages('two.md', {content: 'this is two'});
app.pages('three.md', {content: 'this is three'});
app.use(generator({views: app.views.pages}));
assert(app.tasks.hasOwnProperty('one'));
assert(app.tasks.hasOwnProperty('two'));
assert(app.tasks.hasOwnProperty('three'));
assert(!app.tasks.hasOwnProperty('foo'));
});
it('should generate a file', function(cb) {
app.option('dest', path.resolve(__dirname, 'actual'));
app.template('foo.md', {content: 'this is foo'});
app.use(generator());
app.generate('foo', exists('foo.md', cb));
});
it('should generate a file with the given default task', function(cb) {
var expected = path.resolve(__dirname, 'actual/foo.md');
app.option('dest', path.resolve(__dirname, 'actual'));
app.template('foo.md', {content: 'this is foo'});
app.use(generator({default: 'foo'}));
app.generate(function(err) {
if (err) return cb(err);
assert(existsSync(expected));
del(expected, cb);
});
});
it('should generate multiple files with the given default task', function(cb) {
var foo = path.resolve(__dirname, 'actual/foo.md');
var bar = path.resolve(__dirname, 'actual/bar.md');
app.option('dest', path.resolve(__dirname, 'actual'));
app.template('foo.md', {content: 'this is foo'});
app.template('bar.md', {content: 'this is bar'});
app.use(generator({default: ['foo', 'bar']}));
app.generate(function(err) {
if (err) return cb(err);
assert(existsSync(foo));
assert(existsSync(bar));
del([foo, bar], cb);
});
});
it('should register as a generator', function() {
app.register('file', generator());
assert(app.generators.hasOwnProperty('file'));
});
it('should generate a file on a generator registered with `.generator`', function(cb) {
var expected = path.resolve(__dirname, 'actual/nested/foo.md');
app.option('dest', path.resolve(__dirname, 'actual/nested'));
app.template('foo.md', {content: 'this is foo'});
app.generator('file', generator({views: app.views.templates}));
app.generate('file:foo', function(err) {
if (err) return cb(err);
assert(existsSync(expected));
cb();
});
});
it('should generate a file on a generator registered with `.register`', function(cb) {
var expected = path.resolve(__dirname, 'actual/nested/bar.md');
app.option('dest', path.resolve(__dirname, 'actual/nested'));
app.template('bar.md', {content: 'this is bar'});
app.register('file', generator({views: app.views.templates}));
app.generate('file:bar', function(err) {
if (err) return cb(err);
assert(existsSync(expected));
cb();
});
});
it('should generate a file on a nested sub-generator', function(cb) {
var expected = path.resolve(__dirname, 'actual/nested/bar.md');
app.option('dest', path.resolve(__dirname, 'actual/nested'));
app.template('bar.md', {content: 'this is bar'});
app
.register('foo', generic)
.register('bar', generic)
.register('baz', generic)
.register('file', generator({views: app.views.templates}))
app.generate('foo.bar.baz.file:bar', function(err) {
if (err) return cb(err);
assert(existsSync(expected));
cb();
});
});
});
});