-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathfile_stream.test.js
More file actions
110 lines (100 loc) · 3.49 KB
/
file_stream.test.js
File metadata and controls
110 lines (100 loc) · 3.49 KB
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
const fs = require('fs');
const path = require('path');
const assert = require('assert');
const compressing = require('../..');
describe('test/xz/file_stream.test.js', () => {
const sourceFile = path.join(__dirname, '../fixtures/xx.log');
const xzFile = path.join(__dirname, '../fixtures/xx.log.xz');
it('should compress file to xz', done => {
const dest = path.join(__dirname, '../fixtures/xx.log.xz.tmp');
compressing.xz.compressFile(sourceFile, dest)
.then(() => {
assert(fs.existsSync(dest));
// 文件大小应该小于原始文件
assert(fs.statSync(dest).size < fs.statSync(sourceFile).size);
fs.unlinkSync(dest);
done();
})
.catch(done);
});
it('should decompress xz file to log', done => {
const dest = path.join(__dirname, '../fixtures/xx.log.tmp');
compressing.xz.uncompress(xzFile, dest)
.then(() => {
assert(fs.existsSync(dest));
// 内容应该一致
const raw = fs.readFileSync(sourceFile);
const out = fs.readFileSync(dest);
assert.equal(out.length, raw.length);
assert.deepEqual(out, raw);
fs.unlinkSync(dest);
done();
})
.catch(done);
});
it('should compress buffer to xz', done => {
const buf = fs.readFileSync(sourceFile);
const dest = path.join(__dirname, '../fixtures/xx.log.xz.tmp');
compressing.xz.compressFile(buf, dest)
.then(() => {
assert(fs.existsSync(dest));
fs.unlinkSync(dest);
done();
})
.catch(done);
});
it('should decompress xz buffer to log', done => {
const buf = fs.readFileSync(xzFile);
const dest = path.join(__dirname, '../fixtures/xx.log.tmp');
compressing.xz.uncompress(buf, dest)
.then(() => {
assert(fs.existsSync(dest));
const raw = fs.readFileSync(sourceFile);
const out = fs.readFileSync(dest);
assert.equal(out.length, raw.length);
assert.deepEqual(out, raw);
fs.unlinkSync(dest);
done();
})
.catch(done);
});
it('should compress/decompress utf-8 text to xz', async () => {
const buf = Buffer.from('你好\nhello xz\nWindows\r\n');
const dest = path.join(__dirname, '../fixtures/xx.log.xz.utf8.tmp');
await compressing.xz.compressFile(buf, dest);
assert(fs.existsSync(dest));
const dest2 = path.join(__dirname, '../fixtures/xx.log.utf8.tmp');
const xzBuf = fs.readFileSync(dest);
await compressing.xz.uncompress(xzBuf, dest2);
const outBuf = fs.readFileSync(dest2);
assert.deepEqual(outBuf.toString(), buf.toString());
fs.unlinkSync(dest);
fs.unlinkSync(dest2);
});
it('should compress stream to xz', done => {
const src = fs.createReadStream(sourceFile);
const dest = path.join(__dirname, '../fixtures/xx.log.xz.tmp');
compressing.xz.compressFile(src, dest)
.then(() => {
assert(fs.existsSync(dest));
fs.unlinkSync(dest);
done();
})
.catch(done);
});
it('should decompress xz stream to log', done => {
const src = fs.createReadStream(xzFile);
const dest = path.join(__dirname, '../fixtures/xx.log.tmp');
compressing.xz.uncompress(src, dest)
.then(() => {
assert(fs.existsSync(dest));
const raw = fs.readFileSync(sourceFile);
const out = fs.readFileSync(dest);
assert.equal(out.length, raw.length);
assert.equal(out.toString(), raw.toString());
fs.unlinkSync(dest);
done();
})
.catch(done);
});
});