moveAndMaybeCompressFile-test.js 4.2 KB

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