Skip to content

Instantly share code, notes, and snippets.

@dfch
Last active December 26, 2015 09:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dfch/ecfa48a38dbf51726892 to your computer and use it in GitHub Desktop.
Save dfch/ecfa48a38dbf51726892 to your computer and use it in GitHub Desktop.
[Bug] PowerShell Hashtables and duplicate keys with Import-CliXml
#Requires -Module Pester
#Requires -Version 3.0
# see http://d-fens.ch/2015/06/28/bug-powershell-import-clixml-incorrectly-creates-hashtables-with-duplicate-keys/ for further explanation
$here = Split-Path -Parent $MyInvocation.MyCommand.Path
$ImportCliXmlDuplicateKeys = "{0}.xml" -f $MyInvocation.MyCommand.Name.Replace('.Tests.ps1', '');
Describe -Tags "Test-ImportCliXml" "Test-ImportCliXml" {
$ImportCliXmlDuplicateKeysPathAndFileName = Join-Path -Path $here -ChildPath $ImportCliXmlDuplicateKeys;
Context "Test-ImportCliXmlWithDuplicateKeys" {
It 'ShouldThrow-OnImportWithDuplicateKeys' {
{ Import-CliXml $ImportCliXmlDuplicateKeysPathAndFileName } | Should Throw;
}
It 'ShouldNotBe-TypeHashtable' {
$hashtable = Import-CliXml $ImportCliXmlDuplicateKeysPathAndFileName;
$hashtable -is [hashtable] | Should Not Be $True;
}
It 'ShouldNot-Contain2Keys' {
$hashtable = Import-CliXml $ImportCliXmlDuplicateKeysPathAndFileName;
$hashtable.Count | Should Not Be 2;
}
It 'ShouldNotThrow-ExceptionWhenAdding' {
$hashtable = Import-CliXml $ImportCliXmlDuplicateKeysPathAndFileName;
$hashtableTemp = @{};
{
foreach($i in $hashtable.GetEnumerator())
{
$hashtableTemp.Add($i.Name, $i.Value);
}
} | Should Not Throw;
}
It 'ShouldBe-TrueAndFindKey' {
$hashtable = Import-CliXml $ImportCliXmlDuplicateKeysPathAndFileName;
foreach($i in $hashtable.Keys)
{
$keyName = '{0}' -f $i;
break;
}
$hashtable.ContainsKey($keyName) | Should Be $true;
}
It 'ShouldBe-TrueAndFindValues' {
$hashtable = Import-CliXml $ImportCliXmlDuplicateKeysPathAndFileName;
$hashtable.ContainsValue('some other arbitrary value') | Should Be $true;
$hashtable.ContainsValue('arbitrary value') | Should Be $true;
}
}
}
#
# Copyright 2015 Ronald Rink, d-fens GmbH
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
<Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04">
<Obj RefId="0">
<TN RefId="0">
<T>System.Collections.Hashtable</T>
<T>System.Object</T>
</TN>
<DCT>
<En>
<S N="Key">DuplicateKey</S>
<S N="Value">arbitrary value</S>
</En>
<En>
<S N="Key">DuplicateKey</S>
<S N="Value">some other arbitrary value</S>
</En>
</DCT>
</Obj>
</Objs>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment