-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
148 lines (144 loc) · 4.63 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
'use strict';
/* global describe:false, it:false */
var chai = require('chai');
var expect = chai.expect;
var read = require('./index.js');
var fs = require('graceful-fs');
var vinylFs = require('vinyl-fs');
var through = require('through2').obj;
var isStream = require('isstream');
var concat = require('concat-stream');
describe('read()', function() {
before(function() {
fs.appendFileSync('test.txt', '\uFEFFabc123');
fs.symlinkSync('test.txt', 'test.link');
});
after(function() {
fs.unlink('test.link');
fs.unlink('test.txt');
});
it('should not reread file contents', function(done) {
var originalContents;
vinylFs.src('test.txt')
.pipe(through(function(file, enc, cb) {
originalContents = file.contents;
cb(null, file);
}))
.pipe(read())
.pipe(through(function(file) {
expect(file.contents).to.be.instanceOf(Buffer);
expect(file.contents === originalContents).to.equal(true);
done();
}));
});
it('should reread file contents if force option is true', function(done) {
var originalContents;
vinylFs.src('test.txt')
.pipe(through(function(file, enc, cb) {
originalContents = file.contents;
cb(null, file);
}))
.pipe(read({force:true}))
.pipe(through(function(file) {
expect(file.contents).to.be.instanceOf(Buffer);
expect(file.contents.toString()).to.equal(originalContents.toString());
expect(file.contents === originalContents).to.equal(false);
done();
}));
});
it('should not read file contents if file is directory', function(done) {
vinylFs.src('.', { read:false })
.pipe(through(function(file, enc, cb) {
expect(file.contents).to.equal(null);
cb(null, file);
}))
.pipe(read())
.pipe(through(function(file) {
expect(file.contents).to.equal(null);
done();
}));
});
it('should not read file contents if file is symlink', function(done) {
// followSymlinks: vinyl-fs: 2.x
// resolveSymlinks: vinyl-fs: 3.x
vinylFs.src('test.link', { read:false, followSymlinks:false, resolveSymlinks:false })
.pipe(through(function(file, enc, cb) {
expect(file.contents).to.equal(null);
cb(null, file);
}))
.pipe(read())
.pipe(through(function(file) {
expect(file.contents).to.equal(null);
expect(file.symlink).to.equal('test.txt');
done();
}));
});
it('should not read file contents if file is followed symlink', function(done) {
vinylFs.src('test.link', { read:false })
.pipe(through(function(file, enc, cb) {
expect(file.contents).to.equal(null);
cb(null, file);
}))
.pipe(read())
.pipe(through(function(file) {
expect(file.contents.toString()).to.equal('abc123');
done();
}));
});
it('should read file contents as buffer', function(done) {
vinylFs.src('test.txt', { read:false })
.pipe(through(function(file, enc, cb) {
expect(file.contents).to.equal(null);
cb(null, file);
}))
.pipe(read())
.pipe(through(function(file) {
expect(file.contents).to.be.instanceOf(Buffer);
expect(file.contents.toString()).to.equal('abc123');
done();
}));
});
it('should support stripBOM option for buffers', function(done) {
vinylFs.src('test.txt', { read:false})
.pipe(through(function(file, enc, cb) {
expect(file.contents).to.equal(null);
cb(null, file);
}))
.pipe(read({ stripBOM:false }))
.pipe(through(function(file) {
expect(file.contents).to.be.instanceOf(Buffer);
expect(file.contents.toString()).to.equal('\uFEFFabc123');
done();
}));
});
it('should read file contents as stream', function(done) {
vinylFs.src('test.txt', { read:false })
.pipe(through(function(file, enc, cb) {
expect(file.contents).to.equal(null);
cb(null, file);
}))
.pipe(read({ buffer:false }))
.pipe(through(function(file) {
expect(isStream(file.contents)).to.equal(true);
file.contents.pipe(concat(function(contents) {
expect(contents.toString()).to.equal('abc123');
done();
}));
}));
});
it('should support stripBOM option for streams', function(done) {
vinylFs.src('test.txt', { read:false })
.pipe(through(function(file, enc, cb) {
expect(file.contents).to.equal(null);
cb(null, file);
}))
.pipe(read({ buffer:false, stripBOM:false }))
.pipe(through(function(file) {
expect(isStream(file.contents)).to.equal(true);
file.contents.pipe(concat(function(contents) {
expect(contents.toString()).to.equal('\uFEFFabc123');
done();
}));
}));
});
});