cron 1.7.1 → 1.8.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,406 @@
1
+ /* eslint-disable no-new */
2
+ const sinon = require('sinon');
3
+ const cron = require('../lib/cron');
4
+ const moment = require('moment-timezone');
5
+
6
+ describe('crontime', function() {
7
+ it('should test stars (* * * * * *)', function() {
8
+ expect(function() {
9
+ new cron.CronTime('* * * * * *');
10
+ }).not.toThrow();
11
+ });
12
+
13
+ it('should test digit (0 * * * * *)', function() {
14
+ expect(function() {
15
+ new cron.CronTime('0 * * * * *');
16
+ }).not.toThrow();
17
+ });
18
+
19
+ it('should test multi digits (08 * * * * *)', function() {
20
+ expect(function() {
21
+ new cron.CronTime('08 * * * * *');
22
+ }).not.toThrow();
23
+ });
24
+
25
+ it('should test all digits (08 8 8 8 8 5)', function() {
26
+ expect(function() {
27
+ new cron.CronTime('08 * * * * *');
28
+ }).not.toThrow();
29
+ });
30
+
31
+ it('should test too many digits (08 8 8 8 8 5)', function() {
32
+ expect(function() {
33
+ new cron.CronTime('08 * * * * *');
34
+ }).not.toThrow();
35
+ });
36
+
37
+ it('should test standard cron format (* * * * *)', function() {
38
+ expect(function() {
39
+ new cron.CronTime('* * * * *');
40
+ }).not.toThrow();
41
+ });
42
+
43
+ it('should test standard cron format (8 8 8 8 5)', function() {
44
+ const standard = new cron.CronTime('8 8 8 8 5');
45
+ const extended = new cron.CronTime('0 8 8 8 8 5');
46
+
47
+ expect(standard.dayOfWeek).toEqual(extended.dayOfWeek);
48
+ expect(standard.month).toEqual(extended.month);
49
+ expect(standard.dayOfMonth).toEqual(extended.dayOfMonth);
50
+ expect(standard.hour).toEqual(extended.hour);
51
+ expect(standard.minute).toEqual(extended.minute);
52
+ expect(standard.second).toEqual(extended.second);
53
+ });
54
+
55
+ it('should test hyphen (0-10 * * * * *)', function() {
56
+ expect(function() {
57
+ new cron.CronTime('0-10 * * * * *');
58
+ }).not.toThrow();
59
+ });
60
+
61
+ it('should test multi hyphens (0-10 0-10 * * * *)', function() {
62
+ expect(function() {
63
+ new cron.CronTime('0-10 0-10 * * * *');
64
+ }).not.toThrow();
65
+ });
66
+
67
+ it('should test all hyphens (0-10 0-10 1-10 1-10 0-6 0-1)', function() {
68
+ expect(function() {
69
+ new cron.CronTime('0-10 0-10 1-10 1-10 0-6 0-1');
70
+ }).not.toThrow();
71
+ });
72
+
73
+ it('should test comma (0,10 * * * * *)', function() {
74
+ expect(function() {
75
+ new cron.CronTime('0,10 * * * * *');
76
+ }).not.toThrow();
77
+ });
78
+
79
+ it('should test multi commas (0,10 0,10 * * * *)', function() {
80
+ expect(function() {
81
+ new cron.CronTime('0,10 0,10 * * * *');
82
+ }).not.toThrow();
83
+ });
84
+
85
+ it('should test all commas (0,10 0,10 1,10 1,10 0,6 0,1)', function() {
86
+ expect(function() {
87
+ new cron.CronTime('0,10 0,10 1,10 1,10 0,6 0,1');
88
+ }).not.toThrow();
89
+ });
90
+
91
+ it('should test alias (* * * * jan *)', function() {
92
+ expect(function() {
93
+ new cron.CronTime('* * * * jan *');
94
+ }).not.toThrow();
95
+ });
96
+
97
+ it('should test multi aliases (* * * * jan,feb *)', function() {
98
+ expect(function() {
99
+ new cron.CronTime('* * * * jan,feb *');
100
+ }).not.toThrow();
101
+ });
102
+
103
+ it('should test all aliases (* * * * jan,feb mon,tue)', function() {
104
+ expect(function() {
105
+ new cron.CronTime('* * * * jan,feb mon,tue');
106
+ }).not.toThrow();
107
+ });
108
+
109
+ it('should test unknown alias (* * * * jar *)', function() {
110
+ expect(function() {
111
+ new cron.CronTime('* * * * jar *');
112
+ }).toThrow();
113
+ });
114
+
115
+ it('should test unknown alias - short (* * * * j *)', function() {
116
+ expect(function() {
117
+ new cron.CronTime('* * * * j *');
118
+ }).toThrow();
119
+ });
120
+
121
+ it('should test too few fields', function() {
122
+ expect(function() {
123
+ new cron.CronTime('* * * *', null, null);
124
+ }).toThrow();
125
+ });
126
+
127
+ it('should test too many fields', function() {
128
+ expect(function() {
129
+ new cron.CronTime('* * * * * * *', null, null);
130
+ }).toThrow();
131
+ });
132
+
133
+ it('should test out of range values', function() {
134
+ expect(function() {
135
+ new cron.CronTime('* * * * 1234', null, null);
136
+ }).toThrow();
137
+ });
138
+
139
+ it('should test invalid wildcard expression', function() {
140
+ expect(function() {
141
+ new cron.CronTime('* * * * 0*');
142
+ }).toThrow();
143
+ });
144
+
145
+ it('should test invalid step', function() {
146
+ expect(function() {
147
+ new cron.CronTime('* * * 1/0 *');
148
+ }).toThrow();
149
+ });
150
+
151
+ it('should test invalid range', function() {
152
+ expect(function() {
153
+ new cron.CronTime('* 2-1 * * *');
154
+ }).toThrow();
155
+ });
156
+
157
+ it('should test Date', function() {
158
+ const d = new Date();
159
+ const ct = new cron.CronTime(d);
160
+ expect(ct.source.isSame(d.getTime())).toBe(true);
161
+ });
162
+
163
+ it('should test day roll-over', function() {
164
+ const numHours = 24;
165
+ const ct = new cron.CronTime('0 0 17 * * *');
166
+
167
+ for (let hr = 0; hr < numHours; hr++) {
168
+ const start = new Date(2012, 3, 16, hr, 30, 30);
169
+ const next = ct._getNextDateFrom(start);
170
+ expect(next - start).toBeLessThan(24 * 60 * 60 * 1000);
171
+ expect(next._d.getTime()).toBeGreaterThan(start.getTime());
172
+ }
173
+ });
174
+
175
+ it('should test illegal repetition syntax', function() {
176
+ expect(function() {
177
+ new cron.CronTime('* * /4 * * *');
178
+ }).toThrow();
179
+ });
180
+
181
+ it('should test next date', function() {
182
+ const ct = new cron.CronTime('0 0 */4 * * *');
183
+
184
+ const nextDate = new Date();
185
+ nextDate.setHours(23);
186
+ const nextdt = ct._getNextDateFrom(nextDate);
187
+
188
+ expect(nextdt._d.getTime()).toBeGreaterThan(nextDate.getTime());
189
+ expect(nextdt.hours() % 4).toEqual(0);
190
+ });
191
+
192
+ it('should throw an exception because next date is invalid', function() {
193
+ const ct = new cron.CronTime('0 0 * * * *');
194
+ const nextDate = new Date('My invalid date string');
195
+ try {
196
+ // eslint-disable-next-line no-unused-vars
197
+ const nextdt = ct._getNextDateFrom(nextDate);
198
+ } catch (e) {
199
+ expect(e.message).toEqual('ERROR: You specified an invalid date.');
200
+ }
201
+ });
202
+
203
+ it('should test next real date', function() {
204
+ const initialDate = new Date();
205
+ const ct = new cron.CronTime(initialDate);
206
+
207
+ const nextDate = new Date();
208
+ nextDate.setMonth(nextDate.getMonth() + 1);
209
+ expect(nextDate.getTime()).toBeGreaterThan(ct.source._d.getTime());
210
+ const nextdt = ct.sendAt(0);
211
+ // there shouldn't be a "next date" when using a real date.
212
+ // execution happens once
213
+ // so the return should be the date passed in unless explicitly reset
214
+ expect(nextdt.isBefore(nextDate)).toBeTruthy();
215
+ expect(nextdt.isSame(initialDate)).toBeTruthy();
216
+ });
217
+
218
+ describe('should throw an exception because `L` not supported', function() {
219
+ it('(* * * L * *)', function() {
220
+ expect(function() {
221
+ new cron.CronTime('* * * L * *');
222
+ }).toThrow();
223
+ });
224
+
225
+ it('(* * * * * L)', function() {
226
+ expect(function() {
227
+ new cron.CronTime('* * * * * L');
228
+ }).toThrow();
229
+ });
230
+ });
231
+
232
+ it('should strip off millisecond', function() {
233
+ const cronTime = new cron.CronTime('0 */10 * * * *');
234
+ const x = cronTime._getNextDateFrom(new Date('2018-08-10T02:20:00.999Z'));
235
+ expect(x.toISOString()).toEqual('2018-08-10T02:30:00.000Z');
236
+ });
237
+
238
+ it('should strip off millisecond (2)', function() {
239
+ const cronTime = new cron.CronTime('0 */10 * * * *');
240
+ const x = cronTime._getNextDateFrom(new Date('2018-08-10T02:19:59.999Z'));
241
+ expect(x.toISOString()).toEqual('2018-08-10T02:20:00.000Z');
242
+ });
243
+
244
+ it('should generete the right next days when cron is set to every minute', function() {
245
+ const cronTime = new cron.CronTime('* * * * *');
246
+ const min = 60000;
247
+ let previousDate = new Date(Date.UTC(2018, 5, 3, 0, 0));
248
+ for (let i = 0; i < 25; i++) {
249
+ const nextDate = cronTime._getNextDateFrom(previousDate);
250
+ expect(nextDate.valueOf()).toEqual(previousDate.valueOf() + min);
251
+ previousDate = nextDate;
252
+ }
253
+ });
254
+
255
+ it('should generete the right next days when cron is set to every 15 min', function() {
256
+ const cronTime = new cron.CronTime('*/15 * * * *');
257
+ const min = 60000 * 15;
258
+ let previousDate = new Date(Date.UTC(2016, 6, 3, 0, 0));
259
+ for (let i = 0; i < 25; i++) {
260
+ const nextDate = cronTime._getNextDateFrom(previousDate);
261
+ expect(nextDate.valueOf()).toEqual(previousDate.valueOf() + min);
262
+ previousDate = nextDate;
263
+ }
264
+ });
265
+ it('should work around time zone changes that shifts time back (1)', function() {
266
+ const d = new Date('10-7-2018');
267
+ // America/Sao_Paulo has a time zone change around NOV 3 2018.
268
+ const cronTime = new cron.CronTime('0 0 9 4 * *');
269
+ const nextDate = cronTime._getNextDateFrom(d, 'America/Sao_Paulo');
270
+ expect(nextDate.valueOf()).toEqual(
271
+ moment('2018-11-04T09:00:00.000-02:00').valueOf()
272
+ );
273
+ });
274
+ it('should work around time zone changes that shifts time back (2)', function() {
275
+ // Asia/Amman DST ends in 26 - OCT-2018 (-1 to hours)
276
+ const d = moment.tz('2018-10-25T23:00', 'Asia/Amman');
277
+ const cronTime = new cron.CronTime('0 0 * * *');
278
+ const nextDate = cronTime._getNextDateFrom(d, 'Asia/Amman');
279
+ expect(nextDate - moment.tz('2018-10-26T00:00', 'Asia/Amman')).toEqual(0);
280
+ });
281
+ it('should work around time zone changes that shifts time forward', function() {
282
+ // Asia/Amman DST starts in 30-March-2018 (+1 to hours)
283
+ let currentDate = moment.tz('2018-03-29T23:00', 'Asia/Amman');
284
+ const cronTime = new cron.CronTime('* * * * *');
285
+ for (let i = 0; i < 100; i++) {
286
+ const nextDate = cronTime._getNextDateFrom(currentDate, 'Asia/Amman');
287
+ expect(nextDate - currentDate).toEqual(1000 * 60);
288
+ currentDate = nextDate;
289
+ }
290
+ });
291
+ it('should generate the right N next days for * * * * *', function() {
292
+ const cronTime = new cron.CronTime('* * * * *');
293
+ let currentDate = moment()
294
+ .seconds(0)
295
+ .milliseconds(0);
296
+ for (let i = 0; i < 100; i++) {
297
+ const nextDate = cronTime._getNextDateFrom(currentDate);
298
+ expect(nextDate - currentDate).toEqual(1000 * 60);
299
+ currentDate = nextDate;
300
+ }
301
+ });
302
+ it('should generate the right N next days for 0 0 9 * * *', function() {
303
+ const cronTime = new cron.CronTime('0 0 9 * * *');
304
+ let currentDate = moment()
305
+ .utc()
306
+ .seconds(0)
307
+ .milliseconds(0)
308
+ .hours(9)
309
+ .minutes(0);
310
+ for (let i = 0; i < 100; i++) {
311
+ const nextDate = cronTime._getNextDateFrom(currentDate);
312
+ expect(nextDate - currentDate).toEqual(1000 * 60 * 60 * 24);
313
+ currentDate = nextDate;
314
+ }
315
+ });
316
+ it('should generate the right N next days for 0 0 * * * with a time zone', function() {
317
+ const cronTime = new cron.CronTime('0 * * * *');
318
+ let currentDate = moment
319
+ .tz('2018-11-02T23:00', 'America/Sao_Paulo')
320
+ .seconds(0)
321
+ .milliseconds(0);
322
+ for (let i = 0; i < 25; i++) {
323
+ const nextDate = cronTime._getNextDateFrom(
324
+ currentDate,
325
+ 'America/Sao_Paulo'
326
+ );
327
+ expect(nextDate - currentDate).toEqual(1000 * 60 * 60);
328
+ currentDate = nextDate;
329
+ }
330
+ });
331
+ it('should generate the right N next days for */3 * * * * with a time zone', function() {
332
+ const cronTime = new cron.CronTime('*/3 * * * *');
333
+ let currentDate = moment
334
+ .tz('2018-11-02T23:00', 'America/Sao_Paulo')
335
+ .seconds(0)
336
+ .milliseconds(0);
337
+ for (let i = 0; i < 25; i++) {
338
+ const nextDate = cronTime._getNextDateFrom(
339
+ currentDate,
340
+ 'America/Sao_Paulo'
341
+ );
342
+ expect(nextDate - currentDate).toEqual(1000 * 60 * 3);
343
+ currentDate = nextDate;
344
+ }
345
+ });
346
+ it('should generete the right next day when cron is set to every 15 min in Feb', function() {
347
+ const cronTime = new cron.CronTime('*/15 * * FEB *');
348
+ const previousDate = new Date(Date.UTC(2018, 3, 0, 0, 0));
349
+ const nextDate = cronTime._getNextDateFrom(previousDate, 'UTC');
350
+ expect(nextDate.valueOf()).toEqual(
351
+ new Date(Date.UTC(2019, 1, 1, 0, 0)).valueOf()
352
+ );
353
+ });
354
+ it('should generate the right next day when cron is set to both day of the month and day of the week (1)', function() {
355
+ const cronTime = new cron.CronTime('0 8 1 * 4');
356
+ const previousDate = new Date(Date.UTC(2019, 3, 22, 0, 0));
357
+ const nextDate = cronTime._getNextDateFrom(previousDate, 'UTC');
358
+ expect(nextDate.valueOf()).toEqual(
359
+ new Date(Date.UTC(2019, 3, 25, 8, 0)).valueOf()
360
+ );
361
+ });
362
+ it('should generate the right next day when cron is set to both day of the month and day of the week (2)', function() {
363
+ const cronTime = new cron.CronTime('0 8 1 * 4');
364
+ const previousDate = new Date(Date.UTC(2019, 3, 26, 0, 0));
365
+ const nextDate = cronTime._getNextDateFrom(previousDate, 'UTC');
366
+ expect(nextDate.valueOf()).toEqual(
367
+ new Date(Date.UTC(2019, 4, 1, 8, 0)).valueOf()
368
+ );
369
+ });
370
+ it('should generate the right next day when cron is set to both day of the month and day of the week (3)', function() {
371
+ const cronTime = new cron.CronTime('0 8 1 * 4');
372
+ const previousDate = new Date(Date.UTC(2019, 7, 1, 7, 59));
373
+ const nextDate = cronTime._getNextDateFrom(previousDate, 'UTC');
374
+ expect(nextDate.valueOf()).toEqual(
375
+ new Date(Date.UTC(2019, 7, 1, 8, 0)).valueOf()
376
+ );
377
+ });
378
+
379
+ it('should accept 0 as a valid UTC offset', function() {
380
+ const clock = sinon.useFakeTimers();
381
+
382
+ const cronTime = new cron.CronTime('0 11 * * *', null, 0);
383
+ const expected = moment()
384
+ .add(11, 'hours')
385
+ .unix();
386
+ const actual = cronTime.sendAt().unix();
387
+
388
+ expect(actual).toEqual(expected);
389
+
390
+ clock.restore();
391
+ });
392
+
393
+ it('should accept -120 as a valid UTC offset', function() {
394
+ const clock = sinon.useFakeTimers();
395
+
396
+ const cronTime = new cron.CronTime('0 11 * * *', null, -120);
397
+ const expected = moment()
398
+ .add(13, 'hours')
399
+ .unix();
400
+ const actual = cronTime.sendAt().unix();
401
+
402
+ expect(actual).toEqual(expected);
403
+
404
+ clock.restore();
405
+ });
406
+ });
package/Makefile DELETED
@@ -1,10 +0,0 @@
1
- TESTS = tests/*.js
2
-
3
- all: test
4
-
5
- test:
6
- npm install .
7
- @./node_modules/mocha/bin/mocha \
8
- $(TESTS)
9
-
10
- .PHONY: test