Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I pass VisualStudio Edition name through a compiler option?

I am using Microsoft.QualityTools.Testing.Fakes to mock some unit tests.

But this assembly is only available to users with VisualStudio Ultimate.

Users with other editions (Professional) can't build and run this test project, and it gives an error on their environments.

So I have created a compiler directive to deal this:

#define Ultimate

#if Ultimate
using Microsoft.QualityTools.Testing.Fakes;
#endif

And my test method is:

#if Ultimate
    using (ShimsContext.Create())
    {
        ... My code
    }
#else
    Assert.Inconclusive("This test needs VS Ultimate to run");
#endif

This is working great, but a user still needs to comment/uncomment the define line.

So, is there a way to pass my VS Edition to the compiler? Or is there another approach?

like image 879
rkawano Avatar asked Feb 10 '23 19:02

rkawano


1 Answers

Open the csproj in a text editor and, after the last <PropertyGroup> add:

<PropertyGroup Condition=" $(VisualStudioEdition.Contains('Ultimate')) ">
  <DefineConstants>$(DefineConstants);ULTIMATE</DefineConstants>    
</PropertyGroup>

note that I've written the constant in all upper-case, so you'll have to change your code to:

#if ULTIMATE

If you want to future-proof yourself, as suggested by @Damian:

<PropertyGroup Condition=" $(VisualStudioEdition.Contains('Ultimate')) Or $(VisualStudioEdition.Contains('Enterprise')) ">
  <DefineConstants>$(DefineConstants);ULTIMATE</DefineConstants>    
</PropertyGroup>

Note that, as written by @Uwe, these are "hacks"... manually editing csproj is living the risky life :-)

like image 154
xanatos Avatar answered Feb 12 '23 08:02

xanatos