Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/extensions/git/src/test/smoke.test.ts
3320 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 'mocha';
7
import assert from 'assert';
8
import { workspace, commands, window, Uri, WorkspaceEdit, Range, TextDocument, extensions, TabInputTextDiff } from 'vscode';
9
import * as cp from 'child_process';
10
import * as fs from 'fs';
11
import * as path from 'path';
12
import { GitExtension, API, Repository, Status } from '../api/git';
13
import { eventToPromise } from '../util';
14
15
suite('git smoke test', function () {
16
const cwd = workspace.workspaceFolders![0].uri.fsPath;
17
18
function file(relativePath: string) {
19
return path.join(cwd, relativePath);
20
}
21
22
function uri(relativePath: string) {
23
return Uri.file(file(relativePath));
24
}
25
26
async function open(relativePath: string) {
27
const doc = await workspace.openTextDocument(uri(relativePath));
28
await window.showTextDocument(doc);
29
return doc;
30
}
31
32
async function type(doc: TextDocument, text: string) {
33
const edit = new WorkspaceEdit();
34
const end = doc.lineAt(doc.lineCount - 1).range.end;
35
edit.replace(doc.uri, new Range(end, end), text);
36
await workspace.applyEdit(edit);
37
}
38
39
let git: API;
40
let repository: Repository;
41
42
suiteSetup(async function () {
43
fs.writeFileSync(file('app.js'), 'hello', 'utf8');
44
fs.writeFileSync(file('index.pug'), 'hello', 'utf8');
45
cp.execSync('git init -b main', { cwd });
46
cp.execSync('git config user.name testuser', { cwd });
47
cp.execSync('git config user.email [email protected]', { cwd });
48
cp.execSync('git config commit.gpgsign false', { cwd });
49
cp.execSync('git add .', { cwd });
50
cp.execSync('git commit -m "initial commit"', { cwd });
51
52
// make sure git is activated
53
const ext = extensions.getExtension<GitExtension>('vscode.git');
54
await ext?.activate();
55
git = ext!.exports.getAPI(1);
56
57
if (git.repositories.length === 0) {
58
const onDidOpenRepository = eventToPromise(git.onDidOpenRepository);
59
await commands.executeCommand('git.openRepository', cwd);
60
await onDidOpenRepository;
61
}
62
63
assert.strictEqual(git.repositories.length, 1);
64
assert.strictEqual(git.repositories[0].rootUri.fsPath, cwd);
65
66
repository = git.repositories[0];
67
});
68
69
test('reflects working tree changes', async function () {
70
await commands.executeCommand('workbench.view.scm');
71
72
const appjs = await open('app.js');
73
await type(appjs, ' world');
74
await appjs.save();
75
await repository.status();
76
77
assert.strictEqual(repository.state.workingTreeChanges.length, 1);
78
assert.strictEqual(repository.state.workingTreeChanges[0].uri.path, appjs.uri.path);
79
assert.strictEqual(repository.state.workingTreeChanges[0].status, Status.MODIFIED);
80
81
fs.writeFileSync(file('newfile.txt'), '');
82
const newfile = await open('newfile.txt');
83
await type(newfile, 'hey there');
84
await newfile.save();
85
await repository.status();
86
87
assert.strictEqual(repository.state.workingTreeChanges.length, 2);
88
assert.strictEqual(repository.state.workingTreeChanges[0].uri.path, appjs.uri.path);
89
assert.strictEqual(repository.state.workingTreeChanges[0].status, Status.MODIFIED);
90
assert.strictEqual(repository.state.workingTreeChanges[1].uri.path, newfile.uri.path);
91
assert.strictEqual(repository.state.workingTreeChanges[1].status, Status.UNTRACKED);
92
});
93
94
test('opens diff editor', async function () {
95
const appjs = uri('app.js');
96
await commands.executeCommand('git.openChange', appjs);
97
98
assert(window.activeTextEditor);
99
assert.strictEqual(window.activeTextEditor!.document.uri.path, appjs.path);
100
101
assert(window.tabGroups.activeTabGroup.activeTab);
102
assert(window.tabGroups.activeTabGroup.activeTab!.input instanceof TabInputTextDiff);
103
});
104
105
test('stages correctly', async function () {
106
const appjs = uri('app.js');
107
const newfile = uri('newfile.txt');
108
109
await repository.add([appjs.fsPath]);
110
111
assert.strictEqual(repository.state.indexChanges.length, 1);
112
assert.strictEqual(repository.state.indexChanges[0].uri.path, appjs.path);
113
assert.strictEqual(repository.state.indexChanges[0].status, Status.INDEX_MODIFIED);
114
115
assert.strictEqual(repository.state.workingTreeChanges.length, 1);
116
assert.strictEqual(repository.state.workingTreeChanges[0].uri.path, newfile.path);
117
assert.strictEqual(repository.state.workingTreeChanges[0].status, Status.UNTRACKED);
118
119
await repository.revert([appjs.fsPath]);
120
121
assert.strictEqual(repository.state.indexChanges.length, 0);
122
123
assert.strictEqual(repository.state.workingTreeChanges.length, 2);
124
assert.strictEqual(repository.state.workingTreeChanges[0].uri.path, appjs.path);
125
assert.strictEqual(repository.state.workingTreeChanges[0].status, Status.MODIFIED);
126
assert.strictEqual(repository.state.workingTreeChanges[1].uri.path, newfile.path);
127
assert.strictEqual(repository.state.workingTreeChanges[1].status, Status.UNTRACKED);
128
});
129
130
test('stages, commits changes and verifies outgoing change', async function () {
131
const appjs = uri('app.js');
132
const newfile = uri('newfile.txt');
133
134
await repository.add([appjs.fsPath]);
135
await repository.commit('second commit');
136
137
assert.strictEqual(repository.state.workingTreeChanges.length, 1);
138
assert.strictEqual(repository.state.workingTreeChanges[0].uri.path, newfile.path);
139
assert.strictEqual(repository.state.workingTreeChanges[0].status, Status.UNTRACKED);
140
141
assert.strictEqual(repository.state.indexChanges.length, 0);
142
143
await repository.commit('third commit', { all: true });
144
145
assert.strictEqual(repository.state.workingTreeChanges.length, 0);
146
assert.strictEqual(repository.state.indexChanges.length, 0);
147
});
148
149
test('rename/delete conflict', async function () {
150
await commands.executeCommand('workbench.view.scm');
151
152
const appjs = file('app.js');
153
const renamejs = file('rename.js');
154
155
await repository.createBranch('test', true);
156
157
// Delete file (test branch)
158
fs.unlinkSync(appjs);
159
await repository.commit('commit on test', { all: true });
160
161
await repository.checkout('main');
162
163
// Rename file (main branch)
164
fs.renameSync(appjs, renamejs);
165
await repository.commit('commit on main', { all: true });
166
167
try {
168
await repository.merge('test');
169
} catch (e) { }
170
171
assert.strictEqual(repository.state.mergeChanges.length, 1);
172
assert.strictEqual(repository.state.mergeChanges[0].status, Status.DELETED_BY_THEM);
173
174
assert.strictEqual(repository.state.workingTreeChanges.length, 0);
175
assert.strictEqual(repository.state.indexChanges.length, 0);
176
});
177
});
178
179