views:

29

answers:

1

I'm working on a VS plugin for handling a new test type. One of the things we want to do is add an MSBuild import to the project file when one of our tests is in the test project, to run our custom build tasks.

I can add the import element by using Microsoft.Build.BuildEngine.Project.Imports, but if I save the project file via the BuildEngine object I get a "File has been modified outside of Visual Studio" warning. Adding new Imports to the Project.Imports collection doesn't seem to mark the project as dirty in Visual Studio though, so I can't rely on VS to save the file correctly normally.

Is there any way I can access this part of MSBuild functionality through the DTE.Project or VSLangProj.Project objects?

Thanks.

A: 

I'd suggest you add a fixed Import to your .csproj and decide inside that .targets whether to execute the tests or not:

Your .csproj

<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="CustomTest.targets" Condition="Exists('CustomTest.targets')" />

Note the Condition to check if the .targets is available.

Your CustomTest.targets

Determine which tests to run by setting the according conditions.

<Project DefaultTargets="RunTests" xmlns="...">
    <ItemGroup>
        <MyCustomTests Include="Test_A" Condition="Exists('Test_A.cs')" />
        <MyCustomTests Include="Test_B" Condition="Exists('Test_B.cs')" />
    </ItemGroup>

    <Target Name="RunTests" Condition="@(MyCustomTests)!=''">
        <Message Text="Running Test %(MyCustomTests.Identity)" />
    </Target>
</Project>

You could even extend your MyCustomTests Item with some metadata you may need for running your test:

    ...
    <ItemGroup>
        <MyCustomTests Include="Test_A" Condition="Exists('Test_A.cs')">
            <TestType>Boundary</TestType>
        </MyCustomTests>
        <MyCustomTests Include="Test_B" Condition="Exists('Test_B.cs')">
            <TestType>SQLInjection</TestType>
        </MyCustomTests>
    </ItemGroup>

    ...
    <Message Text="Running %(MyCustomTests.TestType) Test %(MyCustomTests.Identity)" />
    ...
Filburt
A complicating issue is that we don't create or maintain the .csproj file; we're providing a VS plugin with a new test type for customers to use.We're trying not to junk up customers' .csproj files with Imports unless they contain one of our tests. That might be a usable workaround though. Is it possible to write an Exists condition with wildcards? e.g. Condition="Exists('*.foo')"
Greg