Skip to content

Commit 64fb427

Browse files
authored
Merge pull request #87 from edgardmessias/base_for_tests
Added base for unit tests (#74)
2 parents 2ddd8e5 + e630987 commit 64fb427

File tree

3 files changed

+178
-6
lines changed

3 files changed

+178
-6
lines changed

src/test/extension.test.ts

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,38 @@ import * as assert from "assert";
99
// You can import and use all API from the 'vscode' module
1010
// as well as import your extension to test it
1111
import * as vscode from "vscode";
12-
import * as myExtension from "../extension";
12+
import * as testUtil from "./testUtil";
13+
import { Uri } from "vscode";
14+
import { Svn } from "../svn";
15+
import { Model } from "../model";
16+
import { SvnFinder } from "../svnFinder";
1317

1418
// Defines a Mocha test suite to group tests of similar kind together
1519
suite("Extension Tests", () => {
16-
// Defines a Mocha unit test
17-
test("Something 1", () => {
18-
assert.equal(-1, [1, 2, 3].indexOf(5));
19-
assert.equal(-1, [1, 2, 3].indexOf(0));
20+
21+
//Before Each
22+
setup(async () => {
23+
});
24+
25+
teardown(() => {
26+
testUtil.destroyAllTempPaths();
27+
});
28+
29+
test("Find Repository", async () => {
30+
const svnFinder = new SvnFinder();
31+
const info = await svnFinder.findSvn();
32+
});
33+
34+
test("Try Open Repository", async () => {
35+
const repoUrl = await testUtil.createRepoServer();
36+
await testUtil.createStandardLayout(repoUrl);
37+
const checkoutDir = await testUtil.createRepoCheckout(repoUrl + "/trunk");
38+
39+
const svnFinder = new SvnFinder();
40+
const info = await svnFinder.findSvn();
41+
const svn = new Svn({ svnPath: info.path, version: info.version });
42+
const model = new Model(svn);
43+
await model.tryOpenRepository(checkoutDir);
44+
model.dispose();
2045
});
2146
});

src/test/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ if (!tty.getWindowSize) {
2323
}
2424

2525
let mocha = new Mocha({
26-
ui: "tdd"
26+
ui: "tdd",
27+
timeout: 10000
2728
});
2829

2930
mocha.useColors(true);

src/test/testUtil.ts

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
import * as cp from "child_process";
2+
import * as path from "path";
3+
import * as fs from "fs";
4+
import * as os from "os";
5+
import { Uri } from "vscode";
6+
import { SpawnOptions, ChildProcess } from "child_process";
7+
8+
const tempDir = os.tmpdir();
9+
var tempDirList: string[] = [];
10+
11+
export function spawn(
12+
command: string,
13+
args?: string[],
14+
options?: SpawnOptions
15+
): ChildProcess {
16+
let proc = cp.spawn(command, args, options);
17+
18+
// let fullCommand = "command: " + command;
19+
20+
// if (args) {
21+
// fullCommand += ' "' + args.join('" "') + '"';
22+
// }
23+
// console.log(fullCommand);
24+
25+
// proc.stdout.on("data", function(data) {
26+
// console.log("stdout: " + data.toString());
27+
// });
28+
29+
// proc.stderr.on("data", function(data) {
30+
// console.log("stderr: " + data.toString());
31+
// });
32+
33+
// proc.on("exit", function(code) {
34+
// console.log("child process exited with code " + code.toString());
35+
// });
36+
37+
return proc;
38+
}
39+
40+
export function newTempDir(prefix: string) {
41+
const fullpath = fs.mkdtempSync(path.join(tempDir, prefix));
42+
43+
tempDirList.push(fullpath);
44+
45+
return fullpath;
46+
}
47+
48+
export function createRepoServer() {
49+
return new Promise<string>((resolve, reject) => {
50+
const fullpath = newTempDir("svn_server_");
51+
const dirname = path.basename(fullpath);
52+
53+
if (fs.existsSync(fullpath)) {
54+
destroyPath(fullpath);
55+
}
56+
57+
let proc = spawn("svnadmin", ["create", dirname], { cwd: tempDir });
58+
59+
proc.once("exit", exitCode => {
60+
if (exitCode === 0) {
61+
const url = "file:///" + fullpath.replace(/\\/g, "/");
62+
resolve(url);
63+
}
64+
reject();
65+
});
66+
});
67+
}
68+
69+
export function importToRepoServer(
70+
url: string,
71+
path: string,
72+
message = "imported",
73+
cwd?: string
74+
) {
75+
return new Promise<void>((resolve, reject) => {
76+
let proc = spawn("svn", ["import", path, url, "-m", message], {
77+
cwd: cwd
78+
});
79+
80+
proc.once("exit", exitCode => {
81+
if (exitCode === 0) {
82+
resolve();
83+
}
84+
reject();
85+
});
86+
});
87+
}
88+
89+
export async function createStandardLayout(
90+
url: string,
91+
trunk = "trunk",
92+
branches = "branches",
93+
tags = "tags"
94+
) {
95+
const fullpath = newTempDir("svn_layout_");
96+
const dirname = path.basename(fullpath);
97+
98+
fs.mkdirSync(path.join(fullpath, trunk));
99+
fs.mkdirSync(path.join(fullpath, branches));
100+
fs.mkdirSync(path.join(fullpath, tags));
101+
102+
await importToRepoServer(url, fullpath, "Created Standard Layout");
103+
104+
destroyPath(fullpath);
105+
}
106+
107+
export function createRepoCheckout(url: string) {
108+
return new Promise<string>((resolve, reject) => {
109+
const fullpath = newTempDir("svn_checkout_");
110+
111+
let proc = spawn("svn", ["checkout", url, fullpath], { cwd: tempDir });
112+
113+
proc.once("exit", exitCode => {
114+
if (exitCode === 0) {
115+
resolve(fullpath);
116+
}
117+
reject();
118+
});
119+
});
120+
}
121+
122+
export function destroyPath(fullPath: string) {
123+
fullPath = fullPath.replace(/^file\:\/\/\//, "");
124+
125+
if (!fs.existsSync(fullPath)) {
126+
return false;
127+
}
128+
129+
if (!fs.lstatSync(fullPath).isDirectory()) {
130+
fs.unlinkSync(fullPath);
131+
return true;
132+
}
133+
134+
const files = fs.readdirSync(fullPath);
135+
for (let file of files) {
136+
destroyPath(path.join(fullPath, file));
137+
}
138+
fs.rmdirSync(fullPath);
139+
return true;
140+
}
141+
142+
export function destroyAllTempPaths() {
143+
for (let path of tempDirList) {
144+
destroyPath(path);
145+
}
146+
}

0 commit comments

Comments
 (0)