@@ -6,16 +6,22 @@ import * as fsapi from 'fs-extra';
66import * as os from 'os' ;
77import * as path from 'path' ;
88import * as sinon from 'sinon' ;
9- import { Disposable , Event , EventEmitter , Progress , Terminal , TerminalOptions , Uri , WorkspaceConfiguration } from 'vscode' ;
9+ import {
10+ Disposable ,
11+ Event ,
12+ EventEmitter ,
13+ Progress ,
14+ Terminal ,
15+ TerminalOptions ,
16+ Uri ,
17+ WorkspaceConfiguration ,
18+ } from 'vscode' ;
1019import { PythonEnvironment } from '../../../api' ;
1120import * as windowApis from '../../../common/window.apis' ;
1221import * as workspaceApis from '../../../common/workspace.apis' ;
1322import * as activationUtils from '../../../features/common/activation' ;
1423import * as shellDetector from '../../../features/common/shellDetector' ;
15- import {
16- ShellEnvsProvider ,
17- ShellStartupScriptProvider ,
18- } from '../../../features/terminal/shells/startupProvider' ;
24+ import { ShellEnvsProvider , ShellStartupScriptProvider } from '../../../features/terminal/shells/startupProvider' ;
1925import {
2026 DidChangeTerminalActivationStateEvent ,
2127 TerminalActivationInternal ,
@@ -309,13 +315,109 @@ suite('TerminalManager - terminal naming', () => {
309315 try {
310316 await terminalManager . getProjectTerminal ( projectUri , env ) ;
311317
318+ assert . strictEqual ( optionsList [ 0 ] ?. name , 'Python' , 'Project terminal should use the Python title' ) ;
319+ } finally {
320+ await fsapi . remove ( tempRoot ) ;
321+ }
322+ } ) ;
323+
324+ // Regression test for https://github.com/microsoft/vscode-python-environments/issues/1230
325+ test ( 'getDedicatedTerminal with string key uses string as terminal name' , async ( ) => {
326+ mockGetAutoActivationType . returns ( terminalUtils . ACT_TYPE_OFF ) ;
327+ terminalManager = createTerminalManager ( ) ;
328+ const env = createMockEnvironment ( ) ;
329+
330+ const optionsList : TerminalOptions [ ] = [ ] ;
331+ createTerminalStub . callsFake ( ( options ) => {
332+ optionsList . push ( options ) ;
333+ return mockTerminal as Terminal ;
334+ } ) ;
335+
336+ const tempRoot = await fsapi . mkdtemp ( path . join ( os . tmpdir ( ) , 'py-envs-' ) ) ;
337+ const projectPath = path . join ( tempRoot , 'project' ) ;
338+ await fsapi . ensureDir ( projectPath ) ;
339+ const projectUri = Uri . file ( projectPath ) ;
340+
341+ const config = { get : sinon . stub ( ) . returns ( false ) } as unknown as WorkspaceConfiguration ;
342+ sinon . stub ( workspaceApis , 'getConfiguration' ) . returns ( config ) ;
343+
344+ try {
345+ await terminalManager . getDedicatedTerminal ( 'my-terminal-key' , projectUri , env ) ;
346+
312347 assert . strictEqual (
313348 optionsList [ 0 ] ?. name ,
314- 'Python' ,
315- 'Project terminal should use the Python title' ,
349+ 'Python: my-terminal-key' ,
350+ 'Dedicated terminal with string key should use the string in the title' ,
351+ ) ;
352+ assert . strictEqual (
353+ Uri . file ( optionsList [ 0 ] ?. cwd as string ) . fsPath ,
354+ Uri . file ( projectPath ) . fsPath ,
355+ 'Dedicated terminal with string key should use project directory as cwd' ,
316356 ) ;
317357 } finally {
318358 await fsapi . remove ( tempRoot ) ;
319359 }
320360 } ) ;
361+
362+ // Regression test for https://github.com/microsoft/vscode-python-environments/issues/1230
363+ test ( 'getDedicatedTerminal with string key reuses terminal for same key' , async ( ) => {
364+ mockGetAutoActivationType . returns ( terminalUtils . ACT_TYPE_OFF ) ;
365+ terminalManager = createTerminalManager ( ) ;
366+ const env = createMockEnvironment ( ) ;
367+
368+ let createCount = 0 ;
369+ createTerminalStub . callsFake ( ( options ) => {
370+ createCount ++ ;
371+ return { ...mockTerminal , name : options . name } as Terminal ;
372+ } ) ;
373+
374+ const tempRoot = await fsapi . mkdtemp ( path . join ( os . tmpdir ( ) , 'py-envs-' ) ) ;
375+ const projectPath = path . join ( tempRoot , 'project' ) ;
376+ await fsapi . ensureDir ( projectPath ) ;
377+ const projectUri = Uri . file ( projectPath ) ;
378+
379+ const config = { get : sinon . stub ( ) . returns ( false ) } as unknown as WorkspaceConfiguration ;
380+ sinon . stub ( workspaceApis , 'getConfiguration' ) . returns ( config ) ;
381+
382+ try {
383+ const terminal1 = await terminalManager . getDedicatedTerminal ( 'my-key' , projectUri , env ) ;
384+ const terminal2 = await terminalManager . getDedicatedTerminal ( 'my-key' , projectUri , env ) ;
385+
386+ assert . strictEqual ( terminal1 , terminal2 , 'Same string key should return the same terminal' ) ;
387+ assert . strictEqual ( createCount , 1 , 'Terminal should be created only once' ) ;
388+ } finally {
389+ await fsapi . remove ( tempRoot ) ;
390+ }
391+ } ) ;
392+
393+ // Regression test for https://github.com/microsoft/vscode-python-environments/issues/1230
394+ test ( 'getDedicatedTerminal with string key uses different terminals for different keys' , async ( ) => {
395+ mockGetAutoActivationType . returns ( terminalUtils . ACT_TYPE_OFF ) ;
396+ terminalManager = createTerminalManager ( ) ;
397+ const env = createMockEnvironment ( ) ;
398+
399+ let createCount = 0 ;
400+ createTerminalStub . callsFake ( ( options ) => {
401+ createCount ++ ;
402+ return { ...mockTerminal , name : options . name , id : createCount } as unknown as Terminal ;
403+ } ) ;
404+
405+ const tempRoot = await fsapi . mkdtemp ( path . join ( os . tmpdir ( ) , 'py-envs-' ) ) ;
406+ const projectPath = path . join ( tempRoot , 'project' ) ;
407+ await fsapi . ensureDir ( projectPath ) ;
408+ const projectUri = Uri . file ( projectPath ) ;
409+
410+ const config = { get : sinon . stub ( ) . returns ( false ) } as unknown as WorkspaceConfiguration ;
411+ sinon . stub ( workspaceApis , 'getConfiguration' ) . returns ( config ) ;
412+
413+ try {
414+ const terminal1 = await terminalManager . getDedicatedTerminal ( 'key-1' , projectUri , env ) ;
415+ const terminal2 = await terminalManager . getDedicatedTerminal ( 'key-2' , projectUri , env ) ;
416+
417+ assert . notStrictEqual ( terminal1 , terminal2 , 'Different string keys should return different terminals' ) ;
418+ assert . strictEqual ( createCount , 2 , 'Two terminals should be created' ) ;
419+ } finally {
420+ await fsapi . remove ( tempRoot ) ;
421+ }
422+ } ) ;
321423} ) ;
0 commit comments