forked from log4js-node/streamroller
/
moveAndMaybeCompressFile-test.js
166 lines (134 loc) · 6.11 KB
/
moveAndMaybeCompressFile-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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
require("should");
const fs = require('fs-extra');
const path = require('path');
const zlib = require('zlib');
const proxyquire = require('proxyquire').noPreserveCache();
const moveAndMaybeCompressFile = require('../lib/moveAndMaybeCompressFile');
const TEST_DIR = path.normalize(`/tmp/moveAndMaybeCompressFile_${Math.floor(Math.random()*10000)}`);
describe('moveAndMaybeCompressFile', () => {
beforeEach(async () => {
await fs.emptyDir(TEST_DIR);
});
after(async () => {
await fs.remove(TEST_DIR);
});
it('should move the source file to a new destination', async () => {
const source = path.join(TEST_DIR, 'test.log');
const destination = path.join(TEST_DIR, 'moved-test.log');
await fs.outputFile(source, 'This is the test file.');
await moveAndMaybeCompressFile(source, destination);
const contents = await fs.readFile(destination, 'utf8');
contents.should.equal('This is the test file.');
const exists = await fs.pathExists(source);
exists.should.be.false();
});
it('should compress the source file at the new destination', async () => {
const source = path.join(TEST_DIR, 'test.log');
const destination = path.join(TEST_DIR, 'moved-test.log.gz');
await fs.outputFile(source, 'This is the test file.');
const moveAndCompressOptions = {compress: true}
await moveAndMaybeCompressFile(source, destination, moveAndCompressOptions);
const zippedContents = await fs.readFile(destination);
const contents = await new Promise(resolve => {
zlib.gunzip(zippedContents, (e, data) => {
resolve(data.toString());
});
});
contents.should.equal('This is the test file.');
const exists = await fs.pathExists(source);
exists.should.be.false();
});
it('should do nothing if the source file and destination are the same', async () => {
const source = path.join(TEST_DIR, 'pants.log');
const destination = path.join(TEST_DIR, 'pants.log');
await fs.outputFile(source, 'This is the test file.');
await moveAndMaybeCompressFile(source, destination);
(await fs.readFile(source, 'utf8')).should.equal('This is the test file.');
});
it('should do nothing if the source file does not exist', async () => {
const source = path.join(TEST_DIR, 'pants.log');
const destination = path.join(TEST_DIR, 'moved-pants.log');
await moveAndMaybeCompressFile(source, destination);
(await fs.pathExists(destination)).should.be.false();
});
it('should use copy+truncate if source file is locked (windows)', async () => {
const moveWithMock = proxyquire('../lib/moveAndMaybeCompressFile', {
"fs-extra": {
exists: () => Promise.resolve(true),
move: () => Promise.reject({ code: 'EBUSY', message: 'all gone wrong'}),
copy: (fs.copy.bind(fs)),
truncate: (fs.truncate.bind(fs))
}
});
const source = path.join(TEST_DIR, 'test.log');
const destination = path.join(TEST_DIR, 'moved-test.log');
await fs.outputFile(source, 'This is the test file.');
await moveWithMock(source, destination);
const contents = await fs.readFile(destination, 'utf8');
contents.should.equal('This is the test file.');
// won't delete the source, but it will be empty
(await fs.readFile(source, 'utf8')).should.be.empty()
});
it('should truncate file if remove fails when compressed (windows)', async () => {
const moveWithMock = proxyquire('../lib/moveAndMaybeCompressFile', {
"fs-extra": {
exists: () => Promise.resolve(true),
unlink: () => Promise.reject({ code: 'EBUSY', message: 'all gone wrong'}),
createReadStream: fs.createReadStream.bind(fs),
truncate: fs.truncate.bind(fs)
}
});
const source = path.join(TEST_DIR, 'test.log');
const destination = path.join(TEST_DIR, 'moved-test.log.gz');
await fs.outputFile(source, 'This is the test file.');
const options = {compress: true};
await moveWithMock(source, destination, options);
const zippedContents = await fs.readFile(destination);
const contents = await new Promise(resolve => {
zlib.gunzip(zippedContents, (e, data) => {
resolve(data.toString());
});
});
contents.should.equal('This is the test file.');
// won't delete the source, but it will be empty
(await fs.readFile(source, 'utf8')).should.be.empty()
});
it('should compress the source file at the new destination with 0o775 rights', async () => {
const source = path.join(TEST_DIR, 'test.log');
const destination = path.join(TEST_DIR, 'moved-test.log.gz');
await fs.outputFile(source, 'This is the test file.');
const moveAndCompressOptions = {compress: true, mode:0o775}
await moveAndMaybeCompressFile(source, destination, moveAndCompressOptions);
const destinationStats = await fs.stat(destination);
const destMode = (destinationStats.mode & 0o775).toString(8);
destMode.should.equal('775');
const zippedContents = await fs.readFile(destination);
const contents = await new Promise(resolve => {
zlib.gunzip(zippedContents, (e, data) => {
resolve(data.toString());
});
});
contents.should.equal('This is the test file.');
const exists = await fs.pathExists(source);
exists.should.be.false();
});
it('should compress the source file at the new destination with 0o400 rights', async () => {
const source = path.join(TEST_DIR, 'test.log');
const destination = path.join(TEST_DIR, 'moved-test.log.gz');
await fs.outputFile(source, 'This is the test file.');
const moveAndCompressOptions = {compress: true, mode:0o400}
await moveAndMaybeCompressFile(source, destination, moveAndCompressOptions);
const destinationStats = await fs.stat(destination);
const destMode = (destinationStats.mode & 0o400).toString(8);
destMode.should.equal('400');
const zippedContents = await fs.readFile(destination);
const contents = await new Promise(resolve => {
zlib.gunzip(zippedContents, (e, data) => {
resolve(data.toString());
});
});
contents.should.equal('This is the test file.');
const exists = await fs.pathExists(source);
exists.should.be.false();
});
});