UNPKG

6.38 kBJavaScriptView Raw
1'use strict';
2
3var fs = require('fs');
4var es = require('event-stream');
5var should = require('should');
6
7require('mocha');
8
9var gutil = require('gulp-util'),
10 wrap = require('../');
11
12describe('gulp-wrap', function () {
13
14 var expectedFile = new gutil.File({
15 path: 'test/expected/hello.txt',
16 cwd: 'test/',
17 base: 'test/expected',
18 contents: fs.readFileSync('test/expected/hello.txt')
19 });
20
21 it('should produce expected file via buffer', function (done) {
22
23 var srcFile = new gutil.File({
24 path: 'test/fixtures/hello.txt',
25 cwd: 'test/',
26 base: 'test/fixtures',
27 contents: fs.readFileSync('test/fixtures/hello.txt')
28 });
29
30 var stream = wrap('BEFORE <%= contents %> AFTER');
31
32 stream.on('error', function (err) {
33 should.exist(err);
34 done(err);
35 });
36
37 stream.on('data', function (newFile) {
38
39 should.exist(newFile);
40 should.exist(newFile.contents);
41
42 String(newFile.contents).should.equal(String(expectedFile.contents));
43 done();
44 });
45
46 stream.write(srcFile);
47 stream.end();
48 });
49
50 it('should produce expected file via stream', function (done) {
51
52 var srcFile = new gutil.File({
53 path: 'test/fixtures/hello.txt',
54 cwd: 'test/',
55 base: 'test/fixtures',
56 contents: fs.createReadStream('test/fixtures/hello.txt')
57 });
58
59 var stream = wrap('BEFORE <%= contents %> AFTER');
60
61 stream.on('error', function (err) {
62 should.exist(err);
63 done();
64 });
65
66 stream.on('data', function (newFile) {
67
68 should.exist(newFile);
69 should.exist(newFile.contents);
70
71 newFile.contents.pipe(es.wait(function (err, data) {
72 should.not.exist(err);
73 data.should.equal(String(expectedFile.contents));
74 done();
75 }));
76 });
77
78 stream.write(srcFile);
79 stream.end();
80 });
81
82 it('should error when no template is provided', function () {
83 (function(){
84 wrap();
85 }).should.throw('gulp-wrap: Missing template parameter');
86 });
87
88 it('should handle a template from a file src via buffer', function (done) {
89
90 var srcFile = new gutil.File({
91 path: 'test/fixtures/hello.txt',
92 cwd: 'test/',
93 base: 'test/fixtures',
94 contents: fs.readFileSync('test/fixtures/hello.txt')
95 });
96
97 var stream = wrap({src: 'test/fixtures/template.txt'});
98
99 stream.on('error', function (err) {
100 should.exist(err);
101 done(err);
102 });
103
104 stream.on('data', function (newFile) {
105
106 should.exist(newFile);
107 should.exist(newFile.contents);
108
109 String(newFile.contents).should.equal(String(expectedFile.contents));
110 done();
111 });
112
113 stream.write(srcFile);
114 stream.end();
115
116 });
117
118 it('should handle template data and options', function (done) {
119
120 var srcFile = new gutil.File({
121 path: 'test/fixtures/hello.txt',
122 cwd: 'test/',
123 base: 'test/fixtures',
124 contents: fs.readFileSync('test/fixtures/hello.txt')
125 });
126
127 var stream = wrap('BEFORE <%= data.contents %> <%= data.someVar %> AFTER', { someVar: 'someVal'}, { variable: 'data' });
128
129 stream.on('error', function (err) {
130 should.exist(err);
131 done(err);
132 });
133
134 stream.on('data', function (newFile) {
135
136 should.exist(newFile);
137 should.exist(newFile.contents);
138
139 String(newFile.contents).should.equal('BEFORE Hello someVal AFTER');
140 done();
141 });
142
143 stream.write(srcFile);
144 stream.end();
145 });
146
147 it('should allow file props in the template data', function (done) {
148
149 var srcFile = new gutil.File({
150 path: 'test/fixtures/hello.txt',
151 cwd: 'test/',
152 base: 'test/fixtures',
153 contents: fs.readFileSync('test/fixtures/hello.txt')
154 });
155
156 srcFile.someProp = 'someValue';
157
158 var stream = wrap('Contents: [<%= contents %>] - File prop: [<%= file.someProp %>]');
159
160 stream.on('error', function (err) {
161 should.exist(err);
162 done(err);
163 });
164
165 stream.on('data', function (newFile) {
166
167 should.exist(newFile);
168 should.exist(newFile.contents);
169
170 String(newFile.contents).should.equal('Contents: [Hello] - File prop: [someValue]');
171 done();
172 });
173
174 stream.write(srcFile);
175 stream.end();
176 });
177
178 it('should make data props override file data', function (done) {
179
180 var srcFile = new gutil.File({
181 path: 'test/fixtures/hello.txt',
182 cwd: 'test/',
183 base: 'test/fixtures',
184 contents: fs.readFileSync('test/fixtures/hello.txt')
185 });
186
187 srcFile.someProp = 'someValue';
188
189 var stream = wrap('Contents: [<%= contents %>] - File prop: [<%= file.someProp %>]', {
190 file: {
191 someProp: 'valueFromData'
192 }
193 });
194
195 stream.on('error', function (err) {
196 should.exist(err);
197 done(err);
198 });
199
200 stream.on('data', function (newFile) {
201
202 should.exist(newFile);
203 should.exist(newFile.contents);
204
205 String(newFile.contents).should.equal('Contents: [Hello] - File prop: [valueFromData]');
206 done();
207 });
208
209 stream.write(srcFile);
210 stream.end();
211 });
212
213 it('should not pollute file data across multiple streams', function (done) {
214
215 var srcFile1 = new gutil.File({
216 path: 'test/fixtures/hello.txt',
217 cwd: 'test/',
218 base: 'test/fixtures',
219 contents: fs.readFileSync('test/fixtures/hello.txt')
220 });
221
222 srcFile1.somePropFor1 = 'someValueFrom1';
223
224 var srcFile2 = new gutil.File({
225 path: 'test/fixtures/hello2.txt',
226 cwd: 'test/',
227 base: 'test/fixtures',
228 contents: fs.readFileSync('test/fixtures/hello2.txt')
229 });
230
231 srcFile2.somePropFor2 = 'someValueFrom2';
232
233 var stream = wrap('Contents: [<%= contents %>] - File prop from 1: [<%= file.somePropFor1 %>] - File prop from 2: [<%= file.somePropFor2 %>]');
234
235 stream.on('error', function (err) {
236 should.exist(err);
237 done(err);
238 });
239
240 stream.on('data', function (newFile) {
241
242 should.exist(newFile);
243 should.exist(newFile.contents);
244
245 if (newFile.relative === 'hello.txt') {
246 String(newFile.contents).should.equal('Contents: [Hello] - File prop from 1: [someValueFrom1] - File prop from 2: []');
247 } else if (newFile.relative === 'hello2.txt') {
248 String(newFile.contents).should.equal('Contents: [Hello2] - File prop from 1: [] - File prop from 2: [someValueFrom2]');
249 }
250 });
251
252 stream.write(srcFile1);
253 stream.write(srcFile2);
254
255 stream.end();
256 done();
257 });
258
259});