Skip to content

Commit 236c7c5

Browse files
committed
Added commands tests
1 parent 8c59259 commit 236c7c5

File tree

4 files changed

+244
-4
lines changed

4 files changed

+244
-4
lines changed

src/commands.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,8 @@ export class SvnCommands implements IDisposable {
176176
}
177177

178178
@command("svn.fileOpen")
179-
fileOpen(resourceUri: Uri) {
180-
commands.executeCommand("vscode.open", resourceUri);
179+
async fileOpen(resourceUri: Uri) {
180+
await commands.executeCommand("vscode.open", resourceUri);
181181
}
182182

183183
@command("svn.commitWithMessage", { repository: true })

src/test/commands.test.ts

Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
//
2+
// Note: This example test is leveraging the Mocha test framework.
3+
// Please refer to their documentation on https://mochajs.org/ for help.
4+
//
5+
6+
// The module 'assert' provides assertion methods from node
7+
import * as assert from "assert";
8+
9+
// You can import and use all API from the 'vscode' module
10+
// as well as import your extension to test it
11+
import * as fs from "fs";
12+
import * as path from "path";
13+
import * as vscode from "vscode";
14+
import * as testUtil from "./testUtil";
15+
import {
16+
Uri,
17+
commands,
18+
workspace,
19+
window,
20+
InputBoxOptions,
21+
CancellationToken,
22+
QuickPickOptions,
23+
QuickPickItem
24+
} from "vscode";
25+
import { Svn } from "../svn";
26+
import { Model } from "../model";
27+
import { SvnFinder, ISvn } from "../svnFinder";
28+
import { Repository } from "../repository";
29+
import { timeout } from "../util";
30+
import { SvnResourceGroup } from "../resource";
31+
32+
// Defines a Mocha test suite to group tests of similar kind together
33+
suite("Commands Tests", () => {
34+
let repoUri: Uri;
35+
let checkoutDir: Uri;
36+
let model: Model;
37+
38+
suiteSetup(async () => {
39+
await testUtil.activeExtension();
40+
41+
repoUri = await testUtil.createRepoServer();
42+
await testUtil.createStandardLayout(testUtil.getSvnUrl(repoUri));
43+
checkoutDir = await testUtil.createRepoCheckout(
44+
testUtil.getSvnUrl(repoUri) + "/trunk"
45+
);
46+
47+
model = (await commands.executeCommand(
48+
"svn._getModel",
49+
checkoutDir
50+
)) as Model;
51+
52+
await model.tryOpenRepository(checkoutDir.fsPath);
53+
});
54+
55+
suiteTeardown(() => {
56+
model.openRepositories.forEach(repository => repository.dispose());
57+
testUtil.destroyAllTempPaths();
58+
});
59+
60+
test("File Open", async function() {
61+
const file = path.join(checkoutDir.fsPath, "new.txt");
62+
fs.writeFileSync(file, "test");
63+
64+
await commands.executeCommand("svn.fileOpen", Uri.file(file));
65+
});
66+
67+
test("Add File", async function() {
68+
const repository = model.getRepository(checkoutDir) as Repository;
69+
70+
await commands.executeCommand("svn.refresh");
71+
assert.equal(repository.unversioned.resourceStates.length, 1);
72+
assert.equal(repository.changes.resourceStates.length, 0);
73+
74+
const resource = repository.unversioned.resourceStates[0];
75+
76+
await commands.executeCommand("svn.add", resource);
77+
78+
assert.equal(repository.unversioned.resourceStates.length, 0);
79+
assert.equal(repository.changes.resourceStates.length, 1);
80+
});
81+
82+
test("Commit File", async function() {
83+
const repository = model.getRepository(checkoutDir) as Repository;
84+
repository.inputBox.value = "First Commit";
85+
86+
await commands.executeCommand("svn.commitWithMessage");
87+
});
88+
89+
test("Update", async function() {
90+
await commands.executeCommand("svn.update");
91+
});
92+
93+
test("Show Log", async function() {
94+
await commands.executeCommand("svn.log");
95+
});
96+
97+
test("Open Changes", async function() {
98+
const file = path.join(checkoutDir.fsPath, "new.txt");
99+
fs.writeFileSync(file, "test 2");
100+
const uri = Uri.file(file);
101+
102+
await commands.executeCommand("svn.refresh");
103+
await commands.executeCommand("svn.openChangeBase", uri);
104+
await commands.executeCommand("svn.openChangeHead", uri);
105+
});
106+
107+
test("Open File", async function() {
108+
const file = path.join(checkoutDir.fsPath, "new.txt");
109+
const uri = Uri.file(file);
110+
111+
await commands.executeCommand("svn.openFile", uri);
112+
await commands.executeCommand("svn.openHEADFile", uri);
113+
});
114+
115+
test("Open Diff (Double click o source control)", async function() {
116+
const repository = model.getRepository(checkoutDir) as Repository;
117+
118+
await commands.executeCommand("svn.refresh");
119+
assert.equal(repository.changes.resourceStates.length, 1);
120+
121+
const resource = repository.changes.resourceStates[0];
122+
123+
await commands.executeCommand("svn.openResourceBase", resource);
124+
await commands.executeCommand("svn.openResourceHead", resource);
125+
});
126+
127+
test("Add Changelist", async function() {
128+
const repository = model.getRepository(checkoutDir) as Repository;
129+
130+
await commands.executeCommand("svn.refresh");
131+
assert.equal(repository.changes.resourceStates.length, 1);
132+
133+
const resource = repository.changes.resourceStates[0];
134+
135+
testUtil.overrideNextShowQuickPick(0);
136+
testUtil.overrideNextShowInputBox("changelist-test");
137+
138+
await commands.executeCommand("svn.addChangelist", resource);
139+
assert.ok(repository.changelists.has("changelist-test"));
140+
});
141+
142+
test("Remove Changelist", async function() {
143+
const repository = model.getRepository(checkoutDir) as Repository;
144+
145+
const group = repository.changelists.get(
146+
"changelist-test"
147+
) as SvnResourceGroup;
148+
const resource = group.resourceStates[0];
149+
150+
await commands.executeCommand("svn.removeChangelist", resource);
151+
assert.equal(group.resourceStates.length, 0);
152+
});
153+
154+
test("Show Patch", async function() {
155+
await commands.executeCommand("svn.patch");
156+
});
157+
158+
test("Commit Selected File", async function() {
159+
const repository = model.getRepository(checkoutDir) as Repository;
160+
161+
await commands.executeCommand("svn.refresh");
162+
assert.equal(repository.changes.resourceStates.length, 1);
163+
164+
const resource = repository.changes.resourceStates[0];
165+
166+
testUtil.overrideNextShowInputBox("Second Commit");
167+
await commands.executeCommand("svn.commit", resource);
168+
169+
assert.equal(repository.changes.resourceStates.length, 0);
170+
});
171+
172+
test("Commit File", async function() {
173+
const repository = model.getRepository(checkoutDir) as Repository;
174+
repository.inputBox.value = "First Commit";
175+
176+
await commands.executeCommand("svn.commitWithMessage");
177+
});
178+
179+
test("New Branch", async function() {
180+
testUtil.overrideNextShowInputBox("test");
181+
await commands.executeCommand("svn.branch");
182+
183+
const repository = model.getRepository(checkoutDir) as Repository;
184+
assert.equal(await repository.getCurrentBranch(), "test");
185+
});
186+
187+
test("Switch Branch", async function() {
188+
testUtil.overrideNextShowQuickPick(1);
189+
await commands.executeCommand("svn.switchBranch");
190+
191+
const repository = model.getRepository(checkoutDir) as Repository;
192+
assert.equal(await repository.getCurrentBranch(), "trunk");
193+
});
194+
});

src/test/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ if (!tty.getWindowSize) {
2424

2525
let mocha = new Mocha({
2626
ui: "tdd",
27-
timeout: 10000
27+
timeout: 30000
2828
});
2929

3030
mocha.useColors(true);

src/test/testUtil.ts

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ import * as cp from "child_process";
22
import * as path from "path";
33
import * as fs from "fs";
44
import * as os from "os";
5-
import { Uri, extensions } from "vscode";
5+
import { Uri, extensions, window } from "vscode";
66
import { SpawnOptions, ChildProcess } from "child_process";
77
import { timeout } from "../util";
8+
import { type } from "os";
89

910
const tempDir = os.tmpdir();
1011
let tempDirList: string[] = [];
@@ -177,3 +178,48 @@ export function activeExtension() {
177178
}
178179
});
179180
}
181+
182+
const overridesShowInputBox: any[] = [];
183+
184+
export function overrideNextShowInputBox(value: any) {
185+
overridesShowInputBox.push(value);
186+
}
187+
188+
const originalShowInputBox = window.showInputBox;
189+
190+
window.showInputBox = (options?: any, token?: any) => {
191+
const next = overridesShowInputBox.shift();
192+
if (typeof next === "undefined") {
193+
return originalShowInputBox.call(null, arguments);
194+
}
195+
return new Promise((resolve, reject) => {
196+
resolve(next);
197+
});
198+
};
199+
200+
const overridesShowQuickPick: any[] = [];
201+
202+
export function overrideNextShowQuickPick(value: any) {
203+
overridesShowQuickPick.push(value);
204+
}
205+
206+
const originalShowQuickPick = window.showQuickPick;
207+
208+
window.showQuickPick = (
209+
items: any[] | Thenable<any[]>,
210+
options?: any,
211+
token?: any
212+
): Thenable<any | undefined> => {
213+
let next = overridesShowQuickPick.shift();
214+
if (typeof next === "undefined") {
215+
return originalShowQuickPick.call(null, arguments);
216+
}
217+
218+
if (typeof next === "number" && Array.isArray(items)) {
219+
next = items[next];
220+
}
221+
222+
return new Promise((resolve, reject) => {
223+
resolve(next);
224+
});
225+
};

0 commit comments

Comments
 (0)