2

I have added the xunit.runners package to a solution. The current version is 1.9.1, so I have hard-coded the path to the executable in an MSBuild project file:

<StartAction>Program</StartAction>
<StartProgram>$(MSBuildProjectDirectory)\..\..\Packages\xunit.runners.1.9.1\tools\xunit.gui.clr4.exe</StartProgram>
<StartArguments>"$(MSBuildProjectDirectory)\$(OutPutPath)$(AssemblyName).dll"</StartArguments>

(Off-topic: with this configuration, F5 starts the xUnit GUI runner and I can debug specific unit tests.)

I know that everytime I update the Nuget package, I will forget to change the path. Changing the path is a minor nuisance, since I have to unload the project, edit the file, then reload the project.

How can I start the executable, regardless of the actual version of the package? Can I find the executable in the folder named xunit.runners.* using a wildcard in MSBuild, then use that as a property in the <StartProgram> element?

Edit:

Something like:

<ItemGroup>
    <Runners Include="$(MSBuildProjectDirectory)\..\..\Packages\xunit.runners.*\tools\xunit.gui.clr4.exe" />
</ItemGroup>

Will give me all runners in @(Runners), sorted by version. How can I get one of them, preferably the last one?

Michiel van Oosterhout
  • 22,839
  • 15
  • 90
  • 132

2 Answers2

1

For filtering you can build a custom task. It can even be inline http://msdn.microsoft.com/en-us/library/dd722601.aspx, were you can write the c# code you need to loop over the items and pick the right one. Then you can expose the chosen path in an output property that you then use to set the value of the StartProgram property.

This question shows a custom inline task that gets an item array and does stuff with it. You can probably start from there.

You'll need to use this task in a target that runs before the target that initiates the debugging.

Community
  • 1
  • 1
fsimonazzi
  • 2,975
  • 15
  • 13
  • Doesn't look like VS actually runs the targets and picks the value of the property as specified in a property group. The issue is that properties are evaluated first, then items. – fsimonazzi Nov 06 '12 at 19:37
  • Indeed, it works great in a target (cool concept, inline custom tasks, thanks!), but the custom task can't be used in the evaluation phase. – Michiel van Oosterhout Nov 06 '12 at 19:41
  • 1
    You can try with a custom property function http://blogs.msdn.com/b/visualstudio/archive/2010/04/02/msbuild-property-functions.aspx. But it requires explicit enabling, and you cannot do it inline AFAIK. – fsimonazzi Nov 06 '12 at 20:01
0

With fsimonazzi's comment I ended up with this:

<PropertyGroup>
  <Package>$([System.IO.Directory]::GetDirectories("$(MSBuildProjectDirectory)\\..\\..\\Packages\\", "xunit.runners.*").GetValue(0))</Package>
  <StartAction>Program</StartAction>
  <StartProgram>$(Package)\tools\xunit.gui.clr4.exe</StartProgram>
  <StartArguments>"$(MSBuildProjectDirectory)\$(OutPutPath)$(AssemblyName).dll"</StartArguments>
</PropertyGroup>

Apparently, NuGet will guarantee there's only one version of the package.

Community
  • 1
  • 1
Michiel van Oosterhout
  • 22,839
  • 15
  • 90
  • 132