@@ -4,6 +4,7 @@ import * as path from "path";
4
4
5
5
import * as vscode from "vscode" ;
6
6
import sinon from "sinon" ;
7
+ import { afterEach , beforeEach } from "mocha" ;
7
8
8
9
import { Ruby , ManagerIdentifier } from "../../ruby" ;
9
10
import { WorkspaceChannel } from "../../workspaceChannel" ;
@@ -16,6 +17,7 @@ import {
16
17
VALUE_SEPARATOR ,
17
18
} from "../../ruby/versionManager" ;
18
19
20
+ import { CONTEXT } from "./helpers" ;
19
21
import { FAKE_TELEMETRY } from "./fakeTelemetry" ;
20
22
21
23
suite ( "Ruby environment activation" , ( ) => {
@@ -27,37 +29,36 @@ suite("Ruby environment activation", () => {
27
29
name : path . basename ( workspacePath ) ,
28
30
index : 0 ,
29
31
} ;
30
- const context = {
31
- extensionMode : vscode . ExtensionMode . Test ,
32
- workspaceState : {
33
- get : ( ) => undefined ,
34
- update : ( ) => undefined ,
35
- } ,
36
- extensionUri : vscode . Uri . file ( path . join ( workspacePath , "vscode" ) ) ,
37
- } as unknown as vscode . ExtensionContext ;
38
32
const outputChannel = new WorkspaceChannel ( "fake" , LOG_CHANNEL ) ;
33
+ let sandbox : sinon . SinonSandbox ;
34
+
35
+ beforeEach ( ( ) => {
36
+ sandbox = sinon . createSandbox ( ) ;
37
+ } ) ;
38
+
39
+ afterEach ( ( ) => {
40
+ sandbox . restore ( ) ;
41
+ } ) ;
39
42
40
43
test ( "Activate fetches Ruby information when outside of Ruby LSP" , async ( ) => {
41
44
const manager = process . env . CI
42
45
? ManagerIdentifier . None
43
46
: ManagerIdentifier . Chruby ;
44
47
45
- const configStub = sinon
46
- . stub ( vscode . workspace , "getConfiguration" )
47
- . returns ( {
48
- get : ( name : string ) => {
49
- if ( name === "rubyVersionManager" ) {
50
- return { identifier : manager } ;
51
- } else if ( name === "bundleGemfile" ) {
52
- return "" ;
53
- }
48
+ sandbox . stub ( vscode . workspace , "getConfiguration" ) . returns ( {
49
+ get : ( name : string ) => {
50
+ if ( name === "rubyVersionManager" ) {
51
+ return { identifier : manager } ;
52
+ } else if ( name === "bundleGemfile" ) {
53
+ return "" ;
54
+ }
54
55
55
- return undefined ;
56
- } ,
57
- } as unknown as vscode . WorkspaceConfiguration ) ;
56
+ return undefined ;
57
+ } ,
58
+ } as unknown as vscode . WorkspaceConfiguration ) ;
58
59
59
60
const ruby = new Ruby (
60
- context ,
61
+ CONTEXT ,
61
62
workspaceFolder ,
62
63
outputChannel ,
63
64
FAKE_TELEMETRY ,
@@ -70,31 +71,27 @@ suite("Ruby environment activation", () => {
70
71
undefined ,
71
72
"Expected YJIT support to be set to true or false" ,
72
73
) ;
73
-
74
- configStub . restore ( ) ;
75
74
} ) . timeout ( 10000 ) ;
76
75
77
76
test ( "Deletes verbose and GC settings from activated environment" , async ( ) => {
78
77
const manager = process . env . CI
79
78
? ManagerIdentifier . None
80
79
: ManagerIdentifier . Chruby ;
81
80
82
- const configStub = sinon
83
- . stub ( vscode . workspace , "getConfiguration" )
84
- . returns ( {
85
- get : ( name : string ) => {
86
- if ( name === "rubyVersionManager" ) {
87
- return { identifier : manager } ;
88
- } else if ( name === "bundleGemfile" ) {
89
- return "" ;
90
- }
81
+ sandbox . stub ( vscode . workspace , "getConfiguration" ) . returns ( {
82
+ get : ( name : string ) => {
83
+ if ( name === "rubyVersionManager" ) {
84
+ return { identifier : manager } ;
85
+ } else if ( name === "bundleGemfile" ) {
86
+ return "" ;
87
+ }
91
88
92
- return undefined ;
93
- } ,
94
- } as unknown as vscode . WorkspaceConfiguration ) ;
89
+ return undefined ;
90
+ } ,
91
+ } as unknown as vscode . WorkspaceConfiguration ) ;
95
92
96
93
const ruby = new Ruby (
97
- context ,
94
+ CONTEXT ,
98
95
workspaceFolder ,
99
96
outputChannel ,
100
97
FAKE_TELEMETRY ,
@@ -111,23 +108,20 @@ suite("Ruby environment activation", () => {
111
108
delete process . env . VERBOSE ;
112
109
delete process . env . DEBUG ;
113
110
delete process . env . RUBY_GC_HEAP_GROWTH_FACTOR ;
114
- configStub . restore ( ) ;
115
111
} ) ;
116
112
117
113
test ( "Sets gem path for version managers based on shims" , async ( ) => {
118
- const configStub = sinon
119
- . stub ( vscode . workspace , "getConfiguration" )
120
- . returns ( {
121
- get : ( name : string ) => {
122
- if ( name === "rubyVersionManager" ) {
123
- return { identifier : ManagerIdentifier . Rbenv } ;
124
- } else if ( name === "bundleGemfile" ) {
125
- return "" ;
126
- }
127
-
128
- return undefined ;
129
- } ,
130
- } as unknown as vscode . WorkspaceConfiguration ) ;
114
+ sandbox . stub ( vscode . workspace , "getConfiguration" ) . returns ( {
115
+ get : ( name : string ) => {
116
+ if ( name === "rubyVersionManager" ) {
117
+ return { identifier : ManagerIdentifier . Rbenv } ;
118
+ } else if ( name === "bundleGemfile" ) {
119
+ return "" ;
120
+ }
121
+
122
+ return undefined ;
123
+ } ,
124
+ } as unknown as vscode . WorkspaceConfiguration ) ;
131
125
132
126
const envStub = [
133
127
"3.3.5" ,
@@ -136,20 +130,18 @@ suite("Ruby environment activation", () => {
136
130
`ANY${ VALUE_SEPARATOR } true` ,
137
131
] . join ( FIELD_SEPARATOR ) ;
138
132
139
- const execStub = sinon . stub ( common , "asyncExec" ) . resolves ( {
133
+ sandbox . stub ( common , "asyncExec" ) . resolves ( {
140
134
stdout : "" ,
141
135
stderr : `${ ACTIVATION_SEPARATOR } ${ envStub } ${ ACTIVATION_SEPARATOR } ` ,
142
136
} ) ;
143
137
144
138
const ruby = new Ruby (
145
- context ,
139
+ CONTEXT ,
146
140
workspaceFolder ,
147
141
outputChannel ,
148
142
FAKE_TELEMETRY ,
149
143
) ;
150
144
await ruby . activateRuby ( ) ;
151
- execStub . restore ( ) ;
152
- configStub . restore ( ) ;
153
145
154
146
assert . deepStrictEqual ( ruby . gemPath , [
155
147
"~/.gem/ruby/3.3.5" ,
@@ -159,7 +151,7 @@ suite("Ruby environment activation", () => {
159
151
160
152
test ( "mergeComposedEnv merges environment variables" , ( ) => {
161
153
const ruby = new Ruby (
162
- context ,
154
+ CONTEXT ,
163
155
workspaceFolder ,
164
156
outputChannel ,
165
157
FAKE_TELEMETRY ,
@@ -176,9 +168,9 @@ suite("Ruby environment activation", () => {
176
168
177
169
test ( "Ignores untrusted workspace for telemetry" , async ( ) => {
178
170
const telemetry = { ...FAKE_TELEMETRY , logError : sinon . stub ( ) } ;
179
- const ruby = new Ruby ( context , workspaceFolder , outputChannel , telemetry ) ;
171
+ const ruby = new Ruby ( CONTEXT , workspaceFolder , outputChannel , telemetry ) ;
180
172
181
- const failureStub = sinon
173
+ sandbox
182
174
. stub ( Shadowenv . prototype , "activate" )
183
175
. rejects ( new UntrustedWorkspaceError ( ) ) ;
184
176
@@ -187,7 +179,43 @@ suite("Ruby environment activation", () => {
187
179
} ) ;
188
180
189
181
assert . ok ( ! telemetry . logError . called ) ;
182
+ } ) ;
190
183
191
- failureStub . restore ( ) ;
184
+ test ( "Clears outdated workspace Ruby path caches" , async ( ) => {
185
+ const manager = process . env . CI
186
+ ? ManagerIdentifier . None
187
+ : ManagerIdentifier . Chruby ;
188
+
189
+ sandbox . stub ( vscode . workspace , "getConfiguration" ) . returns ( {
190
+ get : ( name : string ) => {
191
+ if ( name === "rubyVersionManager" ) {
192
+ return { identifier : manager } ;
193
+ } else if ( name === "bundleGemfile" ) {
194
+ return "" ;
195
+ }
196
+
197
+ return undefined ;
198
+ } ,
199
+ } as unknown as vscode . WorkspaceConfiguration ) ;
200
+
201
+ await CONTEXT . workspaceState . update (
202
+ `rubyLsp.workspaceRubyPath.${ workspaceFolder . name } ` ,
203
+ "/totally/non/existent/path/ruby" ,
204
+ ) ;
205
+ const ruby = new Ruby (
206
+ CONTEXT ,
207
+ workspaceFolder ,
208
+ outputChannel ,
209
+ FAKE_TELEMETRY ,
210
+ ) ;
211
+
212
+ await ruby . activateRuby ( ) ;
213
+
214
+ assert . strictEqual (
215
+ CONTEXT . workspaceState . get (
216
+ `rubyLsp.workspaceRubyPath.${ workspaceFolder . name } ` ,
217
+ ) ,
218
+ undefined ,
219
+ ) ;
192
220
} ) ;
193
221
} ) ;
0 commit comments