forked from jamesknelson/gulp-rev-replace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
150 lines (128 loc) · 4.32 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
'use strict';
/* global it */
var assert = require('assert');
var gutil = require('gulp-util');
var filter = require('gulp-filter');
var rev = require('gulp-rev');
var revReplace = require('./index');
var path = require('path');
var svgFileBody = '<?xml version="1.0" standalone="no"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg xmlns="http://www.w3.org/2000/svg"></svg>';
var cssFileBody = '@font-face { font-family: \'test\'; src: url(\'/fonts/font.svg\'); }\nbody { color: red; }';
var htmlFileBody = '<html><head><link rel="stylesheet" href="/css/style.css" /></head><body><img src="images/image.png" /><img src="images/image.png" /></body></html>';
it('should by default replace filenames in .css and .html files', function (cb) {
var filesToRevFilter = filter(['**/*.css', '**/*.svg', '**/*.png']);
var stream = filesToRevFilter
.pipe(rev())
.pipe(filesToRevFilter.restore())
.pipe(revReplace());
var fileCount = 0;
var unreplacedCSSFilePattern = /style\.css/;
var unreplacedSVGFilePattern = /font\.svg/;
var unreplacedPNGFilePattern = /image\.png/;
stream.on('data', function(file) {
var contents = file.contents.toString();
var extension = path.extname(file.path);
if (extension === '.html') {
assert(
!unreplacedCSSFilePattern.test(contents),
'The renamed CSS file\'s name should be replaced'
);
assert(
!unreplacedPNGFilePattern.test(contents),
'The renamed PNG file\'s name should be globally replaced'
);
} else if (extension === '.css') {
assert(
!unreplacedSVGFilePattern.test(contents),
'The renamed SVG file\'s name should be replaced'
);
} else if (extension === '.svg') {
assert(
contents === svgFileBody,
'The SVG file should not be modified'
);
}
fileCount++;
});
stream.on('end', function() {
assert.equal(fileCount, 4, 'Only four files should pass through the stream');
cb();
});
filesToRevFilter.write(new gutil.File({
path: path.join('css', 'style.css'),
contents: new Buffer(cssFileBody)
}));
filesToRevFilter.write(new gutil.File({
path: path.join('fonts', 'font.svg'),
contents: new Buffer(svgFileBody)
}));
filesToRevFilter.write(new gutil.File({
path: 'images/image.png',
contents: new Buffer('PNG')
}));
filesToRevFilter.write(new gutil.File({
path: 'index.html',
contents: new Buffer(htmlFileBody)
}));
filesToRevFilter.end();
});
it('should not replace filenames in extensions not in replaceInExtensions', function (cb) {
var filesToRevFilter = filter(['**/*.css']);
var stream = filesToRevFilter
.pipe(rev())
.pipe(filesToRevFilter.restore())
.pipe(revReplace({replaceInExtensions: ['.svg']}));
var unreplacedCSSFilePattern = /style\.css/;
stream.on('data', function(file) {
var contents = file.contents.toString();
var extension = path.extname(file.path);
if (extension === '.html') {
assert(
unreplacedCSSFilePattern.test(contents),
'The renamed CSS file\'s name should not be replaced'
);
}
});
stream.on('end', function() {
cb();
});
filesToRevFilter.write(new gutil.File({
path: 'css\\style.css',
contents: new Buffer(cssFileBody)
}));
filesToRevFilter.write(new gutil.File({
path: 'index.html',
contents: new Buffer(htmlFileBody)
}));
filesToRevFilter.end();
});
it('should not canonicalize URIs when option is off', function (cb) {
var filesToRevFilter = filter(['**/*.css']);
var stream = filesToRevFilter
.pipe(rev())
.pipe(filesToRevFilter.restore())
.pipe(revReplace({canonicalUris: false}));
var unreplacedCSSFilePattern = /style\.css/;
stream.on('data', function(file) {
var contents = file.contents.toString();
var extension = path.extname(file.path);
if (extension === '.html') {
assert(
unreplacedCSSFilePattern.test(contents),
'The renamed CSS file\'s name should not be replaced'
);
}
});
stream.on('end', function() {
cb();
});
filesToRevFilter.write(new gutil.File({
path: 'css\\style.css',
contents: new Buffer(cssFileBody)
}));
filesToRevFilter.write(new gutil.File({
path: 'index.html',
contents: new Buffer(htmlFileBody)
}));
filesToRevFilter.end();
});