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