|
4 | 4 | using System; |
5 | 5 | using System.Linq; |
6 | 6 | using System.Threading.Tasks; |
| 7 | +using System.Xml.Linq; |
7 | 8 | using FluentAssertions; |
8 | 9 | using NuGet.Commands.Test; |
9 | 10 | using NuGet.Common; |
@@ -71,6 +72,110 @@ await SimpleTestPackageUtility.CreatePackagesAsync( |
71 | 72 | result.LockFile.Targets[1].Libraries[0].Name.Should().Be("y"); |
72 | 73 | } |
73 | 74 |
|
| 75 | + // P1 (apple) -> X (has build/x.targets and build/x.props) |
| 76 | + // P1 (banana) -> Y (has build/y.targets and build/y.props) |
| 77 | + [Fact] |
| 78 | + public async Task RestoreCommand_WithAliasesOfSameFramework_BuildPropsAndTargetsAreIncluded() |
| 79 | + { |
| 80 | + using var pathContext = new SimpleTestPathContext(); |
| 81 | + var rootProject = @" |
| 82 | + { |
| 83 | + ""frameworks"": { |
| 84 | + ""apple"": { |
| 85 | + ""framework"": ""net10.0"", |
| 86 | + ""targetAlias"": ""apple"", |
| 87 | + ""dependencies"": { |
| 88 | + ""x"": { |
| 89 | + ""version"": ""[1.0.0,)"", |
| 90 | + ""target"": ""Package"", |
| 91 | + } |
| 92 | + } |
| 93 | + }, |
| 94 | + ""banana"": { |
| 95 | + ""framework"": ""net10.0"", |
| 96 | + ""targetAlias"": ""banana"", |
| 97 | + ""dependencies"": { |
| 98 | + ""y"": { |
| 99 | + ""version"": ""[1.0.0,)"", |
| 100 | + ""target"": ""Package"", |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + }"; |
| 106 | + |
| 107 | + // Create packages with build props and targets |
| 108 | + var packageX = new SimpleTestPackageContext("x", "1.0.0"); |
| 109 | + packageX.AddFile("build/x.targets"); |
| 110 | + packageX.AddFile("build/x.props"); |
| 111 | + |
| 112 | + var packageY = new SimpleTestPackageContext("y", "1.0.0"); |
| 113 | + packageY.AddFile("build/y.targets"); |
| 114 | + packageY.AddFile("build/y.props"); |
| 115 | + |
| 116 | + // Setup project |
| 117 | + var projectSpec = ProjectTestHelpers.GetPackageSpecWithProjectNameAndSpec("Project1", pathContext.SolutionRoot, rootProject); |
| 118 | + await SimpleTestPackageUtility.CreatePackagesAsync( |
| 119 | + pathContext.PackageSource, |
| 120 | + packageX, |
| 121 | + packageY); |
| 122 | + |
| 123 | + // Act |
| 124 | + var result = await RunRestoreAsync(pathContext, projectSpec); |
| 125 | + |
| 126 | + // Assert |
| 127 | + result.Success.Should().BeTrue(); |
| 128 | + result.LockFile.Targets.Should().HaveCount(2); |
| 129 | + |
| 130 | + var appleTarget = result.LockFile.GetTarget("apple", null); |
| 131 | + appleTarget.Should().NotBeNull(); |
| 132 | + appleTarget.Libraries.Should().HaveCount(1); |
| 133 | + appleTarget.Libraries[0].Name.Should().Be("x"); |
| 134 | + appleTarget.Libraries[0].Build.Should().Contain(item => item.Path.Equals("build/x.props")); |
| 135 | + appleTarget.Libraries[0].Build.Should().Contain(item => item.Path.Equals("build/x.targets")); |
| 136 | + |
| 137 | + var bananaTarget = result.LockFile.GetTarget("banana", null); |
| 138 | + bananaTarget.Should().NotBeNull(); |
| 139 | + bananaTarget.Libraries.Should().HaveCount(1); |
| 140 | + bananaTarget.Libraries[0].Name.Should().Be("y"); |
| 141 | + bananaTarget.Libraries[0].Build.Should().Contain(item => item.Path.Equals("build/y.props")); |
| 142 | + bananaTarget.Libraries[0].Build.Should().Contain(item => item.Path.Equals("build/y.targets")); |
| 143 | + |
| 144 | + // Validate MSBuild output files contain correct ImportGroup conditions per alias |
| 145 | + var targetsOutput = result.MSBuildOutputFiles.First(f => f.Path.EndsWith(".targets")); |
| 146 | + var propsOutput = result.MSBuildOutputFiles.First(f => f.Path.EndsWith(".props")); |
| 147 | + |
| 148 | + targetsOutput.Content.Should().NotBeNull(); |
| 149 | + propsOutput.Content.Should().NotBeNull(); |
| 150 | + |
| 151 | + var targetImportGroups = targetsOutput.Content!.Root!.Elements() |
| 152 | + .Where(e => e.Name.LocalName == "ImportGroup") |
| 153 | + .ToList(); |
| 154 | + var propsImportGroups = propsOutput.Content!.Root!.Elements() |
| 155 | + .Where(e => e.Name.LocalName == "ImportGroup") |
| 156 | + .ToList(); |
| 157 | + |
| 158 | + // There should be an ImportGroup per alias |
| 159 | + targetImportGroups.Should().Contain(g => g.Attribute(XName.Get("Condition"))!.Value.Contains("'$(TargetFramework)' == 'apple'")); |
| 160 | + targetImportGroups.Should().Contain(g => g.Attribute(XName.Get("Condition"))!.Value.Contains("'$(TargetFramework)' == 'banana'")); |
| 161 | + |
| 162 | + propsImportGroups.Should().Contain(g => g.Attribute(XName.Get("Condition"))!.Value.Contains("'$(TargetFramework)' == 'apple'")); |
| 163 | + propsImportGroups.Should().Contain(g => g.Attribute(XName.Get("Condition"))!.Value.Contains("'$(TargetFramework)' == 'banana'")); |
| 164 | + |
| 165 | + // Verify the imports reference the correct packages per alias |
| 166 | + var appleTargetsGroup = targetImportGroups.First(g => g.Attribute(XName.Get("Condition"))!.Value.Contains("apple")); |
| 167 | + appleTargetsGroup.Elements().Should().Contain(e => e.Attribute(XName.Get("Project"))!.Value.Contains("x.targets")); |
| 168 | + |
| 169 | + var bananaTargetsGroup = targetImportGroups.First(g => g.Attribute(XName.Get("Condition"))!.Value.Contains("banana")); |
| 170 | + bananaTargetsGroup.Elements().Should().Contain(e => e.Attribute(XName.Get("Project"))!.Value.Contains("y.targets")); |
| 171 | + |
| 172 | + var applePropsGroup = propsImportGroups.First(g => g.Attribute(XName.Get("Condition"))!.Value.Contains("apple")); |
| 173 | + applePropsGroup.Elements().Should().Contain(e => e.Attribute(XName.Get("Project"))!.Value.Contains("x.props")); |
| 174 | + |
| 175 | + var bananaPropsGroup = propsImportGroups.First(g => g.Attribute(XName.Get("Condition"))!.Value.Contains("banana")); |
| 176 | + bananaPropsGroup.Elements().Should().Contain(e => e.Attribute(XName.Get("Project"))!.Value.Contains("y.props")); |
| 177 | + } |
| 178 | + |
74 | 179 | // P (apple) -> Net472 package, with ATF, succeeds |
75 | 180 | // P (banana) -> Net472 package, with ATF, fails |
76 | 181 | [Fact] |
|
0 commit comments