UNPKG

10.6 kBJavaScriptView Raw
1var test = require('tape');
2var modul = require('../');
3var gulp;
4
5var exec = require('child_process').exec;
6var fs = require('fs');
7var path = require('path');
8var stream = require('stream');
9
10process.chdir('./test');
11exec('mkdir test && mkdir dist && mkdir build');
12
13function checkFile(t, file, msg) {
14 try {
15 t.ok(fs.statSync(file).isFile(), msg);
16 } catch (ex) {
17 t.error(ex, msg);
18 }
19}
20
21function checkImmutable(t, ob, field, alias) {
22 var remember = ob[field];
23 ob[field] = 'something else';
24 t.equal(remember, ob[field], alias + '.' + field + ' should be immutable');
25}
26
27test('gulp compliance test', function (t) {
28 t.plan(8);
29
30 t.equal(typeof modul, "function", 'plugin should be a function');
31 if (typeof gulp === 'undefined') {
32 gulp = modul();
33 }
34
35 t.equal(typeof gulp.task, "function", 'gulp.task should be a function');
36 t.equal(typeof gulp.src, "function", 'gulp.src should be a function');
37 t.equal(typeof gulp.src('./**').on, 'function', 'gulp.src(\'./**\') should return a readable stream');
38 t.equal(typeof gulp.dest, "function", 'gulp.dest should be a function');
39 t.equal(typeof gulp.dest('./').on, 'function', 'gulp.dest(\'./\') should return a writable stream');
40 t.equal(typeof gulp.watch, "function", 'gulp.watch should be a function');
41
42 exec('mocha --reporter spec gulp-test/test', function(err, stdout, stderr) {
43 t.error(err, 'the original gulp tests should not return any errors');
44 });
45});
46
47test('gulp.src()', function (t) {
48 t.plan(1);
49 if (typeof gulp === 'undefined') {
50 gulp = modul();
51 }
52
53 t.equal(typeof gulp.src().on, 'function', 'gulp.src() should return a readable stream');
54
55});
56
57test('gulp.dest()', function (t) {
58 t.plan(1);
59 if (typeof gulp === 'undefined') {
60 gulp = modul();
61 }
62
63 t.equal(typeof gulp.dest().on, 'function', 'gulp.dest() should return a writable stream');
64
65});
66
67test('gulp.name', function (t) {
68 t.plan(3);
69 if (typeof gulp === 'undefined') {
70 gulp = modul();
71 }
72
73 checkImmutable(t, gulp, 'name', 'gulp');
74 t.equal(typeof gulp.name, 'string', 'gulp.name should be a string value');
75 t.equal(gulp.name, 'gulp-loader-test-0.0.0.js', 'gulp.name should be in accorance with package.json file');
76
77});
78
79test('gulp.main', function (t) {
80 t.plan(3);
81 if (typeof gulp === 'undefined') {
82 gulp = modul();
83 }
84
85 checkImmutable(t, gulp, 'main', 'gulp');
86 t.equal(typeof gulp.main, 'string', 'gulp.main should be a string value');
87 t.equal(gulp.main, 'src/main.js', 'gulp.main should default to src/main.js if "main" is not set in package.json');
88
89});
90
91test('gulp.debug', function (t) {
92 t.plan(2);
93 if (typeof gulp === 'undefined') {
94 gulp = modul();
95 }
96
97 checkImmutable(t, gulp, 'debug', 'gulp');
98 t.equal(typeof gulp.debug, 'boolean', 'gulp.debug should be a boolean value');
99
100});
101
102test('gulp.dirs', function (t) {
103 t.plan(13);
104 if (typeof gulp === 'undefined') {
105 gulp = modul();
106 }
107
108 checkImmutable(t, gulp, 'dirs', 'gulp');
109
110 checkImmutable(t, gulp.dirs, 'source', 'gulp.dirs');
111 t.equal(typeof gulp.dirs.source, 'string', 'gulp.dirs.source should be a string value');
112 t.equal(gulp.dirs.source, 'src', 'gulp.dirs.source should default to src if the corresponding setting is not set in package.json');
113
114 checkImmutable(t, gulp.dirs, 'build', 'gulp.dirs');
115 t.equal(typeof gulp.dirs.build, 'string', 'gulp.dirs.build should be a string value');
116 t.equal(gulp.dirs.build, 'build', 'gulp.dirs.source should default to build if the corresponding setting is not set in package.json');
117
118 checkImmutable(t, gulp.dirs, 'test', 'gulp.dirs');
119 t.equal(typeof gulp.dirs.test, 'string', 'gulp.dirs.test should be a string value');
120 t.equal(gulp.dirs.test, 'test', 'gulp.dirs.test should default to src if the corresponding setting is not set in package.json');
121
122 checkImmutable(t, gulp.dirs, 'dist', 'gulp.dirs');
123 t.equal(typeof gulp.dirs.dist, 'string', 'gulp.dirs.dist should be a string value');
124 t.equal(gulp.dirs.dist, 'dist', 'gulp.dirs.dist should default to dist if the corresponding setting is not set in package.json');
125
126});
127
128test('> gulp clean', function(t) {
129 var testFile = './build/not-cleaned';
130 fs.open(testFile, 'w', 0666, function(err, fd) {
131 if (err) t.fail('Couldn\'t create file to test clean task.');
132
133 var buffer = new Buffer('something');
134
135 fs.write(fd, buffer, 0, buffer.length, null, function(err, written, buffer) {
136 if (err) t.fail('Couldn\'t write to file');
137
138 exec('gulp clean', function(err, stdout, stderr) {
139 t.plan(2);
140 fs.exists(testFile, function(exists) {
141 t.notOk(exists, 'File should no longer exist after clean is executed');
142 });
143 fs.exists(path.dirname(testFile), function(exists) {
144 t.ok(exists, 'Folder should still exist after clean is executed');
145 });
146 });
147 });
148 });
149});
150
151test('> gulp clean:test', function(t) {
152 var testFile = './test/not-cleaned';
153 fs.open(testFile, 'w', 0666, function(err, fd) {
154 if (err) t.fail('Couldn\'t create file to test clean task.');
155
156 var buffer = new Buffer('something');
157
158 fs.write(fd, buffer, 0, buffer.length, null, function(err, written, buffer) {
159 if (err) t.fail('Couldn\'t write to file');
160
161 exec('gulp clean:test', function(err, stdout, stderr) {
162 t.plan(2);
163 fs.exists(testFile, function(exists) {
164 t.notOk(exists, 'File should no longer exist after clean is executed');
165 });
166 fs.exists(path.dirname(testFile), function(exists) {
167 t.ok(exists, 'Folder should still exist after clean is executed');
168 });
169 });
170 });
171 });
172});
173
174test('> gulp clean:dist', function(t) {
175 var testFile = './dist/not-cleaned';
176 fs.open(testFile, 'w', 0666, function(err, fd) {
177 if (err) t.fail('Couldn\'t create file to test clean task.');
178
179 var buffer = new Buffer('something');
180
181 fs.write(fd, buffer, 0, buffer.length, null, function(err, written, buffer) {
182 if (err) t.fail('Couldn\'t write to file');
183
184 exec('gulp clean:dist', function(err, stdout, stderr) {
185 t.plan(2);
186 fs.exists(testFile, function(exists) {
187 t.notOk(exists, 'File should no longer exist after clean is executed');
188 });
189 fs.exists(path.dirname(testFile), function(exists) {
190 t.ok(exists, 'Folder should still exist after clean is executed');
191 });
192 });
193 });
194 });
195});
196
197test('> gulp [default]', function(t) {
198 fs.exists('./src/dummy.js', function(exists) {
199 if (!exists) {
200 t.fail('Cannot perform test if source file is not present');
201 } else {
202 t.plan(4);
203 exec('gulp clean', function(err, stdout, stderr) {
204 t.error(err, '\'gulp clean\' should execute without errors');
205 fs.exists('./build/copied-dummy.js', function(exists) {
206 t.notOk(exists, 'File should not exist after cleanup');
207
208 exec('gulp', function(err, stdout, stderr) {
209 t.error(err, '\'gulp\' should execute without errors');
210
211 checkFile(t, './build/copied-dummy.js', 'gulp should have copied file to build dir');
212 });
213 });
214 });
215 }
216 });
217});
218
219test('> gulp deploy', function(t) {
220 fs.exists('./build/copied-dummy.js', function(exists) {
221 if (!exists) {
222 t.fail('Cannot perform test if built file is not present');
223 t.end();
224 } else {
225 t.plan(4);
226 exec('gulp clean:test', function(err, stdout, stderr) {
227 t.error(err, '\'gulp clean:test\' should execute without errors');
228 fs.exists('./test/copied-dummy.js', function(exists) {
229 t.notOk(exists, 'File should not exist after cleanup');
230
231 exec('gulp deploy', function(err, stdout, stderr) {
232 t.error(err, '\'gulp deploy\' should execute without errors');
233
234 checkFile(t, './test/copied-dummy.js', 'gulp should have copied file to test dir');
235 });
236 });
237 });
238 }
239 });
240});
241
242test('> gulp --target=production deploy', function(t) {
243 fs.exists('./build/copied-dummy.js', function(exists) {
244 if (!exists) {
245 t.fail('Cannot perform test if built file is not present');
246 t.end();
247 } else {
248 t.plan(4);
249 exec('gulp clean:dist', function(err, stdout, stderr) {
250 t.error(err, '\'gulp clean:dist\' should execute without errors');
251 fs.exists('./dist/copied-dummy.js', function(exists) {
252 t.notOk(exists, 'File should not exist after cleanup');
253
254 exec('gulp --target=production deploy', function(err, stdout, stderr) {
255 t.error(err, '\'gulp --target=production deploy\' should execute without errors');
256
257 checkFile(t, './dist/copied-dummy.js', 'gulp should have copied file to dist dir');
258 });
259 });
260 });
261 }
262 });
263});
264
265test('robustness test', function (t) {
266 t.plan(2);
267
268 try {
269 var gulp = modul();
270 t.pass('Could load gulp-loader even though some tasks contained syntax errors');
271 } catch (e) {
272 t.fail('tasks shouldn\'t fail to load even though one of them contains an error');
273 }
274
275 exec('gulp dependency-error', function (error, stdout, stderr) {
276 t.ok(error instanceof Error, 'gulp command should throw en error');
277 });
278
279});
280
281test('coffeescript test', function (t) {
282 t.plan(2);
283
284 exec('gulp coffee', function (error, stdout, stderr) {
285 t.error(error, 'gulp command should execute without errors');
286 t.ok(stdout.match(/\n\[(([0-1][0-9])|(2[0-3])):[0-5][0-9]:[0-5][0-9]\] yes\n/), 'coffeescript task should execute without problems');
287 });
288
289});
290
291test('configurability test', function(t) {
292 t.plan(8);
293
294 exec('gulp', {cwd: path.resolve('./settings')}, function (error, stdout, stderr) {
295
296 t.error(error, 'gulp command should execute without errors');
297 var match = stdout.match(/\n{[^\n]*\n/);
298 t.ok(match, 'coffeescript task should execute without problems');
299 var dump = JSON.parse(match[0].trim());
300
301 t.equal(dump.main, 'index.js', 'gulp.main should match the setting in package.json');
302 t.equal(dump.name, 'gulp-loader-test-settings@0.0.1.coffee', 'gulp.name should be according to the settings in package.json');
303
304 t.equal(dump.dirs.source, '.', 'gulp.dirs.source should match the setting in package.json');
305 t.equal(dump.dirs.build, 'tmp', 'gulp.dirs.build should match the setting in package.json');
306 t.equal(dump.dirs.test, 'debug', 'gulp.dirs.test should match the setting in package.json');
307 t.equal(dump.dirs.dist, 'production', 'gulp.dirs.dist should match the setting in package.json');
308 });
309});
\No newline at end of file