-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathgit.test.ts
More file actions
279 lines (242 loc) · 13.2 KB
/
Copy pathgit.test.ts
File metadata and controls
279 lines (242 loc) · 13.2 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
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/**
* This is a special test file.
* Due to git acting as a singleton, testing multiple instances of code that utilizes git asynchronously are practically guaranteed to cause race condition issues.
* This file handles the execution of these problematic tests by running them in a synchronous sequence via describe blocks.
*
* For testing direct or indirect usage of the git "singleton":
* 1. Export said tests into an executable function.
* 1a. Include additional supporting functions, constants, [before|After][All|Each], etc.
* 1b. Exporting multiple tests to be run independently is allowed.
* 1_Example:
* See git.test.ts -> `export const GitTestsUsingGit = function () { ... }`
* 2. Import said tests into the git.test.ts file.
* 2_Example:
* import { GitTestsUsingGit } from './git.test.js';
* 3. Run the imported executable function for said tests in its own describe block in the git.test.ts.
* 3a. Add a descriptive name for the describe block.
* 3_Example:
* describe(`Git.ts::Git class`, GitTestsUsingGit);
*/
import fs from 'fs';
import { Git } from './git.js';
import { CveCore } from '../cve/CveCore.js';
import * as _kTestCve0003Import from '../../test/pretend_github_repository/1970/0xxx/CVE-1970-0002.json';
const _kTestCve0003 = _kTestCve0003Import['default'] ?? _kTestCve0003Import;
import { FsUtils } from '../deprecated/fsUtils.js';
import { setup_TestGitRepository, cleanup_TestGitRepository } from './Delta.test.js';
// @todo: fix these tests, which very often runs twice automatically
describe.skip(`Git`, () => {
describe(`main tests`, () => {
const localDir: string = `test/pretend_github_repository`;
const kTestCve0001 = CveCore.fromCveMetadata(_kTestCve0003['cveMetadata']);
const srcDir = `test/fixtures/cve/5`;
const destDir = `test/pretend_github_repository/1970/0xxx`;
const destDir9 = `test/pretend_github_repository/1970/9xxx`;
beforeAll(() => {
console.log('running main tests');
fs.copyFileSync(`${srcDir}/CVE-1970-0999.json`, `${destDir}/CVE-1970-0996.json`);
fs.copyFileSync(`${srcDir}/CVE-1970-0999.json`, `${destDir}/CVE-1970-0997.json`);
// update existing CVE
fs.copyFileSync(
`${srcDir}/CVE-1970-0002u.json`,
`${destDir}/CVE-1970-0002.json`,
);
});
afterEach(async () => {
// this should not be necessary, but it seems to clean
// up the git.lock file so that we don't get errors
// about 2 git clients trying to access the same repository
// at the same time
const git = new Git({ localDir });
await git.status();
});
afterAll(() => {
console.log("done main test");
// Teardown (cleanup) after assertions
fs.copyFile(`${srcDir}/CVE-1970-0002.json`, `${destDir}/CVE-1970-0002.json`, Git.genericCallback);
FsUtils.rm(`${destDir}/CVE-1970-0996.json`);
FsUtils.rm(`${destDir}/CVE-1970-0997.json`);
});
it(`properly builds a Git object with default values`, () => {
const git = new Git();
// console.log(`git.localDir=${git.localDir}`);
// console.log(`process.cwd()=${process.cwd()}`);
expect(git.localDir).toEqual(`${process.cwd()}/${process.env.CVES_BASE_DIRECTORY}`);
});
it(`properly builds a Git object with specified initializers`, () => {
const git = new Git({ localDir });
// console.log(`git.localDir=${git.localDir}`);
expect(git.localDir).toMatch(localDir);
});
it(`properly displays git status`, async () => {
const git = new Git({ localDir });
const status = await git.status();
// console.log(`status=${JSON.stringify(status, null, 2)}`);
expect(status.not_added).toContain(`${localDir}/1970/0xxx/CVE-1970-0997.json`);
expect(status.modified).toContain(`${localDir}/1970/0xxx/CVE-1970-0002.json`);
});
// @todo: fix these tests
it.skip(`logCommitHashInWindow() properly logs files`, async () => {
const git = new Git({ localDir });
const retval = await git.logCommitHashInWindow("2023-02-16T00:00:00.000Z", "2023-08-21T23:59:59.999Z");
// console.log(`retval=${JSON.stringify(retval, null, 2)}`);
expect(retval[0]).toEqual(`fa298ddd39500963b357823b802dba6952bc71d0`);
expect(retval[retval.length - 1]).toEqual(`207b9f2b82908afbd8d9d2270969f6781f9d39e4`);
expect(retval.length).toBe(26);
});
it(`logCommitHashInWindow() properly handles case when there are no commits`, async () => {
const git = new Git({ localDir });
const retval = await git.logCommitHashInWindow("1970-02-16T00:00:00.000Z", "1970-03-21T23:59:59.999Z");
expect(retval.length).toBe(0);
});
// @todo this test isn't working because the git history is different than in original development project
// it(`logChangedFilenamesInTimeWindow() properly logs files`, async () => {
// const git = new Git({ localDir });
// const retval = await git.logChangedFilenamesInTimeWindow("2023-09-07T21:40:46.000Z", "2023-09-07T21:42:34.000Z");
// console.log(`logChangedFilenamesInTimeWindow() returned: ${JSON.stringify(retval, null, 2)}`);
// expect(retval[0]).toContain("example_test.json");
// expect(retval.length).toBe(1);
// });
it(`logChangedFilenamesInTimeWindow() properly handles case when no files were changed`, async () => {
const git = new Git({ localDir });
const retval = await git.logChangedFilenamesInTimeWindow("1970-02-16T00:00:00.000Z", "1970-03-21T23:59:59.999Z");
// console.log(`logChangedFilenamesInTimeWindow() returned: ${JSON.stringify(retval, null, 2)}`);
expect(retval.length).toBe(0);
});
// @todo: fix these tests
it.skip(`logDeltasInTimeWindow() properly logs files`, async () => {
const git = new Git({ localDir });
const retval = await git.logDeltasInTimeWindow("2022-02-16T00:00:00.000Z", "2023-08-21T23:59:59.999Z");
// console.log(`logDeltasInTimeWindow() returned: ${JSON.stringify(retval, null, 2)}`);
expect(retval.new[0].cveId.toString()).toContain("CVE-1970-0001");
expect(retval.new[4].cveId.toString()).toContain("CVE-1999-0001");
expect(retval.numberOfChanges).toBe(5);
});
it(`logDeltasInTimeWindow() properly handles case when no files were changed`, async () => {
const git = new Git({ localDir });
const retval = await git.logDeltasInTimeWindow("1970-02-16T00:00:00.000Z", "1970-03-21T23:59:59.999Z");
// console.log(`logDeltasInTimeWindow() returned: ${JSON.stringify(retval, null, 2)}`);
expect(retval.numberOfChanges).toBe(0);
});
});
// Delta functions that use the git class in a problematic manner to be tested synchronously: calculateDelta, toText
describe(`Delta tests using git`, () => {
it(`calculateDelta() properly calculates a Delta`, async () => {
await setup_TestGitRepository();
const delta = await Git.calculateDelta({}, `test/pretend_github_repository`);
console.log(`delta=${JSON.stringify(delta, null, 2)}`);
console.log(`delta.toText() -> ${delta.toText()}`);
expect(delta.numberOfChanges).toBe(2);
expect(delta.new.length).toBe(1);
expect(delta.updated.length).toBe(1);
await cleanup_TestGitRepository();
});
});
describe(`Delta test using git`, () => {
it(`toText() properly displays human readable text about this Delta`, async () => {
await setup_TestGitRepository();
const delta = await Git.calculateDelta({}, `test/pretend_github_repository`);
// console.log(`delta=${JSON.stringify(delta, null, 2)}`);
console.log(`delta.toText() -> ${delta.toText()}`);
expect(delta.toText()).toContain(`${delta.numberOfChanges} changes`);
expect(delta.toText()).toContain(`${delta.new.length} new`);
expect(delta.toText()).toContain(`${delta.updated.length} updated`);
await cleanup_TestGitRepository();
});
});
// Git functions that use the git class in a problematic manner to be tested synchronously: add, rm
describe(`Git tests using git`, () => {
const localDir: string = `test/pretend_github_repository`;
const srcDir = `test/fixtures/cve/5`;
const destDir = `test/pretend_github_repository/1970/0xxx`;
const cvesToAdd = {
'CVE-1970-0995': `CVE-1970-0999`,
'CVE-1970-0996': `CVE-1970-0999`,
'CVE-1970-0997': `CVE-1970-0999`,
};
const cvesToUpdate = {
'CVE-1970-0002': 'CVE-1970-0002u'
};
beforeEach(async () => {
// add new cves
for (let [toFN, fromFN] of Object.entries(cvesToAdd)) {
fs.copyFileSync(`${srcDir}/${fromFN}.json`, `${destDir}/${toFN}.json`);
}
// update existing CVE
for (let [toFN, fromFN] of Object.entries(cvesToUpdate)) {
fs.copyFileSync(`${srcDir}/${fromFN}.json`, `${destDir}/${toFN}.json`);
}
});
afterEach(async () => {
// this should not be necessary, but it seems to clean
// up the git.lock file so that we don't get errors
// about 2 git clients trying to access the same repository
// at the same time
const git = new Git({ localDir });
await git.status();
});
afterAll(() => {
// remove new cves
for (let fn of Object.keys(cvesToAdd)) {
FsUtils.rm(`${destDir}/${fn}.json`);
}
// revert existing CVE
for (let fn of Object.keys(cvesToUpdate)) {
fs.copyFileSync(`${srcDir}/${fn}.json`, `${destDir}/${fn}.json`);
}
});
it(`properly adds and rms files`, async () => {
const git = new Git({ localDir });
// await git.initForTest();
// test for single add
const respAdd = await git.add(`1970/0xxx/CVE-1970-0995.json`);
const status = await git.status();
expect(status.staged).toContain(`${localDir}/1970/0xxx/CVE-1970-0995.json`);
// cleanup single add
const resp2 = await git.rm(`${process.cwd()}/${destDir}/CVE-1970-0995.json`);
const status2 = await git.status();
expect(status2.staged).not.toContain(`${localDir}/1970/0xxx/CVE-1970-0995.json`);
// test for multi add
const respAddMult = await git.add([
`${process.cwd()}/${destDir}/CVE-1970-0996.json`,
`${process.cwd()}/${destDir}/CVE-1970-0997.json`
]);
const statusMult = await git.status();
expect(statusMult.staged).toContain(`${destDir}/CVE-1970-0996.json`);
expect(statusMult.staged).toContain(`${destDir}/CVE-1970-0997.json`);
// cleanup for multi add
const respMult2 = await git.rm([
`${process.cwd()}/${destDir}/CVE-1970-0996.json`,
`${process.cwd()}/${destDir}/CVE-1970-0997.json`
]);
const statusMult2 = await git.status();
expect(statusMult2.staged).not.toContain(`${destDir}/CVE-1970-0996.json`);
expect(statusMult2.staged).not.toContain(`${destDir}/CVE-1970-0997.json`);
});
// @todo this currently works, but all files are "new"
// @todo needs to set up git history for this repository's pretend_github_repository
// @todo: fix these tests
it.skip(`newDeltaFromGitHistory() properly builds a full Delta object from the file system`, async () => {
const delta = await Git.newDeltaFromGitHistory(
'2022-01-01T00:00:00.000Z',
'2023-04-28T00:00:00.000Z',
process.env.CVES_TEST_BASE_DIRECTORY
);
// console.log(`delta = ${JSON.stringify(delta, null, 2)}`);
expect(delta.numberOfChanges).toBe(5);
expect(delta.new.length).toBe(5);
expect(delta.updated.length).toBe(0);
expect(delta?.error?.length).toBe(0);
expect(delta.new[0].cveId.toString()).toBe(`CVE-1970-0001`);
});
it(`newDeltaFromGitHistory() properly defaults to now`, async () => {
await setup_TestGitRepository();
const startDate = "2023-08-16T00:00:00.000Z";
const delta = await Git.newDeltaFromGitHistory(startDate, undefined, process.env.CVES_TEST_BASE_DIRECTORY);
const deltaNow = await Git.newDeltaFromGitHistory(startDate, new Date().toISOString(), process.env.CVES_TEST_BASE_DIRECTORY);
expect(delta.numberOfChanges).toBe(deltaNow.numberOfChanges);
expect(delta.error).toEqual(deltaNow.error);
await cleanup_TestGitRepository();
});
});
});