Skip to content

Commit 86cad5e

Browse files
Copilotnkolev92
andcommitted
Migrate NetCore E2E tests from PS to Apex
Migrate Test-NetCoreProjectSystemCacheUpdateEvent and Test-NetCoreVSandMSBuildNoOp from NetCoreProjectTest.ps1 to NetCoreProjectTestCase.cs Apex tests. Remove Test-NetCoreConsoleAppClean and Test-NetCoreConsoleAppRebuildDoesNotDeleteCacheFile from PS (already covered by Daily Apex VerifyCacheFileInsideObjFolder). Three remaining PS tests (TargetFrameworks, MultipleTargetFrameworks, Tools NoOp) are left as-is since they use project templates without Apex equivalents. Agent-Logs-Url: https://github.com/NuGet/NuGet.Client/sessions/9ef1f0b8-2661-453f-92d3-d71db5dd4ac2 Co-authored-by: nkolev92 <[email protected]>
1 parent bff510e commit 86cad5e

2 files changed

Lines changed: 74 additions & 93 deletions

File tree

test/EndToEnd/tests/NetCoreProjectTest.ps1

Lines changed: 0 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,3 @@
1-
# VSSolutionManager and ProjectSystemCache event test for .net core
2-
function Test-NetCoreProjectSystemCacheUpdateEvent {
3-
4-
# Arrange
5-
$projectA = New-NetCoreConsoleApp
6-
Build-Solution
7-
Assert-NetCoreProjectCreation $projectA
8-
9-
$componentModel = Get-VSComponentModel
10-
$solutionManager = $componentModel.GetService([NuGet.PackageManagement.ISolutionManager])
11-
12-
$cacheEvent = $null
13-
14-
Get-Event | Remove-Event
15-
Register-ObjectEvent -InputObject $solutionManager -EventName AfterNuGetCacheUpdated -SourceIdentifier SolutionManagerCacheUpdated
16-
17-
Try
18-
{
19-
# Act
20-
$projectA | Install-Package Newtonsoft.Json -Version '13.0.1'
21-
22-
$cacheEvent = Wait-Event -SourceIdentifier SolutionManagerCacheUpdated -TimeoutSec 10
23-
}
24-
Finally
25-
{
26-
Unregister-Event -SourceIdentifier SolutionManagerCacheUpdated
27-
}
28-
29-
# Assert
30-
Assert-NotNull $cacheEvent -Message "Cache update event should've been raised"
31-
}
32-
33-
function Test-NetCoreConsoleAppClean {
34-
35-
# Arrange & Act
36-
$project = New-NetCoreConsoleApp ConsoleApp
37-
38-
Build-Solution
39-
40-
Assert-ProjectCacheFileExists $project
41-
42-
#Act
43-
Clean-Solution
44-
45-
#Assert
46-
Assert-ProjectCacheFileNotExists $project
47-
}
48-
49-
function Test-NetCoreConsoleAppRebuildDoesNotDeleteCacheFile {
50-
# Arrange & Act
51-
$project = New-NetCoreConsoleApp ConsoleApp
52-
Build-Solution
53-
54-
Assert-ProjectCacheFileExists $project
55-
56-
AdviseSolutionEvents
57-
58-
#Act
59-
Rebuild-Solution
60-
61-
WaitUntilRebuildCompleted
62-
UnadviseSolutionEvents
63-
64-
#Assert
65-
Assert-ProjectCacheFileExists $project
66-
}
67-
68-
function Test-NetCoreVSandMSBuildNoOp {
69-
[SkipTest('https://github.com/NuGet/Home/issues/13003')]
70-
param ()
71-
72-
# Arrange
73-
$project = New-NetCoreConsoleApp ConsoleApp
74-
Build-Solution
75-
76-
Assert-ProjectCacheFileExists $project
77-
$cacheFile = Get-ProjectCacheFilePath $project
78-
79-
#Act
80-
81-
$VSRestoreTimestamp =( [datetime](Get-ItemProperty -Path $cacheFile -Name LastWriteTime).lastwritetime).Ticks
82-
83-
$MSBuildExe = Get-MSBuildExe
84-
85-
& "$MSBuildExe" /t:restore $project.FullName
86-
Assert-True ($LASTEXITCODE -eq 0)
87-
88-
$MsBuildRestoreTimestamp =( [datetime](Get-ItemProperty -Path $cacheFile -Name LastWriteTime).lastwritetime).Ticks
89-
90-
#Assert
91-
Assert-True ($MsBuildRestoreTimestamp -eq $VSRestoreTimestamp)
92-
}
93-
941
function Test-NetCoreTargetFrameworksVSandMSBuildNoOp {
952
[SkipTest('https://github.com/NuGet/Home/issues/13003')]
963
param ()

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

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Collections.Generic;
2+
using System.Diagnostics;
23
using System.IO;
34
using System.Threading.Tasks;
45
using Microsoft.Test.Apex.VisualStudio.Solution;
@@ -233,6 +234,79 @@ public async Task WithSourceMappingEnabled_InstallPackageFromPMUIAndNoSourcesFou
233234
}
234235
}
235236

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+
236310
// There is a bug with VS or Apex where NetCoreConsoleApp and NetCoreClassLib create netcore 2.1 projects that are not supported by the sdk
237311
// Commenting out any NetCoreConsoleApp or NetCoreClassLib template and swapping it for NetStandardClassLib as both are package ref.
238312

0 commit comments

Comments
 (0)