-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
242 lines (197 loc) · 8.44 KB
/
test.js
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
'use strict'
const chai = require('chai');
const chaiAsPromised = require('chai-as-promised');
const expect = chai.expect;
const mocha = require('mocha');
const describe = mocha.describe;
const it = mocha.it;
const sinon = require('sinon');
const sinonChai = require('sinon-chai');
chai.use(chaiAsPromised);
chai.use(sinonChai);
const shell = require('shelljs')
const fs = require('fs')
const GitOutOfHours = require('./src/index.js');
describe('#gitoutofhours', () => {
const sandbox = sinon.createSandbox();
afterEach(() => {
sandbox.restore();
})
it('should filter out different author', async () => {
const logData = [];
const commits = [];
commits.push({
author: 'John Smith',
email: '[email protected]',
date: '2021-11-05 10:00:00',
message: 'Test Commit 1'
});
commits.push({
author: 'John Smith',
email: '[email protected]',
date: '2021-11-05 10:00:00',
message: 'Test Commit 2'
});
commits.push({
author: 'Jane Smith',
email: '[email protected]',
date: '2021-11-06 10:05:00',
message: 'Test Commit 3'
});
const out = GitOutOfHours.parseCommitData(logData, commits, 'John Smith');
expect(Object.keys(out).length).to.be.equal(1);
});
it('should filter out empty commit data', async () => {
const logData = [];
const commits = [];
const out = GitOutOfHours.parseCommitData(logData, commits, 'John Smith');
console.log("out", out);
expect(Object.keys(out).length).to.be.equal(0);
});
it('should filter out empty commit date', async () => {
const logData = [];
const commits = [];
commits.push({
author: 'John Smith',
email: '[email protected]',
message: 'Test Commit 2'
});
const out = GitOutOfHours.parseCommitData(logData, commits, 'John Smith');
console.log("out", out);
expect(Object.keys(out).length).to.be.equal(0);
});
it('should not GitOutOfHours.pluralise word', () => {
expect(GitOutOfHours.pluralise(1, 'dog')).to.be.equal('1 dog');
});
it('should GitOutOfHours.pluralise word', () => {
expect(GitOutOfHours.pluralise(2, 'dog')).to.be.equal('2 dogs');
});
it('should error when no results found', async () => {
await expect(GitOutOfHours.displayResults(null))
.to.eventually.be.rejectedWith('No commit history found');
});
it('should not error with valid display', async () => {
await expect(GitOutOfHours.displayResults({})).to.eventually.be.eq(false);
});
it('should not allow empty results key values', async () => {
const result = await GitOutOfHours.displayResults({ a: null, b: null}, { author: null });
expect(result).to.be.eq(false);
});
it('should not allow single history promise fail', async () => {
const promiseA = new Promise((resolve, reject) => resolve());
const promiseB = new Promise((resolve, reject) => reject('History promise fail'));
const result = await GitOutOfHours.runHistoryPromises([promiseA, promiseB]);
expect(result).to.be.eq(false);
});
it('should not allow valid history promises to display', async () => {
const promiseA = new Promise((resolve, reject) => resolve( { null: null, null2: null } ));
const result = await GitOutOfHours.runHistoryPromises([promiseA]);
expect(result).to.be.eq(false);
});
it('should display valid commit history data with author filter', async () => {
const commits = {
'2021-11-06 10:05:00': {
author: 'John Smith',
email: '[email protected]',
date: '10:00:00',
message: 'Test Commit 1'
}
};
const result = await GitOutOfHours.displayResults(commits, { author: 'John Smith'});
expect(result).to.be.eq(true);
});
it('should display valid commit history data without author', async () => {
const commits = {
'2021-11-06 10:05:00': {
author: 'John Smith',
email: '[email protected]',
date: '10:00:00',
message: 'Test Commit 1'
}
};
const result = await GitOutOfHours.displayResults(commits, { author: null });
expect(result).to.be.eq(true);
});
before(() => {
// Setup git repo example
shell.config.resetForTesting();
shell.cd(__dirname);
shell.rm('-rf', 'temp');
shell.mkdir('temp');
shell.cd('temp');
shell.exec('git init');
});
it('should return error with no object', async () => {
await expect(GitOutOfHours.gitoutofhours())
.to.eventually.be.rejectedWith('No parameter object specified');
});
it('should return error with empty object', async () => {
await expect(GitOutOfHours.gitoutofhours({}))
.to.eventually.be.rejectedWith('No parameter values specified');
});
it('should error with empty parameters', async () => {
await expect(GitOutOfHours.gitoutofhours({ dayCount: null }))
.to.eventually.be.rejectedWith('Amount of days to search for is required');
});
it('should error when amount of days is not a number', async () => {
await expect(GitOutOfHours.gitoutofhours({ dayCount: "This is not a number"}))
.to.eventually.be.rejectedWith('Amount of days needs to be a number');
});
it('should find no commits', () => {
shell.cd(__dirname + "/temp");
const log = shell.exec('git log -1 --format=format:"%H"').stdout;
expect(log).to.not.have.lengthOf.above(2);
});
it('should throw an error if there are no commits', async () => {
await expect(GitOutOfHours.gitoutofhours({ dayCount: 3}))
.to.eventually.be.rejectedWith('Error retrieving commits');
});
it('should create commits with no errors', async () => {
await shell.cd(__dirname);
await shell.cd('temp');
await shell.mkdir('helloworld');
await shell.cd('helloworld');
await fs.writeFileSync('test.txt', 'Some test content');
await shell.exec('git config user.name "gitoutofhours"');
const log = await shell.exec('git add --all && git commit -m"Initial commit"').stdout;
expect(log).to.be.a('string');
expect(log).to.have.lengthOf.above(2);
});
it('should find a commit', () => {
shell.cd(__dirname + "/temp");
const log = shell.exec('git log -1 --format=format:"%H"').stdout;
console.log(log);
expect(log).to.be.a('string');
expect(log).to.have.lengthOf.above(2);
});
it('should find a commit using gitoutofhours', async() => {
const getDataStub = sandbox.stub(GitOutOfHours, 'gitoutofhours').resolves(true);
const result = await GitOutOfHours.gitoutofhours({ dayCount: 2, skipTimeCheck: true });
expect(result).to.be.eq(true);
expect(getDataStub).to.have.been.calledOnceWith({ dayCount: 2, skipTimeCheck: true });
});
it('should find a commit using gitoutofhours with username', async () => {
const getDataStub = sandbox.stub(GitOutOfHours, 'gitoutofhours').resolves(true);
const result = await GitOutOfHours.gitoutofhours({ dayCount: 2, skipTimeCheck: false, author: 'gitoutofhours' });
expect(result).to.be.eq(true);
expect(getDataStub).to.have.been.calledOnceWith({ dayCount: 2, skipTimeCheck: false, author: 'gitoutofhours' });
});
it('should find not find a commit using gitoutofhours with username', async () => {
await GitOutOfHours.gitoutofhours({ dayCount: 2, skipTimeCheck: false, author: 'notgitoutofhours' }).then((out) => {
console.log('x', out);
}).catch(err => {
console.log('err', err);
});
});
it('should return ', async() => {
const getDataStub = sandbox.stub(GitOutOfHours, 'gitoutofhours').resolves(true);
const result = await GitOutOfHours.gitoutofhours({ dayCount: 2, skipTimeCheck: true });
expect(result).to.be.eq(true);
expect(getDataStub).to.have.been.calledOnceWith({ dayCount: 2, skipTimeCheck: true });
});
after('cleaning up', () => {
shell.config.resetForTesting();
shell.cd(__dirname);
shell.rm('-rf', 'temp');
});
});