Wednesday, 3 June 2009

Testing Code Generation and System.Xml file version

I found a very interesting problem the other day whilst trying to write automated test for a class that generated code.

I started by creating a file with what I expected the generated code to look like as my code generator was dumping its output straight to a file and then comparing the outcome. All went well and my tests past. Things only started to get strange when I checked in my code and it hit the CI server. The test started to fail.

By default the Code generator was putting the following attribute at the top of my classes:

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "TOKEN_VERSION")]

Where TOKEN_VERSION was the file version of the System.Xml.dll assembly. Wierdly enough the version of this file on the CI server was different, but only at the file level. Now My machine and the CI server bothe had 3.5 SP1 installed and so I was baffled. I did some googling and found that many different applications can install different version of this file. So then I decided to use a token in my expected file and change this with the actual version when the test was ran. I could have chosen to remove the attribute altogether, but this was the chosen path...

I was unaware of the following class but found it very useful in getting the file version, here is the end code:

    1 var assembly = AppDomain.CurrentDomain.GetAssemblies().First(a => a.GetName().Name == "System.Xml");
    2 
    3 var version = FileVersionInfo.GetVersionInfo(new Uri(assembly.CodeBase).AbsolutePath).ProductVersion;

Simple eh?

Submit this story to DotNetKicks Shout it

No comments:

Post a Comment