|
1 | 1 | using System.Collections.Generic; |
2 | 2 | using System.Diagnostics; |
3 | 3 | using System.IO; |
| 4 | +using System.Linq; |
4 | 5 | using System.Threading.Tasks; |
| 6 | +using System.Xml.Linq; |
5 | 7 | using Microsoft.Test.Apex.VisualStudio.Solution; |
6 | 8 | using Microsoft.VisualStudio.TestTools.UnitTesting; |
7 | 9 |
|
@@ -269,6 +271,59 @@ public void NetCoreVSAndMSBuildRestoreIsNoOp() |
269 | 271 | "MSBuild restore should be a no-op after VS restore - cache file timestamp should not change."); |
270 | 272 | } |
271 | 273 |
|
| 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 | + |
272 | 327 | // There is a bug with VS or Apex where NetCoreConsoleApp and NetCoreClassLib create netcore 2.1 projects that are not supported by the sdk |
273 | 328 | // Commenting out any NetCoreConsoleApp or NetCoreClassLib template and swapping it for NetStandardClassLib as both are package ref. |
274 | 329 |
|
|
0 commit comments