1 |
|
2 | var lib = require('../');
|
3 |
|
4 | var fs = require('fs');
|
5 | var assert = require('assert');
|
6 |
|
7 | var data = new Buffer('testing 1 2 3');
|
8 |
|
9 | function checkWrittenFile (path) {
|
10 |
|
11 | var content = fs.readFileSync(path);
|
12 | assert.equal(content.toString(), data.toString());
|
13 |
|
14 | }
|
15 |
|
16 | describe('createWriteStream', function () {
|
17 |
|
18 | var cleanup = [];
|
19 |
|
20 | after(function () {
|
21 |
|
22 | cleanup.forEach(function (path) {
|
23 | try { fs.unlinkSync(path); } catch (err) {}
|
24 | });
|
25 |
|
26 | cleanup = [];
|
27 |
|
28 | });
|
29 |
|
30 | it('should write a stream', function (done) {
|
31 |
|
32 | var s = lib.createWriteStream();
|
33 | var pathEmitted = false;
|
34 |
|
35 | s.on('path', function (path) {
|
36 | cleanup.push(path);
|
37 | pathEmitted = true;
|
38 | });
|
39 |
|
40 | s.on('finish', function () {
|
41 | assert.ok(pathEmitted);
|
42 | checkWrittenFile(s.path);
|
43 | done();
|
44 | });
|
45 |
|
46 | s.end(data);
|
47 |
|
48 | });
|
49 |
|
50 | it('should accept string and encoding', function (done) {
|
51 |
|
52 | var s = lib.createWriteStream({ encoding: 'utf-8' });
|
53 | var pathEmitted = false;
|
54 |
|
55 | s.on('path', function (path) {
|
56 | cleanup.push(path);
|
57 | pathEmitted = true;
|
58 | });
|
59 |
|
60 | s.on('finish', function () {
|
61 | assert.ok(pathEmitted);
|
62 | checkWrittenFile(s.path);
|
63 | done();
|
64 | });
|
65 |
|
66 | s.end(data.toString());
|
67 |
|
68 | });
|
69 |
|
70 | });
|