|
1 | 1 | using System.Collections.Generic; |
| 2 | +using System.Diagnostics; |
2 | 3 | using System.IO; |
3 | 4 | using System.Threading.Tasks; |
4 | 5 | using Microsoft.Test.Apex.VisualStudio.Solution; |
@@ -233,6 +234,79 @@ public async Task WithSourceMappingEnabled_InstallPackageFromPMUIAndNoSourcesFou |
233 | 234 | } |
234 | 235 | } |
235 | 236 |
|
| 237 | + // Migrated from Test-NetCoreProjectSystemCacheUpdateEvent in NetCoreProjectTest.ps1 |
| 238 | + [TestMethod] |
| 239 | + [Timeout(DefaultTimeout)] |
| 240 | + public async Task InstallPackageFromPMC_TriggersNuGetCacheUpdatedEventAsync() |
| 241 | + { |
| 242 | + // Arrange |
| 243 | + using var testContext = new ApexTestContext(VisualStudio, ProjectTemplate.NetCoreConsoleApp, Logger, addNetStandardFeeds: true); |
| 244 | + |
| 245 | + var packageName = "TestPackage"; |
| 246 | + var packageVersion = "1.0.0"; |
| 247 | + await CommonUtility.CreatePackageInSourceAsync(testContext.PackageSource, packageName, packageVersion); |
| 248 | + |
| 249 | + testContext.SolutionService.Build(); |
| 250 | + testContext.NuGetApexTestService.WaitForAutoRestore(); |
| 251 | + |
| 252 | + var nugetConsole = GetConsole(testContext.Project); |
| 253 | + |
| 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' }"); |
| 267 | + |
| 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()}"); |
| 271 | + } |
| 272 | + |
| 273 | + // Migrated from Test-NetCoreVSandMSBuildNoOp in NetCoreProjectTest.ps1 |
| 274 | + [Ignore("https://github.com/NuGet/Home/issues/13003")] |
| 275 | + [TestMethod] |
| 276 | + [Timeout(DefaultTimeout)] |
| 277 | + public void NetCoreVSandMSBuildRestoreIsNoOp() |
| 278 | + { |
| 279 | + // Arrange |
| 280 | + using var testContext = new ApexTestContext(VisualStudio, ProjectTemplate.NetCoreConsoleApp, Logger, addNetStandardFeeds: true); |
| 281 | + |
| 282 | + testContext.SolutionService.Build(); |
| 283 | + testContext.NuGetApexTestService.WaitForAutoRestore(); |
| 284 | + |
| 285 | + var cacheFilePath = CommonUtility.GetCacheFilePath(testContext.Project.FullPath); |
| 286 | + CommonUtility.WaitForFileExists(cacheFilePath); |
| 287 | + |
| 288 | + var vsRestoreTimestamp = File.GetLastWriteTime(cacheFilePath.FullName).Ticks; |
| 289 | + |
| 290 | + // Act - run MSBuild restore externally |
| 291 | + using var process = new Process(); |
| 292 | + process.StartInfo.FileName = "dotnet"; |
| 293 | + process.StartInfo.Arguments = $"msbuild /t:restore \"{testContext.Project.FullPath}\""; |
| 294 | + process.StartInfo.UseShellExecute = false; |
| 295 | + process.StartInfo.RedirectStandardOutput = true; |
| 296 | + process.StartInfo.RedirectStandardError = true; |
| 297 | + process.Start(); |
| 298 | + string standardError = process.StandardError.ReadToEnd(); |
| 299 | + process.WaitForExit(); |
| 300 | + |
| 301 | + Assert.AreEqual(0, process.ExitCode, $"MSBuild restore failed: {standardError}"); |
| 302 | + |
| 303 | + var msbuildRestoreTimestamp = File.GetLastWriteTime(cacheFilePath.FullName).Ticks; |
| 304 | + |
| 305 | + // Assert - MSBuild restore should be a no-op, cache file timestamp should not change |
| 306 | + Assert.AreEqual(vsRestoreTimestamp, msbuildRestoreTimestamp, |
| 307 | + "MSBuild restore should be a no-op after VS restore - cache file timestamp should not change."); |
| 308 | + } |
| 309 | + |
236 | 310 | // There is a bug with VS or Apex where NetCoreConsoleApp and NetCoreClassLib create netcore 2.1 projects that are not supported by the sdk |
237 | 311 | // Commenting out any NetCoreConsoleApp or NetCoreClassLib template and swapping it for NetStandardClassLib as both are package ref. |
238 | 312 |
|
|
0 commit comments