cyclonedx-dotnet icon indicating copy to clipboard operation
cyclonedx-dotnet copied to clipboard

Test projects are not excluded with option "exclude-test-projects"

Open patspaeth opened this issue 3 years ago • 5 comments

We have dotnet core 5.0 projects where the property < IsTestProject>true</ IsTestProject> is not present, but vs still identfy it as test project. To avoid manual effort to add this property to all test projects it would be nice if cyclonedx dotnet can also identfy testprojects in a way how visualstudio is doing it.

patspaeth avatar Mar 19 '21 08:03 patspaeth

Does anyone know what mechanism is used to detect test projects? I started digging into the .NET source but had trouble finding it.

coderpatros avatar Mar 19 '21 20:03 coderpatros

@coderpatros I believe it is here: /CycloneDX.Core/Services/ProjectFileService.cs

        public static bool IsTestProject(string projectFilePath)
        {
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.Load(projectFilePath);

            XmlElement elt = xmldoc.SelectSingleNode("/Project/PropertyGroup[IsTestProject='true']") as XmlElement;

            return elt != null;
        }

Arturas-K avatar Aug 17 '21 14:08 Arturas-K

I think it is done by the vstest adapters to identify through its test framework.This might be an overkill to re-implement this here for all test frameworks so I would like to close this issue with the comment that all testprojects need to add the IsTestProject property with the value true (maybe a hint in the readme?)

If there are no others who want this issue to be opened - I will close it by end of this week.

patspaeth avatar Aug 18 '21 06:08 patspaeth

@coderpatros I believe it is here: /CycloneDX.Core/Services/ProjectFileService.cs

        public static bool IsTestProject(string projectFilePath)
        {
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.Load(projectFilePath);

            XmlElement elt = xmldoc.SelectSingleNode("/Project/PropertyGroup[IsTestProject='true']") as XmlElement;

            return elt != null;
        }

I should have been more specific. How does the .NET tooling detect a test project.

It might be as simple as looking for a dependency reference to the Microsoft.NET.Test.Sdk test adapter.

I think it is done by the vstest adapters to identify through its test framework.This might be an overkill to re-implement this here for all test frameworks so I would like to close this issue with the comment that all testprojects need to add the IsTestProject property with the value true (maybe a hint in the readme?)

If there are no others who want this issue to be opened - I will close it by end of this week.

There should be a section added to the README on how test dependencies are detected and handled. But I would like to keep the issue open until there is a better solution than just use the IsTestProject property.

coderpatros avatar Aug 18 '21 09:08 coderpatros

Another topic, I think for now the < IsTestProject> property does not work when the property is imported from a property sheet, for example, our project is configured like <Import Project="..\..\_Globals\PropertySheets\CSharpNetCoreAppTest.props" /> and in the CSharpNetCoreAppTest.props we already add the property < IsTestProject>true</ IsTestProject>, but this project is not excluded, only when we add the property explicitly in the project, then it's excluded, so could you please check that?

lwyxxj avatar Feb 16 '22 05:02 lwyxxj

I ran into the same problem, since we reference it in a more 'general' place. The code simple loads de .csproj file and now (after PR 585) checks for Microsoft.Net.Test.SDK reference or explicit IsTestProject.

Both are provided via NuGet in my case (Test.SDK as 2nd level package reference and IsTestProject via buildtransitive .props.

It looks like we still need to set IsTestProject explicitly for the moment.

Ildrial avatar Oct 19 '22 09:10 Ildrial

The PR #585 did not fix it at all, I use Directory.Build.props in my root test folder, there is the <IsTestProject>true</IsTestProject>, but since the code just look into the csproj xml, its not enough

AlbertoMonteiro avatar Mar 29 '23 22:03 AlbertoMonteiro