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+ } ) ;
0 commit comments