Whenever NuGet updates or restores a NuGet package, the config files within it are overwritten. Here’s a method to make sure the changes are reapplied via a config transform whenever the solution is built.
I’m using the NUnit.Runners NuGet packages. To get our coverage tool to play nicely, I need to replace <supportedRuntime "v2.0.50727"> with <supportedRuntime "v4.0.30319"> within the NUnit-console-x86.exe.config.
Normally, a config transform is for modifying the web.config or app.config files. Here, we need to modify a config file within the packages subdirectory.
In my .csproj file, I have added the following:
123456789101112131415
<PropertyGroup><NUnitRunnerDir>$(SolutionDir)packages\NUnit.Runners.2.6.3\tools\</NUnitRunnerDir><PropertyGroup><!-- Default NUnit test runner requires a modification to the config file--><UsingTaskTaskName="TransformXml"AssemblyFile="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v12.0\Web\Microsoft.Web.Publishing.Tasks.dll"/><TargetName="AfterBuild"Condition="exists('$(NUnitRunnerDir)NUnit-console-x86.exe.config')"><TransformXmlSource="$(NUnitRunnerDir)NUnit-console-x86.exe.config"Destination="$(NUnitRunnerDir)NUnit-console-x86.exe.config"Transform="$(SolutionDir)UnitTests\Transforms\NUnit-console-x86.exe.CLR4.config"/></Target>