Skip to content

Commit f24366d

Browse files
committed
Fix unit testing
1 parent 5dba017 commit f24366d

4 files changed

Lines changed: 482 additions & 560 deletions

File tree

Source/Configuration.psm1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ function ParameterBinder {
2222
[System.Management.Automation.PSModuleInfo]$Module = . {
2323
$mi = ($CallStack)[0].InvocationInfo.MyCommand.Module
2424
if($mi -and $mi.ExportedCommands.Count -eq 0) {
25-
if($mi2 = Get-Module $mi.ModuleBase -ListAvailable | Where-Object Name -eq $mi.Name | Where-Object ExportedCommands | Select-Object -First 1) {
25+
if($mi2 = Get-Module $mi.ModuleBase -ListAvailable | Where-Object { ($_.Name -eq $mi.Name) -and $_.ExportedCommands } | Select-Object -First 1) {
2626
$mi = $mi2
2727
}
2828
}

Source/Metadata.psm1

Lines changed: 64 additions & 143 deletions
Original file line numberDiff line numberDiff line change
@@ -91,24 +91,24 @@ function Add-MetadataConverter {
9191

9292
if($Converters.Count) {
9393
switch ($Converters.Keys.GetEnumerator()) {
94-
{$Converters.$_ -isnot [ScriptBlock]} {
95-
Write-Error "Ignoring $_ converter, value must be ScriptBlock"
94+
{$Converters[$_] -isnot [ScriptBlock]} {
95+
Write-Error "Ignoring $_ converter, the value must be ScriptBlock!"
9696
continue
9797
}
9898

9999
{$_ -is [String]}
100100
{
101101
# Write-Debug "Storing deserialization function: $_"
102-
Set-Content "function:script:$_" $Converters.$_
103-
# We need to store the given function name in MetadataConverters too
104-
$MetadataConverters.$_ = $Converters.$_
102+
Set-Content "function:script:$_" $Converters[$_]
103+
# We need to store the function in MetadataDeserializers
104+
$MetadataDeserializers[$_] = $Converters[$_]
105105
continue
106106
}
107107

108108
{$_ -is [Type]}
109109
{
110110
# Write-Debug "Adding serializer for $($_.FullName)"
111-
$MetadataConverters.$_ = $Converters.$_
111+
$MetadataSerializers[$_] = $Converters[$_]
112112
continue
113113
}
114114
default {
@@ -171,11 +171,13 @@ function ConvertTo-Metadata {
171171
)
172172
begin {
173173
$t = " "
174-
$Script:OriginalMetadataConverters = $Script:MetadataConverters.Clone()
174+
$Script:OriginalMetadataSerializers = $Script:MetadataSerializers.Clone()
175+
$Script:OriginalMetadataDeserializers = $Script:MetadataDeserializers.Clone()
175176
Add-MetadataConverter $Converters
176177
}
177178
end {
178-
$Script:MetadataConverters = $Script:OriginalMetadataConverters.Clone()
179+
$Script:MetadataSerializers = $Script:OriginalMetadataSerializers.Clone()
180+
$Script:MetadataDeserializers = $Script:OriginalMetadataDeserializers.Clone()
179181
}
180182
process {
181183
if($Null -eq $InputObject) {
@@ -218,16 +220,16 @@ function ConvertTo-Metadata {
218220
}) -f ($(
219221
ForEach($key in $InputObject | Get-Member -MemberType Properties | Select-Object -ExpandProperty Name) {
220222
if("$key" -match '^(\w+|-?\d+\.?\d*)$') {
221-
"$key = " + (ConvertTo-Metadata $InputObject.($key) -AsHashtable:$AsHashtable)
223+
"$key = " + (ConvertTo-Metadata $InputObject[$key] -AsHashtable:$AsHashtable)
222224
}
223225
else {
224-
"'$key' = " + (ConvertTo-Metadata $InputObject.($key) -AsHashtable:$AsHashtable)
226+
"'$key' = " + (ConvertTo-Metadata $InputObject[$key] -AsHashtable:$AsHashtable)
225227
}
226228
}
227229
) -split "`n" -join "`n$t")
228230
}
229-
elseif($MetadataConverters.ContainsKey($InputObject.GetType())) {
230-
$Str = ForEach-Object $MetadataConverters.($InputObject.GetType()) -InputObject $InputObject
231+
elseif($MetadataSerializers.ContainsKey($InputObject.GetType())) {
232+
$Str = ForEach-Object $MetadataSerializers.($InputObject.GetType()) -InputObject $InputObject
231233

232234
[bool]$IsCommand = & {
233235
$ErrorActionPreference = "Stop"
@@ -293,16 +295,17 @@ function ConvertFrom-Metadata {
293295
[Switch]$Ordered
294296
)
295297
begin {
296-
$Script:OriginalMetadataConverters = $Script:MetadataConverters.Clone()
298+
$Script:OriginalMetadataSerializers = $Script:MetadataSerializers.Clone()
299+
$Script:OriginalMetadataDeserializers = $Script:MetadataDeserializers.Clone()
297300
Add-MetadataConverter $Converters
298301
[string[]]$ValidCommands = @(
299-
"PSObject", "ConvertFrom-StringData", "Join-Path", "Split-Path", "ConvertTo-SecureString",
300-
"Guid", "bool", "SecureString", "Version", "DateTime", "DateTimeOffset", "PSCredential", "ConsoleColor", "ScriptBlock"
301-
) + @($MetadataConverters.Keys.GetEnumerator() | Where-Object { $_ -isnot [Type] })
302+
"ConvertFrom-StringData", "Join-Path", "Split-Path", "ConvertTo-SecureString"
303+
) + @($MetadataDeserializers.Keys)
302304
[string[]]$ValidVariables = "PSScriptRoot", "ScriptRoot", "PoshCodeModuleRoot","PSCulture","PSUICulture","True","False","Null"
303305
}
304306
end {
305-
$Script:MetadataConverters = $Script:OriginalMetadataConverters.Clone()
307+
$Script:MetadataSerializers = $Script:OriginalMetadataSerializers.Clone()
308+
$Script:MetadataDeserializers = $Script:OriginalMetadataDeserializers.Clone()
306309
}
307310
process {
308311
$ErrorActionPreference = "Stop"
@@ -420,9 +423,9 @@ function Import-Metadata {
420423
}
421424
if(!(Test-Path $Path)) {
422425
WriteError -ExceptionType System.Management.Automation.ItemNotFoundException `
423-
-Message "Can't find file $Path" `
424-
-ErrorId "PathNotFound,Metadata\Import-Metadata" `
425-
-Category "ObjectNotFound"
426+
-Message "Can't find file $Path" `
427+
-ErrorId "PathNotFound,Metadata\Import-Metadata" `
428+
-Category "ObjectNotFound"
426429
return
427430
}
428431
try {
@@ -440,7 +443,7 @@ function Export-Metadata {
440443
.Description
441444
Serves as a wrapper for ConvertTo-Metadata to explicitly support exporting to files
442445
443-
Note that exportable data is limited by the rules of data sections (see about_Data_Sections) and the available MetadataConverters (see Add-MetadataConverter)
446+
Note that exportable data is limited by the rules of data sections (see about_Data_Sections) and the available MetadataSerializers (see Add-MetadataConverter)
444447
445448
The only things inherently importable in PowerShell metadata files are Strings, Booleans, and Numbers ... and Arrays or Hashtables where the values (and keys) are all strings, booleans, or numbers.
446449
@@ -691,141 +694,59 @@ function Get-Metadata {
691694
Set-Alias Update-Manifest Update-Metadata
692695
Set-Alias Get-ManifestValue Get-Metadata
693696

694-
# These functions are simple helpers for use in data sections (see about_data_sections) and .psd1 files (see ConvertFrom-Metadata)
695-
function PSObject {
696-
<#
697-
.Synopsis
698-
Creates a new PSCustomObject with the specified properties
699-
.Description
700-
This is just a wrapper for the PSObject constructor with -Property $Value
701-
It exists purely for the sake of psd1 serialization
702-
.Parameter Value
703-
The hashtable of properties to add to the created objects
704-
#>
705-
param([hashtable]$Value)
706-
New-Object System.Management.Automation.PSObject -Property $Value
707-
}
708-
709-
function DateTime {
710-
<#
711-
.Synopsis
712-
Creates a DateTime with the specified value
713-
.Description
714-
This is basically just a type cast to DateTime, the string needs to be castable.
715-
It exists purely for the sake of psd1 serialization
716-
.Parameter Value
717-
The DateTime value, preferably from .Format('o'), the .Net round-trip format
718-
#>
719-
param([string]$Value)
720-
[DateTime]$Value
721-
}
722-
723-
function DateTimeOffset {
724-
<#
725-
.Synopsis
726-
Creates a DateTimeOffset with the specified value
727-
.Description
728-
This is basically just a type cast to DateTimeOffset, the string needs to be castable.
729-
It exists purely for the sake of psd1 serialization
730-
.Parameter Value
731-
The DateTimeOffset value, preferably from .Format('o'), the .Net round-trip format
732-
#>
733-
param([string]$Value)
734-
[DateTimeOffset]$Value
735-
}
736-
737-
function PSCredential {
738-
<#
739-
.Synopsis
740-
Creates a new PSCredential with the specified properties
741-
.Description
742-
This is just a wrapper for the PSObject constructor with -Property $Value
743-
It exists purely for the sake of psd1 serialization
744-
.Parameter Value
745-
The hashtable of properties to add to the created objects
746-
#>
747-
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingPlainTextForPassword","EncodedPassword")]
748-
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingUserNameAndPasswordParams","")]
749-
param(
750-
# The UserName for this credential
751-
[string]$UserName,
752-
# The Password for this credential, encoded via ConvertFrom-SecureString
753-
[string]$EncodedPassword
754-
)
755-
New-Object PSCredential $UserName, (ConvertTo-SecureString $EncodedPassword)
756-
}
757-
758-
function ConsoleColor {
759-
<#
760-
.Synopsis
761-
Creates a ConsoleColor with the specified value
762-
.Description
763-
This is basically just a type cast to ConsoleColor, the string needs to be castable.
764-
It exists purely for the sake of psd1 serialization
765-
.Parameter Value
766-
The ConsoleColor value, preferably from .ToString()
767-
#>
768-
param([string]$Value)
769-
[ConsoleColor]$Value
770-
}
771-
772-
function ScriptBlock {
773-
<#
774-
.Synopsis
775-
Creates a ScriptBlock from a string
776-
.Description
777-
Just calls [ScriptBlock]::Create with the passed-in value
778-
.Parameter Value
779-
The ScriptBlock as a string
780-
#>
781-
param([string]$Value)
782-
[scriptblock]::Create($Value)
783-
}
784-
785-
786-
$MetadataConverters = @{}
697+
$MetadataSerializers = @{}
698+
$MetadataDeserializers = @{}
787699

788700
if($Converters -is [Collections.IDictionary]) {
789701
Add-MetadataConverter $Converters
790702
}
791703

792-
# The OriginalMetadataConverters
704+
# The OriginalMetadataSerializers
793705
Add-MetadataConverter @{
794-
[bool] = { if($_) { '$True' } else { '$False' } }
706+
[bool] = { if($_) { '$True' } else { '$False' } }
707+
[Version] = { "'$_'" }
708+
[PSCredential] = { 'PSCredential "{0}" "{1}"' -f $_.UserName, (ConvertFrom-SecureString $_.Password) }
709+
[SecureString] = { "ConvertTo-SecureString {0}" -f (ConvertFrom-SecureString $_) }
710+
[Guid] = { "Guid '$_'" }
711+
[DateTime] = { "DateTime '{0}'" -f $InputObject.ToString('o') }
712+
[DateTimeOffset] = { "DateTimeOffset '{0}'" -f $InputObject.ToString('o') }
713+
[ConsoleColor] = { "ConsoleColor {0}" -f $InputObject.ToString() }
795714

796715
[System.Management.Automation.SwitchParameter] = { if($_) { '$True' } else { '$False' } }
797716

798-
[Version] = { "'$_'" }
799-
800-
[PSCredential] = { 'PSCredential "{0}" "{1}"' -f $_.UserName, (ConvertFrom-SecureString $_.Password) }
801-
802-
[SecureString] = { "ConvertTo-SecureString {0}" -f (ConvertFrom-SecureString $_) }
803-
804-
# This GUID is here instead of as a function
805-
# just to make sure the tests can validate the converter hashtables
806-
Guid = {
807-
<#
808-
.Synopsis
809-
Creates a GUID with the specified value
810-
.Description
811-
This is basically just a type cast to GUID.
717+
# This GUID is here instead of as a function
718+
# just to make sure the tests can validate the converter hashtables
719+
"Guid" = { [Guid]$Args[0] }
720+
"PSObject" = { New-Object System.Management.Automation.PSObject -Property $Args[0] }
721+
"DateTime" = { [DateTime]$Args[0] }
722+
"DateTimeOffset" = { [DateTimeOffset]$Args[0] }
723+
"ConsoleColor" = { [ConsoleColor]$Args[0] }
724+
"ScriptBlock" = { [scriptblock]::Create($Args[0]) }
725+
"PSCredential" = {
726+
<#
727+
.Synopsis
728+
Creates a new PSCredential with the specified properties
729+
.Description
730+
This is just a wrapper for the PSObject constructor with -Property $Value
812731
It exists purely for the sake of psd1 serialization
813-
.Parameter Value
814-
The GUID value.
815-
#>
816-
param([string]$Value)
817-
[Guid]$Value
818-
}
819-
[Guid] = { "Guid '$_'" }
820-
821-
[DateTime] = { "DateTime '{0}'" -f $InputObject.ToString('o') }
822-
823-
[DateTimeOffset] = { "DateTimeOffset '{0}'" -f $InputObject.ToString('o') }
732+
.Parameter Value
733+
The hashtable of properties to add to the created objects
734+
#>
735+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingPlainTextForPassword","EncodedPassword")]
736+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSAvoidUsingUserNameAndPasswordParams","")]
737+
param(
738+
# The UserName for this credential
739+
[string]$UserName,
740+
# The Password for this credential, encoded via ConvertFrom-SecureString
741+
[string]$EncodedPassword
742+
)
743+
New-Object PSCredential $UserName, (ConvertTo-SecureString $EncodedPassword)
744+
}
824745

825-
[ConsoleColor] = { "ConsoleColor {0}" -f $InputObject.ToString() }
826746
}
827747

828-
$Script:OriginalMetadataConverters = $MetadataConverters.Clone()
748+
$Script:OriginalMetadataSerializers = $MetadataSerializers.Clone()
749+
$Script:OriginalMetadataDeserializers = $MetadataDeserializers.Clone()
829750

830751
function Update-Object {
831752
<#

Specs/Configuration.Steps.ps1

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ $PSModuleAutoLoadingPreference = "None"
22

33
BeforeEachFeature {
44
Remove-Module Configuration -ErrorAction Ignore -Force
5+
Import-Module Configuration -MinimumVersion 1.1
56
}
67
AfterEachFeature {
78
Remove-Module Configuration -ErrorAction Ignore -Force
8-
Import-Module Configuration
9+
Import-Module Configuration -MinimumVersion 1.1
910
}
1011

1112
Given 'the configuration module is imported with testing paths:' {

0 commit comments

Comments
 (0)