1+ import * as assert from 'assert' ;
2+ import * as path from 'path' ;
3+ import * as vscode from 'vscode' ;
4+ import { ShebangCodeLensProvider } from '../../client/providers/shebangCodeLensProvider'
5+
6+ import { initialize , IS_TRAVIS , closeActiveWindows } from '../initialize' ;
7+
8+ const autoCompPath = path . join ( __dirname , '..' , '..' , '..' , 'src' , 'test' , 'pythonFiles' , 'shebang' ) ;
9+ const fileShebang = path . join ( autoCompPath , 'shebang.py' ) ;
10+ const filePlain = path . join ( autoCompPath , 'plain.py' ) ;
11+
12+ var settings = vscode . workspace . getConfiguration ( "python" ) ;
13+ const origPythonPath = settings . get ( "pythonPath" ) ;
14+
15+ suite ( "Shebang detection" , ( ) => {
16+ suiteSetup ( async ( ) => {
17+ await initialize ( ) ;
18+ } ) ;
19+
20+ suiteTeardown ( async ( ) => {
21+ await vscode . workspace . getConfiguration ( "python" ) . update ( "pythonPath" , origPythonPath ) ;
22+ } ) ;
23+
24+ teardown ( ( ) => closeActiveWindows ( ) ) ;
25+ setup ( ( ) => {
26+ settings = vscode . workspace . getConfiguration ( "python" ) ;
27+ } ) ;
28+
29+ test ( "Shebang available, CodeLens showing" , async ( ) => {
30+ await settings . update ( "pythonPath" , "python" ) ;
31+ const editor = await openFile ( fileShebang ) ;
32+ const codeLenses = await setupCodeLens ( editor ) ;
33+
34+ assert . equal ( codeLenses . length , 1 , "No CodeLens available" ) ;
35+ let codeLens = codeLenses [ 0 ] ;
36+ assert ( codeLens . range . isSingleLine , 'Invalid CodeLens Range' ) ;
37+ assert . equal ( codeLens . command . command , 'python.setShebangInterpreter' ) ;
38+
39+ } ) ;
40+
41+ test ( "Shebang available, CodeLens hiding" , async ( ) => {
42+ await settings . update ( "pythonPath" , "/usr/bin/test" ) ;
43+ const editor = await openFile ( fileShebang ) ;
44+ const codeLenses = await setupCodeLens ( editor ) ;
45+ assert ( ! codeLenses , "CodeLens available although interpreters are equal" ) ;
46+
47+ } ) ;
48+
49+ test ( "Shebang missing, CodeLens hiding" , async ( ) => {
50+ const editor = await openFile ( filePlain ) ;
51+ const codeLenses = await setupCodeLens ( editor ) ;
52+ assert ( ! codeLenses , "CodeLens available although no shebang" ) ;
53+
54+ } ) ;
55+
56+ async function openFile ( fileName : string ) {
57+ const document = await vscode . workspace . openTextDocument ( fileName ) ;
58+ const editor = await vscode . window . showTextDocument ( document ) ;
59+ assert ( vscode . window . activeTextEditor , 'No active editor' ) ;
60+ return editor ;
61+ }
62+
63+ async function setupCodeLens ( editor : vscode . TextEditor ) {
64+ const document = editor . document ;
65+ const codeLensProvider = new ShebangCodeLensProvider ( ) ;
66+ const codeLenses = await codeLensProvider . provideCodeLenses ( document , null ) ;
67+ return codeLenses ;
68+ }
69+ } ) ;
0 commit comments