-
-
Notifications
You must be signed in to change notification settings - Fork 562
/
Copy pathinit-git.spec.ts
68 lines (50 loc) · 2.11 KB
/
init-git.spec.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { execSync } from 'node:child_process';
import fs from 'node:fs';
import os from 'node:os';
import path from 'node:path';
import { beforeEach, describe, expect, it } from 'vitest';
import { initGit } from '../../src/api/init-scripts/init-git';
let dir: string;
let dirID = Date.now();
const ensureTestDirIsNonexistent = async () => {
dir = path.resolve(os.tmpdir(), `electron-forge-git-test-${dirID}`);
dirID += 1;
await fs.promises.rm(dir, { recursive: true, force: true });
};
describe('init-git', () => {
beforeEach(async () => {
await ensureTestDirIsNonexistent();
await fs.promises.mkdir(dir, { recursive: true });
});
it('creates Git repository when run inside non-Git directory', async () => {
await initGit(dir);
const gitDir = path.join(dir, '.git');
expect(fs.existsSync(gitDir), 'the .git directory inside the folder').toEqual(true);
});
it('skips when run at root of Git repository', async () => {
execSync('git init', { cwd: dir });
const gitDir = path.join(dir, '.git');
const config = path.join(gitDir, 'config');
const statBefore = await fs.promises.lstat(config);
const before = statBefore.mtimeMs;
await initGit(dir);
const statAfter = await fs.promises.lstat(config);
const after = statAfter.mtimeMs;
expect(after, 'the config file in the repository').toEqual(before);
});
it('skips when run in subdirectory of Git repository', async () => {
execSync('git init', { cwd: dir });
const gitDir = path.join(dir, '.git');
const config = path.join(gitDir, 'config');
const statBefore = await fs.promises.lstat(config);
const before = statBefore.mtimeMs;
const subdir = path.join(dir, 'some', 'other', 'folder');
const innerGitDir = path.join(subdir, '.git');
await fs.promises.mkdir(subdir, { recursive: true });
await initGit(subdir);
const statAfter = await fs.promises.lstat(config);
const after = statAfter.mtimeMs;
expect(after, 'the config file in the repository').toEqual(before);
expect(fs.existsSync(innerGitDir), 'a nested .git directory inside the repository').toEqual(false);
});
});