33
44import { expect } from 'chai' ;
55import * as path from 'path' ;
6+ import * as sinon from 'sinon' ;
67import * as TypeMoq from 'typemoq' ;
78import {
89 Disposable ,
@@ -22,6 +23,7 @@ import { IDisposableRegistry } from '../../../client/common/types';
2223import { IServiceContainer } from '../../../client/ioc/types' ;
2324import { ITerminalAutoActivation } from '../../../client/terminals/types' ;
2425import { createPythonInterpreter } from '../../utils/interpreters' ;
26+ import * as workspaceApis from '../../../client/common/vscodeApis/workspaceApis' ;
2527
2628suite ( 'Terminal Service' , ( ) => {
2729 let service : TerminalService ;
@@ -37,6 +39,9 @@ suite('Terminal Service', () => {
3739 let terminalShellIntegration : TypeMoq . IMock < TerminalShellIntegration > ;
3840 let onDidEndTerminalShellExecutionEmitter : EventEmitter < TerminalShellExecutionEndEvent > ;
3941 let event : TerminalShellExecutionEndEvent ;
42+ let getConfigurationStub : sinon . SinonStub ;
43+ let pythonConfig : TypeMoq . IMock < WorkspaceConfiguration > ;
44+ let editorConfig : TypeMoq . IMock < WorkspaceConfiguration > ;
4045
4146 setup ( ( ) => {
4247 terminal = TypeMoq . Mock . ofType < VSCodeTerminal > ( ) ;
@@ -88,12 +93,22 @@ suite('Terminal Service', () => {
8893 mockServiceContainer . setup ( ( c ) => c . get ( IWorkspaceService ) ) . returns ( ( ) => workspaceService . object ) ;
8994 mockServiceContainer . setup ( ( c ) => c . get ( ITerminalActivator ) ) . returns ( ( ) => terminalActivator . object ) ;
9095 mockServiceContainer . setup ( ( c ) => c . get ( ITerminalAutoActivation ) ) . returns ( ( ) => terminalAutoActivator . object ) ;
96+ getConfigurationStub = sinon . stub ( workspaceApis , 'getConfiguration' ) ;
97+ pythonConfig = TypeMoq . Mock . ofType < WorkspaceConfiguration > ( ) ;
98+ editorConfig = TypeMoq . Mock . ofType < WorkspaceConfiguration > ( ) ;
99+ getConfigurationStub . callsFake ( ( section : string ) => {
100+ if ( section === 'python' ) {
101+ return pythonConfig . object ;
102+ }
103+ return editorConfig . object ;
104+ } ) ;
91105 } ) ;
92106 teardown ( ( ) => {
93107 if ( service ) {
94108 service . dispose ( ) ;
95109 }
96110 disposables . filter ( ( item ) => ! ! item ) . forEach ( ( item ) => item . dispose ( ) ) ;
111+ sinon . restore ( ) ;
97112 } ) ;
98113
99114 test ( 'Ensure terminal is disposed' , async ( ) => {
@@ -103,13 +118,15 @@ suite('Terminal Service', () => {
103118 const os : string = 'windows' ;
104119 service = new TerminalService ( mockServiceContainer . object ) ;
105120 const shellPath = 'powershell.exe' ;
121+ // TODO: switch over legacy Terminal code to use workspace getConfiguration from workspaceApis instead of directly from vscode.workspace
106122 workspaceService
107123 . setup ( ( w ) => w . getConfiguration ( TypeMoq . It . isValue ( 'terminal.integrated.shell' ) ) )
108124 . returns ( ( ) => {
109125 const workspaceConfig = TypeMoq . Mock . ofType < WorkspaceConfiguration > ( ) ;
110126 workspaceConfig . setup ( ( c ) => c . get ( os ) ) . returns ( ( ) => shellPath ) ;
111127 return workspaceConfig . object ;
112128 } ) ;
129+ pythonConfig . setup ( ( p ) => p . get ( 'terminal.shellIntegration.enabled' ) ) . returns ( ( ) => false ) ;
113130
114131 platformService . setup ( ( p ) => p . isWindows ) . returns ( ( ) => os === 'windows' ) ;
115132 platformService . setup ( ( p ) => p . isLinux ) . returns ( ( ) => os === 'linux' ) ;
@@ -134,6 +151,7 @@ suite('Terminal Service', () => {
134151 } ) ;
135152
136153 test ( 'Ensure command is sent to terminal and it is shown' , async ( ) => {
154+ pythonConfig . setup ( ( p ) => p . get ( 'terminal.shellIntegration.enabled' ) ) . returns ( ( ) => false ) ;
137155 terminalHelper
138156 . setup ( ( helper ) => helper . getEnvironmentActivationCommands ( TypeMoq . It . isAny ( ) , TypeMoq . It . isAny ( ) ) )
139157 . returns ( ( ) => Promise . resolve ( undefined ) ) ;
@@ -171,6 +189,69 @@ suite('Terminal Service', () => {
171189 terminal . verify ( ( t ) => t . sendText ( TypeMoq . It . isValue ( textToSend ) ) , TypeMoq . Times . exactly ( 1 ) ) ;
172190 } ) ;
173191
192+ test ( 'Ensure sendText is used when Python shell integration is disabled' , async ( ) => {
193+ pythonConfig
194+ . setup ( ( p ) => p . get ( 'terminal.shellIntegration.enabled' ) )
195+ . returns ( ( ) => false )
196+ . verifiable ( TypeMoq . Times . once ( ) ) ;
197+
198+ terminalHelper
199+ . setup ( ( helper ) => helper . getEnvironmentActivationCommands ( TypeMoq . It . isAny ( ) , TypeMoq . It . isAny ( ) ) )
200+ . returns ( ( ) => Promise . resolve ( undefined ) ) ;
201+ service = new TerminalService ( mockServiceContainer . object ) ;
202+ const textToSend = 'Some Text' ;
203+ terminalHelper . setup ( ( h ) => h . identifyTerminalShell ( TypeMoq . It . isAny ( ) ) ) . returns ( ( ) => TerminalShellType . bash ) ;
204+ terminalManager . setup ( ( t ) => t . createTerminal ( TypeMoq . It . isAny ( ) ) ) . returns ( ( ) => terminal . object ) ;
205+
206+ service . ensureTerminal ( ) ;
207+ service . executeCommand ( textToSend , true ) ;
208+
209+ terminal . verify ( ( t ) => t . show ( TypeMoq . It . isValue ( true ) ) , TypeMoq . Times . exactly ( 1 ) ) ;
210+ terminal . verify ( ( t ) => t . sendText ( TypeMoq . It . isValue ( textToSend ) ) , TypeMoq . Times . exactly ( 1 ) ) ;
211+ } ) ;
212+
213+ test ( 'Ensure sendText is called when terminal.shellIntegration enabled but Python shell integration disabled' , async ( ) => {
214+ pythonConfig
215+ . setup ( ( p ) => p . get ( 'terminal.shellIntegration.enabled' ) )
216+ . returns ( ( ) => false )
217+ . verifiable ( TypeMoq . Times . once ( ) ) ;
218+
219+ terminalHelper
220+ . setup ( ( helper ) => helper . getEnvironmentActivationCommands ( TypeMoq . It . isAny ( ) , TypeMoq . It . isAny ( ) ) )
221+ . returns ( ( ) => Promise . resolve ( undefined ) ) ;
222+ service = new TerminalService ( mockServiceContainer . object ) ;
223+ const textToSend = 'Some Text' ;
224+ terminalHelper . setup ( ( h ) => h . identifyTerminalShell ( TypeMoq . It . isAny ( ) ) ) . returns ( ( ) => TerminalShellType . bash ) ;
225+ terminalManager . setup ( ( t ) => t . createTerminal ( TypeMoq . It . isAny ( ) ) ) . returns ( ( ) => terminal . object ) ;
226+
227+ service . ensureTerminal ( ) ;
228+ service . executeCommand ( textToSend , true ) ;
229+
230+ terminal . verify ( ( t ) => t . show ( TypeMoq . It . isValue ( true ) ) , TypeMoq . Times . exactly ( 1 ) ) ;
231+ terminal . verify ( ( t ) => t . sendText ( TypeMoq . It . isValue ( textToSend ) ) , TypeMoq . Times . exactly ( 1 ) ) ;
232+ } ) ;
233+
234+ test ( 'Ensure sendText is NOT called when Python shell integration and terminal shell integration are both enabled' , async ( ) => {
235+ pythonConfig
236+ . setup ( ( p ) => p . get ( 'terminal.shellIntegration.enabled' ) )
237+ . returns ( ( ) => true )
238+ . verifiable ( TypeMoq . Times . once ( ) ) ;
239+
240+ terminalHelper
241+ . setup ( ( helper ) => helper . getEnvironmentActivationCommands ( TypeMoq . It . isAny ( ) , TypeMoq . It . isAny ( ) ) )
242+ . returns ( ( ) => Promise . resolve ( undefined ) ) ;
243+ service = new TerminalService ( mockServiceContainer . object ) ;
244+ const textToSend = 'Some Text' ;
245+ terminalHelper . setup ( ( h ) => h . identifyTerminalShell ( TypeMoq . It . isAny ( ) ) ) . returns ( ( ) => TerminalShellType . bash ) ;
246+ terminalManager . setup ( ( t ) => t . createTerminal ( TypeMoq . It . isAny ( ) ) ) . returns ( ( ) => terminal . object ) ;
247+
248+ service . ensureTerminal ( ) ;
249+ service . executeCommand ( textToSend , true ) ;
250+
251+ terminal . verify ( ( t ) => t . show ( TypeMoq . It . isValue ( true ) ) , TypeMoq . Times . exactly ( 1 ) ) ;
252+ terminal . verify ( ( t ) => t . sendText ( TypeMoq . It . isValue ( textToSend ) ) , TypeMoq . Times . never ( ) ) ;
253+ } ) ;
254+
174255 test ( 'Ensure terminal is not shown if `hideFromUser` option is set to `true`' , async ( ) => {
175256 terminalHelper
176257 . setup ( ( helper ) => helper . getEnvironmentActivationCommands ( TypeMoq . It . isAny ( ) , TypeMoq . It . isAny ( ) ) )
0 commit comments