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