forked from dotnet/vscode-dotnet-runtime
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathDotnetCoreAcquisitionExtension.test.ts
More file actions
622 lines (531 loc) · 32 KB
/
Copy pathDotnetCoreAcquisitionExtension.test.ts
File metadata and controls
622 lines (531 loc) · 32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
/*---------------------------------------------------------------------------------------------
* Licensed to the .NET Foundation under one or more agreements.
* The .NET Foundation licenses this file to you under the MIT license.
*--------------------------------------------------------------------------------------------*/
import * as chai from 'chai';
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import * as rimraf from 'rimraf';
import * as vscode from 'vscode';
import {
FileUtilities,
IDotnetAcquireContext,
IDotnetAcquireResult,
IExistingPaths,
IDotnetListVersionsContext,
IDotnetListVersionsResult,
getInstallIdCustomArchitecture,
ITelemetryEvent,
MockExtensionConfiguration,
MockExtensionContext,
MockTelemetryReporter,
MockWebRequestWorker,
MockWindowDisplayWorker,
getMockAcquisitionContext,
DotnetInstallMode,
DotnetInstallType,
MockEventStream,
IDotnetFindPathContext,
getDotnetExecutable,
DotnetVersionSpecRequirement,
EnvironmentVariableIsDefined,
MockEnvironmentVariableCollection,
getPathSeparator,
} from 'vscode-dotnet-runtime-library';
import * as extension from '../../extension';
import { warn } from 'console';
import { InstallTrackerSingleton } from 'vscode-dotnet-runtime-library/dist/Acquisition/InstallTrackerSingleton';
const assert : any = chai.assert;
const standardTimeoutTime = 40000;
const originalPATH = process.env.PATH;
suite('DotnetCoreAcquisitionExtension End to End', function()
{
this.retries(1);
const storagePath = path.join(__dirname, 'tmp');
const mockState = new MockExtensionContext();
const extensionPath = path.join(__dirname, '/../../..');
const logPath = path.join(__dirname, 'logs');
const requestingExtensionId = 'fake.extension';
const mockDisplayWorker = new MockWindowDisplayWorker();
let extensionContext: vscode.ExtensionContext;
const environmentVariableCollection = new MockEnvironmentVariableCollection();
const existingPathVersionToFake = '5.0.2~x64'
const pathWithIncorrectVersionForTest = path.join(__dirname, `/.dotnet/${existingPathVersionToFake}/dotnet`);
const mockExistingPathsWithGlobalConfig: IExistingPaths = {
individualizedExtensionPaths: [{extensionId: 'alternative.extension', path: pathWithIncorrectVersionForTest}],
sharedExistingPath: undefined
}
const mockReleasesData = `{
"releases-index": [
{
"channel-version": "8.0",
"latest-release": "8.0.0-preview.2",
"latest-runtime": "8.0.0-preview.2.23128.3",
"latest-sdk": "8.0.100-preview.2.23157.25",
"release-type" : "lts",
"support-phase": "preview"
},
{
"channel-version": "7.0",
"latest-release": "7.0.4",
"latest-release-date": "2023-03-14",
"latest-runtime": "7.0.4",
"latest-sdk": "7.0.202",
"release-type" : "sts",
"support-phase": "active"
}
]
}`;
this.beforeAll(async () => {
extensionContext = {
subscriptions: [],
globalStoragePath: storagePath,
globalState: mockState,
extensionPath,
logPath,
environmentVariableCollection
} as any;
process.env.DOTNET_INSTALL_TOOL_UNDER_TEST = 'true';
extension.ReEnableActivationForManualActivation();
extension.activate(extensionContext, {
telemetryReporter: new MockTelemetryReporter(),
extensionConfiguration: new MockExtensionConfiguration(mockExistingPathsWithGlobalConfig.individualizedExtensionPaths!, true, mockExistingPathsWithGlobalConfig.sharedExistingPath!),
displayWorker: mockDisplayWorker,
});
});
this.afterEach(async () => {
// Tear down tmp storage for fresh run
process.env.PATH = originalPATH;
await vscode.commands.executeCommand<string>('dotnet.uninstallAll');
mockState.clear();
MockTelemetryReporter.telemetryEvents = [];
rimraf.sync(storagePath);
InstallTrackerSingleton.getInstance(new MockEventStream(), new MockExtensionContext()).clearPromises();
}).timeout(standardTimeoutTime);
test('Activate', async () => {
// Commands should now be registered
assert.exists(extensionContext);
assert.isAbove(extensionContext.subscriptions.length, 0);
}).timeout(standardTimeoutTime);
async function installRuntime(dotnetVersion : string, installMode : DotnetInstallMode, arch? : string)
{
let context: IDotnetAcquireContext = { version: dotnetVersion, requestingExtensionId, mode: installMode };
if(arch)
{
context.architecture = arch;
}
const result = await vscode.commands.executeCommand<IDotnetAcquireResult>('dotnet.acquire', context);
assert.exists(result, 'Command results a result');
assert.exists(result!.dotnetPath, 'The return type of the local runtime install command has a .dotnetPath property');
assert.isTrue(fs.existsSync(result!.dotnetPath), 'The returned path of .net does exist');
assert.include(result!.dotnetPath, '.dotnet', '.dotnet is in the path of the local runtime install');
assert.include(result!.dotnetPath, context.version, 'the path of the local runtime install includes the version of the runtime requested');
return result.dotnetPath ?? 'runtimePathNotFound';
}
async function installMultipleVersions(versions : string[], installMode : DotnetInstallMode)
{
let dotnetPaths: string[] = [];
for (const version of versions) {
const result = await vscode.commands.executeCommand<IDotnetAcquireResult>('dotnet.acquire', { version, requestingExtensionId, mode: installMode });
assert.exists(result, 'acquire command returned a result/success');
assert.exists(result!.dotnetPath, 'the result has a path');
assert.include(result!.dotnetPath, version, 'the path includes the version');
if (result!.dotnetPath) {
dotnetPaths = dotnetPaths.concat(result!.dotnetPath);
}
}
// All versions are still there after all installs are completed
for (const dotnetPath of dotnetPaths) {
assert.isTrue(fs.existsSync(dotnetPath));
}
}
async function installUninstallOne(dotnetVersion : string, versionToKeep : string, installMode : DotnetInstallMode, type : DotnetInstallType)
{
const context: IDotnetAcquireContext = { version: dotnetVersion, requestingExtensionId, mode: installMode, installType: type };
const contextToKeep: IDotnetAcquireContext = { version: versionToKeep, requestingExtensionId, mode: installMode, installType: type };
const result = await vscode.commands.executeCommand<IDotnetAcquireResult>('dotnet.acquire', context);
const resultToKeep = await vscode.commands.executeCommand<IDotnetAcquireResult>('dotnet.acquire', contextToKeep);
assert.exists(result?.dotnetPath, 'The install succeeds and returns a path');
assert.exists(resultToKeep?.dotnetPath, 'The 2nd install succeeds and returns a path');
const uninstallResult = await vscode.commands.executeCommand<string>('dotnet.uninstall', context);
assert.equal(uninstallResult, '0', 'Uninstall returns 0');
assert.isFalse(fs.existsSync(result!.dotnetPath), 'the dotnet path result does not exist after uninstall');
assert.isTrue(fs.existsSync(resultToKeep!.dotnetPath), 'Only one thing is uninstalled.');
}
async function installUninstallAll(dotnetVersion : string, installMode : DotnetInstallMode)
{
const context: IDotnetAcquireContext = { version: dotnetVersion, requestingExtensionId, mode: installMode };
const result = await vscode.commands.executeCommand<IDotnetAcquireResult>('dotnet.acquire', context);
assert.exists(result);
assert.exists(result!.dotnetPath);
assert.isTrue(fs.existsSync(result!.dotnetPath!));
assert.include(result!.dotnetPath, context.version);
await vscode.commands.executeCommand<string>('dotnet.uninstallAll', context.version);
assert.isFalse(fs.existsSync(result!.dotnetPath), 'the dotnet path result does not exist after uninstall');
}
async function uninstallWithMultipleOwners(dotnetVersion : string, installMode : DotnetInstallMode, type : DotnetInstallType)
{
const context: IDotnetAcquireContext = { version: dotnetVersion, requestingExtensionId, mode: installMode, installType: type };
const contextFromOtherId: IDotnetAcquireContext = { version: dotnetVersion, requestingExtensionId: 'fake.extension.two', mode: installMode, installType: type };
const result = await vscode.commands.executeCommand<IDotnetAcquireResult>('dotnet.acquire', context);
const resultToKeep = await vscode.commands.executeCommand<IDotnetAcquireResult>('dotnet.acquire', contextFromOtherId);
assert.exists(result?.dotnetPath, 'The install succeeds and returns a path');
assert.equal(result?.dotnetPath, resultToKeep?.dotnetPath, 'The two dupe installs use the same path');
const uninstallResult = await vscode.commands.executeCommand<string>('dotnet.uninstall', context);
assert.equal(uninstallResult, '0', '1st owner Uninstall returns 0');
assert.isTrue(fs.existsSync(resultToKeep!.dotnetPath), 'Nothing is uninstalled without FORCE if theres multiple owners.');
const finalUninstallResult = await vscode.commands.executeCommand<string>('dotnet.uninstall', contextFromOtherId);
assert.equal(finalUninstallResult, '0', '2nd owner Uninstall returns 0');
assert.isFalse(fs.existsSync(result!.dotnetPath), 'the dotnet path result does not exist after uninstalling from all owners');
}
function includesPathWithLikelyDotnet(pathToCheck : string) : boolean
{
const lowerPath = pathToCheck.toLowerCase();
return lowerPath.includes('dotnet') || lowerPath.includes('program') || lowerPath.includes('share') || lowerPath.includes('bin') || lowerPath.includes('snap') || lowerPath.includes('homebrew');
}
async function findPathWithRequirementAndInstall(version : string, iMode : DotnetInstallMode, arch : string, condition : DotnetVersionSpecRequirement, shouldFind : boolean, contextToLookFor? : IDotnetAcquireContext, setPath = true,
blockNoArch = false)
{
const installPath = await installRuntime(version, iMode, arch);
// use path.dirname : the dotnet.exe cant be on the PATH
if(setPath)
{
process.env.PATH = `${path.dirname(installPath)}${getPathSeparator()}${process.env.PATH?.split(getPathSeparator()).filter((x : string) => !(includesPathWithLikelyDotnet(x))).join(getPathSeparator())}`;
}
else
{
// remove dotnet so the test will work on machines with dotnet installed
process.env.PATH = `${process.env.PATH?.split(getPathSeparator()).filter((x : string) => !(includesPathWithLikelyDotnet(x))).join(getPathSeparator())}`;
process.env.DOTNET_ROOT = path.dirname(installPath);
}
extensionContext.environmentVariableCollection.replace('PATH', process.env.PATH ?? '');
if(blockNoArch)
{
extensionContext.environmentVariableCollection.replace('DOTNET_INSTALL_TOOL_DONT_ACCEPT_UNKNOWN_ARCH', '1');
process.env.DOTNET_INSTALL_TOOL_DONT_ACCEPT_UNKNOWN_ARCH = '1';
}
const result : IDotnetAcquireResult = await vscode.commands.executeCommand<IDotnetAcquireResult>('dotnet.findPath',
{ acquireContext : contextToLookFor ?? { version, requestingExtensionId : requestingExtensionId, mode: iMode, architecture : arch } as IDotnetAcquireContext,
versionSpecRequirement : condition} as IDotnetFindPathContext
);
extensionContext.environmentVariableCollection.replace('DOTNET_INSTALL_TOOL_DONT_ACCEPT_UNKNOWN_ARCH', '0');
process.env.DOTNET_INSTALL_TOOL_DONT_ACCEPT_UNKNOWN_ARCH = '0';
if(shouldFind)
{
assert.exists(result.dotnetPath, 'find path command returned a result');
assert.equal(result.dotnetPath.toLowerCase(), installPath.toLowerCase(), 'The path returned by findPath is correct');
}
else
{
assert.equal(result, undefined, 'find path command returned no undefined if no path matches condition');
}
}
test('Install Local Runtime Command', async () =>
{
await installRuntime('2.2', 'runtime');
}).timeout(standardTimeoutTime);
test('Install Local ASP.NET Runtime Command', async () =>
{
await installRuntime('7.0', 'aspnetcore');
}).timeout(standardTimeoutTime);
test('Uninstall One Local Runtime Command', async () => {
await installUninstallOne('2.2', '7.0', 'runtime', 'local');
}).timeout(standardTimeoutTime);
test('Uninstall One Local ASP.NET Runtime Command', async () => {
await installUninstallOne('2.2', '6.0', 'aspnetcore', 'local');
}).timeout(standardTimeoutTime);
test('Uninstall All Local Runtime Command', async () => {
await installUninstallAll('2.2', 'runtime')
}).timeout(standardTimeoutTime);
test('Uninstall All Local ASP.NET Runtime Command', async () => {
await installUninstallAll('2.2', 'aspnetcore')
}).timeout(standardTimeoutTime);
test('Uninstall Runtime Only Once No Owners Exist', async () => {
await uninstallWithMultipleOwners('8.0', 'runtime', 'local');
}).timeout(standardTimeoutTime);
test('Uninstall ASP.NET Runtime Only Once No Owners Exist', async () => {
await uninstallWithMultipleOwners('8.0', 'aspnetcore', 'local');
}).timeout(standardTimeoutTime);
test('Install and Uninstall Multiple Local Runtime Versions', async () => {
await installMultipleVersions(['2.2', '3.0', '3.1'], 'runtime');
}).timeout(standardTimeoutTime * 2);
test('Install and Uninstall Multiple Local ASP.NET Runtime Versions', async () => {
await installMultipleVersions(['2.2', '3.0', '3.1'], 'aspnetcore');
}).timeout(standardTimeoutTime * 2);
test('Find dotnet PATH Command Met Condition', async () => {
// install 5.0 then look for 5.0 path
await findPathWithRequirementAndInstall('5.0', 'runtime', os.arch(), 'greater_than_or_equal', true);
}).timeout(standardTimeoutTime);
test('Find dotnet PATH Command Met ROOT Condition', async () => {
// install 7.0, set dotnet_root and not path, then look for root
const oldROOT = process.env.DOTNET_ROOT;
await findPathWithRequirementAndInstall('7.0', 'runtime', os.arch(), 'equal', true,
{version : '7.0', mode : 'runtime', architecture : os.arch(), requestingExtensionId : requestingExtensionId}, false
);
if(EnvironmentVariableIsDefined(oldROOT))
{
process.env.DOTNET_ROOT = oldROOT;
}
else
{
delete process.env.DOTNET_ROOT;
}
}).timeout(standardTimeoutTime);
test('Find dotnet PATH Command Met Version Condition', async () => {
// Install 8.0, look for 3.1 with accepting dotnet gr than or eq to 3.1
await findPathWithRequirementAndInstall('8.0', 'runtime', os.arch(), 'greater_than_or_equal', true,
{version : '3.1', mode : 'runtime', architecture : os.arch(), requestingExtensionId : requestingExtensionId}
);
}).timeout(standardTimeoutTime);
test('Find dotnet PATH Command Met Version Condition with Double Digit Major', async () => {
await findPathWithRequirementAndInstall('9.0', 'runtime', os.arch(), 'less_than_or_equal', true,
{version : '11.0', mode : 'runtime', architecture : os.arch(), requestingExtensionId : requestingExtensionId}
);
}).timeout(standardTimeoutTime);
test('Find dotnet PATH Command Unmet Version Condition', async () => {
// Install 9.0, look for 90.0 which is not equal to 9.0
await findPathWithRequirementAndInstall('9.0', 'runtime', os.arch(), 'equal', false,
{version : '90.0', mode : 'runtime', architecture : os.arch(), requestingExtensionId : requestingExtensionId}
);
}).timeout(standardTimeoutTime);
test('Find dotnet PATH Command Unmet Mode Condition', async () => {
// look for 3.1 runtime but install 3.1 aspnetcore
await findPathWithRequirementAndInstall('3.1', 'runtime', os.arch(), 'equal', false,
{version : '3.1', mode : 'aspnetcore', architecture : os.arch(), requestingExtensionId : requestingExtensionId}
);
}).timeout(standardTimeoutTime);
test('Find dotnet PATH Command Unmet Arch Condition', async () => {
// look for a different architecture of 3.1
if(os.platform() !== 'darwin')
{
// The CI Machines are running on ARM64 for OS X.
// They also have an x64 HOST. We can't set DOTNET_MULTILEVEL_LOOKUP to 0 because it will break the ability to find the host on --info
// As a 3.1 runtime host does not provide the architecture, but we try to use 3.1 because CI machines won't have it.
//
await findPathWithRequirementAndInstall('3.1', 'runtime', os.arch() == 'arm64' ? 'x64' : os.arch(), 'greater_than_or_equal', false,
{version : '3.1', mode : 'runtime', architecture : 'arm64', requestingExtensionId : requestingExtensionId}, true, true
);
}
}).timeout(standardTimeoutTime);
test('Find dotnet PATH Command Unmet Arch Condition With Host that prints Arch', async () => {
if(os.platform() !== 'darwin')
{
await findPathWithRequirementAndInstall('9.0', 'runtime', os.arch() == 'arm64' ? 'x64' : os.arch(), 'greater_than_or_equal', false,
{version : '9.0', mode : 'runtime', architecture : 'arm64', requestingExtensionId : requestingExtensionId}
);
}
}).timeout(standardTimeoutTime);
test('Find dotnet PATH Command No Arch Available But Accept By Default', async () => {
// look for a different architecture of 3.1
if(os.platform() !== 'darwin')
{
await findPathWithRequirementAndInstall('3.1', 'runtime', os.arch() == 'arm64' ? 'x64' : os.arch(), 'greater_than_or_equal', true,
{version : '3.1', mode : 'runtime', architecture : 'arm64', requestingExtensionId : requestingExtensionId}
);
}
}).timeout(standardTimeoutTime);
test('Find dotnet PATH Command Unmet Runtime Patch Condition', async () => {
// Install 8.0.{LATEST, which will be < 99}, look for 8.0.99 with accepting dotnet gr than or eq to 8.0.99
// No tests for SDK since that's harder to replicate with a global install and different machine states
await findPathWithRequirementAndInstall('8.0', 'runtime', os.arch(), 'greater_than_or_equal', true,
{version : '8.0.99', mode : 'runtime', architecture : os.arch(), requestingExtensionId : requestingExtensionId}
);
}).timeout(standardTimeoutTime);
test('Install SDK Globally E2E (Requires Admin)', async () => {
// We only test if the process is running under ADMIN because non-admin requires user-intervention.
if(new FileUtilities().isElevated())
{
const originalPath = process.env.PATH;
const sdkVersion = '7.0.103';
const context : IDotnetAcquireContext = { version: sdkVersion, requestingExtensionId: 'sample-extension', installType: 'global' };
// We cannot use the describe pattern to restore the environment variables using vscode's extension testing infrastructure.
// So we must set and unset it ourselves, which isn't ideal as this variable could remain.
let result : IDotnetAcquireResult;
let error : any;
let pathAfterInstall;
// We cannot test much as we don't want to leave global installs on dev boxes. But we do want to make sure the e-2-e goes through the right path. Vendors can test the rest.
// So we have this environment variable that tells us to stop before running any real install.
process.env.VSCODE_DOTNET_GLOBAL_INSTALL_FAKE_PATH = 'true';
try
{
result = await vscode.commands.executeCommand<IDotnetAcquireResult>('dotnet.acquireGlobalSDK', context);
}
catch(err)
{
error = err;
}
finally
{
pathAfterInstall = process.env.PATH;
process.env.VSCODE_DOTNET_GLOBAL_INSTALL_FAKE_PATH = undefined;
process.env.PATH = originalPath;
if(error)
{
throw(new Error(`The test failed to run the acquire command successfully. Error: ${error}`));
}
}
assert.exists(result!, 'The global acquisition command did not provide a result?');
assert.exists(result!.dotnetPath);
assert.equal(result!.dotnetPath, 'fake-sdk');
assert.exists(pathAfterInstall, 'The environment variable PATH for DOTNET was not found?');
assert.include(pathAfterInstall, result!.dotnetPath, 'Is the PATH correctly set by the global installer?');
}
else
{
// We could run the installer without privilege but it would require human interaction to use the UAC
// And we wouldn't be able to kill the process so the test would leave a lot of hanging processes on the machine
warn('The Global SDK E2E Install test cannot run as the machine is unprivileged.');
}
}).timeout(standardTimeoutTime*1000);
test('Telemetry Sent During Install and Uninstall', async () => {
if(!vscode.env.isTelemetryEnabled)
{
console.warn('The telemetry test cannot run as VS Code Telemetry is disabled in user settings.');
return;
}
const rntVersion = '2.2';
const fullyResolvedVersion = '2.2.8'; // 2.2 is very much out of support, so we don't expect this to change to a newer version
const installId = getInstallIdCustomArchitecture(fullyResolvedVersion, os.arch(), 'runtime', 'local');
const context: IDotnetAcquireContext = { version: rntVersion, requestingExtensionId };
const result = await vscode.commands.executeCommand<IDotnetAcquireResult>('dotnet.acquire', context);
assert.exists(result);
assert.exists(result!.dotnetPath);
assert.include(result!.dotnetPath, context.version);
// Check that we got the expected telemetry
const requestedEvent = MockTelemetryReporter.telemetryEvents.find((event: ITelemetryEvent) => event.eventName === 'DotnetAcquisitionRequested');
assert.exists(requestedEvent, 'The acquisition requested event is found');
assert.include(requestedEvent!.properties!.AcquisitionStartVersion, rntVersion, 'The acquisition requested event contains the version');
// assert that the extension id is hashed by checking that it DNE
assert.notInclude(requestedEvent!.properties!.RequestingExtensionId, requestingExtensionId, 'The extension id is hashed in telemetry');
const startedEvent = MockTelemetryReporter.telemetryEvents.find((event: ITelemetryEvent) => event.eventName === 'DotnetAcquisitionStarted');
assert.exists(startedEvent, 'Acquisition started event gets published');
assert.include(startedEvent!.properties!.AcquisitionStartVersion, rntVersion, 'Acquisition started event has a starting version');
assert.include(startedEvent!.properties!.AcquisitionInstallId, installId, 'Acquisition started event has a install key');
const completedEvent = MockTelemetryReporter.telemetryEvents.find((event: ITelemetryEvent) => event.eventName === 'DotnetAcquisitionCompleted');
assert.exists(completedEvent, 'Acquisition completed events exist');
assert.include(completedEvent!.properties!.AcquisitionCompletedVersion, rntVersion, 'Acquisition completed events have a version');
await vscode.commands.executeCommand<string>('dotnet.uninstallAll');
assert.isFalse(fs.existsSync(result!.dotnetPath), 'Dotnet is uninstalled correctly.');
const uninstallStartedEvent = MockTelemetryReporter.telemetryEvents.find((event: ITelemetryEvent) => event.eventName === 'DotnetUninstallAllStarted');
assert.exists(uninstallStartedEvent, 'Uninstall All is reported in telemetry');
const uninstallCompletedEvent = MockTelemetryReporter.telemetryEvents.find((event: ITelemetryEvent) => event.eventName === 'DotnetUninstallAllCompleted');
assert.exists(uninstallCompletedEvent, 'Uninstall All success is reported in telemetry');
// Check that no errors were reported
const errors = MockTelemetryReporter.telemetryEvents.filter((event: ITelemetryEvent) => event.eventName.includes('Error') && event.eventName !== 'CommandExecutionStdError');
assert.isEmpty(errors, `No error events were reported in telemetry reporting. ${JSON.stringify(errors)}`);
}).timeout(standardTimeoutTime);
test('Telemetry Sent on Error', async () => {
if(!vscode.env.isTelemetryEnabled)
{
console.warn('The telemetry test cannot run as VS Code Telemetry is disabled in user settings.');
return;
}
const context: IDotnetAcquireContext = { version: 'foo', requestingExtensionId };
try {
await vscode.commands.executeCommand<IDotnetAcquireResult>('dotnet.acquire', context);
assert.isTrue(false); // An error should have been thrown
} catch (error) {
const versionError = MockTelemetryReporter.telemetryEvents.find((event: ITelemetryEvent) => event.eventName === '[ERROR]:DotnetVersionResolutionError');
assert.exists(versionError, 'The version resolution error appears in telemetry');
}
}).timeout(standardTimeoutTime/2);
test('Install Local Runtime Command Passes With Warning With No RequestingExtensionId', async () => {
const context: IDotnetAcquireContext = { version: '3.1' };
const result = await vscode.commands.executeCommand<IDotnetAcquireResult>('dotnet.acquire', context);
assert.exists(result, 'A result from the API exists');
assert.exists(result!.dotnetPath, 'The result has a dotnet path');
assert.include(result!.dotnetPath, context.version, 'The version is included in the path');
assert.include(mockDisplayWorker.warningMessage, 'Ignoring existing .NET paths');
}).timeout(standardTimeoutTime);
test('Install Local Runtime Command With Path Setting', async () => {
const context: IDotnetAcquireContext = { version: '5.0', requestingExtensionId: 'alternative.extension' };
const resultForAcquiringPathSettingRuntime = await vscode.commands.executeCommand<IDotnetAcquireResult>('dotnet.acquire', context);
assert.exists(resultForAcquiringPathSettingRuntime!.dotnetPath, 'Basic acquire works');
// The runtime setting on the path needs to be a match for a runtime but also a different folder name
// so that we can tell the setting was used. We cant tell it to install an older besides latest,
// but we can rename the folder then re-acquire for latest and see that it uses the existing 'older' runtime path
assert.notEqual(path.dirname(resultForAcquiringPathSettingRuntime.dotnetPath), path.dirname(pathWithIncorrectVersionForTest));
fs.cpSync(path.dirname(resultForAcquiringPathSettingRuntime.dotnetPath), path.dirname(pathWithIncorrectVersionForTest), {recursive: true});
assert.isTrue(fs.existsSync(path.dirname(pathWithIncorrectVersionForTest)), 'The copy of the real dotnet to the new wrong-versioned path succeeded');
fs.rmSync(resultForAcquiringPathSettingRuntime.dotnetPath);
assert.isTrue(!fs.existsSync(resultForAcquiringPathSettingRuntime.dotnetPath), 'The deletion of the real path succeeded');
const result = await vscode.commands.executeCommand<IDotnetAcquireResult>('dotnet.acquire', context);
assert.exists(result);
assert.exists(result!.dotnetPath);
assert.equal(result!.dotnetPath, pathWithIncorrectVersionForTest); // this is set for the alternative.extension in the settings
}).timeout(standardTimeoutTime);
test('List Sdks & Runtimes', async () => {
const mockAcquisitionContext = getMockAcquisitionContext('sdk', '');
const webWorker = new MockWebRequestWorker(mockAcquisitionContext, '');
webWorker.response = JSON.parse(mockReleasesData);
// The API can find the available SDKs and list their versions.
const apiContext: IDotnetListVersionsContext = { listRuntimes: false };
const result = await vscode.commands.executeCommand<IDotnetListVersionsResult>('dotnet.listVersions', apiContext, webWorker);
assert.exists(result);
assert.equal(result?.length, 2, `It can find both versions of the SDKs. Found: ${result}`);
assert.equal(result?.filter((sdk : any) => sdk.version === '7.0.202').length, 1, 'The mock SDK with the expected version {7.0.200} was not found by the API parsing service.');
assert.equal(result?.filter((sdk : any) => sdk.channelVersion === '7.0').length, 1, 'The mock SDK with the expected channel version {7.0} was not found by the API parsing service.');
assert.equal(result?.filter((sdk : any) => sdk.supportPhase === 'active').length, 1, 'The mock SDK with the expected support phase of {active} was not found by the API parsing service.');
// The API can find the available runtimes and their versions.
apiContext.listRuntimes = true;
const runtimeResult = await vscode.commands.executeCommand<IDotnetListVersionsResult>('dotnet.listVersions', apiContext, webWorker);
assert.exists(runtimeResult);
assert.equal(runtimeResult?.length, 2, `It can find both versions of the runtime. Found: ${result}`);
assert.equal(runtimeResult?.filter((runtime : any) => runtime.version === '7.0.4').length, 1, 'The mock Runtime with the expected version was not found by the API parsing service.');
}).timeout(standardTimeoutTime);
test('Get Recommended SDK Version', async () => {
const mockAcquisitionContext = getMockAcquisitionContext('sdk', '');
const webWorker = new MockWebRequestWorker(mockAcquisitionContext, '');
webWorker.response = JSON.parse(mockReleasesData);
const result = await vscode.commands.executeCommand<IDotnetListVersionsResult>('dotnet.recommendedVersion', null, webWorker);
assert.exists(result);
if(os.platform() !== 'linux')
{
assert.equal(result[0].version, '7.0.202', 'The SDK did not recommend the version it was supposed to, which should be {7.0.200} from the mock data.');
}
else
{
assert.equal(result[0].version, '8.0.1xx', 'The SDK did not recommend the version it was supposed to, which should be N.0.1xx based on surface level distro knowledge. If a new version is available, this test may need to be updated to the newest version.');
}
}).timeout(standardTimeoutTime);
async function testAcquire(installMode : DotnetInstallMode)
{
// Runtime is not yet installed
const context: IDotnetAcquireContext = { version: '3.1', requestingExtensionId, mode: installMode };
let result = await vscode.commands.executeCommand<IDotnetAcquireResult>('dotnet.acquireStatus', context);
assert.notExists(result);
// Install runtime
result = await vscode.commands.executeCommand<IDotnetAcquireResult>('dotnet.acquire', context);
assert.exists(result);
assert.exists(result!.dotnetPath);
assert.isTrue(fs.existsSync(result!.dotnetPath!));
// Runtime has been installed
result = await vscode.commands.executeCommand<IDotnetAcquireResult>('dotnet.acquireStatus', context);
assert.exists(result);
assert.exists(result!.dotnetPath);
assert.isTrue(fs.existsSync(result!.dotnetPath!));
rimraf.sync(result!.dotnetPath!);
}
test('Install Runtime Status Command', async () =>
{
await testAcquire('runtime');
}).timeout(standardTimeoutTime);
test('Install Aspnet runtime Status Command', async () =>
{
await testAcquire('aspnetcore');
}).timeout(standardTimeoutTime);
test('acquireStatus does respect Mode', async () =>
{
const runtimeContext : IDotnetAcquireContext = { version: '5.0', requestingExtensionId, mode: 'runtime' };
const aspNetContext : IDotnetAcquireContext = { version: '5.0', requestingExtensionId, mode: 'aspnetcore' };
let result = await vscode.commands.executeCommand<IDotnetAcquireResult>('dotnet.acquireStatus', runtimeContext);
assert.notExists(result);
result = await vscode.commands.executeCommand<IDotnetAcquireResult>('dotnet.acquireStatus', aspNetContext);
assert.notExists(result);
result = await vscode.commands.executeCommand<IDotnetAcquireResult>('dotnet.acquire', runtimeContext);
assert.exists(result);
result = await vscode.commands.executeCommand<IDotnetAcquireResult>('dotnet.acquireStatus', aspNetContext);
assert.equal(undefined, result, 'Acquire Status for no ASP.NET installed when Runtime is installed should not mistake Runtime Install as ASP.NET Install');
}).timeout(standardTimeoutTime);
});