1 |
|
2 | var lib = require('../');
|
3 |
|
4 | var fs = require('fs');
|
5 | var assert = require('assert');
|
6 |
|
7 | var libWithTemplate = lib.template('abc-%s-xyz');
|
8 |
|
9 | function assertPathMatches(path) {
|
10 | assert.ok(/abc-.*-xyz/.test(path), 'path matches pattern');
|
11 | }
|
12 |
|
13 | describe('template', function () {
|
14 |
|
15 | var cleanup = [];
|
16 |
|
17 | after(function () {
|
18 |
|
19 | cleanup.forEach(function (fn) {
|
20 | try { fn(); } catch (err) {}
|
21 | });
|
22 |
|
23 | cleanup = [];
|
24 |
|
25 | });
|
26 |
|
27 | it('should open a file', function (done) {
|
28 |
|
29 | libWithTemplate.open('w', function (err, res) {
|
30 | assert.ifError(err);
|
31 | cleanup.push(fs.closeSync.bind(fs, res.fd));
|
32 | cleanup.push(fs.unlinkSync.bind(fs, res.path));
|
33 | assertPathMatches(res.path);
|
34 | done();
|
35 | });
|
36 |
|
37 | });
|
38 |
|
39 | it('should create a directory', function (done) {
|
40 |
|
41 | libWithTemplate.mkdir(function (err, path) {
|
42 | assert.ifError(err);
|
43 | cleanup.push(fs.rmdirSync.bind(fs, path));
|
44 | assertPathMatches(path);
|
45 | done();
|
46 | });
|
47 |
|
48 | });
|
49 |
|
50 | it('should write a file', function (done) {
|
51 |
|
52 | var content = new Buffer('hello world');
|
53 |
|
54 | libWithTemplate.writeFile(content, function (err, path) {
|
55 | assert.ifError(err);
|
56 | cleanup.push(fs.unlinkSync.bind(fs, path));
|
57 | assertPathMatches(path);
|
58 | done();
|
59 | });
|
60 |
|
61 | });
|
62 |
|
63 | it('should write a stream', function (done) {
|
64 |
|
65 | var content = new Buffer('hello world');
|
66 | var out = libWithTemplate.createWriteStream();
|
67 |
|
68 | out.on('finish', function () {
|
69 | cleanup.push(fs.unlinkSync.bind(fs, out.path));
|
70 | assertPathMatches(out.path);
|
71 | done();
|
72 | });
|
73 |
|
74 | out.end(content);
|
75 |
|
76 | });
|
77 |
|
78 | });
|