Skip to content

Commit e380909

Browse files
Copilotnkolev92
andcommitted
Use ISolutionManager from NuGetApexTestService instead of PowerShell commands
Fetch ISolutionManager via VisualStudioObjectProviders.GetComponentModelService (matching the IVsPackageInstaller pattern) and subscribe to AfterNuGetCacheUpdated in C# with ManualResetEventSlim. Agent-Logs-Url: https://github.com/NuGet/NuGet.Client/sessions/89d1f6cd-cac2-473e-a612-1f47b026fb2b Co-authored-by: nkolev92 <[email protected]>
1 parent f5f5801 commit e380909

2 files changed

Lines changed: 27 additions & 17 deletions

File tree

test/NuGet.Tests.Apex/NuGet.Tests.Apex/Apex/NuGetApexTestService.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
using Microsoft.VisualStudio.Shell.Interop;
1919
using Microsoft.VisualStudio.Shell.ServiceBroker;
2020
using NuGet.Console.TestContract;
21+
using NuGet.PackageManagement;
2122
using NuGet.PackageManagement.UI.TestContract;
2223
using NuGet.SolutionRestoreManager;
2324
using NuGet.Versioning;
@@ -56,6 +57,11 @@ protected internal IVsSolutionRestoreStatusProvider SolutionRestoreStatusProvide
5657

5758
protected internal IVsPathContextProvider2 PathContextProvider2 => VisualStudioObjectProviders.GetComponentModelService<IVsPathContextProvider2>();
5859

60+
/// <summary>
61+
/// Gets the NuGet ISolutionManager
62+
/// </summary>
63+
protected internal ISolutionManager SolutionManager => VisualStudioObjectProviders.GetComponentModelService<ISolutionManager>();
64+
5965
/// <summary>
6066
/// Wait for all nominations and auto restore to complete.
6167
/// This uses an Action to log since the xunit logger is not fully serializable.

test/NuGet.Tests.Apex/NuGet.Tests.Apex/NuGetEndToEndTests/NetCoreProjectTestCase.cs

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
using System.Collections.Generic;
22
using System.Diagnostics;
33
using System.IO;
4+
using System.Threading;
45
using System.Threading.Tasks;
56
using Microsoft.Test.Apex.VisualStudio.Solution;
67
using Microsoft.VisualStudio.TestTools.UnitTesting;
8+
using NuGet.PackageManagement;
79

810
namespace NuGet.Tests.Apex
911
{
@@ -249,25 +251,27 @@ public async Task InstallPackageFromPMC_TriggersNuGetCacheUpdatedEventAsync()
249251
testContext.SolutionService.Build();
250252
testContext.NuGetApexTestService.WaitForAutoRestore();
251253

252-
var nugetConsole = GetConsole(testContext.Project);
254+
// Subscribe to the ISolutionManager.AfterNuGetCacheUpdated event
255+
using var cacheUpdatedEvent = new ManualResetEventSlim(false);
256+
var solutionManager = testContext.NuGetApexTestService.SolutionManager;
257+
void OnAfterNuGetCacheUpdated(object sender, NuGetEventArgs<string> e) => cacheUpdatedEvent.Set();
258+
solutionManager.AfterNuGetCacheUpdated += OnAfterNuGetCacheUpdated;
253259

254-
// Register for the ISolutionManager.AfterNuGetCacheUpdated event via PMC PowerShell session
255-
nugetConsole.Execute("Get-Event | Remove-Event");
256-
nugetConsole.Execute("$componentModel = Get-VSComponentModel");
257-
nugetConsole.Execute("$solutionManager = $componentModel.GetService([NuGet.PackageManagement.ISolutionManager])");
258-
nugetConsole.Execute("Register-ObjectEvent -InputObject $solutionManager -EventName AfterNuGetCacheUpdated -SourceIdentifier SolutionManagerCacheUpdated");
259-
260-
// Act
261-
nugetConsole.InstallPackageFromPMC(packageName, packageVersion);
262-
263-
// Assert - verify the cache update event was raised
264-
nugetConsole.Execute("$cacheEvent = Wait-Event -SourceIdentifier SolutionManagerCacheUpdated -TimeoutSec 10");
265-
nugetConsole.Execute("Unregister-Event -SourceIdentifier SolutionManagerCacheUpdated");
266-
nugetConsole.Execute("if ($cacheEvent) { Write-Host 'CACHE_EVENT_RECEIVED' } else { Write-Host 'CACHE_EVENT_NOT_RECEIVED' }");
260+
try
261+
{
262+
// Act
263+
var nugetConsole = GetConsole(testContext.Project);
264+
nugetConsole.InstallPackageFromPMC(packageName, packageVersion);
267265

268-
Assert.IsTrue(
269-
nugetConsole.IsMessageFoundInPMC("CACHE_EVENT_RECEIVED"),
270-
$"Cache update event should have been raised after package install. Actual PMC output: {nugetConsole.GetText()}");
266+
// Assert
267+
Assert.IsTrue(
268+
cacheUpdatedEvent.Wait(TimeSpan.FromSeconds(10)),
269+
"Cache update event should have been raised after package install.");
270+
}
271+
finally
272+
{
273+
solutionManager.AfterNuGetCacheUpdated -= OnAfterNuGetCacheUpdated;
274+
}
271275
}
272276

273277
// Migrated from Test-NetCoreVSandMSBuildNoOp in NetCoreProjectTest.ps1

0 commit comments

Comments
 (0)