Skip to content

Commit 1ce5859

Browse files
committed
add test for .sln with two unrelated projects
1 parent 8797b4d commit 1ce5859

File tree

2 files changed

+96
-6
lines changed

2 files changed

+96
-6
lines changed

test/Microsoft.ComponentDetection.Detectors.Tests/MSBuildTestUtilities.cs

+5-4
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,15 @@ public static class MSBuildTestUtilities
5656
});
5757

5858
public static async Task<Stream> GetBinLogStreamFromFileContentsAsync(
59-
string projectContents,
59+
string defaultFilePath,
60+
string defaultFileContents,
6061
(string FileName, string Contents)[] additionalFiles = null,
6162
(string Name, string Version, string TargetFramework, string AdditionalMetadataXml)[] mockedPackages = null)
6263
{
6364
// write all files
6465
using var tempDir = new TemporaryProjectDirectory();
65-
var fullProjectPath = Path.Combine(tempDir.DirectoryPath, "project.csproj");
66-
await File.WriteAllTextAsync(fullProjectPath, projectContents);
66+
var fullDefaultFilePath = Path.Combine(tempDir.DirectoryPath, defaultFilePath);
67+
await File.WriteAllTextAsync(fullDefaultFilePath, defaultFileContents);
6768
if (additionalFiles is not null)
6869
{
6970
foreach (var (fileName, contents) in additionalFiles)
@@ -78,7 +79,7 @@ public static async Task<Stream> GetBinLogStreamFromFileContentsAsync(
7879
await MockNuGetPackagesInDirectoryAsync(tempDir, mockedPackages);
7980

8081
// generate the binlog
81-
var (exitCode, stdOut, stdErr) = await RunProcessAsync("dotnet", $"build \"{fullProjectPath}\" /t:GenerateBuildDependencyFile /bl:msbuild.binlog", workingDirectory: tempDir.DirectoryPath);
82+
var (exitCode, stdOut, stdErr) = await RunProcessAsync("dotnet", $"build \"{fullDefaultFilePath}\" /t:GenerateBuildDependencyFile /bl:msbuild.binlog", workingDirectory: tempDir.DirectoryPath);
8283
exitCode.Should().Be(0, $"STDOUT:\n{stdOut}\n\nSTDERR:\n{stdErr}");
8384

8485
// copy it to memory so the temporary directory can be cleaned up

test/Microsoft.ComponentDetection.Detectors.Tests/NuGetMSBuildBinaryLogComponentDetectorTests.cs

+91-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Linq;
44
using System.Threading.Tasks;
55
using FluentAssertions;
6+
using Microsoft.ComponentDetection.Contracts;
67
using Microsoft.ComponentDetection.Contracts.TypedComponent;
78
using Microsoft.ComponentDetection.Detectors.NuGet;
89
using Microsoft.ComponentDetection.TestsUtilities;
@@ -109,12 +110,100 @@ public async Task RemovedPackagesAreNotReported()
109110
packages.Should().Equal("Some.Package/1.2.3");
110111
}
111112

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(
113202
string projectContents,
114203
(string FileName, string Content)[] additionalFiles = null,
115204
(string Name, string Version, string TargetFramework, string DependenciesXml)[] mockedPackages = null)
116205
{
117-
using var binLogStream = await MSBuildTestUtilities.GetBinLogStreamFromFileContentsAsync(projectContents, additionalFiles, mockedPackages);
206+
using var binLogStream = await MSBuildTestUtilities.GetBinLogStreamFromFileContentsAsync("project.csproj", projectContents, additionalFiles, mockedPackages);
118207
var (scanResult, componentRecorder) = await this.DetectorTestUtility
119208
.WithFile("msbuild.binlog", binLogStream)
120209
.ExecuteDetectorAsync();

0 commit comments

Comments
 (0)