UNPKG

1.26 kBJavaScriptView Raw
1
2var lib = require('../');
3
4var fs = require('fs');
5var assert = require('assert');
6
7var data = new Buffer('testing 1 2 3');
8
9function checkWrittenFile (path) {
10
11 var content = fs.readFileSync(path);
12 assert.equal(content.toString(), data.toString());
13
14}
15
16describe('writeFile', 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 file async', function (done) {
31
32 lib.writeFile(data, function (err, path) {
33 assert.ifError(err);
34
35 cleanup.push(path);
36 checkWrittenFile(path);
37
38 done();
39 });
40
41 });
42
43 it('should write a file sync', function () {
44
45 var path = lib.writeFileSync(data);
46
47 cleanup.push(path);
48 checkWrittenFile(path);
49
50 });
51
52 it('should accept string and encoding async', function (done) {
53
54 lib.writeFile(data.toString(), 'utf-8', function (err, path) {
55 assert.ifError(err);
56
57 cleanup.push(path);
58 checkWrittenFile(path);
59
60 done();
61 });
62
63 });
64
65 it('should accept string and encoding sync', function () {
66
67 var path = lib.writeFileSync(data.toString(), 'utf-8');
68
69 cleanup.push(path);
70 checkWrittenFile(path);
71
72 });
73
74});