@@ -18,7 +18,7 @@ describe('utils/pid', () => {
18
18
afterEach ( ( ) => {
19
19
sinonUtil . restore ( [
20
20
os . platform ,
21
- child_process . execSync ,
21
+ child_process . spawnSync ,
22
22
fs . existsSync ,
23
23
fs . readFileSync
24
24
] ) ;
@@ -33,18 +33,37 @@ describe('utils/pid', () => {
33
33
34
34
it ( 'retrieves process name on Windows' , ( ) => {
35
35
sinon . stub ( os , 'platform' ) . callsFake ( ( ) => 'win32' ) ;
36
- sinon . stub ( child_process , 'execSync' ) . callsFake ( ( ) => 'pwsh' ) ;
36
+ sinon . stub ( child_process , 'spawnSync' ) . callsFake ( ( ) => {
37
+ return {
38
+ stdout : 'pwsh'
39
+ } as any ;
40
+ } ) ;
37
41
38
42
assert . strictEqual ( pid . getProcessName ( 123 ) , 'pwsh' ) ;
39
43
} ) ;
40
44
41
45
it ( 'retrieves process name on macOS' , ( ) => {
42
46
sinon . stub ( os , 'platform' ) . callsFake ( ( ) => 'darwin' ) ;
43
- sinon . stub ( child_process , 'execSync' ) . callsFake ( ( ) => '/bin/bash' ) ;
47
+ sinon . stub ( child_process , 'spawnSync' ) . callsFake ( ( ) => {
48
+ return {
49
+ stdout : '/bin/bash'
50
+ } as any ;
51
+ } ) ;
44
52
45
53
assert . strictEqual ( pid . getProcessName ( 123 ) , '/bin/bash' ) ;
46
54
} ) ;
47
55
56
+ it ( 'retrieves undefined on macOS when retrieving the process name failed' , ( ) => {
57
+ sinon . stub ( os , 'platform' ) . callsFake ( ( ) => 'darwin' ) ;
58
+ sinon . stub ( child_process , 'spawnSync' ) . callsFake ( ( ) => {
59
+ return {
60
+ error : 'An error has occurred'
61
+ } as any ;
62
+ } ) ;
63
+
64
+ assert . strictEqual ( pid . getProcessName ( 123 ) , undefined ) ;
65
+ } ) ;
66
+
48
67
it ( 'retrieves process name on Linux' , ( ) => {
49
68
sinon . stub ( os , 'platform' ) . callsFake ( ( ) => 'linux' ) ;
50
69
sinon . stub ( fs , 'existsSync' ) . callsFake ( ( ) => true ) ;
@@ -66,16 +85,43 @@ describe('utils/pid', () => {
66
85
assert . strictEqual ( pid . getProcessName ( 123 ) , undefined ) ;
67
86
} ) ;
68
87
69
- it ( 'returns undefined when retrieving process name fails' , ( ) => {
88
+ it ( 'returns undefined when retrieving process name on Windows fails' , ( ) => {
89
+ sinon . stub ( os , 'platform' ) . callsFake ( ( ) => 'win32' ) ;
90
+ sinon . stub ( child_process , 'spawnSync' ) . callsFake ( ( ) => {
91
+ return {
92
+ error : 'An error has occurred'
93
+ } as any ;
94
+ } ) ;
95
+
96
+ assert . strictEqual ( pid . getProcessName ( 123 ) , undefined ) ;
97
+ } ) ;
98
+
99
+ it ( 'returns undefined when extracting process name on Windows' , ( ) => {
70
100
sinon . stub ( os , 'platform' ) . callsFake ( ( ) => 'win32' ) ;
71
- sinon . stub ( child_process , 'execSync' ) . throws ( ) ;
101
+ sinon . stub ( child_process , 'spawnSync' ) . callsFake ( command => {
102
+ if ( command === 'wmic' ) {
103
+ return {
104
+ stdout : 'Caption\
105
+ pwsh.exe\
106
+ '
107
+ } ;
108
+ }
109
+
110
+ return {
111
+ error : 'An error has occurred'
112
+ } as any ;
113
+ } ) ;
72
114
73
115
assert . strictEqual ( pid . getProcessName ( 123 ) , undefined ) ;
74
116
} ) ;
75
117
76
118
it ( 'stores retrieved process name in cache' , ( ) => {
77
119
sinon . stub ( os , 'platform' ) . callsFake ( ( ) => 'win32' ) ;
78
- sinon . stub ( child_process , 'execSync' ) . callsFake ( ( ) => 'pwsh' ) ;
120
+ sinon . stub ( child_process , 'spawnSync' ) . callsFake ( ( ) => {
121
+ return {
122
+ stdout : 'pwsh'
123
+ } as any ;
124
+ } ) ;
79
125
80
126
pid . getProcessName ( 123 ) ;
81
127
0 commit comments