UNPKG

3.68 kBJavaScriptView Raw
1var should = require('should');
2var less = require('../');
3var gutil = require('gulp-util');
4var fs = require('fs');
5var pj = require('path').join;
6
7function createVinyl(lessFileName, contents) {
8 var base = pj(__dirname, 'fixtures');
9 var filePath = pj(base, lessFileName);
10
11 return new gutil.File({
12 cwd: __dirname,
13 base: base,
14 path: filePath,
15 contents: contents || fs.readFileSync(filePath)
16 });
17}
18
19describe('gulp-less', function () {
20 describe('less()', function () {
21
22 it('should pass file when it isNull()', function (done) {
23 var stream = less();
24 var emptyFile = {
25 isNull: function () { return true; }
26 };
27 stream.on('data', function (data) {
28 data.should.equal(emptyFile);
29 done();
30 });
31 stream.write(emptyFile);
32 });
33
34 it('should emit error when file isStream()', function (done) {
35 var stream = less();
36 var streamFile = {
37 isNull: function () { return false; },
38 isStream: function () { return true; }
39 };
40 stream.on('error', function (err) {
41 err.message.should.equal('Streaming not supported');
42 done();
43 });
44 stream.write(streamFile);
45 });
46
47 it('should compile single less file', function (done) {
48 var lessFile = createVinyl('buttons.less');
49
50 var stream = less();
51 stream.on('data', function (cssFile) {
52 should.exist(cssFile);
53 should.exist(cssFile.path);
54 should.exist(cssFile.relative);
55 should.exist(cssFile.contents);
56 cssFile.path.should.equal(pj(__dirname, 'fixtures', 'buttons.css'));
57 String(cssFile.contents).should.equal(
58 fs.readFileSync(pj(__dirname, 'expect/buttons.css'), 'utf8'));
59 done();
60 });
61 stream.write(lessFile);
62 });
63
64 it('should emit error when less contains errors', function (done) {
65 var stream = less();
66 var errorFile = createVinyl('somefile.less',
67 new Buffer('html { color: @undefined-variable; }'));
68 stream.on('error', function (err) {
69 err.message.should.equal('variable @undefined-variable is undefined in file '+errorFile.path+' line no. 1');
70 done();
71 });
72 stream.write(errorFile);
73 });
74
75 it('should compile multiple less files', function (done) {
76 var files = [
77 createVinyl('buttons.less'),
78 createVinyl('forms.less'),
79 createVinyl('normalize.less')
80 ];
81
82 var stream = less();
83 var count = files.length;
84 stream.on('data', function (cssFile) {
85 should.exist(cssFile);
86 should.exist(cssFile.path);
87 should.exist(cssFile.relative);
88 should.exist(cssFile.contents);
89 if (!--count) { done(); }
90 });
91
92 files.forEach(function (file) {
93 stream.write(file);
94 });
95 });
96
97 it('should produce sourcemap filenames and mappings', function (done) {
98 var files = [
99 createVinyl('buttons.less'),
100 createVinyl('forms.less'),
101 createVinyl('normalize.less')
102 ] .map(function (file) {
103 file.sourceMap = {
104 file: '',
105 version : 3,
106 sourceRoot : '',
107 sources: [],
108 names: [],
109 mappings: ''
110 };
111
112 return file;
113 });
114
115 var stream = less();
116 var count = files.length;
117 stream.on('data', function (cssFile) {
118 should.exist(cssFile.sourceMap.file);
119 should.exist(cssFile.sourceMap.mappings);
120 should(cssFile.sourceMap.mappings.length).be.greaterThan(1);
121 });
122 stream.on('end', done);
123
124 files.forEach(function (file) {
125 stream.write(file);
126 });
127 stream.end();
128 });
129 });
130});