fileNameParser-test.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. const should = require("should");
  2. describe("fileNameParser", () => {
  3. describe("with default options", () => {
  4. const parser = require("../lib/fileNameParser")({
  5. file: {
  6. dir: "/path/to/file",
  7. base: "thefile.log",
  8. ext: ".log",
  9. name: "thefile"
  10. }
  11. });
  12. it("should return null for filenames that do not match", () => {
  13. should(parser("cheese.txt")).not.be.ok();
  14. should(parser("thefile.log.biscuits")).not.be.ok();
  15. });
  16. it("should take a filename and return the index", () => {
  17. parser("thefile.log.2").should.eql({
  18. filename: "thefile.log.2",
  19. index: 2,
  20. isCompressed: false
  21. });
  22. parser("thefile.log.2.gz").should.eql({
  23. filename: "thefile.log.2.gz",
  24. index: 2,
  25. isCompressed: true
  26. });
  27. });
  28. });
  29. describe("with pattern option", () => {
  30. const parser = require("../lib/fileNameParser")({
  31. file: {
  32. dir: "/path/to/file",
  33. base: "thefile.log",
  34. ext: ".log",
  35. name: "thefile"
  36. },
  37. pattern: "yyyy-MM-dd"
  38. });
  39. it("should return null for files that do not match", () => {
  40. should(parser("thefile.log.biscuits")).not.be.ok();
  41. should(parser("thefile.log.2019")).not.be.ok();
  42. should(parser("thefile.log.3.2")).not.be.ok();
  43. should(parser("thefile.log.04-18")).not.be.ok();
  44. should(parser("anotherfile.log.2020-04-18")).not.be.ok();
  45. should(parser("2020-05-18")).not.be.ok();
  46. });
  47. it("should take a filename and return the date", () => {
  48. parser("thefile.log.2019-07-17").should.eql({
  49. filename: "thefile.log.2019-07-17",
  50. index: 0,
  51. date: "2019-07-17",
  52. timestamp: new Date(2019, 6, 17).getTime(),
  53. isCompressed: false
  54. });
  55. parser("thefile.log.gz").should.eql({
  56. filename: "thefile.log.gz",
  57. index: 0,
  58. isCompressed: true
  59. });
  60. });
  61. it("should take a filename and return both date and index", () => {
  62. parser("thefile.log.2019-07-17.2").should.eql({
  63. filename: "thefile.log.2019-07-17.2",
  64. index: 2,
  65. date: "2019-07-17",
  66. timestamp: new Date(2019, 6, 17).getTime(),
  67. isCompressed: false
  68. });
  69. parser("thefile.log.2019-07-17.2.gz").should.eql({
  70. filename: "thefile.log.2019-07-17.2.gz",
  71. index: 2,
  72. date: "2019-07-17",
  73. timestamp: new Date(2019, 6, 17).getTime(),
  74. isCompressed: true
  75. });
  76. });
  77. });
  78. describe("with keepFileExt option", () => {
  79. const parser = require("../lib/fileNameParser")({
  80. file: {
  81. dir: "/path/to/file",
  82. base: "thefile.log",
  83. ext: ".log",
  84. name: "thefile"
  85. },
  86. keepFileExt: true
  87. });
  88. it("should take a filename and return the index", () => {
  89. should(parser("thefile.log.2")).not.be.ok();
  90. should(parser("thefile.log.2.gz")).not.be.ok();
  91. parser("thefile.2.log").should.eql({
  92. filename: "thefile.2.log",
  93. index: 2,
  94. isCompressed: false
  95. });
  96. parser("thefile.2.log.gz").should.eql({
  97. filename: "thefile.2.log.gz",
  98. index: 2,
  99. isCompressed: true
  100. });
  101. });
  102. });
  103. describe("with a two-digit date pattern", () => {
  104. const parser = require("../lib/fileNameParser")({
  105. file: {
  106. dir: "/path/to/file",
  107. base: "thing.log",
  108. ext: ".log",
  109. name: "thing"
  110. },
  111. pattern: "mm"
  112. });
  113. it("should take a filename and return the date", () => {
  114. const expectedTimestamp = new Date(0, 0);
  115. expectedTimestamp.setMinutes(34);
  116. parser("thing.log.34").should.eql({
  117. filename: "thing.log.34",
  118. date: "34",
  119. isCompressed: false,
  120. index: 0,
  121. timestamp: expectedTimestamp.getTime()
  122. });
  123. });
  124. })
  125. describe("with a four-digit date pattern", () => {
  126. const parser = require("../lib/fileNameParser")({
  127. file: {
  128. dir: "/path/to/file",
  129. base: "stuff.log",
  130. ext: ".log",
  131. name: "stuff"
  132. },
  133. pattern: "mm-ss"
  134. });
  135. it("should return null for files that do not match", () => {
  136. should(parser("stuff.log.2020-04-18")).not.be.ok();
  137. should(parser("09-18")).not.be.ok();
  138. });
  139. it("should take a filename and return the date", () => {
  140. const expectedTimestamp = new Date(0, 0);
  141. expectedTimestamp.setMinutes(34);
  142. expectedTimestamp.setSeconds(59);
  143. parser("stuff.log.34-59").should.eql({
  144. filename: "stuff.log.34-59",
  145. date: "34-59",
  146. isCompressed: false,
  147. index: 0,
  148. timestamp: expectedTimestamp.getTime()
  149. });
  150. });
  151. it("should take a filename and return both date and index", () => {
  152. const expectedTimestamp_1 = new Date(0, 0);
  153. expectedTimestamp_1.setMinutes(7);
  154. expectedTimestamp_1.setSeconds(17);
  155. parser("stuff.log.07-17.2").should.eql({
  156. filename: "stuff.log.07-17.2",
  157. index: 2,
  158. date: "07-17",
  159. timestamp: expectedTimestamp_1.getTime(),
  160. isCompressed: false
  161. });
  162. const expectedTimestamp_2 = new Date(0, 0);
  163. expectedTimestamp_2.setMinutes(17);
  164. expectedTimestamp_2.setSeconds(30);
  165. parser("stuff.log.17-30.3.gz").should.eql({
  166. filename: "stuff.log.17-30.3.gz",
  167. index: 3,
  168. date: "17-30",
  169. timestamp: expectedTimestamp_2.getTime(),
  170. isCompressed: true
  171. });
  172. });
  173. })
  174. });