Skip to content
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
13 changes: 13 additions & 0 deletions CycloneDX.Tests/FunctionalTests/FunctionalTestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,19 @@ public static async Task<Bom> Test(string assetsJson, RunOptions options, INuget
return await Test(options, nugetService, mockFileSystem, expectedExitCode).ConfigureAwait(false);
}

public static async Task<Bom> TestWithProjectFile(string assetsJson, string projectFileContents, RunOptions options, INugetServiceFactory nugetService = null, ExitCode expectedExitCode = ExitCode.OK)
{
nugetService ??= CreateMockNugetServiceFactory();

var mockFileSystem = new MockFileSystem(new Dictionary<string, MockFileData>
{
{ MockUnixSupport.Path("c:/ProjectPath/Project.csproj"), new MockFileData(projectFileContents) },
{ MockUnixSupport.Path("c:/ProjectPath/obj/project.assets.json"), new MockFileData(assetsJson) }
});

return await Test(options, nugetService, mockFileSystem, expectedExitCode).ConfigureAwait(false);
}


public static async Task<Bom> Test(RunOptions options, MockFileSystem mockFileSystem,
ExitCode expectedExitCode = ExitCode.OK) => await Test(options, CreateMockNugetServiceFactory(),
Expand Down
24 changes: 24 additions & 0 deletions CycloneDX.Tests/FunctionalTests/ProjectFileVersionMetadata.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System.IO;
using System.Threading.Tasks;
using CycloneDX.Models;
using Xunit;

namespace CycloneDX.Tests.FunctionalTests
{
public class ProjectFileVersionMetadata
{
[Fact(Timeout = 15000)]
public async Task MetadataVersionFromProjectFile()
{
var assetsJson = File.ReadAllText(Path.Combine("FunctionalTests", "TestcaseFiles", "SimpleNETStandardLibrary.json"));
var options = new RunOptions
{
};

var csproj = "<Project Sdk=\"Microsoft.NET.Sdk\">\n <PropertyGroup>\n <OutputType>Exe</OutputType>\n <PackageId>SampleProject</PackageId>\n <Version>1.2.3</Version>\n </PropertyGroup>\n <ItemGroup>\n </ItemGroup>\n</Project>\n";

var bom = await FunctionalTestHelper.TestWithProjectFile(assetsJson, csproj, options);
Assert.Equal("1.2.3", bom.Metadata.Component.Version);
}
}
}
1 change: 1 addition & 0 deletions CycloneDX/Interfaces/IProjectFileService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public interface IProjectFileService
Task<HashSet<DotnetDependency>> RecursivelyGetProjectDotnetDependencysAsync(string projectFilePath, string baseIntermediateOutputPath, bool excludeTestProjects, string framework, string runtime);
Task<HashSet<DotnetDependency>> RecursivelyGetProjectReferencesAsync(string projectFilePath);
Component GetComponent(DotnetDependency dotnetDependency);
(string name, string version) GetAssemblyNameAndVersion(string projectFilePath);
bool IsTestProject(string projectFilePath);
}
}
16 changes: 16 additions & 0 deletions CycloneDX/Runner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,14 @@ public async Task<int> HandleCommandAsync(RunOptions options)
}
packages = await projectFileService.RecursivelyGetProjectDotnetDependencysAsync(fullSolutionOrProjectFilePath, baseIntermediateOutputPath, excludetestprojects, framework, runtime).ConfigureAwait(false);
topLevelComponent.Name = fileSystem.Path.GetFileNameWithoutExtension(SolutionOrProjectFile);
if (string.IsNullOrEmpty(setVersion))
{
var (_, projectVersion) = projectFileService.GetAssemblyNameAndVersion(fullSolutionOrProjectFilePath);
if (!string.IsNullOrEmpty(projectVersion))
{
topLevelComponent.Version = projectVersion;
}
}
}
else if (Utils.IsSupportedProjectType(SolutionOrProjectFile))
{
Expand All @@ -194,6 +202,14 @@ public async Task<int> HandleCommandAsync(RunOptions options)
}
packages = await projectFileService.GetProjectDotnetDependencysAsync(fullSolutionOrProjectFilePath, baseIntermediateOutputPath, excludetestprojects, framework, runtime).ConfigureAwait(false);
topLevelComponent.Name = fileSystem.Path.GetFileNameWithoutExtension(SolutionOrProjectFile);
if (string.IsNullOrEmpty(setVersion))
{
var (_, projectVersion) = projectFileService.GetAssemblyNameAndVersion(fullSolutionOrProjectFilePath);
if (!string.IsNullOrEmpty(projectVersion))
{
topLevelComponent.Version = projectVersion;
}
}
}
else if (fileSystem.Path.GetFileName(SolutionOrProjectFile).ToLowerInvariant().Equals("packages.config", StringComparison.OrdinalIgnoreCase))
{
Expand Down
4 changes: 2 additions & 2 deletions CycloneDX/Services/ProjectFileService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public bool IsTestProject(string projectFilePath)
return testProjectPropertyGroup != null;
}

private (string name, string version) GetAssemblyNameAndVersion(string projectFilePath)
public (string name, string version) GetAssemblyNameAndVersion(string projectFilePath)
{
if (!_fileSystem.File.Exists(projectFilePath))
{
Expand Down Expand Up @@ -111,7 +111,7 @@ public bool IsTestProject(string projectFilePath)

// Extract Version
XmlElement versionElement = xmldoc.SelectSingleNode("/Project/PropertyGroup/Version") as XmlElement;
string version = versionElement?.Value;
string version = versionElement?.InnerText;

if (version == null)
{
Expand Down