Skip to content

Commit

Permalink
add test for .sln with two unrelated projects
Browse files Browse the repository at this point in the history
  • Loading branch information
brettfo committed Sep 19, 2024
1 parent 8797b4d commit 1ce5859
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,15 @@ public static class MSBuildTestUtilities
});

public static async Task<Stream> GetBinLogStreamFromFileContentsAsync(
string projectContents,
string defaultFilePath,
string defaultFileContents,
(string FileName, string Contents)[] additionalFiles = null,
(string Name, string Version, string TargetFramework, string AdditionalMetadataXml)[] mockedPackages = null)
{
// write all files
using var tempDir = new TemporaryProjectDirectory();
var fullProjectPath = Path.Combine(tempDir.DirectoryPath, "project.csproj");
await File.WriteAllTextAsync(fullProjectPath, projectContents);
var fullDefaultFilePath = Path.Combine(tempDir.DirectoryPath, defaultFilePath);
await File.WriteAllTextAsync(fullDefaultFilePath, defaultFileContents);
if (additionalFiles is not null)
{
foreach (var (fileName, contents) in additionalFiles)
Expand All @@ -78,7 +79,7 @@ public static async Task<Stream> GetBinLogStreamFromFileContentsAsync(
await MockNuGetPackagesInDirectoryAsync(tempDir, mockedPackages);

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

// copy it to memory so the temporary directory can be cleaned up
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using System.Threading.Tasks;
using FluentAssertions;
using Microsoft.ComponentDetection.Contracts;
using Microsoft.ComponentDetection.Contracts.TypedComponent;
using Microsoft.ComponentDetection.Detectors.NuGet;
using Microsoft.ComponentDetection.TestsUtilities;
Expand Down Expand Up @@ -109,12 +110,100 @@ public async Task RemovedPackagesAreNotReported()
packages.Should().Equal("Some.Package/1.2.3");
}

private async Task<(Contracts.IndividualDetectorScanResult ScanResult, Contracts.IComponentRecorder ComponentRecorder)> ExecuteDetectorAndGetBinLogAsync(
[TestMethod]
public async Task PackagesReportedFromSeparateProjectsDoNotOverlap()
{
// In this test, a top-level solution file references two projects which have no relationship between them.
// The result should be that each project only reports its own dependencies.
var slnContents = @"
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 17
VisualStudioVersion = 17.0.31808.319
MinimumVisualStudioVersion = 15.0.26124.0
Project(""{9A19103F-16F7-4668-BE54-9A1E7A4F7556}"") = ""project1"", ""project1\project1.csproj"", ""{782E0C0A-10D3-444D-9640-263D03D2B20C}""
EndProject
Project(""{9A19103F-16F7-4668-BE54-9A1E7A4F7556}"") = ""project2"", ""project2\project2.csproj"", ""{CBA73BF8-C922-4DD7-A41D-88CD22914356}""
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{782E0C0A-10D3-444D-9640-263D03D2B20C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{782E0C0A-10D3-444D-9640-263D03D2B20C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{782E0C0A-10D3-444D-9640-263D03D2B20C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{782E0C0A-10D3-444D-9640-263D03D2B20C}.Release|Any CPU.Build.0 = Release|Any CPU
{CBA73BF8-C922-4DD7-A41D-88CD22914356}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CBA73BF8-C922-4DD7-A41D-88CD22914356}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CBA73BF8-C922-4DD7-A41D-88CD22914356}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CBA73BF8-C922-4DD7-A41D-88CD22914356}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
";
using var binLogStream = await MSBuildTestUtilities.GetBinLogStreamFromFileContentsAsync(
defaultFilePath: "solution.sln",
defaultFileContents: slnContents,
additionalFiles: new[]
{
("project1/project1.csproj", $@"
<Project Sdk=""Microsoft.NET.Sdk"">
<PropertyGroup>
<TargetFramework>{MSBuildTestUtilities.TestTargetFramework}</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include=""Package.A"" Version=""1.2.3"" />
</ItemGroup>
</Project>
"),
("project2/project2.csproj", $@"
<Project Sdk=""Microsoft.NET.Sdk"">
<PropertyGroup>
<TargetFramework>{MSBuildTestUtilities.TestTargetFramework}</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include=""Package.B"" Version=""4.5.6"" />
</ItemGroup>
</Project>
"),
},
mockedPackages: new[]
{
("Package.A", "1.2.3", MSBuildTestUtilities.TestTargetFramework, "<dependencies />"),
("Package.B", "4.5.6", MSBuildTestUtilities.TestTargetFramework, "<dependencies />"),
});
var (scanResult, componentRecorder) = await this.DetectorTestUtility
.WithFile("msbuild.binlog", binLogStream)
.ExecuteDetectorAsync();

var detectedComponents = componentRecorder.GetDetectedComponents();

var project1Components = detectedComponents
.Where(d => d.FilePaths.Any(p => p.Replace("\\", "/").EndsWith("/project1.csproj")))
.Select(d => d.Component)
.Cast<NuGetComponent>()
.OrderBy(c => c.Name)
.Select(c => $"{c.Name}/{c.Version}");
project1Components.Should().Equal("Package.A/1.2.3");

var project2Components = detectedComponents
.Where(d => d.FilePaths.Any(p => p.Replace("\\", "/").EndsWith("/project2.csproj")))
.Select(d => d.Component)
.Cast<NuGetComponent>()
.OrderBy(c => c.Name)
.Select(c => $"{c.Name}/{c.Version}");
project2Components.Should().Equal("Package.B/4.5.6");
}

private async Task<(IndividualDetectorScanResult ScanResult, IComponentRecorder ComponentRecorder)> ExecuteDetectorAndGetBinLogAsync(
string projectContents,
(string FileName, string Content)[] additionalFiles = null,
(string Name, string Version, string TargetFramework, string DependenciesXml)[] mockedPackages = null)
{
using var binLogStream = await MSBuildTestUtilities.GetBinLogStreamFromFileContentsAsync(projectContents, additionalFiles, mockedPackages);
using var binLogStream = await MSBuildTestUtilities.GetBinLogStreamFromFileContentsAsync("project.csproj", projectContents, additionalFiles, mockedPackages);
var (scanResult, componentRecorder) = await this.DetectorTestUtility
.WithFile("msbuild.binlog", binLogStream)
.ExecuteDetectorAsync();
Expand Down

0 comments on commit 1ce5859

Please sign in to comment.