phmonte / Buildalyzer

A utility to perform design-time builds of .NET projects without having to think too hard about it.
MIT License
593 stars 94 forks source link

Detect test projects without build #110

Open eNeRGy164 opened 5 years ago

eNeRGy164 commented 5 years ago

When loading a solution, I'ld like to differentiate between "normal" projects and "test" projects without compiling the project. It seems that Buildalyzer doesn't surface the properties that shows these details.

I could look at PackageReferences, but these are not parsed in the ProjectFile class. It only shows if there are any references. This would be a small change and can help if the person analyzing a solution knows which test framework is used for a certain project.

There is also a more generic and robust way. As far as I can determine, Visual Studio looks at the IsTestProject property that is inserted by NuGet packages like xunit.core or Microsoft.NET.Test.Sdk using a .props file like this:

<Project>
  <PropertyGroup>
    <IsTestProject>true</IsTestProject>
  </PropertyGroup>
</Project>

For example, removing the last package reference containing this property from a project will immediately change the icon of a project in Visual Studio.

Currently, only the AnalyzeResults show this property after calling the expensive Build() method. Is there a way to access the loaded .props using Buildalyzer?

daveaglick commented 5 years ago

I could look at PackageReferences, but these are not parsed in the ProjectFile class. It only shows if there are any references.

Thanks for the PR! I'm assuming that addresses this method of checking for test projects?

Currently, only the AnalyzeResults show this property after calling the expensive Build() method. Is there a way to access the loaded .props using Buildalyzer?

No, not currently. I'm not even totally sure what that would look like - Buildalyzer gets it's data by hooking the logging that MSBuild performs during a build. I'd need some other mechanism to get the props file and it would have to be cross-platform and have a reasonable fallback if the project weren't built yet.

There may be another easier way. In searching for IsTestProject uses across the dotnet org, it looks like that property typically gets defined based on path:

https://github.com/dotnet/buildtools/blob/28606b8791ccf507651febbf41c480dd87d4a507/src/Microsoft.DotNet.Build.Tasks/PackageFiles/Build.Common.props#L60

We could either expose something similar or, given that I'm not sure I want to bake this in since it's not exactly authoritative, you could do a similar check for tests in the path using ProjectFile.Path.

eNeRGy164 commented 5 years ago

Thanks for the PR! I'm assuming that addresses this method of checking for test projects?

Yup, thanks for merging.

No, not currently. I'm not even totally sure what that would look like - Buildalyzer gets it's data by hooking the logging that MSBuild performs during a build. I'd need some other mechanism to get the props file and it would have to be cross-platform and have a reasonable fallback if the project weren't built yet.

There may be another easier way. In searching for IsTestProject uses across the dotnet org, it looks like that property typically gets defined based on path:

https://github.com/dotnet/buildtools/blob/28606b8791ccf507651febbf41c480dd87d4a507/src/Microsoft.DotNet.Build.Tasks/PackageFiles/Build.Common.props#L60

We could either expose something similar or, given that I'm not sure I want to bake this in since it's not exactly authoritative, you could do a similar check for tests in the path using ProjectFile.Path.

I think there is somewhere, something available that should make this visible without a build(). But I'm not sure where to look for the msbuild properties/analyzer results for the designtime build that is produced.

daveaglick commented 5 years ago

:thumbsup: I'll leave this issue open as a feature request and we can add more support if anyone figures out what to look at.

molekp commented 2 years ago

Hi I would also like to have AnalyzerResult without calling Build().

I'm working on new feature in Stryker.Net that will allow to execute it without building projects (see https://github.com/stryker-mutator/stryker-net/issues/466). We currently calling it to examine properties of testing project (like TargetFramework). When we using it, our (already) builded projects are cleared.

Is it posiible to prevent to clear output directory or implement this feature (getting AnalyzerResult without building project)?