Today I’ve been fixing up the build system for Microsoft.Activities and Microsoft.Activities.UnitTesting.  Over time I’ve been using a combination of strange tools and batch files to build things but it became such a mess I decided to take the time to finally build a good project file that I can use MSBuild on that will do everything

  • Build sources
  • Build Help files
  • Build NuGet Packages

The first problem I ran into was that my unit tests use the [DeploymentItem] attribute to deploy files when testing with MSTest.  The problem is that the test considers the $(SolutionDir) the root folder for the test.  Previously I had just the Solutions for Microsoft.Activities and Microsoft.Activities.UnitTesting but now I made a single solution file in a different location with everything.  This cause my tests to be broken because the deployment paths were wrong.

After much searching I finally decided the best thing to do was to use an environment variable.  Since I call these projects “Labs" projects (for historical reasons) I decided to use a variable called “LABDIR”.

   1: [TestMethod]
   2: [DeploymentItem(@"%LABDIR%\Microsoft.Activities\Microsoft.Activities.Tests\AddToNumOrThrow.xaml")]
   3: public void LoadAndInvokeShouldRun()
Now I can get my items deployed successfully regardless of where the solution file is opened.

But then, what if I try to build on another machine where LABDIR is not set?  I decided to modify the Test Project so that it will fail the build if the variable is not set.  Ok – search engines take note of the following question so that the next guy searching for this can find it.

How do I cause a build failure if an environment variable is not set?

Just add this to the end of your Test project .csproj file

   1: <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
   2: <!--To modify your build process, add your task inside one of the targets below and uncomment it. 
   3:      Other similar extension points exist, see Microsoft.Common.targets.-->
   4: <Target Name="BeforeBuild">
   5:   <Error Text="The LABDIR property must be set on the command line or environment variable must be supplied."
   6:             Condition="'$(LABDIR)' == ''" />
   7: </Target>
   8: <Target Name="AfterBuild">
   9: </Target>
  10: </Project>

Now if I try to build the test project without LABDIR set, I get this.

Error  The LABDIR property must be set on the command line or environment variable must be supplied.

Happy Coding
Ron Jacobs
Twitter: @ronljacobs

Blog Post by: Ron Jacobs