A place for spare thoughts

12/04/2013

MSTest broke compatibility. Again

Filed under: tools, Unit testing, VisualStudio — Ivan Danilov @ 20:20

Visual Studio 2012 Update 2 has changed something in MSTest so that it broke compatibility with ReSharper. From the description it seems it forced JetBrains to release next version on ReSharper, so users of R# had problems only for one week. Great work, actually: I bet it is not a trivial task to establish development process that allow something of R# caliber to be released in one week.

Though it is another nail to MSTest coffin, IMO. It is not only stuck in place and not evolving (gosh, they can’t make asserts for exceptions or even fix bug that ExpectedException attribute don’t check message even if asked to for years!), but actually disrupt work process from time to time with breaking changes, strange SEH exceptions or incompatibilities.

I’m extremely happy we were able to abandon MSTest and adopt NUnit.

If you have new project and deciding which unit-test framework to use – do yourself a favor and avoid MSTest as much as possible.

30/08/2012

Visual Studio 2012: several rows of tabs

Filed under: VisualStudio — Ivan Danilov @ 16:50

There’s very useful ability in Visual Studio 2012 that is disabled be default – I don’t know why. It can be enabled through Tools -> Options -> Environment -> Tabs and Windows -> Show pinned tabs in separate row.

For example, this is VS window with default settings:

You can see that all tabs placed to single row. And some of them even hidden – to look at them – you should click drop down button at the right side of tabs row and select tab you’re looking for. Well, when you need more than four or five tabs at once (maybe a little more depending on your display size) – it quickly becomes very inconvenient experience.

After you turn on “Show pinned tabs in separate row” option… well, nothing changed. That’s because we haven’t pinned any tabs yet, of course. When you do – you’ll see something like that:

And even better: your pinned tabs will never hide under drop down button! When you pin more tabs than one row can take – another row appears:

It is almost as if Visual Studio 6.0 is back! Do you remember that VS 6.0 has multi-row tabs from the start and then in VS 7.0 this useful feature was removed for some reasons? Well, now it is back. And in conjunction with Preview Tab (you can find more info about it here) it shines even better than before.

22/02/2012

Visual Studio usability gem – Perspectives extension

Filed under: VisualStudio — Ivan Danilov @ 03:31

Back in VS 6.0 days there was a feature to manage environments of the Studio: what tool windows you have opened, their layout, toolbar configuration and position etc. It was very handy because when you write code – you don’t need bunch of windows dedicated to debug process like threads, watches, call stacks and the like. After VS 6.0 this feature was silently buried. Instead what you have now is just two configurations of layout: one for debug and one for non-debug times. And VS switches them itself without your intervention.

While these two could be enough sometimes, it is not always the case apparently. In the multi-monitor days it becomes even more true. If you’re actively writing code and looking at connected parts of codebase constantly – you want as much space for your editor windows as possible. Even on several monitors at once. If you have MSDN or stackoverflow opened in your browser and you’re trying to figure out how could they be applied to your situation – you want probably to see browser on the second screen, don’t you? But VS’ toolwindow will readily go on top of the browser instantly as you activate IDE. Arghhh! What can you do? Close toolwindow or dock it somewhere else (just to return it back manually when you no longer need browser). And there are several such scenarios that are forcing to shuffle windows forth and back constantly.

Today I found Perspectives. Basically it is what VS 6.0 had a decade ago – layout manager. It is somewhat buggy, but still can apply and save layouts wonderfully. With that functionality I definitely can tolerate several minor UI issues.

25/07/2011

Checking solutions and project files against some set of rules at build-time

Filed under: MSBuild, Unit testing, VisualStudio — Ivan Danilov @ 04:23

Currently I’m busy with multi-solution project. There’re several teams on the project and each team works on one or several solutions. Sometimes together, sometimes not. There are network of dependencies between solutions (e.g. some shared library that is produced as build artifact of one solution and used by two other).

Clearly that we have some set of rules how things should be organized. And this set at some stage became complex enough to be almost unmaintainable manually.

For example, each project should contain property named OutputRoot defined relative to $(SolutionDir) and OutputPath should be relative to it. Thus we achieve that every artifact is placed to some well-known place under OutputRoot which could be redefined from msbuild command line.

Or the rule that requires HintPaths properties for references should be relative to that $(OutputRoot) or $(SolutionDir). So that no one could place absolute path there that will work only in his environment. Well, it could work on others machines as well by some lucky accident. But build robustness is not that sort of things we want to have by accident.

Also there’s some general rules:

  • every C# project (*.csproj file) should belong to the solution and only one;
  • solutions can’t be nested so if some folder has *.sln file – entire tree from this point should have to other *.slns;
  • ProjectReference couldn’t point outside of solution directory or have absolute path;
  • every project should have property TreatWarningsAsErrors with value true;
  • if Deployment project exists in solution – every other project should have reference to it;
  • test projects (we are using convenience that test projects should be named like *.Tests) are marked with corresponding guids so that test runner can run them;
  • etc…

And so the idea of having some kind of source checker was born. In fact, *.sln and *.csproj are not very complex formats to parse. And we’re not going to support every possible thing in them. We want only to read and check some set of rules.

This source checker now runs on each commit in the source control from the build server and tests every rule we decided to check. If some rule was violated – it reports that, fails build and suggests what should you do to fix the thing.

It is able also to ignore some directories (for example it ignores itself because it shouldn’t be built into $(OutputRoot)). Other useful thing is that it could be integrated with TeamCity so that build log looks pretty there (you should specify /teamcity key in the command line).

Also rules could be easily added or removed.

It proved to be very useful in our day-to-day work and so I’d like to share them with everybody.

It could be not production quality sometimes and several hard-coded things could be inside but nevertheless it is still usable.

Sources could be found here. It is targeting .NET 4.0, but currently it is almost trivial to retarget it against .NET 3.5, so if you’re still there – it is not a problem.

20/07/2011

Deployment as part of development build

Filed under: MSBuild, VisualStudio — Ivan Danilov @ 19:25

Last two years or so I’ve been mostly developing solutions that are plugins to some other systems. Sometimes it is third-party library, sometimes it is some kind of corporate-standard-framework with some common presentation functionality whatever.

The shared feature of every such project is that is should be installed in some way into that external system. Most often installing is just copying DLLs into some predefined folder for plugins. Sometimes it is also required to register something in registry, settings of the system or somewhere else. And that is the problem. When you’re working from Visual Studio – the most natural process is to make changes and hit F5 to run and see your changes live (I don’t take testing into consideration here but it is an important part of course). Well, VS can run external application under debugger. But who will make installation steps?

I’ve seen several approaches there. Some teams just point OutputPath to external system’s plugins folder so that building takes place there. It is not a good idea as csproj files are under source version control system and paths could be different on different machines. Most obvious example is that when external system is 32bit – it has different paths on 32bit and 64bit windows. Other example is build server that could even not having this external system installed. Its task usually is to build (and probably run unit tests, code coverage, make installation kit etc), not to run actual system. Moreover if external system is running and have your assemblies from previous build locked (as they’re loaded into it) – VS can’t clean them or replace with new ones which sometimes lead to confusion.

Yet another teams perform build with default settings and then manually (or with help of some batch file) copying needed artifacts to the place where they should be. It is slightly better in terms of build server integration and locked files. But it requires three steps to run application: build solution from VS, switch to explorer (or whatever you’re using) and run batch file, switch back to VS and run external system with debugger attached. It is not only a pain, it is also error-prone as one could easily forget to do one of these steps.

And yet another teams have post-build step in some project. And that post-build step executes actual install. Here we have problem with project build skipping. VS has some heuristics to determine which projects should be built and which could be skipped. First of all it skips everything that is up-to-date (i.e. hasn’t any file or referenced project changed after last successful build). Also VS skips everything that wasn’t set up for build in current configuration (in the solution properties). Thus said if project build is skipped – post-build step is skipped also. Oops. VS doesn’t know that you have some dependencies in the post-build step. And worse it would be skipped without any warning so that you can find it only when something goes wrong. Not good definitely.

The solution to this problem that I came with in the end is to have separate project in the solution dedicated to this installation step. And this solution should have references to any other project in the solution. To be clear here, this Deployment project is not going to call compiler or produce build errors if referenced projects were not built (due to current configuration for example). The single purpose of these references is to give VS knowledge about when it could really skip build of Deployment project – only when no projects in the solution changed. Otherwise it should build Deployment.

The downside of this approach is evident: you should carefully watch over rule that Deployment has these references actual. But this task could be automated more or less easily. Maybe I will write a blog post soon about such thing. We have this checking as a part of build process so if anyone makes a change violating the rules we have failed build. It just works.

UPDATE: I’ve written such post. See here.

And here is example of Deployment.csproj file:

<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
  <PropertyGroup>
    <ProjectGuid>{5739A0D8-2EE2-4CEE-BFCD-C748E47E320D}</ProjectGuid>
    <OutputType>Library</OutputType>
    <!-- VS2010 for some reason requires OutputType property set. Otherwise it throws
         an error when you try to open project properties window -->
  </PropertyGroup>
  <ItemGroup>
    <ProjectReference Include="..\YourProject\YourProject.csproj">
      <Project>{3612C9DD-7086-436A-9AB7-A3EAA995881F}</Project>
      <Name>YourProject</Name>
    </ProjectReference>
    <!-- Other project references goes there -->
  </ItemGroup>
  <Target Name="Build">
    <ItemGroup>
      <ToCopy Include="..\YourProject\bin\Debug\*.dll" />
      <ToCopy Include="..\YourProject\bin\Debug\*.pdb" />
      <ToCopy Include="..\YourProject\bin\Debug\*.config" />
      <ToCopy Include="..\AnotherDirToCopyFrom\*.*" />
      <!-- Actually you could have any set of things to copy here -->
    </ItemGroup>
    <Message Text="ToCopy = $(ToCopy)" />
    <Message Text="DestinationFolder = $(InstallDir)\Plugins\YourPluginName" />
    <Copy Condition=" '$(InstallDir)' != '') " 
          SourceFiles="@(ToCopy)" 
          DestinationFolder="$(InstallDir)\Plugins\YourPluginName" />
    <!-- This target copy things only if %InstallDir% environment variable exists.
         It prevents copying from executing on build server if it doesn't have the system
         installed.
         You could have every conditional logic you want instead. -->
  </Target>
  <Target Name="Clean" />
  <!-- If you need to do something on the Clean - put it here. But don't delete
       this target at all because VS will give you an error that project does not have
       Clean target on each Clean or Rebuild -->
  <Target Name="Rebuild" DependsOnTargets="Build" />
</Project>

The main point here is that we don’t import $(MSBuildToolsPath)\Microsoft.CSharp.targets and define our own Build, Clean and Rebuild targets which are called by VS. Probably you will have to define also Publish target if you use it.

12/07/2011

MSTest 2010 on the build server without VS2010 installed

Filed under: Unit testing, VisualStudio — Ivan Danilov @ 23:24

Some time ago I found description how to get MSTest 2008 running without Visual Studio. This time I faced the same question with VS2010. Quick googling hadn’t result in any decent answer, so I did some digging.

I won’t enumerate countless exceptions, silent hangs, unclear error messages and happy MSTest’s reports about ‘Not executed’ tests without any reason. I’m pissed as hell with MSTest and its problems but forced to use it. So I will just describe how to have it working.

What you’ll need is some machine with VS2010 SP1 installed to get assemblies and MSTest.exe from. SP1 is very desirable if you want to test .NET 4.0 project and absolutely mandatory in case of earlier platform versions because without SP1 it is impossible AFAIK. Go to that box and take:

  1. c:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\PrivateAssemblies\Microsoft.VisualStudio.QualityTools.*.dll
  2. c:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\PublicAssemblies\Microsoft.VisualStudio.QualityTools.*.dll
  3. c:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\MSTest.exe
    c:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\MSTest.exe.config
  4. c:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\QTAgent.exe
    c:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\QTAgent.exe.config
    c:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\QTAgent32.exe
    c:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\QTAgent32.exe.config
    c:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\QTAgent32_35.exe
    c:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\QTAgent32_35.exe.config
    c:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\QTAgent_35.exe
    c:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\QTAgent_35.exe.config

Make on target machine folder C:\VS2010Stub\ and copy these assemblies/configs there preserving the structure (so you’ll have C:\VS2010Stub\Common7\IDE\ and everything mentioned there and in PrivateAssemblies/PublicAssemblies).

The first step is done. The next is getting dependencies from GAC. Take gacutil.exe (check that you have one from .NET 4, i.e. it should have version like 4.0.30319.1 – older versions will result in an error), take gacutlrc.dll (without it gacutil.exe will exit silently – at least it was so on my build server) and copy them to target machine. We will need them to register extracted dependencies to GAC.

Now you need to take Microsoft.VisualStudio.QualityTools.*.dll from source machine’s GAC. Notice that some of these assemblies have two versions if you have VS2010 SP1 installed: 10.0.0.0 and 10.1.0.0. You need them both. Do not rename assembly files because gacutil.exe won’t be able to register renamed assemblies afterwards. Just put different versions with same name in different folders.

In my dev box these files was placed in C:\Windows\assembly\GAC_MSIL\Microsoft.VisualStudio.QualityTools.*\Microsoft.VisualStudio.QualityTools.*.dll and in c:\Windows\Microsoft.NET\assembly\GAC_MSIL\Microsoft.VisualStudio.QualityTools.*\…\Microsoft.VisualStudio.QualityTools.*.dll. Most probably you will find them there as well, but as I don’t know actual GAC structure (I’m not sure official documentation on GAC internals exists actually) – I can’t guarantee that.

UPD: if you will use database unit testing, you will need three more assemblies from the GAC – Microsoft.Data.Schema.dll, Microsoft.Data.Schema.Utilities.dll and Microsoft.Data.Schema.UnitTesting.dll. Note that archive at the end of this post doesn’t include them.

In any case take each of these assemblies to the target box and execute ‘gacutil.exe /i Microsoft.etc-etc-etc.dll’. You should see ‘Assembly successfully added to the cache’ for each one.

The second step is done. Now goes the last one. MSTest has some settings in the registry about file extensions that could be tested. Actually now you have MSTest functional, but if you try to run it on any assembly with tests it will say something about ‘dll is not correct test container’. Well, lets fix that.

Go to registry on the source box, find key HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\VisualStudio\10.0\EnterpriseTools\QualityTools\TestTypes (*)  and export it in some *.reg. And import on the target box. Be careful though to run reg file from 64-bit program. Explorer will go well. I have Total Commander which is 32-bit and sometimes accidentally run reg files from it. Which results in doubling Wow6432Node part.

That’s  it. Now you should be able to run MSTest.exe and test something with it successfully.

UPD: If you’re using MSTest private accessors – you’ll also need this as I discovered much later. Zip-archive linked below is already updated.

In case you’re lazy, there‘s zip-archive with everything already made for you. Just extract contents of the MSTest2010Install.zip to the target box and run install.bat.

 

(*) I’m assuming both source and target boxes have 64-bit OS. Otherwise you should cope with Wow6432Node part. It is easy but somewhat annoying. Feel free to ask me if you don’t know how to do this.

11/07/2011

Troubleshooting: MSTest assembly doesn’t get discovered by VS native test runner

Filed under: Unit testing, VisualStudio — Ivan Danilov @ 16:06

Well, I already faced similar problem here so I know where to look. And it was really the cause.

To have test runner see tests in the assembly its csproj file should contain this line:

<ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>

Troubleshooting: Resharper, Visual Studio 2010, MSTest and .NET 3.5 in single bottle

Filed under: Unit testing, VisualStudio — Ivan Danilov @ 15:59

We are in process of moving .NET 3.5 project to .NET 4.0 and VS2010. We’re still not ready to move our app to .NET 4.0 but decided to adopt VS2010 first. It is somewhat unusual situation so it caused some troubles.

First of all, unit testing projects under MSTest just can’t target framework v3.5 prior to VS2010 SP1.

VS2008 for unit testing was using Microsoft.VisualStudio.QualityTools.UnitTestFramework assembly, version 9.0.0.0. It was for v3.5 so everything is cool. Except the fact that if VS2010 sees version 9.0.0.0 in any reference to this assembly – it starts Project Conversion Dialog and prompts you to convert your solution. Thus in VS2010 you just can’t have reference to 9.0.0.0 (nevertheless MSBuild works well with it, so it seems it is VS limitation).

VS2010 (without SP1) switched to 10.0.0.0 with the same name. Well, to be precise file version is 10.0.30319.1. If your unit testing project references 10.0.0.0 – it can’t have TargetFrameworkVersion=v3.5. VS just sets it back to v4.0. So if you need to test v3.5 assemblies with it – well, you’re in trouble.

Fortunately, MS recognized the problem before I faced it and with SP1 there’s new version of the assembly – 10.1.0.0 or 10.0.40219.1. It allows you to target v3.5 framework and test in the VS2010. After I realized it and changed my references to 10.1.0.0 – native VS test runner get test correctly. Everything was green and bright.

Just until I ran it in the ReSharper 5.1 which I had installed. Well, it had test count correctly but don’t run anything. After some stack overflow searching I ensured that I’m not only one with the problem and that it is fixed in ReSharper 6.0. So I installed 6.0, run tests and… half of them were broken! Something is not right. Native runner got them well.

After some debugging I find out in Debug -> Windows -> Modules that resharper’s runner had 10.0.30319.1 version loaded. Maybe it is the problem was my first thought. Well, how could I force program to load other version of the assembly in .NET? With configuration, namely with assembly binding redirection. I had just happened to see MS guide to troubleshoot VS2010 unit testing. So my problem seemed very close…

So, I had opened my c:\Windows\Microsoft.NET\Framework\v4.0.30319\Config\machine.config and added there such lines:

<runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
        <dependentAssembly>
            <assemblyIdentity name="Microsoft.VisualStudio.QualityTools.UnitTestFramework" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
            <bindingRedirect oldVersion="10.1.0.0" newVersion="10.0.0.0"/>
          </dependentAssembly>
        <dependentAssembly>    
            <assemblyIdentity name="Microsoft.VisualStudio.QualityTools.Tips.UnitTest.Adapter" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
            <bindingRedirect oldVersion="10.1.0.0" newVersion="10.0.0.0"/>
        </dependentAssembly>
        <dependentAssembly>
            <assemblyIdentity name="Microsoft.VisualStudio.QualityTools.Tips.UnitTest.ObjectModel" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
            <bindingRedirect oldVersion="10.1.0.0" newVersion="10.0.0.0"/>
        </dependentAssembly>
        <dependentAssembly>
            <assemblyIdentity name="Microsoft.VisualStudio.QualityTools.Tips.UnitTest.Tip" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
            <bindingRedirect oldVersion="10.1.0.0" newVersion="10.0.0.0"/>
        </dependentAssembly>
    </assemblyBinding>
</runtime>

After that ReSharper gets my tests correctly.

To be honest I don’t understand why this redirection actually solved the problem. We are telling runtime to load 10.0.0.0 instead of 10.1.0.0 in case it is requested, not vice-versa. But it is analogous to config file in many MS applications like devenv.exe.config, mstest.exe.config etc, so probably they know what to do with their own libraries better than I do.

11/05/2011

Missing WPF menu items in Visual Studio

Filed under: VisualStudio, wpf — Ivan Danilov @ 12:21

Probably you as me often were confused by the fact that some useful menu items are absent in the Add New Item in Visual Studio when you are working with Class Library.

When adding item into WPF Application you have this menu:

But with Class Library you have only this:

It turns out that you should be adding not Class Library if you want to work with WPF features but rather WPF Custom Control Library project. And then you will have all you need. I find it myself while reading Karl Shifflett’s article. Thanks, Karl! 🙂

Everything is great. Except the fact that you could already have Class Library with hundreds files inside. What should you do in this case? Replacing project and adding all your files again not seems like a good idea. So lets find out what is the difference between the project files from Visual Studio’s point of view.

I’ve created new solution, added there WPF Custom Control Library and plain old Class Library. The diff between the WpfControlLibrary1.csproj and ClassLibrary1.csproj you could see below:

The interesting point is highlighted. Its the only significant difference. In order to have XAML-related menu items in your project you have to paste this line to your *.csproj file:

<ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>

Enjoy!

Create a free website or blog at WordPress.com.