GivenThatWeWantToPackACppCliProject.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. namespace Microsoft.NET.Build.Tests
  4. {
  5. public class GivenThatWeWantToPackACppCliProject : SdkTest
  6. {
  7. public GivenThatWeWantToPackACppCliProject(ITestOutputHelper log) : base(log)
  8. {
  9. }
  10. [FullMSBuildOnlyFact]
  11. public void It_cannot_pack_the_cppcliproject()
  12. {
  13. var testAsset = _testAssetsManager
  14. .CopyTestAsset("NetCoreCsharpAppReferenceCppCliLib")
  15. .WithSource()
  16. .WithProjectChanges((projectPath, project) => AddPackageReference(projectPath, project, "NewtonSoft.Json", ToolsetInfo.GetNewtonsoftJsonPackageVersion()))
  17. .WithProjectChanges((projectPath, project) => AddBuildProperty(projectPath, project, "EnableManagedpackageReferenceSupport", "true"));
  18. new PackCommand(Log, Path.Combine(testAsset.TestRoot, "NETCoreCppCliTest", "NETCoreCppCliTest.vcxproj"))
  19. .Execute("-p:Platform=x64", "-p:EnableManagedpackageReferenceSupport=true")
  20. .Should()
  21. .Fail()
  22. .And
  23. .HaveStdOutContaining("MSB4057"); // We don't get NETSDK1118 when enabling PackageReference support but can't resolve the apphost without it
  24. }
  25. private void AddPackageReference(string projectPath, XDocument project, string package, string version)
  26. {
  27. if (Path.GetExtension(projectPath) == ".vcxproj")
  28. {
  29. XNamespace ns = project.Root.Name.Namespace;
  30. XElement itemGroup = project.Root.Descendants(ns + "ItemGroup").First();
  31. itemGroup.Add(new XElement(ns + "PackageReference", new XAttribute("Include", package),
  32. new XAttribute("Version", version)));
  33. }
  34. }
  35. private void AddBuildProperty(string projectPath, XDocument project, string property, string value)
  36. {
  37. if (Path.GetExtension(projectPath) == ".vcxproj")
  38. {
  39. XNamespace ns = project.Root.Name.Namespace;
  40. XElement propertyGroup = project.Root.Descendants(ns + "PropertyGroup").First();
  41. propertyGroup.Add(new XElement(ns + $"{property}", value));
  42. }
  43. }
  44. }
  45. }