@@ -10,45 +10,44 @@ import { DebugConfiguration, DebugSessionOptions, RelativePattern, Uri } from 'v
10
10
import * as utils from '../../extension/utils' ;
11
11
import { assert } from 'console' ;
12
12
import * as fs from 'fs' ;
13
+ import * as os from 'os' ;
14
+ import * as crypto from 'crypto' ;
13
15
14
16
suite ( 'setup for no-config debug scenario' , function ( ) {
15
17
let envVarCollectionReplaceStub : sinon . SinonStub ;
16
18
let envVarCollectionAppendStub : sinon . SinonStub ;
17
19
let context : TypeMoq . IMock < IExtensionContext > ;
18
- let debugAdapterEndpointDir : string ;
19
- let debuggerAdapterEndpointPath : string ;
20
20
let noConfigScriptsDir : string ;
21
21
let bundledDebugPath : string ;
22
22
let DEBUGPY_ADAPTER_ENDPOINTS = 'DEBUGPY_ADAPTER_ENDPOINTS' ;
23
23
let BUNDLED_DEBUGPY_PATH = 'BUNDLED_DEBUGPY_PATH' ;
24
+ let tempDirPath : string ;
24
25
25
26
const testDataDir = path . join ( __dirname , 'testData' ) ;
26
27
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
- } ) ;
34
28
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
+ }
43
47
} ) ;
44
48
teardown ( ( ) => {
45
49
sinon . restore ( ) ;
46
50
} ) ;
47
- suiteTeardown ( ( ) => {
48
- if ( fs . existsSync ( testDataDir ) ) {
49
- fs . rmSync ( testDataDir , { recursive : true , force : true } ) ;
50
- }
51
- } ) ;
52
51
53
52
test ( 'should add environment variables for DEBUGPY_ADAPTER_ENDPOINTS, BUNDLED_DEBUGPY_PATH, and PATH' , async ( ) => {
54
53
const environmentVariableCollectionMock = TypeMoq . Mock . ofType < any > ( ) ;
@@ -60,7 +59,7 @@ suite('setup for no-config debug scenario', function () {
60
59
. setup ( ( x ) => x . replace ( TypeMoq . It . isAny ( ) , TypeMoq . It . isAny ( ) ) )
61
60
. callback ( ( key , value ) => {
62
61
if ( key === DEBUGPY_ADAPTER_ENDPOINTS ) {
63
- assert ( value === debuggerAdapterEndpointPath ) ;
62
+ assert ( value . includes ( 'noConfigDebugAdapterEndpoints-1234567899' ) ) ;
64
63
} else if ( key === BUNDLED_DEBUGPY_PATH ) {
65
64
assert ( value === bundledDebugPath ) ;
66
65
}
@@ -80,7 +79,7 @@ suite('setup for no-config debug scenario', function () {
80
79
setupFileSystemWatchers ( ) ;
81
80
82
81
// run init for no config debug
83
- await registerNoConfigDebug ( context . object ) ;
82
+ await registerNoConfigDebug ( context . object . environmentVariableCollection , context . object . extensionPath ) ;
84
83
85
84
// assert that functions called right number of times
86
85
sinon . assert . calledTwice ( envVarCollectionReplaceStub ) ;
@@ -94,11 +93,11 @@ suite('setup for no-config debug scenario', function () {
94
93
let createFileSystemWatcherFunct = setupFileSystemWatchers ( ) ;
95
94
96
95
// Act
97
- await registerNoConfigDebug ( context . object ) ;
96
+ await registerNoConfigDebug ( context . object . environmentVariableCollection , context . object . extensionPath ) ;
98
97
99
98
// Assert
100
99
sinon . assert . calledOnce ( createFileSystemWatcherFunct ) ;
101
- const expectedPattern = new RelativePattern ( debugAdapterEndpointDir , '**/*' ) ;
100
+ const expectedPattern = new RelativePattern ( tempDirPath , '**/*' ) ;
102
101
sinon . assert . calledWith ( createFileSystemWatcherFunct , expectedPattern ) ;
103
102
} ) ;
104
103
@@ -127,7 +126,7 @@ suite('setup for no-config debug scenario', function () {
127
126
const debugStub = sinon . stub ( utils , 'debugStartDebugging' ) . resolves ( true ) ;
128
127
129
128
// Act
130
- await registerNoConfigDebug ( context . object ) ;
129
+ await registerNoConfigDebug ( context . object . environmentVariableCollection , context . object . extensionPath ) ;
131
130
132
131
// Assert
133
132
sinon . assert . calledOnce ( debugStub ) ;
0 commit comments