Skip to content

Commit 281447d

Browse files
Copilotnkolev92
andauthored
Add NetCoreMultipleTargetFrameworksVSAndMSBuildRestoreIsNoOp test for multi-targeted projects
Agent-Logs-Url: https://github.com/NuGet/NuGet.Client/sessions/247a9488-be33-43c9-9494-fadabefae6c8 Co-authored-by: nkolev92 <[email protected]>
1 parent ad4945f commit 281447d

1 file changed

Lines changed: 55 additions & 0 deletions

File tree

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

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
using System.Collections.Generic;
22
using System.Diagnostics;
33
using System.IO;
4+
using System.Linq;
45
using System.Threading.Tasks;
6+
using System.Xml.Linq;
57
using Microsoft.Test.Apex.VisualStudio.Solution;
68
using Microsoft.VisualStudio.TestTools.UnitTesting;
79

@@ -269,6 +271,59 @@ public void NetCoreVSAndMSBuildRestoreIsNoOp()
269271
"MSBuild restore should be a no-op after VS restore - cache file timestamp should not change.");
270272
}
271273

274+
// Migrated from Test-NetCoreMultipleTargetFrameworksVSandMSBuildNoOp in NetCoreProjectTest.ps1
275+
[TestMethod]
276+
[Timeout(DefaultTimeout)]
277+
public void NetCoreMultipleTargetFrameworksVSAndMSBuildRestoreIsNoOp()
278+
{
279+
// Arrange - create project and modify it to be multi-targeted
280+
using var testContext = new ApexTestContext(VisualStudio, ProjectTemplate.NetCoreConsoleApp, Logger, addNetStandardFeeds: true);
281+
282+
testContext.Project.Save();
283+
var doc = XDocument.Load(testContext.Project.FullPath);
284+
var ns = doc.Root.Name.Namespace;
285+
286+
// Switch TargetFramework to TargetFrameworks with multiple TFMs
287+
var tfElement = doc.Root.Descendants(ns + "TargetFramework").FirstOrDefault();
288+
if (tfElement != null)
289+
{
290+
tfElement.Name = ns + "TargetFrameworks";
291+
tfElement.Value = "net8.0;netstandard2.0";
292+
}
293+
294+
// Remove OutputType since netstandard2.0 doesn't support Exe
295+
var outputTypeElement = doc.Root.Descendants(ns + "OutputType").FirstOrDefault();
296+
outputTypeElement?.Remove();
297+
298+
doc.Save(testContext.Project.FullPath);
299+
300+
testContext.SolutionService.Build();
301+
testContext.NuGetApexTestService.WaitForAutoRestore();
302+
303+
var cacheFilePath = CommonUtility.GetCacheFilePath(testContext.Project.FullPath);
304+
CommonUtility.WaitForFileExists(cacheFilePath);
305+
306+
var vsRestoreTimestamp = File.GetLastWriteTime(cacheFilePath.FullName).Ticks;
307+
308+
// Act - run MSBuild restore externally
309+
using var process = new Process();
310+
process.StartInfo.FileName = "dotnet";
311+
process.StartInfo.Arguments = $"msbuild /t:restore \"{testContext.Project.FullPath}\"";
312+
process.StartInfo.UseShellExecute = false;
313+
process.StartInfo.RedirectStandardError = true;
314+
process.Start();
315+
string standardError = process.StandardError.ReadToEnd();
316+
process.WaitForExit();
317+
318+
Assert.AreEqual(0, process.ExitCode, $"MSBuild restore failed: {standardError}");
319+
320+
var msbuildRestoreTimestamp = File.GetLastWriteTime(cacheFilePath.FullName).Ticks;
321+
322+
// Assert - MSBuild restore should be a no-op for multi-targeted project
323+
Assert.AreEqual(vsRestoreTimestamp, msbuildRestoreTimestamp,
324+
"MSBuild restore should be a no-op after VS restore for multi-targeted project - cache file timestamp should not change.");
325+
}
326+
272327
// There is a bug with VS or Apex where NetCoreConsoleApp and NetCoreClassLib create netcore 2.1 projects that are not supported by the sdk
273328
// Commenting out any NetCoreConsoleApp or NetCoreClassLib template and swapping it for NetStandardClassLib as both are package ref.
274329

0 commit comments

Comments
 (0)