12

my continuous integration server (TeamCity) is configured to run all the unit tests in our app on build. Prior to running those tests, i need to change some of the appSettings to make them valid for our CI server. I'm achieving something similar for my web project by using the deployment project provided with Visual Studio. Can i do the same for a Test project?

Thanks, Gonzalo

Enrico Campidoglio
  • 56,676
  • 12
  • 126
  • 154
Gonzalo
  • 679
  • 1
  • 8
  • 18

3 Answers3

29

It's possible to use Web.config Transformations for App.config files through a workaround.

You simply have to invoke the appropriate MSBuild tasks at the right stage in your build process.
Add this code snippet to your project file:

<UsingTask
    TaskName="TransformXml"
    AssemblyFile="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.Tasks.dll" />

<Target Name="AfterCompile" Condition="exists('App.$(Configuration).config')">
    <!-- Generates the transformed App.config in the intermediate directory -->
    <TransformXml
        Source="App.config"
        Destination="$(IntermediateOutputPath)$(TargetFileName).config"
        Transform="App.$(Configuration).config" />
    <!-- Forces the build process to use the transformed configuration file -->
    <ItemGroup>
        <AppConfigWithTargetPath Remove="App.config"/>
        <AppConfigWithTargetPath
            Include="$(IntermediateOutputPath)$(TargetFileName).config">
            <TargetPath>$(TargetFileName).config</TargetPath>
        </AppConfigWithTargetPath>
    </ItemGroup>
</Target>

Then add additional App.config files to your project for each build configuration where you wish to apply a transformation. For example:

<ItemGroup>
    <None Include="App.config" />
    <None Include="App.Release.config">
        <DependentUpon>App.config</DependentUpon>
    </None>
</ItemGroup>

Related resources:

Enrico Campidoglio
  • 56,676
  • 12
  • 126
  • 154
  • I found this too: http://frankmao.com/2011/07/15/tweet-config-transform-to-support-non-web-config-files-and-non-web-project/ which seems to indicate it can be done for any XML file. – Kit Aug 01 '11 at 17:24
  • 1
    Instead of doing this by hand you can use http://visualstudiogallery.msdn.microsoft.com/69023d00-a4f9-4a34-a6cd-7e854ba318b5 – Wouter de Kort Dec 26 '11 at 20:54
  • Yes instead of doing this SlowCheetah should be used – Sayed Ibrahim Hashimi Apr 28 '12 at 02:49
  • SlowCheetah is certainly an excellent solution if you don't want to manually edit the project file. However, it requires a [Visual Studio extension](http://visualstudiogallery.msdn.microsoft.com/69023d00-a4f9-4a34-a6cd-7e854ba318b5) to be installed in order to work. I would say both solutions are equally viable depending on the requirements or personal preference. – Enrico Campidoglio Apr 29 '12 at 13:35
  • The only thing that the VS Extension does is add the menu items in Visual Studio. Otherwise everything is MSBuild. So if you don't want everyone to have to install the extension you can just check the files in see more at http://sedodream.com/2011/12/12/SlowCheetahXMLTransformsFromACIServer.aspx. – Sayed Ibrahim Hashimi Apr 30 '12 at 04:41
  • 2
    SlowCheetah seems to be restricted to having transforms tied to project configurations, i.e. "App.Debug.config". Using Enrico's description I was able to easily do the same for files that do not stick to the configuration convention. Thanks! – galaktor Aug 21 '12 at 09:34
7

I have created a Visual Studio add in which can be used to transform app.config in the same way that web.config is transformed. You can find the add-in, SlowCheetah, at http://visualstudiogallery.msdn.microsoft.com/69023d00-a4f9-4a34-a6cd-7e854ba318b5.

I have posted a blog about how to get this working on a build server as well.

Sayed Ibrahim Hashimi
  • 43,864
  • 17
  • 144
  • 178
6

I suggest that you wrap your config lookups, extract an interface and stub this when testing.

Morten
  • 3,778
  • 2
  • 25
  • 45
  • It's a good choice, but i was hoping for a Visual studio solution (such as the deployment project). Thanks! – Gonzalo Feb 23 '11 at 13:16