-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathgit.int.test.ts
More file actions
197 lines (173 loc) · 5.85 KB
/
git.int.test.ts
File metadata and controls
197 lines (173 loc) · 5.85 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
import { rm, stat, writeFile } from 'node:fs/promises';
import path from 'node:path';
import { type SimpleGit, simpleGit } from 'simple-git';
import {
initGitRepoWithRemote,
teardownTestFolder,
} from '@code-pushup/test-utils';
import { toUnixPath } from '../transform.js';
import {
getGitDefaultBranch,
getGitRoot,
guardAgainstLocalChanges,
safeCheckout,
toGitPath,
} from './git.js';
describe('git utils in a git repo', () => {
const baseDir = path.join(process.cwd(), 'tmp', 'git-tests');
const repoDir = path.join(baseDir, 'repo');
let emptyGit: SimpleGit;
beforeAll(async () => {
emptyGit = await initGitRepoWithRemote(simpleGit, {
baseDir,
baseBranch: 'master',
});
});
afterAll(async () => {
await teardownTestFolder(baseDir);
});
describe('without a branch and commits', () => {
it('getGitRoot should return git root in a set up repo', async () => {
await expect(getGitRoot(emptyGit)).resolves.toMatch(
/tmp\/git-tests\/repo$/,
);
});
});
describe('with a branch and commits clean', () => {
beforeAll(async () => {
await writeFile(path.join(repoDir, 'README.md'), '# hello-world\n');
await emptyGit.add('README.md');
await emptyGit.commit('Create README');
await emptyGit.branch(['feature-branch']);
await emptyGit.checkout(['master']);
});
afterAll(async () => {
await emptyGit.checkout(['master']);
await emptyGit.deleteLocalBranch('feature-branch');
});
it('should find Git root', async () => {
await expect(getGitRoot(emptyGit)).resolves.toBe(toUnixPath(repoDir));
});
it('should convert absolute path to relative Git path', async () => {
await expect(
toGitPath(path.join(repoDir, 'src', 'utils.ts'), emptyGit),
).resolves.toBe('src/utils.ts');
});
it('should convert relative Windows path to relative Git path', async () => {
await expect(
toGitPath(String.raw`Backend\API\Startup.cs`, emptyGit),
).resolves.toBe('../../../Backend/API/Startup.cs');
});
it('should keep relative Unix path as is (already a Git path)', async () => {
await expect(toGitPath('Backend/API/Startup.cs', emptyGit)).resolves.toBe(
'../../../Backend/API/Startup.cs',
);
});
it('guardAgainstLocalChanges should not throw if history is clean', async () => {
await expect(guardAgainstLocalChanges(emptyGit)).resolves.toBeUndefined();
});
it('safeCheckout should checkout feature-branch in clean state', async () => {
await expect(
safeCheckout('feature-branch', undefined, emptyGit),
).resolves.toBeUndefined();
await expect(emptyGit.branch()).resolves.toEqual(
expect.objectContaining({ current: 'feature-branch' }),
);
});
it('safeCheckout should throw if a given branch does not exist', async () => {
await expect(
safeCheckout('non-existing-branch', undefined, emptyGit),
).rejects.toThrow(
"pathspec 'non-existing-branch' did not match any file(s) known to git",
);
});
});
describe('with a branch and commits dirty', () => {
const newFilePath = path.join(repoDir, 'new-file.md');
beforeAll(async () => {
await writeFile(path.join(repoDir, 'README.md'), '# hello-world\n');
await emptyGit.add('README.md');
await emptyGit.commit('Create README');
await emptyGit.branch(['feature-branch']);
await emptyGit.checkout(['master']);
});
beforeEach(async () => {
await writeFile(newFilePath, '# New File\n');
});
afterEach(async () => {
try {
const s = await stat(newFilePath);
if (s.isFile()) {
await rm(newFilePath);
}
} catch {
// file not present (already cleaned)
}
});
afterAll(async () => {
await emptyGit.checkout(['master']);
await emptyGit.deleteLocalBranch('feature-branch');
});
it('safeCheckout should clean local changes and check out to feature-branch', async () => {
await expect(
safeCheckout('feature-branch', true, emptyGit),
).resolves.toBeUndefined();
await expect(emptyGit.branch()).resolves.toEqual(
expect.objectContaining({ current: 'feature-branch' }),
);
await expect(emptyGit.status()).resolves.toEqual(
expect.objectContaining({ files: [] }),
);
});
it('safeCheckout should throw if history is dirty', async () => {
await expect(safeCheckout('master', undefined, emptyGit)).rejects.toThrow(
`Working directory needs to be clean before we you can proceed. Commit your local changes or stash them: \n ${JSON.stringify(
{
not_added: ['new-file.md'],
files: [
{
path: 'new-file.md',
index: '?',
working_dir: '?',
},
],
},
null,
2,
)}`,
);
});
it('guardAgainstLocalChanges should throw if history is dirty', async () => {
let errorMsg;
try {
await guardAgainstLocalChanges(emptyGit);
} catch (error) {
errorMsg = (error as Error).message;
}
expect(errorMsg).toMatch(
'Working directory needs to be clean before we you can proceed. Commit your local changes or stash them:',
);
expect(errorMsg).toMatch(
JSON.stringify(
{
not_added: ['new-file.md'],
files: [
{
path: 'new-file.md',
index: '?',
working_dir: '?',
},
],
},
null,
2,
),
);
});
});
describe('getGitDefaultBranch', () => {
it('should resolve the default branch name from origin/HEAD', async () => {
await expect(getGitDefaultBranch(emptyGit)).resolves.toBe('master');
});
});
});