Friday, September 10, 2010

Flattening an AD group hierarchy

Sometimes it can be difficult to find all the members of an active directory group when there are multiple child or sub groups.

To flatten the hierarchy, use this command:
dsquery group -limit 0 | select-string group_to_find | dsget group -members -expand

Friday, August 6, 2010

Calculating prices after GST changes

There has been some dodgy maths in the New Zealand media recently around how much things will cost after the GST increases in October. The rate will be increasing from 12.5% to 15%, so some media are simply adding 2.5% on top of the price. Unfortunately, the calculation is not that simple. First, you need to remove the current GST portion, and then calculate the new price with the new GST rate:



Unfortunately, the calculation is not easy to do in your head: 46 × originalprice / 45. Another way of looking at it is to add 1/45 of the current price, or ~2.22%

Friday, July 16, 2010

Overriding protected members using Moq and/or RhinoMocks

Sometimes I want to override a protected method on an internal class using a mocking framework and make verifications against calls to that method. This problem also occurs when trying to mock an internal interface in the assembly under test.

When doing this with Moq or RhinoMocks (both of which use the same proxy generator), I got the following error:

"Castle.DynamicProxy.Generators.GeneratorException: Type is not public, so a proxy cannot be generated."

The solution is to publish the internals of the assembly under test to the appropriate assembly:

[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2," +
    "PublicKey=00240000048000009400000006020000002400005" +
    "25341310004000001000100c547cac37abd99c8db225ef2f6c8" +
    "a3602f3b3606cc9891605d02baa56104f4cfc0734aa39b93bf7" +
    "852f7d9266654753cc297e7d2edfe0bac1cdcf9f717241550e0" +
    "a7b191195b7667bb4f64bcb8e2121380fd1d9d46ad2d92d2d15" +
    "605093924cceaf74c4861eff62abf69b9291ed0a340e113be11" +
    "e6a7d3113e92484cf7045cc7")]

Tuesday, July 13, 2010

Relative paths in .testsettings file

To configure relative code coverage paths in the .testsettings file, you will need to manually edit the xml file. First configure your coverage DLLs using the UI, then open the .testsettings file in a text editor, or in VS using the VS text editor.

Edit the absolute paths to be relative to the location of your testsettings file, and use the %outdir% macro instead of bin\Debug:
..\..\Utilities\%outdir%\Utilities.dll

Sunday, June 20, 2010

Extracting files in PowerShell without third party components

Sometimes it is not acceptable to install or use third party components such as 7-zip or the PowerShell Community Extensions onto machines (particularly because they are not digitally signed). This script will extract files using the shell API:

param(
[parameter(mandatory=$true)][string]$SourceFile,
[parameter(mandatory=$true)][string]$DestinationFolder)

if(-not (test-path $DestinationFolder))
{
    md $DestinationFolder | Out-Null
}

$absoluteSource = resolve-path $SourceFile | select -ExpandProperty path
$absoluteDestination = resolve-path $DestinationFolder | select -ExpandProperty path

$app = new-object -ComObject Shell.Application
$archive = $app.Namespace($absoluteSource).Items()
$dest = $app.Namespace($absoluteDestination)
$dest.CopyHere($archive)

Thursday, May 20, 2010

PowerShell and slash-colon parameters

PowerShell plays around with the quotes in parameters when part of the parameter is quoted like this:

/param:"quoted path"

This gets passed to the application as:
"/param:quoted path"

The only workaround I have found works like this:
$cmd = "& `"application`" /p:`"path`"; `$result = `$?"
Invoke-expression $cmd
if(!$result)
{
    throw "Application failed."
}

Monday, May 3, 2010

Running MSTest in Parallel

This blog describes how to execute tests in parallel in MSTest.