Skip to content

Commit e470998

Browse files
committed
Add automated tests, TravisCI test running, lauch tests task, npm test working for both Windows and Linux/OS X.
1 parent 92bf068 commit e470998

40 files changed

+740
-4
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
out
22
node_modules
3+
.vscode-test

.travis.yml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
sudo: true
2+
language: node_js
3+
node_js: "6"
4+
5+
os:
6+
- osx
7+
- linux
8+
9+
before_install:
10+
- if [ $TRAVIS_OS_NAME == "linux" ]; then
11+
export CXX="g++-4.9" CC="gcc-4.9" DISPLAY=:99.0;
12+
sh -e /etc/init.d/xvfb start;
13+
sleep 3;
14+
sudo apt-get install xclip;
15+
fi
16+
17+
install:
18+
- npm install
19+
- npm run vscode:prepublish
20+
21+
script:
22+
- npm run test:linuxOSX

.vscode/launch.json

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,24 @@
77
"type": "extensionHost",
88
"request": "launch",
99
"runtimeExecutable": "${execPath}",
10-
"args": ["--extensionDevelopmentPath=${workspaceRoot}" ],
10+
"args": [
11+
"--extensionDevelopmentPath=${workspaceRoot}"
12+
],
13+
"stopOnEntry": false,
14+
"sourceMaps": true,
15+
"outDir": "${workspaceRoot}/src",
16+
"preLaunchTask": "npm"
17+
},
18+
{
19+
"name": "Launch Tests",
20+
"type": "extensionHost",
21+
"request": "launch",
22+
"runtimeExecutable": "${execPath}",
23+
"args": [
24+
"${workspaceRoot}/test/sampleTestData",
25+
"--extensionDevelopmentPath=${workspaceRoot}",
26+
"--extensionTestsPath=${workspaceRoot}/test"
27+
],
1128
"stopOnEntry": false,
1229
"sourceMaps": true,
1330
"outDir": "${workspaceRoot}/src",

.vscode/tasks.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"showOutput": "silent",
2121

2222
// we run the custom script "compile" as defined in package.json
23-
//"args": ["run", "compile"],
23+
"args": ["run", "compile"],
2424

2525
// The tsc compiler is started in watching mode
2626
"isWatching": true,

.vscodeignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ screenshots/**
55
**/*.map
66
.gitignore
77
tsconfig.json
8+
out/test/**
9+
test/**

package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,14 @@
7878
},
7979
"scripts": {
8080
"vscode:prepublish": "node ./node_modules/vscode/bin/compile",
81-
"compile": "node ./node_modules/vscode/bin/compile -watch -p ./"
81+
"compile": "node ./node_modules/vscode/bin/compile -watch -p ./",
82+
"test:linuxOSX": "CODE_TESTS_WORKSPACE=$(cd \"$(dirname \"$0\")\" && pwd -P)/test/sampleTestData node ./node_modules/vscode/bin/test ",
83+
"test:windows": "cd && code -n %CD%/test/sampleTestData --extensionDevelopmentPath=%CD% --extensionTestsPath=%CD%/test --verbose",
84+
"test": "node ./testScript",
85+
"postinstall": "node ./node_modules/vscode/bin/install"
8286
},
8387
"devDependencies": {
88+
"chai": "^3.5.0",
8489
"typescript": "^1.6.2",
8590
"vscode": "0.11.13"
8691
},

test/extension.test.js

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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+
var VsCode = require('vscode');
6+
var Window = VsCode.window;
7+
var commands = VsCode.commands;
8+
var workspace = VsCode.workspace;
9+
var Position = VsCode.Position;
10+
var selection = VsCode.Selection;
11+
var extensions = VsCode.extensions;
12+
13+
var expect = require("chai").expect;
14+
var copy = require('copy-paste').copy;
15+
var paste = require('copy-paste').paste;
16+
var fs = require("fs");
17+
18+
19+
var extensionID = 'ziyasal.vscode-open-in-github';
20+
var extension = extensions.getExtension(extensionID);
21+
var testsPath = extension.extensionPath + '/test/';
22+
var fakeRepoPath = testsPath + 'sampleTestData/';
23+
var relativeSampleFilePath = 'sampleDirectory/sampleTestFile.txt';
24+
var absoluteSampleFilePath = fakeRepoPath + relativeSampleFilePath;
25+
26+
var fakeUserName = "testUser";
27+
var fakeRepoName = "testRepo";
28+
29+
30+
function testClipboard(expectedClipboardContent) {
31+
expect(paste()).to.be.equal(expectedClipboardContent, "Clipboard content doest not match.");
32+
}
33+
34+
function timeOut() {
35+
return new Promise(function (done) { setTimeout(done, 600) });
36+
}
37+
38+
function setClipboardTo(content) {
39+
return function (done) {
40+
copy(content, done);
41+
};
42+
}
43+
var clearClipboard = setClipboardTo("");
44+
var restoreClipboardContent = setClipboardTo;
45+
46+
suite('GitHub Tests', function () {
47+
var originalClipboardContent;
48+
49+
suiteSetup(function () {
50+
fs.renameSync(`${fakeRepoPath}git`, `${fakeRepoPath}.git`);
51+
originalClipboardContent = paste();
52+
return extension.activate()
53+
});
54+
55+
suiteTeardown(function () {
56+
fs.renameSync(`${fakeRepoPath}.git`, `${fakeRepoPath}git`);
57+
restoreClipboardContent(originalClipboardContent);
58+
});
59+
60+
setup(clearClipboard);
61+
62+
63+
test('Line', function () {
64+
var expectedLineResult = `https://github.com/${fakeUserName}/${fakeRepoName}/blob/master/${relativeSampleFilePath}#L2`;
65+
66+
return workspace.openTextDocument(absoluteSampleFilePath).then(function (workingDocument) {
67+
return Window.showTextDocument(workingDocument);
68+
})
69+
.then(function (editor) {
70+
editor.selection = new selection(new Position(1, 0), new Position(1, 0));
71+
})
72+
.then(function () {
73+
return commands.executeCommand("extension.copyGitHubLinkToClipboard");
74+
})
75+
.then(timeOut)
76+
.then(function () {
77+
testClipboard(expectedLineResult);
78+
});
79+
});
80+
81+
test('File', function () {
82+
var expectedFileResult = `https://github.com/${fakeUserName}/${fakeRepoName}/blob/master/${relativeSampleFilePath}`;
83+
//"workbench.files.action.focusFileExplorer"
84+
// How to get focus on file in explorer and call the command from context menu?
85+
this.skip();
86+
});
87+
88+
test('Repo', function () {
89+
var expectedRepoResult = `https://github.com/${fakeUserName}/${fakeRepoName}/tree/master`;
90+
91+
return commands.executeCommand("workbench.action.closeAllEditors")
92+
.then(timeOut)
93+
.then(function () {
94+
return commands.executeCommand("extension.copyGitHubLinkToClipboard");
95+
})
96+
.then(timeOut)
97+
.then(function () {
98+
testClipboard(expectedRepoResult);
99+
});
100+
});
101+
102+
test('Directory', function () {
103+
var expectedDirResult = `https://github.com/${fakeUserName}/${fakeRepoName}/blob/master/sampleDirectory`;
104+
// How to get focus on directory in explorer and call the command from context menu?
105+
this.skip();
106+
});
107+
});

test/index.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//
2+
// PLEASE DO NOT MODIFY / DELETE UNLESS YOU KNOW WHAT YOU ARE DOING
3+
//
4+
// This file is providing the test runner to use when running extension tests.
5+
// By default the test runner in use is Mocha based.
6+
//
7+
// You can provide your own test runner if you want to override it by exporting
8+
// a function run(testRoot: string, clb: (error:Error) => void) that the extension
9+
// host can call to run the tests. The test runner is expected to use console.log
10+
// to report the results back to the caller. When the tests are finished, return
11+
// a possible error to the callback or null if none.
12+
13+
var testRunner = require('vscode/lib/testrunner');
14+
15+
// You can directly control Mocha options by uncommenting the following lines
16+
// See https://github.com/mochajs/mocha/wiki/Using-mocha-programmatically#set-options for more info
17+
testRunner.configure({
18+
ui: 'tdd', // the TDD UI is being used in extension.test.ts (suite, test, etc.)
19+
useColors: true, // colored output from test results
20+
timeout: 10000
21+
});
22+
23+
module.exports = testRunner;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add dir

test/sampleTestData/git/FETCH_HEAD

Whitespace-only changes.

0 commit comments

Comments
 (0)