source: trip-planner-front/node_modules/streamroller/test/moveAndMaybeCompressFile-test.js@ 6c1585f

Last change on this file since 6c1585f was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 4.2 KB
Line 
1require("should");
2
3const fs = require('fs-extra');
4const path = require('path');
5const zlib = require('zlib');
6const proxyquire = require('proxyquire').noPreserveCache();
7const moveAndMaybeCompressFile = require('../lib/moveAndMaybeCompressFile');
8const TEST_DIR = path.normalize(`/tmp/moveAndMaybeCompressFile_${Math.floor(Math.random()*10000)}`);
9
10describe('moveAndMaybeCompressFile', () => {
11 beforeEach(async () => {
12 await fs.emptyDir(TEST_DIR);
13 });
14
15 after(async () => {
16 await fs.remove(TEST_DIR);
17 });
18
19 it('should move the source file to a new destination', async () => {
20 const source = path.join(TEST_DIR, 'test.log');
21 const destination = path.join(TEST_DIR, 'moved-test.log');
22 await fs.outputFile(source, 'This is the test file.');
23 await moveAndMaybeCompressFile(source, destination);
24
25 const contents = await fs.readFile(destination, 'utf8');
26 contents.should.equal('This is the test file.');
27
28 const exists = await fs.pathExists(source);
29 exists.should.be.false();
30
31 });
32
33 it('should compress the source file at the new destination', async () => {
34 const source = path.join(TEST_DIR, 'test.log');
35 const destination = path.join(TEST_DIR, 'moved-test.log.gz');
36 await fs.outputFile(source, 'This is the test file.');
37 await moveAndMaybeCompressFile(source, destination, true);
38
39 const zippedContents = await fs.readFile(destination);
40 const contents = await new Promise(resolve => {
41 zlib.gunzip(zippedContents, (e, data) => {
42 resolve(data.toString());
43 });
44 });
45 contents.should.equal('This is the test file.');
46
47 const exists = await fs.pathExists(source);
48 exists.should.be.false();
49 });
50
51 it('should do nothing if the source file and destination are the same', async () => {
52 const source = path.join(TEST_DIR, 'pants.log');
53 const destination = path.join(TEST_DIR, 'pants.log');
54 await fs.outputFile(source, 'This is the test file.');
55 await moveAndMaybeCompressFile(source, destination);
56
57 (await fs.readFile(source, 'utf8')).should.equal('This is the test file.');
58 });
59
60 it('should do nothing if the source file does not exist', async () => {
61 const source = path.join(TEST_DIR, 'pants.log');
62 const destination = path.join(TEST_DIR, 'moved-pants.log');
63 await moveAndMaybeCompressFile(source, destination);
64
65 (await fs.pathExists(destination)).should.be.false();
66 });
67
68 it('should use copy+truncate if source file is locked (windows)', async () => {
69 const moveWithMock = proxyquire('../lib/moveAndMaybeCompressFile', {
70 "fs-extra": {
71 exists: () => Promise.resolve(true),
72 move: () => Promise.reject({ code: 'EBUSY', message: 'all gone wrong'}),
73 copy: (fs.copy.bind(fs)),
74 truncate: (fs.truncate.bind(fs))
75 }
76 });
77
78 const source = path.join(TEST_DIR, 'test.log');
79 const destination = path.join(TEST_DIR, 'moved-test.log');
80 await fs.outputFile(source, 'This is the test file.');
81 await moveWithMock(source, destination);
82
83 const contents = await fs.readFile(destination, 'utf8');
84 contents.should.equal('This is the test file.');
85
86 // won't delete the source, but it will be empty
87 (await fs.readFile(source, 'utf8')).should.be.empty()
88
89 });
90
91 it('should truncate file if remove fails when compressed (windows)', async () => {
92 const moveWithMock = proxyquire('../lib/moveAndMaybeCompressFile', {
93 "fs-extra": {
94 exists: () => Promise.resolve(true),
95 unlink: () => Promise.reject({ code: 'EBUSY', message: 'all gone wrong'}),
96 createReadStream: fs.createReadStream.bind(fs),
97 truncate: fs.truncate.bind(fs)
98 }
99 });
100
101 const source = path.join(TEST_DIR, 'test.log');
102 const destination = path.join(TEST_DIR, 'moved-test.log.gz');
103 await fs.outputFile(source, 'This is the test file.');
104 await moveWithMock(source, destination, true);
105
106 const zippedContents = await fs.readFile(destination);
107 const contents = await new Promise(resolve => {
108 zlib.gunzip(zippedContents, (e, data) => {
109 resolve(data.toString());
110 });
111 });
112 contents.should.equal('This is the test file.');
113
114 // won't delete the source, but it will be empty
115 (await fs.readFile(source, 'utf8')).should.be.empty()
116
117 });
118});
Note: See TracBrowser for help on using the repository browser.