Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/workbench/contrib/chat/test/electron-browser/pluginGitCommandService.test.ts
13406 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import assert from 'assert';
7
import { CancellationTokenSource } from '../../../../../base/common/cancellation.js';
8
import { URI } from '../../../../../base/common/uri.js';
9
import { ensureNoDisposablesAreLeakedInTestSuite } from '../../../../../base/test/common/utils.js';
10
import { ILocalGitService } from '../../../../../platform/git/common/localGitService.js';
11
import { NativePluginGitCommandService } from '../../electron-browser/pluginGitCommandService.js';
12
13
suite('NativePluginGitCommandService', () => {
14
const store = ensureNoDisposablesAreLeakedInTestSuite();
15
16
function createLocalGitStub(overrides?: Partial<ILocalGitService>): ILocalGitService {
17
return {
18
_serviceBrand: undefined,
19
clone: async () => { },
20
pull: async () => false,
21
checkout: async () => { },
22
revParse: async () => '',
23
fetch: async () => { },
24
revListCount: async () => 0,
25
cancel: async () => { },
26
...overrides,
27
} as ILocalGitService;
28
}
29
30
test('cloneRepository delegates to ILocalGitService', async () => {
31
const calls: string[] = [];
32
const service = new NativePluginGitCommandService(createLocalGitStub({
33
clone: async (_operationId, url, path, ref) => { calls.push(`clone:${url}:${path}:${ref}`); },
34
}));
35
36
const targetDir = URI.file('/tmp/repo');
37
await service.cloneRepository('https://github.com/test/repo.git', targetDir, 'main');
38
assert.deepStrictEqual(calls, [`clone:https://github.com/test/repo.git:${targetDir.fsPath}:main`]);
39
});
40
41
test('pull delegates to ILocalGitService and returns result', async () => {
42
const service = new NativePluginGitCommandService(createLocalGitStub({
43
pull: async () => true,
44
}));
45
46
const result = await service.pull(URI.file('/tmp/repo'));
47
assert.strictEqual(result, true);
48
});
49
50
test('checkout delegates to ILocalGitService with detached flag', async () => {
51
const calls: string[] = [];
52
const service = new NativePluginGitCommandService(createLocalGitStub({
53
checkout: async (_operationId, _path, treeish, detached) => { calls.push(`checkout:${treeish}:${detached}`); },
54
}));
55
56
await service.checkout(URI.file('/tmp/repo'), 'abc123', true);
57
assert.deepStrictEqual(calls, ['checkout:abc123:true']);
58
});
59
60
test('revParse delegates to ILocalGitService', async () => {
61
const service = new NativePluginGitCommandService(createLocalGitStub({
62
revParse: async () => 'abc123',
63
}));
64
65
const result = await service.revParse(URI.file('/tmp/repo'), 'HEAD');
66
assert.strictEqual(result, 'abc123');
67
});
68
69
test('fetch delegates to ILocalGitService', async () => {
70
const calls: string[] = [];
71
const service = new NativePluginGitCommandService(createLocalGitStub({
72
fetch: async (_operationId, path) => { calls.push(`fetch:${path}`); },
73
}));
74
75
const repoDir = URI.file('/tmp/repo');
76
await service.fetch(repoDir);
77
assert.deepStrictEqual(calls, [`fetch:${repoDir.fsPath}`]);
78
});
79
80
test('fetchRepository delegates to ILocalGitService.fetch', async () => {
81
const calls: string[] = [];
82
const service = new NativePluginGitCommandService(createLocalGitStub({
83
fetch: async (_operationId, path) => { calls.push(`fetch:${path}`); },
84
}));
85
86
const repoDir = URI.file('/tmp/repo');
87
await service.fetchRepository(repoDir);
88
assert.deepStrictEqual(calls, [`fetch:${repoDir.fsPath}`]);
89
});
90
91
test('revListCount delegates to ILocalGitService', async () => {
92
const service = new NativePluginGitCommandService(createLocalGitStub({
93
revListCount: async () => 5,
94
}));
95
96
const result = await service.revListCount(URI.file('/tmp/repo'), 'HEAD', '@{u}');
97
assert.strictEqual(result, 5);
98
});
99
100
test('cancellation token triggers cancel on local git service', async () => {
101
const cts = store.add(new CancellationTokenSource());
102
const cancelledIds: string[] = [];
103
let cloneResolve: (() => void) | undefined;
104
const service = new NativePluginGitCommandService(createLocalGitStub({
105
clone: () => new Promise(resolve => { cloneResolve = resolve; }),
106
cancel: async (id) => { cancelledIds.push(id); },
107
}));
108
109
const p = service.cloneRepository('https://github.com/test/repo.git', URI.file('/tmp/repo'), undefined, cts.token);
110
cts.cancel();
111
assert.strictEqual(cancelledIds.length, 1);
112
cloneResolve!();
113
await p;
114
});
115
});
116
117