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
9 changes: 9 additions & 0 deletions CycloneDX.Tests/CycloneDX.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@
<None Update="FunctionalTests\IPR-TwoLevelTree\project3csproj.xml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="FunctionalTests\Issue669-IsTestProjectEvaluation\makeMeTestProject.props">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="FunctionalTests\Issue669-IsTestProjectEvaluation\nontestproject.csproj.xml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="FunctionalTests\Issue669-IsTestProjectEvaluation\testproject.csproj.xml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="FunctionalTests\Issue826-ProjectFileDoesntExist\project1csproj.xml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System.IO;
using System.IO.Abstractions;
using System.Reflection;
using CycloneDX.Interfaces;
using CycloneDX.Models;
using CycloneDX.Services;
using Moq;
using Xunit;

namespace CycloneDX.Tests.FunctionalTests
{
public sealed class Issue669
{
[Theory]
[InlineData("testproject.csproj.xml", true)]
[InlineData("nontestproject.csproj.xml", false)]
public void IsTestProjectTrueWithImportedPropsTargets(string projectFile, bool expectedIsTestProject)
{
var mockDotnetUtilsService = new Mock<IDotnetUtilsService>();
mockDotnetUtilsService
.Setup(s => s.Restore(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<string>()))
.Returns(new DotnetUtilsResult());
mockDotnetUtilsService
.Setup(s => s.GetAssetsPath(It.IsAny<string>()))
.Returns(new DotnetUtilsResult<string>() { Result = "" });
var mockPackageFileService = new Mock<IPackagesFileService>();
var mockProjectAssetsFileService = new Mock<IProjectAssetsFileService>();

var projectFileService = new ProjectFileService(
new FileSystem(),
mockDotnetUtilsService.Object,
mockPackageFileService.Object,
mockProjectAssetsFileService.Object);

string physicalProjectPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "FunctionalTests", "Issue669-IsTestProjectEvaluation", projectFile);

Assert.Equal(projectFileService.IsTestProject(physicalProjectPath), expectedIsTestProject);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<Project>
<PropertyGroup>
<IsTestProject>true</IsTestProject>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<Import Project="FunctionalTests/Issue669-IsTestProjectEvaluation/makeMeTestProject.props" />

<PropertyGroup>
<IsTestProject>false</IsTestProject>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<IsTestProject>false</IsTestProject>
</PropertyGroup>

<Import Project="FunctionalTests/Issue669-IsTestProjectEvaluation/makeMeTestProject.props" />

</Project>
4 changes: 3 additions & 1 deletion CycloneDX/CycloneDX.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
Expand Down Expand Up @@ -36,6 +36,8 @@
<PackageReference Include="NuGet.ProjectModel" />
<PackageReference Include="System.IO.Abstractions" />
<PackageReference Include="System.CommandLine" />
<PackageReference Include="Microsoft.Build" ExcludeAssets="runtime"/>
<PackageReference Include="Microsoft.Build.Locator" />
</ItemGroup>

<ItemGroup>
Expand Down
34 changes: 20 additions & 14 deletions CycloneDX/Services/ProjectFileService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@

using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml;
using System.IO;
using System.IO.Abstractions;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Xml;
using CycloneDX.Interfaces;
using CycloneDX.Models;
using System.Text.RegularExpressions;
using System.Reflection;
using Microsoft.Build.Evaluation;
using Microsoft.Build.Locator;

namespace CycloneDX.Services
{
Expand All @@ -38,6 +39,12 @@ public DotnetRestoreException(string message, Exception innerException) : base(m

public class ProjectFileService : IProjectFileService
{
static ProjectFileService()
{
// Register the MSBuild instance (do this once per process)
MSBuildLocator.RegisterDefaults();
}

private XmlReaderSettings _xmlReaderSettings = new XmlReaderSettings
{
Async = true
Expand Down Expand Up @@ -67,18 +74,17 @@ public bool IsTestProject(string projectFilePath)
return false;
}

XmlDocument xmldoc = new XmlDocument();
using var fileStream = _fileSystem.FileStream.New(projectFilePath, FileMode.Open, FileAccess.Read);
xmldoc.Load(fileStream);
using var xmlReader = XmlReader.Create(fileStream);

XmlElement testSdkReference = xmldoc.SelectSingleNode("/Project/ItemGroup/PackageReference[@Include='Microsoft.NET.Test.Sdk']") as XmlElement;
if (testSdkReference != null)
{
return true;
}
// if this is meant for old csproj file format, then it's probably not working because there is no namespace given
XmlElement testProjectPropertyGroup = xmldoc.SelectSingleNode("/Project/PropertyGroup[IsTestProject='true']") as XmlElement;
return testProjectPropertyGroup != null;
// load the project (all imports and SDKs are processed)
var project = new Project(xmlReader);

// get property value after evaluation
string propertyValue = project.GetPropertyValue("IsTestProject");

//return is expected parsed boolean value, default false
return bool.TryParse(propertyValue, out bool isTestProject) && isTestProject;
}

private (string name, string version) GetAssemblyNameAndVersion(string projectFilePath)
Expand Down
2 changes: 1 addition & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<ItemGroup>
<PackageVersion Include="CycloneDX.Core" Version="9.0.2" />
<PackageVersion Include="Microsoft.Build" Version="17.3.2" />
<PackageVersion Include="Microsoft.Build.Engine" Version="17.8.3" />
<PackageVersion Include="Microsoft.Build.Locator" Version="1.9.1" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageVersion Include="NuGet.ProjectModel" Version="6.9.1" />
<PackageVersion Include="NuGet.Protocol" Version="6.9.1" />
Expand Down