Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrate Buildalyzer #837

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 107 additions & 0 deletions CycloneDX.Tests/BuildalyzerServiceTestWrapper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.IO.Abstractions;
using System.IO.Abstractions.TestingHelpers;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CycloneDX.Interfaces;
using CycloneDX.Services;

namespace CycloneDX.Tests
{
/// <summary>
/// Buildalyzer doesn't work with System.IO.Abstraction. So we dump it all on the regular FS for it and translate all incoming paths.
/// </summary>
public class BuildalyzerServiceTestWrapper : IBuildalyzerService
{
readonly BuildalyzerService coreBuildAnalyzerService = new();
private string _tempFolderPath;
readonly MockFileSystem fileSystem;

public BuildalyzerServiceTestWrapper(MockFileSystem fileSystem)
{
this.fileSystem = fileSystem;
DumpFilesystemOnDisk();
}

readonly Dictionary<string, string> reversePathMap = [];


void DumpFilesystemOnDisk()
{
// Create a temporary folder to hold the mock file system
_tempFolderPath = Path.Combine(Path.GetTempPath(), Path.GetRandomFileName());
Directory.CreateDirectory(_tempFolderPath);

// Copy all files and directories from the mock file system to the temp folder
foreach (var file in fileSystem.AllFiles)
{
string tempFilePath = TranslatePath(file);
string directoryPath = Path.GetDirectoryName(tempFilePath);

if (!Directory.Exists(directoryPath))
{
Directory.CreateDirectory(directoryPath);
}

File.WriteAllText(tempFilePath, fileSystem.File.ReadAllText(file));
reversePathMap.Add(tempFilePath, file);
}
}

private string TranslatePath(string originalPath)
{
// Translate the original path from the mock file system to the corresponding path in the temp folder
return Path.Combine(_tempFolderPath, fileSystem.Path.GetRelativePath(fileSystem.Path.GetPathRoot(originalPath), originalPath));


}

public string TranslatePathBackwards(string tempPath)
{
return reversePathMap[tempPath];
}

public HashSet<string> GetProjectPathsOfSolution()
{
return coreBuildAnalyzerService
.GetProjectPathsOfSolution()
.Select(TranslatePathBackwards)
.ToHashSet();
}

public void InitializeAnalyzer(string solutionFilePath)
{
coreBuildAnalyzerService.InitializeAnalyzer(TranslatePath(solutionFilePath));
}

public bool IsTestProject(string projectFilePath)
{
return coreBuildAnalyzerService.IsTestProject(TranslatePath(projectFilePath));
}

~BuildalyzerServiceTestWrapper()
{
CleanUpTempFolder();
}

private void CleanUpTempFolder()
{
try
{
if (Directory.Exists(_tempFolderPath))
{
Directory.Delete(_tempFolderPath, true);
}
}
catch (Exception ex)
{
Console.WriteLine($"{ex.GetType()} when deleting {_tempFolderPath}\r\n{ex.Message}");
}
}
}
}
15 changes: 15 additions & 0 deletions CycloneDX.Tests/CycloneDX.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,21 @@
</ItemGroup>

<ItemGroup>
<None Update="FunctionalTests\ExcludeTestDependencies\assetsProject1.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="FunctionalTests\ExcludeTestDependencies\assetsTestProject1.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="FunctionalTests\ExcludeTestDependencies\project1.xml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="FunctionalTests\ExcludeTestDependencies\sln.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="FunctionalTests\ExcludeTestDependencies\testproject1.xml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="FunctionalTests\Issue826alt-ProjectFileDoesntExistWithAssetsFile\project2assets.json">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
using System;
using System.Collections.Generic;
using System.CommandLine;
using System.IO;
using System.IO.Abstractions;
using System.IO.Abstractions.TestingHelpers;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CycloneDX.Models;
using Xunit;

namespace CycloneDX.Tests.FunctionalTests
{
public class ExcludeTestDependencies
{

readonly string testFileFolder = "ExcludeTestDependencies";

private MockFileSystem getMockFS()
{
return new MockFileSystem(new Dictionary<string, MockFileData>
{

{
MockUnixSupport.Path("c:/solution.sln"),
new MockFileData(
File.ReadAllText(Path.Combine("FunctionalTests", testFileFolder, "sln.txt")))
},{
MockUnixSupport.Path("c:/project1/project1.vbproj"),
new MockFileData(
File.ReadAllText(Path.Combine("FunctionalTests", testFileFolder, "project1.xml")))
},{
MockUnixSupport.Path("c:/testProject1/testProject1.csproj"),
new MockFileData(
File.ReadAllText(Path.Combine("FunctionalTests", testFileFolder, "testproject1.xml")))
},{
MockUnixSupport.Path("c:/project1/obj/project.assets.json"),
new MockFileData(
File.ReadAllText(Path.Combine("FunctionalTests", testFileFolder, "assetsProject1.json")))
},{
MockUnixSupport.Path("c:/testProject1/obj/project.assets.json"),
new MockFileData(
File.ReadAllText(Path.Combine("FunctionalTests", testFileFolder, "assetsTestProject1.json")))
}
});
}

[Fact]
public async Task IncludesTestDependenciesByDefault()
{
var options = new RunOptions
{
SolutionOrProjectFile = MockUnixSupport.Path("c:/solution.sln")
};

//Just test that there is no exception
var bom = await FunctionalTestHelper.Test(options, getMockFS());

Assert.Contains(bom.Components, c => string.Compare(c.Name, "log4net", true) == 0 && c.Version == "2.0.15");
Assert.Contains(bom.Components, c => string.Compare(c.Name, "MSTest.TestFramework", true) == 0 && c.Version == "2.2.10");

Assert.True(bom.Components.First(c => string.Compare(c.Name, "MSTest.TestFramework", true) == 0 && c.Version == "2.2.10").Scope == Component.ComponentScope.Excluded);
}


[Fact]
public async Task ExcludesTestDependenciesWhenOptionIsSet()
{
var options = new RunOptions
{
SolutionOrProjectFile = MockUnixSupport.Path("c:/solution.sln"),
excludeTestProjects = true

};

//Just test that there is no exception
var bom = await FunctionalTestHelper.Test(options, getMockFS());

Assert.Contains(bom.Components, c => string.Compare(c.Name, "log4net", true) == 0 && c.Version == "2.0.15");
Assert.DoesNotContain(bom.Components, c => string.Compare(c.Name, "MSTest.TestFramework", true) == 0 && c.Version == "2.2.10");

}
}
}
Loading