RollingFileWriteStream-test.js 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441
  1. require("should");
  2. const path = require("path");
  3. const zlib = require("zlib");
  4. const stream = require("stream");
  5. const fs = require("fs-extra");
  6. const proxyquire = require("proxyquire").noPreserveCache();
  7. let fakeNow = new Date(2012, 8, 12, 10, 37, 11);
  8. const mockNow = () => fakeNow;
  9. const RollingFileWriteStream = proxyquire("../lib/RollingFileWriteStream", {
  10. "./now": mockNow
  11. });
  12. let fakedFsDate = fakeNow;
  13. const mockFs = require("fs-extra");
  14. const oldStatSync = mockFs.statSync;
  15. mockFs.statSync = fd => {
  16. const result = oldStatSync(fd);
  17. result.mtime = fakedFsDate;
  18. return result;
  19. };
  20. function generateTestFile(fileName) {
  21. const dirName = path.join(
  22. __dirname,
  23. "tmp_" + Math.floor(Math.random() * new Date())
  24. );
  25. fileName = fileName || "ignored.log";
  26. const fileNameObj = path.parse(fileName);
  27. return {
  28. dir: dirName,
  29. base: fileNameObj.base,
  30. name: fileNameObj.name,
  31. ext: fileNameObj.ext,
  32. path: path.join(dirName, fileName)
  33. };
  34. }
  35. function resetTime() {
  36. fakeNow = new Date(2012, 8, 12, 10, 37, 11);
  37. fakedFsDate = fakeNow;
  38. }
  39. describe("RollingFileWriteStream", () => {
  40. beforeEach(() => {
  41. resetTime();
  42. });
  43. after(() => {
  44. fs.readdirSync(__dirname)
  45. .filter(f => f.startsWith("tmp_"))
  46. .forEach(f => fs.removeSync(path.join(__dirname, f)));
  47. });
  48. describe("with no arguments", () => {
  49. it("should throw an error", () => {
  50. (() => new RollingFileWriteStream()).should.throw(
  51. /(the )?"?path"? (argument )?must be (a|of type) string\. received (type )?undefined/i
  52. );
  53. });
  54. });
  55. describe("with invalid options", () => {
  56. after(done => {
  57. fs.remove("filename", done);
  58. });
  59. it("should complain about a negative maxSize", () => {
  60. (() => {
  61. new RollingFileWriteStream("filename", { maxSize: -3 });
  62. }).should.throw("options.maxSize (-3) should be > 0");
  63. (() => {
  64. new RollingFileWriteStream("filename", { maxSize: 0 });
  65. }).should.throw("options.maxSize (0) should be > 0");
  66. });
  67. it("should complain about a negative numToKeep", () => {
  68. (() => {
  69. new RollingFileWriteStream("filename", { numToKeep: -3 });
  70. }).should.throw("options.numToKeep (-3) should be > 0");
  71. (() => {
  72. new RollingFileWriteStream("filename", { numToKeep: 0 });
  73. }).should.throw("options.numToKeep (0) should be > 0");
  74. });
  75. });
  76. describe("with default arguments", () => {
  77. const fileObj = generateTestFile();
  78. let s;
  79. before(() => {
  80. s = new RollingFileWriteStream(fileObj.path);
  81. });
  82. after(() => {
  83. s.end(() => fs.removeSync(fileObj.dir));
  84. });
  85. it("should take a filename and options, return Writable", () => {
  86. s.should.be.an.instanceOf(stream.Writable);
  87. s.currentFileStream.path.should.eql(fileObj.path);
  88. s.currentFileStream.mode.should.eql(420);
  89. s.currentFileStream.flags.should.eql("a");
  90. });
  91. it("should apply default options", () => {
  92. s.options.maxSize.should.eql(Number.MAX_SAFE_INTEGER);
  93. s.options.encoding.should.eql("utf8");
  94. s.options.mode.should.eql(420);
  95. s.options.flags.should.eql("a");
  96. s.options.compress.should.eql(false);
  97. s.options.keepFileExt.should.eql(false);
  98. });
  99. });
  100. describe("with 5 maxSize, rotating daily", () => {
  101. const fileObj = generateTestFile("noExtension");
  102. let s;
  103. before(async () => {
  104. fakeNow = new Date(2012, 8, 12, 10, 37, 11);
  105. s = new RollingFileWriteStream(fileObj.path, {
  106. pattern: "yyyy-MM-dd",
  107. maxSize: 5
  108. });
  109. const flows = Array.from(Array(38).keys()).map(i => () => {
  110. fakeNow = new Date(2012, 8, 12 + parseInt(i / 5, 10), 10, 37, 11);
  111. return new Promise(resolve => {
  112. s.write(i.toString(), "utf8", () => resolve());
  113. });
  114. });
  115. for (let i = 0; i < flows.length; i += 1) {
  116. await flows[i]();
  117. }
  118. });
  119. after(done => {
  120. s.end(() => {
  121. fs.removeSync(fileObj.dir);
  122. done();
  123. });
  124. });
  125. it("should rotate using filename with no extension", () => {
  126. const files = fs.readdirSync(fileObj.dir);
  127. const expectedFileList = [
  128. fileObj.base, //353637
  129. fileObj.base + ".2012-09-12.1", // 01234
  130. fileObj.base + ".2012-09-13.1", // 56789
  131. fileObj.base + ".2012-09-14.2", // 101112
  132. fileObj.base + ".2012-09-14.1", // 1314
  133. fileObj.base + ".2012-09-15.2", // 151617
  134. fileObj.base + ".2012-09-15.1", // 1819
  135. fileObj.base + ".2012-09-16.2", // 202122
  136. fileObj.base + ".2012-09-16.1", // 2324
  137. fileObj.base + ".2012-09-17.2", // 252627
  138. fileObj.base + ".2012-09-17.1", // 2829
  139. fileObj.base + ".2012-09-18.2", // 303132
  140. fileObj.base + ".2012-09-18.1" // 3334
  141. ];
  142. files.should.containDeep(expectedFileList);
  143. files.length.should.equal(expectedFileList.length);
  144. fs.readFileSync(path.format(fileObj))
  145. .toString()
  146. .should.equal("353637");
  147. fs.readFileSync(
  148. path.format(
  149. Object.assign({}, fileObj, {
  150. base: fileObj.base + ".2012-09-12.1"
  151. })
  152. )
  153. )
  154. .toString()
  155. .should.equal("01234");
  156. fs.readFileSync(
  157. path.format(
  158. Object.assign({}, fileObj, {
  159. base: fileObj.base + ".2012-09-13.1"
  160. })
  161. )
  162. )
  163. .toString()
  164. .should.equal("56789");
  165. fs.readFileSync(
  166. path.format(
  167. Object.assign({}, fileObj, {
  168. base: fileObj.base + ".2012-09-14.2"
  169. })
  170. )
  171. )
  172. .toString()
  173. .should.equal("101112");
  174. fs.readFileSync(
  175. path.format(
  176. Object.assign({}, fileObj, {
  177. base: fileObj.base + ".2012-09-14.1"
  178. })
  179. )
  180. )
  181. .toString()
  182. .should.equal("1314");
  183. fs.readFileSync(
  184. path.format(
  185. Object.assign({}, fileObj, {
  186. base: fileObj.base + ".2012-09-15.2"
  187. })
  188. )
  189. )
  190. .toString()
  191. .should.equal("151617");
  192. fs.readFileSync(
  193. path.format(
  194. Object.assign({}, fileObj, {
  195. base: fileObj.base + ".2012-09-15.1"
  196. })
  197. )
  198. )
  199. .toString()
  200. .should.equal("1819");
  201. fs.readFileSync(
  202. path.format(
  203. Object.assign({}, fileObj, {
  204. base: fileObj.base + ".2012-09-16.2"
  205. })
  206. )
  207. )
  208. .toString()
  209. .should.equal("202122");
  210. fs.readFileSync(
  211. path.format(
  212. Object.assign({}, fileObj, {
  213. base: fileObj.base + ".2012-09-16.1"
  214. })
  215. )
  216. )
  217. .toString()
  218. .should.equal("2324");
  219. fs.readFileSync(
  220. path.format(
  221. Object.assign({}, fileObj, {
  222. base: fileObj.base + ".2012-09-17.2"
  223. })
  224. )
  225. )
  226. .toString()
  227. .should.equal("252627");
  228. fs.readFileSync(
  229. path.format(
  230. Object.assign({}, fileObj, {
  231. base: fileObj.base + ".2012-09-17.1"
  232. })
  233. )
  234. )
  235. .toString()
  236. .should.equal("2829");
  237. fs.readFileSync(
  238. path.format(
  239. Object.assign({}, fileObj, {
  240. base: fileObj.base + ".2012-09-18.2"
  241. })
  242. )
  243. )
  244. .toString()
  245. .should.equal("303132");
  246. fs.readFileSync(
  247. path.format(
  248. Object.assign({}, fileObj, {
  249. base: fileObj.base + ".2012-09-18.1"
  250. })
  251. )
  252. )
  253. .toString()
  254. .should.equal("3334");
  255. });
  256. });
  257. describe("with default arguments and recreated in the same day", () => {
  258. const fileObj = generateTestFile();
  259. let s;
  260. before(async () => {
  261. const flows = Array.from(Array(3).keys()).map(() => () => {
  262. s = new RollingFileWriteStream(fileObj.path);
  263. return new Promise(resolve => {
  264. s.end("abc", "utf8", () => resolve());
  265. });
  266. });
  267. for (let i = 0; i < flows.length; i += 1) {
  268. await flows[i]();
  269. }
  270. });
  271. after(() => {
  272. fs.removeSync(fileObj.dir);
  273. });
  274. it("should have only 1 file", () => {
  275. const files = fs.readdirSync(fileObj.dir);
  276. const expectedFileList = [fileObj.base];
  277. files.should.containDeep(expectedFileList);
  278. files.length.should.equal(expectedFileList.length);
  279. fs.readFileSync(
  280. path.format(
  281. Object.assign({}, fileObj, {
  282. base: fileObj.base
  283. })
  284. )
  285. )
  286. .toString()
  287. .should.equal("abcabcabc");
  288. });
  289. });
  290. describe("with 5 maxSize, using filename with extension", () => {
  291. const fileObj = generateTestFile("withExtension.log");
  292. let s;
  293. before(async () => {
  294. fakeNow = new Date(2012, 8, 12, 10, 37, 11);
  295. s = new RollingFileWriteStream(fileObj.path, {
  296. pattern: "yyyy-MM-dd",
  297. maxSize: 5
  298. });
  299. const flows = Array.from(Array(38).keys()).map(i => () => {
  300. fakeNow = new Date(2012, 8, 12 + parseInt(i / 10, 10), 10, 37, 11);
  301. return new Promise(resolve => {
  302. s.write(i.toString(), "utf8", () => resolve());
  303. });
  304. });
  305. for (let i = 0; i < flows.length; i += 1) {
  306. await flows[i]();
  307. }
  308. });
  309. after(done => {
  310. s.end(() => {
  311. fs.removeSync(fileObj.dir);
  312. done();
  313. })
  314. });
  315. it("should rotate files within the day, and when the day changes", () => {
  316. const files = fs.readdirSync(fileObj.dir);
  317. const expectedFileList = [
  318. fileObj.base, //3637
  319. fileObj.base + ".2012-09-12.2", //01234
  320. fileObj.base + ".2012-09-12.1", //56789
  321. fileObj.base + ".2012-09-13.4", //101112
  322. fileObj.base + ".2012-09-13.3", //131415
  323. fileObj.base + ".2012-09-13.2", //161718
  324. fileObj.base + ".2012-09-13.1", //19
  325. fileObj.base + ".2012-09-14.4", //202122
  326. fileObj.base + ".2012-09-14.3", //232425
  327. fileObj.base + ".2012-09-14.2", //262728
  328. fileObj.base + ".2012-09-14.1", //29
  329. fileObj.base + ".2012-09-15.2", //303132
  330. fileObj.base + ".2012-09-15.1" //333435
  331. ];
  332. files.should.containDeep(expectedFileList);
  333. files.length.should.equal(expectedFileList.length);
  334. fs.readFileSync(path.format(fileObj))
  335. .toString()
  336. .should.equal("3637");
  337. fs.readFileSync(
  338. path.format(
  339. Object.assign({}, fileObj, {
  340. base: fileObj.base + ".2012-09-12.2"
  341. })
  342. )
  343. )
  344. .toString()
  345. .should.equal("01234");
  346. fs.readFileSync(
  347. path.format(
  348. Object.assign({}, fileObj, {
  349. base: fileObj.base + ".2012-09-12.1"
  350. })
  351. )
  352. )
  353. .toString()
  354. .should.equal("56789");
  355. fs.readFileSync(
  356. path.format(
  357. Object.assign({}, fileObj, {
  358. base: fileObj.base + ".2012-09-13.4"
  359. })
  360. )
  361. )
  362. .toString()
  363. .should.equal("101112");
  364. fs.readFileSync(
  365. path.format(
  366. Object.assign({}, fileObj, {
  367. base: fileObj.base + ".2012-09-13.3"
  368. })
  369. )
  370. )
  371. .toString()
  372. .should.equal("131415");
  373. fs.readFileSync(
  374. path.format(
  375. Object.assign({}, fileObj, {
  376. base: fileObj.base + ".2012-09-13.2"
  377. })
  378. )
  379. )
  380. .toString()
  381. .should.equal("161718");
  382. fs.readFileSync(
  383. path.format(
  384. Object.assign({}, fileObj, {
  385. base: fileObj.base + ".2012-09-13.1"
  386. })
  387. )
  388. )
  389. .toString()
  390. .should.equal("19");
  391. fs.readFileSync(
  392. path.format(
  393. Object.assign({}, fileObj, {
  394. base: fileObj.base + ".2012-09-14.4"
  395. })
  396. )
  397. )
  398. .toString()
  399. .should.equal("202122");
  400. fs.readFileSync(
  401. path.format(
  402. Object.assign({}, fileObj, {
  403. base: fileObj.base + ".2012-09-14.3"
  404. })
  405. )
  406. )
  407. .toString()
  408. .should.equal("232425");
  409. fs.readFileSync(
  410. path.format(
  411. Object.assign({}, fileObj, {
  412. base: fileObj.base + ".2012-09-14.2"
  413. })
  414. )
  415. )
  416. .toString()
  417. .should.equal("262728");
  418. fs.readFileSync(
  419. path.format(
  420. Object.assign({}, fileObj, {
  421. base: fileObj.base + ".2012-09-14.1"
  422. })
  423. )
  424. )
  425. .toString()
  426. .should.equal("29");
  427. fs.readFileSync(
  428. path.format(
  429. Object.assign({}, fileObj, {
  430. base: fileObj.base + ".2012-09-15.2"
  431. })
  432. )
  433. )
  434. .toString()
  435. .should.equal("303132");
  436. fs.readFileSync(
  437. path.format(
  438. Object.assign({}, fileObj, {
  439. base: fileObj.base + ".2012-09-15.1"
  440. })
  441. )
  442. )
  443. .toString()
  444. .should.equal("333435");
  445. });
  446. });
  447. describe("with 5 maxSize and 3 files limit", () => {
  448. const fileObj = generateTestFile();
  449. let s;
  450. before(async () => {
  451. fakeNow = new Date(2012, 8, 12, 10, 37, 11);
  452. s = new RollingFileWriteStream(fileObj.path, {
  453. maxSize: 5,
  454. numToKeep: 3
  455. });
  456. const flows = Array.from(Array(38).keys()).map(i => () => {
  457. fakeNow = new Date(2012, 8, 12 + parseInt(i / 5), 10, 37, 11);
  458. return new Promise(resolve => {
  459. s.write(i.toString(), "utf8", () => resolve());
  460. });
  461. });
  462. for (let i = 0; i < flows.length; i += 1) {
  463. await flows[i]();
  464. }
  465. });
  466. after(done => {
  467. s.end(() => {
  468. fs.removeSync(fileObj.dir);
  469. done();
  470. });
  471. });
  472. it("should rotate with at most 3 backup files not including the hot one", () => {
  473. const files = fs.readdirSync(fileObj.dir);
  474. const expectedFileList = [
  475. fileObj.base,
  476. fileObj.base + ".1",
  477. fileObj.base + ".2",
  478. fileObj.base + ".3"
  479. ];
  480. files.should.containDeep(expectedFileList);
  481. files.length.should.equal(expectedFileList.length);
  482. fs.readFileSync(path.format(fileObj))
  483. .toString()
  484. .should.equal("37");
  485. fs.readFileSync(
  486. path.format(
  487. Object.assign({}, fileObj, {
  488. base: fileObj.base + ".1"
  489. })
  490. )
  491. )
  492. .toString()
  493. .should.equal("343536");
  494. fs.readFileSync(
  495. path.format(
  496. Object.assign({}, fileObj, {
  497. base: fileObj.base + ".2"
  498. })
  499. )
  500. )
  501. .toString()
  502. .should.equal("313233");
  503. fs.readFileSync(
  504. path.format(
  505. Object.assign({}, fileObj, {
  506. base: fileObj.base + ".3"
  507. })
  508. )
  509. )
  510. .toString()
  511. .should.equal("282930");
  512. });
  513. });
  514. describe("with 5 maxSize and 3 files limit, rotating daily", () => {
  515. const fileObj = generateTestFile();
  516. let s;
  517. before(async () => {
  518. fakeNow = new Date(2012, 8, 12, 10, 37, 11);
  519. s = new RollingFileWriteStream(fileObj.path, {
  520. maxSize: 5,
  521. pattern: "yyyy-MM-dd",
  522. numToKeep: 3
  523. });
  524. const flows = Array.from(Array(38).keys()).map(i => () => {
  525. fakeNow = new Date(2012, 8, 12 + parseInt(i / 10), 10, 37, 11);
  526. return new Promise(resolve => {
  527. s.write(i.toString(), "utf8", () => resolve());
  528. });
  529. });
  530. for (let i = 0; i < flows.length; i += 1) {
  531. await flows[i]();
  532. }
  533. });
  534. after(done => {
  535. s.end(() => {
  536. fs.removeSync(fileObj.dir);
  537. done();
  538. });
  539. });
  540. it("should rotate with at most 3 backup files not including the hot one", () => {
  541. const files = fs.readdirSync(fileObj.dir);
  542. const expectedFileList = [
  543. fileObj.base, //3637
  544. fileObj.base + ".2012-09-14.1", //29
  545. fileObj.base + ".2012-09-15.2", //303132
  546. fileObj.base + ".2012-09-15.1" //333435
  547. ];
  548. files.should.containDeep(expectedFileList);
  549. files.length.should.equal(expectedFileList.length);
  550. fs.readFileSync(path.format(fileObj))
  551. .toString()
  552. .should.equal("3637");
  553. fs.readFileSync(
  554. path.format(
  555. Object.assign({}, fileObj, {
  556. base: fileObj.base + ".2012-09-15.1"
  557. })
  558. )
  559. )
  560. .toString()
  561. .should.equal("333435");
  562. fs.readFileSync(
  563. path.format(
  564. Object.assign({}, fileObj, {
  565. base: fileObj.base + ".2012-09-15.2"
  566. })
  567. )
  568. )
  569. .toString()
  570. .should.equal("303132");
  571. fs.readFileSync(
  572. path.format(
  573. Object.assign({}, fileObj, {
  574. base: fileObj.base + ".2012-09-14.1"
  575. })
  576. )
  577. )
  578. .toString()
  579. .should.equal("29");
  580. });
  581. });
  582. describe("with date pattern dd-MM-yyyy", () => {
  583. const fileObj = generateTestFile();
  584. let s;
  585. before(async () => {
  586. fakeNow = new Date(2012, 8, 12, 10, 37, 11);
  587. s = new RollingFileWriteStream(fileObj.path, {
  588. maxSize: 5,
  589. pattern: "dd-MM-yyyy"
  590. });
  591. const flows = Array.from(Array(8).keys()).map(i => () => {
  592. fakeNow = new Date(2012, 8, 12 + parseInt(i / 5, 10), 10, 37, 11);
  593. return new Promise(resolve => {
  594. s.write(i.toString(), "utf8", () => resolve());
  595. });
  596. });
  597. for (let i = 0; i < flows.length; i += 1) {
  598. await flows[i]();
  599. }
  600. });
  601. after(done => {
  602. s.end(() => {
  603. fs.remove(fileObj.dir, done);
  604. });
  605. });
  606. it("should rotate with date pattern dd-MM-yyyy in the file name", () => {
  607. const files = fs.readdirSync(fileObj.dir);
  608. const expectedFileList = [fileObj.base, fileObj.base + ".12-09-2012.1"];
  609. files.should.containDeep(expectedFileList);
  610. files.length.should.equal(expectedFileList.length);
  611. fs.readFileSync(path.format(fileObj))
  612. .toString()
  613. .should.equal("567");
  614. fs.readFileSync(
  615. path.format(
  616. Object.assign({}, fileObj, {
  617. base: fileObj.base + ".12-09-2012.1"
  618. })
  619. )
  620. )
  621. .toString()
  622. .should.equal("01234");
  623. });
  624. });
  625. describe("with compress true", () => {
  626. const fileObj = generateTestFile();
  627. let s;
  628. before(async () => {
  629. fakeNow = new Date(2012, 8, 12, 10, 37, 11);
  630. s = new RollingFileWriteStream(fileObj.path, {
  631. maxSize: 5,
  632. pattern: "yyyy-MM-dd",
  633. compress: true
  634. });
  635. const flows = Array.from(Array(8).keys()).map(i => () => {
  636. fakeNow = new Date(2012, 8, 12 + parseInt(i / 5, 10), 10, 37, 11);
  637. return new Promise(resolve => {
  638. s.write(i.toString(), "utf8", () => resolve());
  639. });
  640. });
  641. for (let i = 0; i < flows.length; i += 1) {
  642. await flows[i]();
  643. }
  644. });
  645. after(done => {
  646. s.end(() => {
  647. fs.removeSync(fileObj.dir);
  648. done();
  649. });
  650. });
  651. it("should rotate with gunzip", () => {
  652. const files = fs.readdirSync(fileObj.dir);
  653. const expectedFileList = [
  654. fileObj.base,
  655. fileObj.base + ".2012-09-12.1.gz"
  656. ];
  657. files.should.containDeep(expectedFileList);
  658. files.length.should.equal(expectedFileList.length);
  659. fs.readFileSync(path.format(fileObj))
  660. .toString()
  661. .should.equal("567");
  662. const content = fs.readFileSync(
  663. path.format(
  664. Object.assign({}, fileObj, {
  665. base: fileObj.base + ".2012-09-12.1.gz"
  666. })
  667. )
  668. );
  669. zlib
  670. .gunzipSync(content)
  671. .toString()
  672. .should.equal("01234");
  673. });
  674. });
  675. describe("with keepFileExt", () => {
  676. const fileObj = generateTestFile("keepFileExt.log");
  677. let s;
  678. before(async () => {
  679. fakeNow = new Date(2012, 8, 12, 10, 37, 11);
  680. s = new RollingFileWriteStream(fileObj.path, {
  681. pattern: "yyyy-MM-dd",
  682. maxSize: 5,
  683. keepFileExt: true
  684. });
  685. const flows = Array.from(Array(8).keys()).map(i => () => {
  686. fakeNow = new Date(2012, 8, 12 + parseInt(i / 5, 10), 10, 37, 11);
  687. return new Promise(resolve => {
  688. s.write(i.toString(), "utf8", () => resolve());
  689. });
  690. });
  691. for (let i = 0; i < flows.length; i += 1) {
  692. await flows[i]();
  693. }
  694. });
  695. after(done => {
  696. s.end(() => {
  697. fs.removeSync(fileObj.dir);
  698. done();
  699. });
  700. });
  701. it("should rotate with the same extension", () => {
  702. const files = fs.readdirSync(fileObj.dir);
  703. const expectedFileList = [
  704. fileObj.base,
  705. fileObj.name + ".2012-09-12.1.log"
  706. ];
  707. files.should.containDeep(expectedFileList);
  708. files.length.should.equal(expectedFileList.length);
  709. fs.readFileSync(path.format(fileObj))
  710. .toString()
  711. .should.equal("567");
  712. fs.readFileSync(
  713. path.format({
  714. dir: fileObj.dir,
  715. base: fileObj.name + ".2012-09-12.1" + fileObj.ext
  716. })
  717. )
  718. .toString()
  719. .should.equal("01234");
  720. });
  721. });
  722. describe("with keepFileExt and compress", () => {
  723. const fileObj = generateTestFile("keepFileExt.log");
  724. let s;
  725. before(async () => {
  726. fakeNow = new Date(2012, 8, 12, 10, 37, 11);
  727. s = new RollingFileWriteStream(fileObj.path, {
  728. maxSize: 5,
  729. pattern: "yyyy-MM-dd",
  730. keepFileExt: true,
  731. compress: true
  732. });
  733. const flows = Array.from(Array(8).keys()).map(i => () => {
  734. fakeNow = new Date(2012, 8, 12 + parseInt(i / 5, 10), 10, 37, 11);
  735. return new Promise(resolve => {
  736. s.write(i.toString(), "utf8", () => resolve());
  737. });
  738. });
  739. for (let i = 0; i < flows.length; i += 1) {
  740. await flows[i]();
  741. }
  742. });
  743. after(done => {
  744. s.end(() => {
  745. fs.removeSync(fileObj.dir);
  746. done();
  747. });
  748. });
  749. it("should rotate with the same extension", () => {
  750. const files = fs.readdirSync(fileObj.dir);
  751. const expectedFileList = [
  752. fileObj.base,
  753. fileObj.name + ".2012-09-12.1.log.gz"
  754. ];
  755. files.should.containDeep(expectedFileList);
  756. files.length.should.equal(expectedFileList.length);
  757. fs.readFileSync(path.format(fileObj))
  758. .toString()
  759. .should.equal("567");
  760. const content = fs.readFileSync(
  761. path.format(
  762. Object.assign({}, fileObj, {
  763. base: fileObj.name + ".2012-09-12.1.log.gz"
  764. })
  765. )
  766. );
  767. zlib
  768. .gunzipSync(content)
  769. .toString()
  770. .should.equal("01234");
  771. });
  772. });
  773. describe("with alwaysIncludePattern and keepFileExt", () => {
  774. const fileObj = generateTestFile("keepFileExt.log");
  775. let s;
  776. before(async () => {
  777. fakeNow = new Date(2012, 8, 12, 10, 37, 11);
  778. s = new RollingFileWriteStream(fileObj.path, {
  779. maxSize: 5,
  780. pattern: "yyyy-MM-dd",
  781. keepFileExt: true,
  782. alwaysIncludePattern: true
  783. });
  784. const flows = Array.from(Array(8).keys()).map(i => () => {
  785. fakeNow = new Date(2012, 8, 12 + parseInt(i / 5, 10), 10, 37, 11);
  786. return new Promise(resolve => {
  787. s.write(i.toString(), "utf8", () => resolve());
  788. });
  789. });
  790. for (let i = 0; i < flows.length; i += 1) {
  791. await flows[i]();
  792. }
  793. });
  794. after(done => {
  795. s.end(() => {
  796. fs.removeSync(fileObj.dir);
  797. done();
  798. });
  799. });
  800. it("should rotate with the same extension and keep date in the filename", () => {
  801. const files = fs.readdirSync(fileObj.dir);
  802. const expectedFileList = [
  803. fileObj.name + ".2012-09-12.1.log",
  804. fileObj.name + ".2012-09-13.log"
  805. ];
  806. files.should.containDeep(expectedFileList);
  807. files.length.should.equal(expectedFileList.length);
  808. fs.readFileSync(
  809. path.format(
  810. Object.assign({}, fileObj, {
  811. base: fileObj.name + ".2012-09-13.log"
  812. })
  813. )
  814. )
  815. .toString()
  816. .should.equal("567");
  817. fs.readFileSync(
  818. path.format(
  819. Object.assign({}, fileObj, {
  820. base: fileObj.name + ".2012-09-12.1.log"
  821. })
  822. )
  823. )
  824. .toString()
  825. .should.equal("01234");
  826. });
  827. });
  828. describe("with 5 maxSize, compress, keepFileExt and alwaysIncludePattern", () => {
  829. const fileObj = generateTestFile("keepFileExt.log");
  830. let s;
  831. before(async () => {
  832. fakeNow = new Date(2012, 8, 12, 10, 37, 11);
  833. s = new RollingFileWriteStream(fileObj.path, {
  834. maxSize: 5,
  835. compress: true,
  836. keepFileExt: true,
  837. alwaysIncludePattern: true,
  838. pattern: "yyyy-MM-dd"
  839. });
  840. const flows = Array.from(Array(38).keys()).map(i => () => {
  841. fakeNow = new Date(2012, 8, 12 + parseInt(i / 5, 10), 10, 37, 11);
  842. return new Promise(resolve => {
  843. s.write(i.toString(), "utf8", () => resolve());
  844. });
  845. });
  846. for (let i = 0; i < flows.length; i += 1) {
  847. await flows[i]();
  848. }
  849. });
  850. after(done => {
  851. s.end(() => {
  852. fs.removeSync(fileObj.dir);
  853. done();
  854. });
  855. });
  856. it("should rotate every day", () => {
  857. const files = fs.readdirSync(fileObj.dir);
  858. const expectedFileList = [
  859. fileObj.name + ".2012-09-12.1.log.gz", //01234
  860. fileObj.name + ".2012-09-13.1.log.gz", //56789
  861. fileObj.name + ".2012-09-14.2.log.gz", //101112
  862. fileObj.name + ".2012-09-14.1.log.gz", //1314
  863. fileObj.name + ".2012-09-15.2.log.gz", //151617
  864. fileObj.name + ".2012-09-15.1.log.gz", //1819
  865. fileObj.name + ".2012-09-16.2.log.gz", //202122
  866. fileObj.name + ".2012-09-16.1.log.gz", //2324
  867. fileObj.name + ".2012-09-17.2.log.gz", //252627
  868. fileObj.name + ".2012-09-17.1.log.gz", //2829
  869. fileObj.name + ".2012-09-18.2.log.gz", //303132
  870. fileObj.name + ".2012-09-18.1.log.gz", //3334
  871. fileObj.name + ".2012-09-19.log" //353637
  872. ];
  873. files.should.containDeep(expectedFileList);
  874. files.length.should.equal(expectedFileList.length);
  875. fs.readFileSync(
  876. path.format(
  877. Object.assign({}, fileObj, {
  878. base: fileObj.name + ".2012-09-19.log"
  879. })
  880. )
  881. )
  882. .toString()
  883. .should.equal("353637");
  884. zlib
  885. .gunzipSync(
  886. fs.readFileSync(
  887. path.format(
  888. Object.assign({}, fileObj, {
  889. base: fileObj.name + ".2012-09-18.1.log.gz"
  890. })
  891. )
  892. )
  893. )
  894. .toString()
  895. .should.equal("3334");
  896. zlib
  897. .gunzipSync(
  898. fs.readFileSync(
  899. path.format(
  900. Object.assign({}, fileObj, {
  901. base: fileObj.name + ".2012-09-18.2.log.gz"
  902. })
  903. )
  904. )
  905. )
  906. .toString()
  907. .should.equal("303132");
  908. zlib
  909. .gunzipSync(
  910. fs.readFileSync(
  911. path.format(
  912. Object.assign({}, fileObj, {
  913. base: fileObj.name + ".2012-09-17.1.log.gz"
  914. })
  915. )
  916. )
  917. )
  918. .toString()
  919. .should.equal("2829");
  920. zlib
  921. .gunzipSync(
  922. fs.readFileSync(
  923. path.format(
  924. Object.assign({}, fileObj, {
  925. base: fileObj.name + ".2012-09-17.2.log.gz"
  926. })
  927. )
  928. )
  929. )
  930. .toString()
  931. .should.equal("252627");
  932. zlib
  933. .gunzipSync(
  934. fs.readFileSync(
  935. path.format(
  936. Object.assign({}, fileObj, {
  937. base: fileObj.name + ".2012-09-16.1.log.gz"
  938. })
  939. )
  940. )
  941. )
  942. .toString()
  943. .should.equal("2324");
  944. zlib
  945. .gunzipSync(
  946. fs.readFileSync(
  947. path.format(
  948. Object.assign({}, fileObj, {
  949. base: fileObj.name + ".2012-09-16.2.log.gz"
  950. })
  951. )
  952. )
  953. )
  954. .toString()
  955. .should.equal("202122");
  956. zlib
  957. .gunzipSync(
  958. fs.readFileSync(
  959. path.format(
  960. Object.assign({}, fileObj, {
  961. base: fileObj.name + ".2012-09-15.1.log.gz"
  962. })
  963. )
  964. )
  965. )
  966. .toString()
  967. .should.equal("1819");
  968. zlib
  969. .gunzipSync(
  970. fs.readFileSync(
  971. path.format(
  972. Object.assign({}, fileObj, {
  973. base: fileObj.name + ".2012-09-15.2.log.gz"
  974. })
  975. )
  976. )
  977. )
  978. .toString()
  979. .should.equal("151617");
  980. zlib
  981. .gunzipSync(
  982. fs.readFileSync(
  983. path.format(
  984. Object.assign({}, fileObj, {
  985. base: fileObj.name + ".2012-09-14.1.log.gz"
  986. })
  987. )
  988. )
  989. )
  990. .toString()
  991. .should.equal("1314");
  992. zlib
  993. .gunzipSync(
  994. fs.readFileSync(
  995. path.format(
  996. Object.assign({}, fileObj, {
  997. base: fileObj.name + ".2012-09-14.2.log.gz"
  998. })
  999. )
  1000. )
  1001. )
  1002. .toString()
  1003. .should.equal("101112");
  1004. zlib
  1005. .gunzipSync(
  1006. fs.readFileSync(
  1007. path.format(
  1008. Object.assign({}, fileObj, {
  1009. base: fileObj.name + ".2012-09-13.1.log.gz"
  1010. })
  1011. )
  1012. )
  1013. )
  1014. .toString()
  1015. .should.equal("56789");
  1016. zlib
  1017. .gunzipSync(
  1018. fs.readFileSync(
  1019. path.format(
  1020. Object.assign({}, fileObj, {
  1021. base: fileObj.name + ".2012-09-12.1.log.gz"
  1022. })
  1023. )
  1024. )
  1025. )
  1026. .toString()
  1027. .should.equal("01234");
  1028. });
  1029. });
  1030. describe("when old files exist", () => {
  1031. const fileObj = generateTestFile();
  1032. let s;
  1033. before(done => {
  1034. fakeNow = new Date(2012, 8, 12, 10, 37, 11);
  1035. fs.ensureFileSync(fileObj.path);
  1036. fs.writeFileSync(fileObj.path, "exist");
  1037. s = new RollingFileWriteStream(fileObj.path);
  1038. s.write("now", "utf8", done);
  1039. });
  1040. after(done => {
  1041. s.end(() => {
  1042. fs.removeSync(fileObj.dir);
  1043. done();
  1044. });
  1045. });
  1046. it("should use write in the old file if not reach the maxSize limit", () => {
  1047. const files = fs.readdirSync(fileObj.dir);
  1048. const expectedFileList = [fileObj.base];
  1049. files.should.containDeep(expectedFileList);
  1050. files.length.should.equal(expectedFileList.length);
  1051. fs.readFileSync(path.format(fileObj))
  1052. .toString()
  1053. .should.equal("existnow");
  1054. });
  1055. });
  1056. describe("when old files exist with contents", () => {
  1057. const fileObj = generateTestFile();
  1058. let s;
  1059. before(done => {
  1060. fakeNow = new Date(2012, 8, 12, 10, 37, 11);
  1061. fs.ensureFileSync(fileObj.path);
  1062. fs.writeFileSync(fileObj.path, "This is exactly 30 bytes long\n");
  1063. s = new RollingFileWriteStream(fileObj.path, { maxSize: 35 });
  1064. s.write("one\n", "utf8"); //34
  1065. s.write("two\n", "utf8"); //38 - file should be rotated next time
  1066. s.write("three\n", "utf8", done); // this should be in a new file.
  1067. });
  1068. after(done => {
  1069. s.end(() => {
  1070. fs.removeSync(fileObj.dir);
  1071. done();
  1072. });
  1073. });
  1074. it("should respect the existing file size", () => {
  1075. const files = fs.readdirSync(fileObj.dir);
  1076. const expectedFileList = [fileObj.base, fileObj.base + ".1"];
  1077. files.should.containDeep(expectedFileList);
  1078. files.length.should.equal(expectedFileList.length);
  1079. fs.readFileSync(path.format(fileObj))
  1080. .toString()
  1081. .should.equal("three\n");
  1082. fs.readFileSync(path.join(fileObj.dir, fileObj.base + ".1"))
  1083. .toString()
  1084. .should.equal("This is exactly 30 bytes long\none\ntwo\n");
  1085. });
  1086. });
  1087. describe("when old files exist with contents and the flag is a+", () => {
  1088. const fileObj = generateTestFile();
  1089. let s;
  1090. before(done => {
  1091. fakeNow = new Date(2012, 8, 12, 10, 37, 11);
  1092. fs.ensureFileSync(fileObj.path);
  1093. fs.writeFileSync(fileObj.path, "This is exactly 30 bytes long\n");
  1094. s = new RollingFileWriteStream(fileObj.path, {
  1095. maxSize: 35,
  1096. flags: "a+"
  1097. });
  1098. s.write("one\n", "utf8"); //34
  1099. s.write("two\n", "utf8"); //38 - file should be rotated next time
  1100. s.write("three\n", "utf8", done); // this should be in a new file.
  1101. });
  1102. after(done => {
  1103. s.end(() => {
  1104. fs.removeSync(fileObj.dir);
  1105. done();
  1106. });
  1107. });
  1108. it("should respect the existing file size", () => {
  1109. const files = fs.readdirSync(fileObj.dir);
  1110. const expectedFileList = [fileObj.base, fileObj.base + ".1"];
  1111. files.should.containDeep(expectedFileList);
  1112. files.length.should.equal(expectedFileList.length);
  1113. fs.readFileSync(path.format(fileObj))
  1114. .toString()
  1115. .should.equal("three\n");
  1116. fs.readFileSync(path.join(fileObj.dir, fileObj.base + ".1"))
  1117. .toString()
  1118. .should.equal("This is exactly 30 bytes long\none\ntwo\n");
  1119. });
  1120. });
  1121. describe("when old files exist with indices", () => {
  1122. const fileObj = generateTestFile();
  1123. let s;
  1124. before(done => {
  1125. fs.ensureFileSync(fileObj.path);
  1126. fs.writeFileSync(
  1127. fileObj.path,
  1128. "This was the base file and it should be more than 30 bytes\n"
  1129. ); // base
  1130. fs.writeFileSync(fileObj.path + ".1", "This was the first old file\n"); // base.1
  1131. s = new RollingFileWriteStream(fileObj.path, {
  1132. maxSize: 30,
  1133. numToKeep: 5
  1134. });
  1135. s.write("This is exactly 30 bytes long\n", "utf8"); // base.1 -> base.2, base -> base.1
  1136. s.write("This is exactly 30 bytes long\n", "utf8"); // base.2 -> base.3, base.1 -> base.2, base -> base.1
  1137. s.write("three\n", "utf8", done); // base.3 -> base.4, base.2 -> base.3, base.1 -> base.2, base -> base.1
  1138. });
  1139. after(done => {
  1140. s.end(() => {
  1141. fs.removeSync(fileObj.dir);
  1142. done();
  1143. });
  1144. });
  1145. it("should rotate the old file indices", () => {
  1146. const files = fs.readdirSync(fileObj.dir);
  1147. const expectedFileList = [
  1148. fileObj.base,
  1149. fileObj.base + ".1",
  1150. fileObj.base + ".2",
  1151. fileObj.base + ".3",
  1152. fileObj.base + ".4"
  1153. ];
  1154. files.should.containDeep(expectedFileList);
  1155. files.length.should.equal(expectedFileList.length);
  1156. fs.readFileSync(path.format(fileObj))
  1157. .toString()
  1158. .should.equal("three\n");
  1159. fs.readFileSync(path.join(fileObj.dir, fileObj.base + ".1"))
  1160. .toString()
  1161. .should.equal("This is exactly 30 bytes long\n");
  1162. fs.readFileSync(path.join(fileObj.dir, fileObj.base + ".2"))
  1163. .toString()
  1164. .should.equal("This is exactly 30 bytes long\n");
  1165. fs.readFileSync(path.join(fileObj.dir, fileObj.base + ".3"))
  1166. .toString()
  1167. .should.equal(
  1168. "This was the base file and it should be more than 30 bytes\n"
  1169. );
  1170. fs.readFileSync(path.join(fileObj.dir, fileObj.base + ".4"))
  1171. .toString()
  1172. .should.equal("This was the first old file\n");
  1173. });
  1174. });
  1175. describe("when old files exist with contents and rolling by date", () => {
  1176. const fileObj = generateTestFile();
  1177. let s;
  1178. before(done => {
  1179. fakeNow = new Date(2012, 8, 12, 10, 37, 11);
  1180. fs.ensureFileSync(fileObj.path);
  1181. fs.writeFileSync(fileObj.path, "This was created Sept 12, 2012.\n");
  1182. fakeNow = new Date(2012, 8, 13, 10, 53, 12);
  1183. s = new RollingFileWriteStream(fileObj.path, { pattern: "yyyy-MM-dd" });
  1184. s.write("It is now Sept 13, 2012.\n", "utf8", done); // this should be in a new file.
  1185. });
  1186. after(done => {
  1187. s.end(() => {
  1188. fs.removeSync(fileObj.dir);
  1189. done();
  1190. });
  1191. });
  1192. it("should respect the existing file date", () => {
  1193. const files = fs.readdirSync(fileObj.dir);
  1194. const expectedFileList = [fileObj.base, fileObj.base + ".2012-09-12"];
  1195. files.should.containDeep(expectedFileList);
  1196. files.length.should.equal(expectedFileList.length);
  1197. fs.readFileSync(path.format(fileObj))
  1198. .toString()
  1199. .should.equal("It is now Sept 13, 2012.\n");
  1200. fs.readFileSync(path.join(fileObj.dir, fileObj.base + ".2012-09-12"))
  1201. .toString()
  1202. .should.equal("This was created Sept 12, 2012.\n");
  1203. });
  1204. });
  1205. describe("when old files exist with contents and stream created with overwrite flag", () => {
  1206. const fileObj = generateTestFile();
  1207. let s;
  1208. before(done => {
  1209. fakeNow = new Date(2012, 8, 12, 10, 37, 11);
  1210. fs.ensureFileSync(fileObj.path);
  1211. fs.writeFileSync(fileObj.path, "This is exactly 30 bytes long\n");
  1212. s = new RollingFileWriteStream(fileObj.path, { maxSize: 35, flags: "w" });
  1213. s.write("there should only be this\n", "utf8", done);
  1214. });
  1215. after(done => {
  1216. s.end(() => {
  1217. fs.removeSync(fileObj.dir);
  1218. done();
  1219. });
  1220. });
  1221. it("should ignore the existing file size", () => {
  1222. const files = fs.readdirSync(fileObj.dir);
  1223. const expectedFileList = [fileObj.base];
  1224. files.should.containDeep(expectedFileList);
  1225. files.length.should.equal(expectedFileList.length);
  1226. s.state.currentSize.should.equal(26);
  1227. fs.readFileSync(path.format(fileObj))
  1228. .toString()
  1229. .should.equal("there should only be this\n");
  1230. });
  1231. });
  1232. describe("when dir does not exist", () => {
  1233. const fileObj = generateTestFile();
  1234. let s;
  1235. before(done => {
  1236. fs.removeSync(fileObj.dir);
  1237. fakeNow = new Date(2012, 8, 12, 10, 37, 11);
  1238. s = new RollingFileWriteStream(fileObj.path);
  1239. s.write("test", "utf8", done);
  1240. });
  1241. after(done => {
  1242. s.end(() => {
  1243. fs.removeSync(fileObj.dir);
  1244. done();
  1245. });
  1246. });
  1247. it("should create the dir", () => {
  1248. const files = fs.readdirSync(fileObj.dir);
  1249. const expectedFileList = [fileObj.base];
  1250. files.should.containDeep(expectedFileList);
  1251. files.length.should.equal(expectedFileList.length);
  1252. fs.readFileSync(path.format(fileObj))
  1253. .toString()
  1254. .should.equal("test");
  1255. });
  1256. });
  1257. describe("when given just a base filename with no dir", () => {
  1258. let s;
  1259. before(done => {
  1260. s = new RollingFileWriteStream("test.log");
  1261. s.write("this should not cause any problems", "utf8", done);
  1262. });
  1263. after(done => {
  1264. s.end(() => {
  1265. fs.removeSync("test.log");
  1266. done();
  1267. });
  1268. });
  1269. it("should use process.cwd() as the dir", () => {
  1270. const files = fs.readdirSync(process.cwd());
  1271. files.should.containDeep(["test.log"]);
  1272. fs.readFileSync(path.join(process.cwd(), "test.log"))
  1273. .toString()
  1274. .should.equal("this should not cause any problems");
  1275. });
  1276. });
  1277. describe("with no callback to write", () => {
  1278. let s;
  1279. before(done => {
  1280. s = new RollingFileWriteStream("no-callback.log");
  1281. s.write("this is all very nice", "utf8", done);
  1282. });
  1283. after(done => {
  1284. fs.remove("no-callback.log", done);
  1285. });
  1286. it("should not complain", done => {
  1287. s.write("I am not bothered if this succeeds or not");
  1288. s.end(done);
  1289. });
  1290. });
  1291. describe("events", () => {
  1292. let s;
  1293. before(done => {
  1294. s = new RollingFileWriteStream("test-events.log");
  1295. s.write("this should not cause any problems", "utf8", done);
  1296. });
  1297. after(done => {
  1298. s.end(() => {
  1299. fs.removeSync("test-events.log");
  1300. done();
  1301. });
  1302. });
  1303. it("should emit the error event of the underlying stream", done => {
  1304. s.on("error", e => {
  1305. e.message.should.equal("oh no");
  1306. done();
  1307. });
  1308. s.currentFileStream.emit("error", new Error("oh no"));
  1309. });
  1310. });
  1311. describe("when deleting old files and there is an error", () => {
  1312. before(done => {
  1313. fs.ensureDir('/tmp/delete-test/logfile.log.2', done);
  1314. });
  1315. it("should not let errors bubble up", done => {
  1316. const s = new RollingFileWriteStream("/tmp/delete-test/logfile.log", {
  1317. maxSize: 10,
  1318. numToKeep: 1
  1319. });
  1320. s.write("length is 10", "utf8", () => {
  1321. // if there's an error during deletion, then done never gets called
  1322. s.write("length is 10", "utf8", done);
  1323. });
  1324. });
  1325. after(done => {
  1326. fs.remove('/tmp/delete-test', done);
  1327. })
  1328. });
  1329. });