|
3 | 3 | using System.Linq;
|
4 | 4 | using System.Threading.Tasks;
|
5 | 5 | using FluentAssertions;
|
| 6 | +using Microsoft.ComponentDetection.Contracts; |
6 | 7 | using Microsoft.ComponentDetection.Contracts.TypedComponent;
|
7 | 8 | using Microsoft.ComponentDetection.Detectors.NuGet;
|
8 | 9 | using Microsoft.ComponentDetection.TestsUtilities;
|
@@ -109,12 +110,100 @@ public async Task RemovedPackagesAreNotReported()
|
109 | 110 | packages.Should().Equal("Some.Package/1.2.3");
|
110 | 111 | }
|
111 | 112 |
|
112 |
| - private async Task<(Contracts.IndividualDetectorScanResult ScanResult, Contracts.IComponentRecorder ComponentRecorder)> ExecuteDetectorAndGetBinLogAsync( |
| 113 | + [TestMethod] |
| 114 | + public async Task PackagesReportedFromSeparateProjectsDoNotOverlap() |
| 115 | + { |
| 116 | + // In this test, a top-level solution file references two projects which have no relationship between them. |
| 117 | + // The result should be that each project only reports its own dependencies. |
| 118 | + var slnContents = @" |
| 119 | +Microsoft Visual Studio Solution File, Format Version 12.00 |
| 120 | +# Visual Studio 17 |
| 121 | +VisualStudioVersion = 17.0.31808.319 |
| 122 | +MinimumVisualStudioVersion = 15.0.26124.0 |
| 123 | +Project(""{9A19103F-16F7-4668-BE54-9A1E7A4F7556}"") = ""project1"", ""project1\project1.csproj"", ""{782E0C0A-10D3-444D-9640-263D03D2B20C}"" |
| 124 | +EndProject |
| 125 | +Project(""{9A19103F-16F7-4668-BE54-9A1E7A4F7556}"") = ""project2"", ""project2\project2.csproj"", ""{CBA73BF8-C922-4DD7-A41D-88CD22914356}"" |
| 126 | +EndProject |
| 127 | +Global |
| 128 | + GlobalSection(SolutionConfigurationPlatforms) = preSolution |
| 129 | + Debug|Any CPU = Debug|Any CPU |
| 130 | + Release|Any CPU = Release|Any CPU |
| 131 | + EndGlobalSection |
| 132 | + GlobalSection(ProjectConfigurationPlatforms) = postSolution |
| 133 | + {782E0C0A-10D3-444D-9640-263D03D2B20C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU |
| 134 | + {782E0C0A-10D3-444D-9640-263D03D2B20C}.Debug|Any CPU.Build.0 = Debug|Any CPU |
| 135 | + {782E0C0A-10D3-444D-9640-263D03D2B20C}.Release|Any CPU.ActiveCfg = Release|Any CPU |
| 136 | + {782E0C0A-10D3-444D-9640-263D03D2B20C}.Release|Any CPU.Build.0 = Release|Any CPU |
| 137 | + {CBA73BF8-C922-4DD7-A41D-88CD22914356}.Debug|Any CPU.ActiveCfg = Debug|Any CPU |
| 138 | + {CBA73BF8-C922-4DD7-A41D-88CD22914356}.Debug|Any CPU.Build.0 = Debug|Any CPU |
| 139 | + {CBA73BF8-C922-4DD7-A41D-88CD22914356}.Release|Any CPU.ActiveCfg = Release|Any CPU |
| 140 | + {CBA73BF8-C922-4DD7-A41D-88CD22914356}.Release|Any CPU.Build.0 = Release|Any CPU |
| 141 | + EndGlobalSection |
| 142 | + GlobalSection(SolutionProperties) = preSolution |
| 143 | + HideSolutionNode = FALSE |
| 144 | + EndGlobalSection |
| 145 | +EndGlobal |
| 146 | +"; |
| 147 | + using var binLogStream = await MSBuildTestUtilities.GetBinLogStreamFromFileContentsAsync( |
| 148 | + defaultFilePath: "solution.sln", |
| 149 | + defaultFileContents: slnContents, |
| 150 | + additionalFiles: new[] |
| 151 | + { |
| 152 | + ("project1/project1.csproj", $@" |
| 153 | + <Project Sdk=""Microsoft.NET.Sdk""> |
| 154 | + <PropertyGroup> |
| 155 | + <TargetFramework>{MSBuildTestUtilities.TestTargetFramework}</TargetFramework> |
| 156 | + </PropertyGroup> |
| 157 | + <ItemGroup> |
| 158 | + <PackageReference Include=""Package.A"" Version=""1.2.3"" /> |
| 159 | + </ItemGroup> |
| 160 | + </Project> |
| 161 | + "), |
| 162 | + ("project2/project2.csproj", $@" |
| 163 | + <Project Sdk=""Microsoft.NET.Sdk""> |
| 164 | + <PropertyGroup> |
| 165 | + <TargetFramework>{MSBuildTestUtilities.TestTargetFramework}</TargetFramework> |
| 166 | + </PropertyGroup> |
| 167 | + <ItemGroup> |
| 168 | + <PackageReference Include=""Package.B"" Version=""4.5.6"" /> |
| 169 | + </ItemGroup> |
| 170 | + </Project> |
| 171 | + "), |
| 172 | + }, |
| 173 | + mockedPackages: new[] |
| 174 | + { |
| 175 | + ("Package.A", "1.2.3", MSBuildTestUtilities.TestTargetFramework, "<dependencies />"), |
| 176 | + ("Package.B", "4.5.6", MSBuildTestUtilities.TestTargetFramework, "<dependencies />"), |
| 177 | + }); |
| 178 | + var (scanResult, componentRecorder) = await this.DetectorTestUtility |
| 179 | + .WithFile("msbuild.binlog", binLogStream) |
| 180 | + .ExecuteDetectorAsync(); |
| 181 | + |
| 182 | + var detectedComponents = componentRecorder.GetDetectedComponents(); |
| 183 | + |
| 184 | + var project1Components = detectedComponents |
| 185 | + .Where(d => d.FilePaths.Any(p => p.Replace("\\", "/").EndsWith("/project1.csproj"))) |
| 186 | + .Select(d => d.Component) |
| 187 | + .Cast<NuGetComponent>() |
| 188 | + .OrderBy(c => c.Name) |
| 189 | + .Select(c => $"{c.Name}/{c.Version}"); |
| 190 | + project1Components.Should().Equal("Package.A/1.2.3"); |
| 191 | + |
| 192 | + var project2Components = detectedComponents |
| 193 | + .Where(d => d.FilePaths.Any(p => p.Replace("\\", "/").EndsWith("/project2.csproj"))) |
| 194 | + .Select(d => d.Component) |
| 195 | + .Cast<NuGetComponent>() |
| 196 | + .OrderBy(c => c.Name) |
| 197 | + .Select(c => $"{c.Name}/{c.Version}"); |
| 198 | + project2Components.Should().Equal("Package.B/4.5.6"); |
| 199 | + } |
| 200 | + |
| 201 | + private async Task<(IndividualDetectorScanResult ScanResult, IComponentRecorder ComponentRecorder)> ExecuteDetectorAndGetBinLogAsync( |
113 | 202 | string projectContents,
|
114 | 203 | (string FileName, string Content)[] additionalFiles = null,
|
115 | 204 | (string Name, string Version, string TargetFramework, string DependenciesXml)[] mockedPackages = null)
|
116 | 205 | {
|
117 |
| - using var binLogStream = await MSBuildTestUtilities.GetBinLogStreamFromFileContentsAsync(projectContents, additionalFiles, mockedPackages); |
| 206 | + using var binLogStream = await MSBuildTestUtilities.GetBinLogStreamFromFileContentsAsync("project.csproj", projectContents, additionalFiles, mockedPackages); |
118 | 207 | var (scanResult, componentRecorder) = await this.DetectorTestUtility
|
119 | 208 | .WithFile("msbuild.binlog", binLogStream)
|
120 | 209 | .ExecuteDetectorAsync();
|
|
0 commit comments