Skip to content

Commit e040427

Browse files
author
Tony Spataro
committed
Create a basic integration test
1 parent 3b7ad6c commit e040427

File tree

7 files changed

+819
-7
lines changed

7 files changed

+819
-7
lines changed

.vscode/launch.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
{
66
"version": "0.2.0",
77
"configurations": [
8+
89
{
910
"name": "Run Extension",
1011
"type": "extensionHost",
@@ -17,5 +18,17 @@
1718
],
1819
"preLaunchTask": "${defaultBuildTask}"
1920
},
21+
{
22+
"name": "Run Extension Tests",
23+
"type": "extensionHost",
24+
"request": "launch",
25+
"runtimeExecutable": "${execPath}",
26+
"args": [
27+
"--extensionDevelopmentPath=${workspaceFolder}",
28+
"--extensionTestsPath=${workspaceFolder}/out/test/suite/index"
29+
],
30+
"outFiles": ["${workspaceFolder}/out/test/**/*.js"],
31+
"preLaunchTask": "${defaultBuildTask}"
32+
}
2033
]
2134
}

package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"description": "VSCode support for the syntax_tree gem",
55
"icon": "doc/logo.png",
66
"version": "0.2.1",
7+
"packageManager": "yarn@1.22.19",
78
"publisher": "ruby-syntax-tree",
89
"repository": {
910
"type": "git",
@@ -96,8 +97,13 @@
9697
"vscode-languageclient": "^8.0.1"
9798
},
9899
"devDependencies": {
100+
"@types/glob": "^7.1.1",
101+
"@types/mocha": "^9.1.1",
99102
"@types/node": "^18.0.0",
100103
"@types/vscode": "^1.68.0",
104+
"@vscode/test-electron": "^1.6.1",
105+
"glob": "^7.1.4",
106+
"mocha": "^9.1.1",
101107
"typescript": "^4.7.4",
102108
"vsce": "^2.9.2"
103109
}

src/test/runTest.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import * as path from 'path';
2+
3+
import { runTests } from '@vscode/test-electron';
4+
5+
async function main() {
6+
try {
7+
// The folder containing the Extension Manifest package.json
8+
// Passed to `--extensionDevelopmentPath`
9+
const extensionDevelopmentPath = path.resolve(__dirname, '../../');
10+
11+
// The path to the extension test script
12+
// Passed to --extensionTestsPath
13+
const extensionTestsPath = path.resolve(__dirname, './suite/index');
14+
15+
// Download VS Code, unzip it and run the integration test
16+
await runTests({ extensionDevelopmentPath, extensionTestsPath });
17+
} catch (err) {
18+
console.error('Failed to run tests');
19+
process.exit(1);
20+
}
21+
}
22+
23+
main();

src/test/suite/automation.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import * as assert from 'assert';
2+
import * as fs from 'fs';
3+
import * as os from 'os';
4+
import * as path from 'path';
5+
import { TextEncoder } from 'util';
6+
7+
import { Uri, commands, window, workspace } from 'vscode';
8+
9+
// Can't actually open this as a workspace (it kills the debug connection),
10+
// but we need somewhere to put temp files.
11+
const WORKSPACE_FOLDER = fs.mkdtempSync(`${os.tmpdir()}${path.sep}vscode-syntax-tree-`);
12+
13+
export async function closeAll() {
14+
while (window.activeTextEditor) {
15+
await commands.executeCommand('workbench.action.closeActiveEditor');
16+
}
17+
}
18+
19+
export async function createEditor(content: string, filename = 'test.rb') {
20+
const uri = Uri.file(`${WORKSPACE_FOLDER}${path.sep}${filename}`);
21+
await workspace.fs.writeFile(uri, new TextEncoder().encode(content));
22+
await window.showTextDocument(uri);
23+
assert.ok(window.activeTextEditor);
24+
return window.activeTextEditor;
25+
}
26+
27+
export async function formatDocument() {
28+
await commands.executeCommand('editor.action.formatDocument', 'ruby-syntax-tree.vscode-syntax-tree');
29+
}

src/test/suite/index.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import * as assert from 'assert';
2+
3+
import * as auto from './automation';
4+
5+
// can directly import extension if needed
6+
// import * as myExtension from '../../extension';
7+
8+
const UNFORMATTED = `class Foo; def bar; puts 'baz'; end; end`;
9+
10+
const FORMATTED = `class Foo
11+
def bar
12+
puts "baz"
13+
end
14+
end
15+
`;
16+
17+
suite('Syntax Tree', () => {
18+
test('Format Document', async () => {
19+
await auto.closeAll();
20+
const editor = await auto.createEditor(UNFORMATTED);
21+
await auto.formatDocument();
22+
assert.equal(editor.document.getText(), FORMATTED);
23+
});
24+
});

src/test/suite/index.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import * as path from 'path';
2+
import * as Mocha from 'mocha';
3+
import * as glob from 'glob';
4+
5+
export function run(): Promise<void> {
6+
// Create the mocha test
7+
const mocha = new Mocha({
8+
asyncOnly: true,
9+
color: true,
10+
timeout: 300000, // five minutes (since we can't say "no timeout")
11+
ui: 'tdd'
12+
});
13+
14+
const testsRoot = path.resolve(__dirname, '..');
15+
16+
return new Promise((c, e) => {
17+
glob('**/**.test.js', { cwd: testsRoot }, (err, files) => {
18+
if (err) {
19+
return e(err);
20+
}
21+
22+
// Add files to the test suite
23+
files.forEach(f => mocha.addFile(path.resolve(testsRoot, f)));
24+
25+
try {
26+
// Run the mocha test
27+
mocha.run(failures => {
28+
if (failures > 0) {
29+
e(new Error(`${failures} tests failed.`));
30+
} else {
31+
c();
32+
}
33+
});
34+
} catch (err) {
35+
console.error(err);
36+
e(err);
37+
}
38+
});
39+
});
40+
}

0 commit comments

Comments
 (0)