Skip to content

Commit b961a18

Browse files
author
Andreas Müller
committed
Merge remote-tracking branch 'f/105-date-shortcut-last-month' into main
2 parents 9eebdda + e3d3a32 commit b961a18

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
const Sinon = require('sinon');
2+
const program = require('commander');
3+
const { assert } = require('chai');
4+
const Config = require('../../src/include/file-config');
5+
const moment = require('moment');
6+
const report = require('../../src/models/report');
7+
8+
describe('Command Report. Date shortcuts', () => {
9+
it('Shortcut last_month registered', () => {
10+
/** @type {Sinon.SinonMock} */
11+
const stub = Sinon.mock(program);
12+
13+
/** @type {Sinon.SinonSpy} */
14+
const spy = Sinon.spy(program, 'option');
15+
16+
stub.expects('parse').throws('Test exception to prevent further execution of gtt-report')
17+
try {
18+
require('../../src/gtt-report');
19+
} catch (e) {}
20+
21+
try {
22+
assert(spy.calledWith('--last_month'), 'last_month not registered');
23+
} finally {
24+
spy.restore();
25+
stub.restore();
26+
}
27+
});
28+
29+
it('Shortcut last_month. Sets From and To to Config', () => {
30+
program.last_month = true;
31+
program.args = [];
32+
let configSpy = Sinon.spy(Config.prototype, 'set');
33+
34+
const reportOriginal = Object.getPrototypeOf(report);
35+
let reportStub = Sinon.stub().callsFake(() => {
36+
throw new Error('Test exception to prevent further execution of gtt-report')
37+
});
38+
Object.setPrototypeOf(report, reportStub);
39+
40+
stub = Sinon.mock(program);
41+
stub.expects('parse').returnsThis();
42+
43+
try {
44+
require('../../src/gtt-report');
45+
} catch (e) {}
46+
47+
try {
48+
assert(
49+
configSpy.calledWith('from', moment().subtract(1, 'months').startOf('month')),
50+
'From is not set to Config'
51+
);
52+
assert(
53+
configSpy.calledWith('to', moment().subtract(1, 'months').endOf('month')),
54+
'To is not set to Config'
55+
);
56+
} finally {
57+
configSpy.restore();
58+
Object.setPrototypeOf(report, reportOriginal);
59+
delete require.cache[require.resolve('../../src/models/report')];
60+
}
61+
})
62+
})

src/gtt-report.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ program
4242
.option('--today', 'ignores --from and --to and queries entries for today')
4343
.option('--this_week', 'ignores --from and --to and queries entries for this week')
4444
.option('--this_month', 'ignores --from and --to and queries entries for this month')
45+
.option('--last_month', 'ignores --from and --to and queries entries for last month')
4546
.option('-c --closed', 'include closed issues')
4647
.option('-m --milestone <milestone>', 'include issues from the given milestone')
4748
.option('--hours_per_day <hours>', 'hours per day for human readable time formats')
@@ -168,6 +169,10 @@ if (program.opts().this_month)
168169
config
169170
.set('from', moment().startOf('month'))
170171
.set('to', moment().endOf('month'));
172+
if (program.last_month)
173+
config
174+
.set('from', moment().subtract(1, 'months').startOf('month'))
175+
.set('to', moment().subtract(1, 'months').endOf('month'));
171176

172177
Cli.quiet = config.get('quiet');
173178
Cli.verbose = config.get('_verbose');

0 commit comments

Comments
 (0)