@@ -10,45 +10,44 @@ import { DebugConfiguration, DebugSessionOptions, RelativePattern, Uri } from 'v
1010import * as utils from '../../extension/utils' ;
1111import { assert } from 'console' ;
1212import * as fs from 'fs' ;
13+ import * as os from 'os' ;
14+ import * as crypto from 'crypto' ;
1315
1416suite ( 'setup for no-config debug scenario' , function ( ) {
1517 let envVarCollectionReplaceStub : sinon . SinonStub ;
1618 let envVarCollectionAppendStub : sinon . SinonStub ;
1719 let context : TypeMoq . IMock < IExtensionContext > ;
18- let debugAdapterEndpointDir : string ;
19- let debuggerAdapterEndpointPath : string ;
2020 let noConfigScriptsDir : string ;
2121 let bundledDebugPath : string ;
2222 let DEBUGPY_ADAPTER_ENDPOINTS = 'DEBUGPY_ADAPTER_ENDPOINTS' ;
2323 let BUNDLED_DEBUGPY_PATH = 'BUNDLED_DEBUGPY_PATH' ;
24+ let tempDirPath : string ;
2425
2526 const testDataDir = path . join ( __dirname , 'testData' ) ;
2627 const testFilePath = path . join ( testDataDir , 'debuggerAdapterEndpoint.txt' ) ;
27- suiteSetup ( ( ) => {
28- // create file called testData/debuggerAdapterEndpoint.txt
29- if ( ! fs . existsSync ( testDataDir ) ) {
30- fs . mkdirSync ( testDataDir ) ;
31- }
32- fs . writeFileSync ( testFilePath , JSON . stringify ( { client : { port : 5678 } } ) ) ;
33- } ) ;
3428 setup ( ( ) => {
35- context = TypeMoq . Mock . ofType < IExtensionContext > ( ) ;
36-
37- context . setup ( ( c ) => ( c as any ) . extensionPath ) . returns ( ( ) => 'fake/extension/path' ) ;
38- context . setup ( ( c ) => c . subscriptions ) . returns ( ( ) => [ ] ) ;
39- debugAdapterEndpointDir = path . join ( context . object . extensionPath , 'noConfigDebugAdapterEndpoints' ) ;
40- debuggerAdapterEndpointPath = path . join ( debugAdapterEndpointDir , 'debuggerAdapterEndpoint.txt' ) ;
41- noConfigScriptsDir = path . join ( context . object . extensionPath , 'bundled/scripts/noConfigScripts' ) ;
42- bundledDebugPath = path . join ( context . object . extensionPath , 'bundled/libs/debugpy' ) ;
29+ try {
30+ context = TypeMoq . Mock . ofType < IExtensionContext > ( ) ;
31+
32+ const randomSuffix = '1234567899' ;
33+ const tempDirName = `noConfigDebugAdapterEndpoints-${ randomSuffix } ` ;
34+ tempDirPath = path . join ( os . tmpdir ( ) , tempDirName ) ;
35+ context . setup ( ( c ) => ( c as any ) . extensionPath ) . returns ( ( ) => 'fake/extension/path' ) ;
36+ context . setup ( ( c ) => c . subscriptions ) . returns ( ( ) => [ ] ) ;
37+ noConfigScriptsDir = path . join ( context . object . extensionPath , 'bundled/scripts/noConfigScripts' ) ;
38+ bundledDebugPath = path . join ( context . object . extensionPath , 'bundled/libs/debugpy' ) ;
39+
40+ // Stub crypto.randomBytes with proper typing
41+ let randomBytesStub = sinon . stub ( crypto , 'randomBytes' ) ;
42+ // Provide a valid Buffer object
43+ randomBytesStub . callsFake ( ( _size : number ) => Buffer . from ( '1234567899' , 'hex' ) ) ;
44+ } catch ( error ) {
45+ console . error ( 'Error in setup:' , error ) ;
46+ }
4347 } ) ;
4448 teardown ( ( ) => {
4549 sinon . restore ( ) ;
4650 } ) ;
47- suiteTeardown ( ( ) => {
48- if ( fs . existsSync ( testDataDir ) ) {
49- fs . rmSync ( testDataDir , { recursive : true , force : true } ) ;
50- }
51- } ) ;
5251
5352 test ( 'should add environment variables for DEBUGPY_ADAPTER_ENDPOINTS, BUNDLED_DEBUGPY_PATH, and PATH' , async ( ) => {
5453 const environmentVariableCollectionMock = TypeMoq . Mock . ofType < any > ( ) ;
@@ -60,7 +59,7 @@ suite('setup for no-config debug scenario', function () {
6059 . setup ( ( x ) => x . replace ( TypeMoq . It . isAny ( ) , TypeMoq . It . isAny ( ) ) )
6160 . callback ( ( key , value ) => {
6261 if ( key === DEBUGPY_ADAPTER_ENDPOINTS ) {
63- assert ( value === debuggerAdapterEndpointPath ) ;
62+ assert ( value . includes ( 'noConfigDebugAdapterEndpoints-1234567899' ) ) ;
6463 } else if ( key === BUNDLED_DEBUGPY_PATH ) {
6564 assert ( value === bundledDebugPath ) ;
6665 }
@@ -80,7 +79,7 @@ suite('setup for no-config debug scenario', function () {
8079 setupFileSystemWatchers ( ) ;
8180
8281 // run init for no config debug
83- await registerNoConfigDebug ( context . object ) ;
82+ await registerNoConfigDebug ( context . object . environmentVariableCollection , context . object . extensionPath ) ;
8483
8584 // assert that functions called right number of times
8685 sinon . assert . calledTwice ( envVarCollectionReplaceStub ) ;
@@ -94,11 +93,11 @@ suite('setup for no-config debug scenario', function () {
9493 let createFileSystemWatcherFunct = setupFileSystemWatchers ( ) ;
9594
9695 // Act
97- await registerNoConfigDebug ( context . object ) ;
96+ await registerNoConfigDebug ( context . object . environmentVariableCollection , context . object . extensionPath ) ;
9897
9998 // Assert
10099 sinon . assert . calledOnce ( createFileSystemWatcherFunct ) ;
101- const expectedPattern = new RelativePattern ( debugAdapterEndpointDir , '**/*' ) ;
100+ const expectedPattern = new RelativePattern ( tempDirPath , '**/*' ) ;
102101 sinon . assert . calledWith ( createFileSystemWatcherFunct , expectedPattern ) ;
103102 } ) ;
104103
@@ -127,7 +126,7 @@ suite('setup for no-config debug scenario', function () {
127126 const debugStub = sinon . stub ( utils , 'debugStartDebugging' ) . resolves ( true ) ;
128127
129128 // Act
130- await registerNoConfigDebug ( context . object ) ;
129+ await registerNoConfigDebug ( context . object . environmentVariableCollection , context . object . extensionPath ) ;
131130
132131 // Assert
133132 sinon . assert . calledOnce ( debugStub ) ;
0 commit comments