11import * as assert from 'assert' ;
22import * as path from 'path' ;
33import * as vscode from 'vscode' ;
4- import { ShebangCodeLensProvider } from '../../client/providers/shebangCodeLensProvider'
4+ import * as child_process from 'child_process' ;
5+ import { IS_WINDOWS } from '../../client/common/configSettings' ;
6+ import { ShebangCodeLensProvider } from '../../client/providers/shebangCodeLensProvider' ;
57
68import { initialize , IS_TRAVIS , closeActiveWindows } from '../initialize' ;
9+ import { getFirstNonEmptyLineFromMultilineString } from '../../client/interpreter/helpers' ;
710
811const autoCompPath = path . join ( __dirname , '..' , '..' , '..' , 'src' , 'test' , 'pythonFiles' , 'shebang' ) ;
912const fileShebang = path . join ( autoCompPath , 'shebang.py' ) ;
13+ const fileShebangEnv = path . join ( autoCompPath , 'shebangEnv.py' ) ;
14+ const fileShebangInvalid = path . join ( autoCompPath , 'shebangInvalid.py' ) ;
1015const filePlain = path . join ( autoCompPath , 'plain.py' ) ;
1116
12- var settings = vscode . workspace . getConfiguration ( " python" ) ;
13- const origPythonPath = settings . get ( " pythonPath" ) ;
17+ var settings = vscode . workspace . getConfiguration ( ' python' ) ;
18+ const origPythonPath = settings . get ( ' pythonPath' ) ;
1419
15- suite ( "Shebang detection" , ( ) => {
16- suiteSetup ( async ( ) => {
17- await initialize ( ) ;
20+ suite ( 'Shebang detection' , ( ) => {
21+ suiteSetup ( ( ) => initialize ( ) ) ;
22+ suiteTeardown ( ( ) => vscode . workspace . getConfiguration ( 'python' ) . update ( 'pythonPath' , origPythonPath ) ) ;
23+ teardown ( async ( ) => {
24+ await closeActiveWindows ( ) ;
25+ await vscode . workspace . getConfiguration ( 'python' ) . update ( 'pythonPath' , origPythonPath ) ;
1826 } ) ;
1927
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" ) ;
28+ test ( 'Shebang available, CodeLens showing' , async ( ) => {
29+ await settings . update ( 'pythonPath' , 'someUnknownInterpreter' ) ;
3130 const editor = await openFile ( fileShebang ) ;
3231 const codeLenses = await setupCodeLens ( editor ) ;
3332
34- assert . equal ( codeLenses . length , 1 , " No CodeLens available" ) ;
33+ assert . equal ( codeLenses . length , 1 , ' No CodeLens available' ) ;
3534 let codeLens = codeLenses [ 0 ] ;
3635 assert ( codeLens . range . isSingleLine , 'Invalid CodeLens Range' ) ;
3736 assert . equal ( codeLens . command . command , 'python.setShebangInterpreter' ) ;
3837
3938 } ) ;
4039
41- test ( "Shebang available, CodeLens hiding" , async ( ) => {
42- await settings . update ( "pythonPath" , "/usr/bin/test" ) ;
40+ test ( 'Shebang available, CodeLens hiding' , async ( ) => {
41+ const pythonPath = await getFullyQualifiedPathToInterpreter ( 'python' ) ;
42+ await settings . update ( 'pythonPath' , pythonPath ) ;
4343 const editor = await openFile ( fileShebang ) ;
4444 const codeLenses = await setupCodeLens ( editor ) ;
45- assert ( ! codeLenses , " CodeLens available although interpreters are equal" ) ;
45+ assert . equal ( codeLenses . length , 0 , ' CodeLens available although interpreters are equal' ) ;
4646
4747 } ) ;
4848
49- test ( "Shebang missing, CodeLens hiding" , async ( ) => {
50- const editor = await openFile ( filePlain ) ;
49+ test ( 'Shebang not available (invalid shebang)' , async ( ) => {
50+ const pythonPath = await getFullyQualifiedPathToInterpreter ( 'python' ) ;
51+ await settings . update ( 'pythonPath' , pythonPath ) ;
52+ const editor = await openFile ( fileShebangInvalid ) ;
5153 const codeLenses = await setupCodeLens ( editor ) ;
52- assert ( ! codeLenses , "CodeLens available although no shebang" ) ;
54+ assert . equal ( codeLenses . length , 0 , 'CodeLens available although shebang is invalid' ) ;
55+ } ) ;
56+
57+ if ( ! IS_WINDOWS ) {
58+ test ( 'Shebang available, CodeLens showing with env' , async ( ) => {
59+ await settings . update ( 'pythonPath' , 'p1' ) ;
60+ const editor = await openFile ( fileShebangEnv ) ;
61+ const codeLenses = await setupCodeLens ( editor ) ;
5362
63+ assert . equal ( codeLenses . length , 1 , 'No CodeLens available' ) ;
64+ let codeLens = codeLenses [ 0 ] ;
65+ assert ( codeLens . range . isSingleLine , 'Invalid CodeLens Range' ) ;
66+ assert . equal ( codeLens . command . command , 'python.setShebangInterpreter' ) ;
67+
68+ } ) ;
69+
70+ test ( 'Shebang available, CodeLens hiding with env' , async ( ) => {
71+ const pythonPath = await getFullyQualifiedPathToInterpreter ( 'python' ) ;
72+ await settings . update ( 'pythonPath' , pythonPath ) ;
73+ const editor = await openFile ( fileShebangEnv ) ;
74+ const codeLenses = await setupCodeLens ( editor ) ;
75+ assert . equal ( codeLenses . length , 0 , 'CodeLens available although interpreters are equal' ) ;
76+ } ) ;
77+ }
78+
79+ test ( 'Shebang missing, CodeLens hiding' , async ( ) => {
80+ const editor = await openFile ( filePlain ) ;
81+ const codeLenses = await setupCodeLens ( editor ) ;
82+ assert . equal ( codeLenses . length , 0 , 'CodeLens available although no shebang' ) ;
5483 } ) ;
5584
5685 async function openFile ( fileName : string ) {
@@ -59,11 +88,18 @@ suite("Shebang detection", () => {
5988 assert ( vscode . window . activeTextEditor , 'No active editor' ) ;
6089 return editor ;
6190 }
91+ async function getFullyQualifiedPathToInterpreter ( pythonPath : string ) {
92+ return new Promise < string > ( resolve => {
93+ child_process . execFile ( pythonPath , [ '-c' , 'import sys;print(sys.executable)' ] , ( _ , stdout ) => {
94+ resolve ( getFirstNonEmptyLineFromMultilineString ( stdout ) ) ;
95+ } ) ;
96+ } ) . catch ( ( ) => undefined ) ;
97+ }
6298
6399 async function setupCodeLens ( editor : vscode . TextEditor ) {
64100 const document = editor . document ;
65101 const codeLensProvider = new ShebangCodeLensProvider ( ) ;
66102 const codeLenses = await codeLensProvider . provideCodeLenses ( document , null ) ;
67103 return codeLenses ;
68104 }
69- } ) ;
105+ } ) ;
0 commit comments