-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathjestHelpers.ts
53 lines (46 loc) · 1.35 KB
/
jestHelpers.ts
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
import { randomBytes } from 'crypto';
import path from 'path';
import fs from 'fs';
import os from 'os';
import init from './commands/init';
import * as utils from './utils';
import { Signature } from './objects/signature';
const root = process.cwd();
/**
* Creates a temp directory to be used as git repo.
* The cwd of the process is also changed to the temp directory.
*
* @export
* @returns {string}
*/
export function createTempGitRepo(): string {
const gitRoot = path.join(os.tmpdir(), randomBytes(2).toString('hex'));
beforeAll(() => {
// Create a new temp dir and change the cwd of the process.
fs.mkdirSync(gitRoot);
process.chdir(gitRoot);
init();
});
afterAll(() => {
// Move the process back to root before cleanup to prevent ENOENT error.
process.chdir(root);
fs.rmSync(gitRoot, { recursive: true, force: true });
});
return gitRoot;
}
export function createDummyFile(): {
text: string;
filePath: string;
expectedHash: string;
} {
const text = 'what is up, doc?';
const filePath = './temp.txt';
fs.writeFileSync(filePath, text);
const expectedHash = 'bd9dbf5aae1a3862dd1526723246b20206e5fc37';
return { text, filePath, expectedHash };
}
export function mockGetSignature() {
jest.spyOn(utils, 'getSignature').mockImplementation(() => {
return new Signature('John Doe', '[email protected]');
});
}