diff --git a/InvokeHelperTest/public/InvokeCommandList.test.ps1 b/InvokeHelperTest/public/InvokeCommandList.test.ps1 index 905db7e..e69e985 100644 --- a/InvokeHelperTest/public/InvokeCommandList.test.ps1 +++ b/InvokeHelperTest/public/InvokeCommandList.test.ps1 @@ -3,27 +3,169 @@ function InvokeHelperTest_InvokeCommandAlias_Get{ Set-InvokeCommandAlias -Alias "commandAlias" -Command 'echo "this is a sample command"' Set-InvokeCommandAlias -Alias "commandAlias2" -Command 'echo "this is a sample command2"' - $result = Get-InvokeCommandAlias + $result = Get-InvokeCommandAliasList - Assert-AreEqual -Expected 'echo "this is a sample command"' -Presented $result["commandAlias"] - Assert-AreEqual -Expected 'echo "this is a sample command2"' -Presented $result["commandAlias2"] + Assert-AreEqual -Expected 'echo "this is a sample command"' -Presented $result["commandAlias"].Command + Assert-AreEqual -Expected 'echo "this is a sample command2"' -Presented $result["commandAlias2"].Command } function InvokeHelperTest_InvokeCommandAlias_Reset{ Reset-InvokeCommandAlias - $result = Get-InvokeCommandAlias + $result = Get-InvokeCommandAliasList Assert-IsNull -Object $result Set-InvokeCommandAlias -Alias "commandAlias" -Command 'echo "this is a sample command"' Set-InvokeCommandAlias -Alias "commandAlias2" -Command 'echo "this is a sample command2"' - $result = Get-InvokeCommandAlias - Assert-AreEqual -Expected 'echo "this is a sample command"' -Presented $result["commandAlias"] - Assert-AreEqual -Expected 'echo "this is a sample command2"' -Presented $result["commandAlias2"] + $result = Get-InvokeCommandAliasList + Assert-AreEqual -Expected 'echo "this is a sample command"' -Presented $result["commandAlias"].Command + Assert-AreEqual -Expected 'echo "this is a sample command2"' -Presented $result["commandAlias2"].Command Reset-InvokeCommandAlias - $result = Get-InvokeCommandAlias + $result = Get-InvokeCommandAliasList Assert-IsNull -Object $result +} + +function InvokeHelperTest_InvokeCommandAlias_Reset_WithTag{ + Reset-InvokeCommandAlias + + $result = Get-InvokeCommandAliasList + Assert-IsNull -Object $result + + Set-InvokeCommandAlias -Alias "commandAlias11" -Command 'echo "this is a sample command11"' -Tag Mock1 + Set-InvokeCommandAlias -Alias "commandAlias21" -Command 'echo "this is a sample command21"' -Tag Mock2 + Set-InvokeCommandAlias -Alias "commandAlias12" -Command 'echo "this is a sample command12"' -Tag Mock1 + Set-InvokeCommandAlias -Alias "commandAlias22" -Command 'echo "this is a sample command22"' -Tag Mock2 + + $result = Get-InvokeCommandAliasList + + Assert-Count -Expected 4 -Presented $result + Assert-AreEqual -Expected 'echo "this is a sample command11"' -Presented $result["commandAlias11"].Command + Assert-AreEqual -Expected 'echo "this is a sample command21"' -Presented $result["commandAlias21"].Command + Assert-AreEqual -Expected 'echo "this is a sample command12"' -Presented $result["commandAlias12"].Command + Assert-AreEqual -Expected 'echo "this is a sample command22"' -Presented $result["commandAlias22"].Command + + + Reset-InvokeCommandAlias -Tag Mock1 + + $result = Get-InvokeCommandAliasList + + Assert-Count -Expected 2 -Presented $result + Assert-AreEqual -Expected 'echo "this is a sample command21"' -Presented $result["commandAlias21"].Command + Assert-AreEqual -Expected 'echo "this is a sample command22"' -Presented $result["commandAlias22"].Command +} + +function InvokeHelperTest_InvokeCommandAlias_Enable_Disable{ + + Set-InvokeCommandAlias -Alias "commandAlias1" -Command "echo $text1" -Tag Mock1 + Set-InvokeCommandAlias -Alias "commandAlias2" -Command "echo $text2" -Tag Mock2 + Set-InvokeCommandAlias -Alias "commandAlias3" -Command "echo $text3" -Tag Mock1 + Set-InvokeCommandAlias -Alias "commandAlias4" -Command "echo $text4" -Tag Mock2 + + Disable-InvokeCommandAlias -Tag Mock1 + + $result = Get-InvokeCommandAliasList + + Assert-IsFalse -Condition $result.commandAlias1.Enabled + Assert-IsFalse -Condition $result.commandAlias3.Enabled + + Assert-IsTrue -Condition $result.commandAlias2.Enabled + Assert-IsTrue -Condition $result.commandAlias4.Enabled + + Enable-InvokeCommandAlias -Tag Mock1 + + Assert-IsTrue -Condition $result.commandAlias1.Enabled + Assert-IsTrue -Condition $result.commandAlias3.Enabled + + Assert-IsTrue -Condition $result.commandAlias2.Enabled + Assert-IsTrue -Condition $result.commandAlias4.Enabled +} + +function InvokeHelperTest_InvokeCommandAlias_Invoke_Enable_Disable{ + + $text1 = "this is a sample command 1" + $text2 = "this is a sample command 2" + + Set-InvokeCommandAlias -Alias "commandAlias1" -Command "echo $text1" -Tag Mock1 + Set-InvokeCommandAlias -Alias "commandAlias2" -Command "echo $text2" -Tag Mock2 + + $result = Invoke-MyCommand -Command "commandAlias1" + Assert-AreEqual -Expected $text1 -Presented $result + + $result = Invoke-MyCommand -Command "commandAlias2" + Assert-AreEqual -Expected $text2 -Presented $result + + Disable-InvokeCommandAlias -Tag Mock1 + + $hasthrow = $false + try{ + $result = Invoke-MyCommand -Command "commandAlias1" + } catch { + $hasthrow = $true + } + Assert-IsTrue -Condition $hasthrow + + $result = Invoke-MyCommand -Command "commandAlias2" + Assert-AreEqual -Expected $text2 -Presented $result + + Enable-InvokeCommandAlias -Tag Mock1 + + $result = Invoke-MyCommand -Command "commandAlias1" + Assert-AreEqual -Expected $text1 -Presented $result +} + +function InvokeHelperTest_InvokeCommandAlias_Invoke_Mock_Disabled{ + + $text1 = "this is a sample command 1" + $text2 = "this is a sample command 2" + $text1Mock = "this is a sample command 1 Mock" + $text2Mock = "this is a sample command 2 Mock" + + Set-InvokeCommandAlias -Alias "commandAlias1" -Command "echo $text1" -Tag "myModule" + Set-InvokeCommandAlias -Alias "commandAlias2" -Command "echo $text2" -Tag "myModule" + Set-InvokeCommandAlias -Alias "commandAlias3" -Command "echo $text3" -Tag "myModule" + + Set-InvokeCommandAlias -Alias "echo $text1" -Command "echo $text1Mock" -Tag Mock1 + Set-InvokeCommandAlias -Alias "echo $text2" -Command "echo $text2Mock" -Tag Mock2 + + # Disable all module commands + Disable-InvokeCommandAlias -Tag "myModule" + + # Call module command with mock + $result = Invoke-MyCommand -Command "commandAlias1" + Assert-AreEqual -Expected $text1Mock -Presented $result + + # Call module command with mock + $result = Invoke-MyCommand -Command "commandAlias2" + Assert-AreEqual -Expected $text2Mock -Presented $result + + # Call module command with no mock + $hasthrow = $false + try{ + $result = Invoke-MyCommand -Command "commandAlias3" + } catch { + $hasthrow = $true + } + Assert-IsTrue -Condition $hasthrow + + # Disable a Mock tag + Disable-InvokeCommandAlias -Tag Mock1 + + # Call a module command with a disabled mock + $hasthrow = $false + try{ + $result = Invoke-MyCommand -Command "commandAlias1" + } catch { + $hasthrow = $true + } + Assert-IsTrue -Condition $hasthrow + + # enable a Mock tag + Enable-InvokeCommandAlias -Tag Mock1 + + # Call a module command with just enabled mock + $result = Invoke-MyCommand -Command "commandAlias1" + Assert-AreEqual -Expected $text1Mock -Presented $result } \ No newline at end of file diff --git a/en-US/about_InvokeHelper.help.txt b/en-US/about_InvokeHelper.help.txt index 7c3641d..53b50ed 100644 --- a/en-US/about_InvokeHelper.help.txt +++ b/en-US/about_InvokeHelper.help.txt @@ -36,7 +36,7 @@ SETTING THE COMMAND LIST ``` ```powershell - > Get-InvokeCommandAlias + > Get-InvokeCommandAliasList Name Value ---- ----- diff --git a/private/BuildCommand.ps1 b/private/BuildCommand.ps1 index 7e39555..6042bad 100644 --- a/private/BuildCommand.ps1 +++ b/private/BuildCommand.ps1 @@ -6,23 +6,55 @@ function Build-Command{ [Parameter()][hashtable]$Parameters ) process { - # Check if command is an alias. If not will return the command. - $cmd = Resolve-InvokeCommandAlias -Alias $Command + # Check if command is an alias. If not will return the command after updating for parameters + if(-Not (Test-InvokeCommandAlias -Alias $Command)){ + $cmd = Update-CommandWithParameter -Command $Command -Parameters $Parameters + return $cmd + } + + # Find the command for this alias + $alias = Get-InvokeCommandAlias -Alias $Command + + # Build the command with parameter + $cmd = Update-CommandWithParameter -Command $alias.Command -Parameters $Parameters + + # Recurse to check for mocks + $mock = Get-InvokeCommandAlias -Alias $cmd + if($mock){ + # We have a mock command + $cmd = $mock.Command + $alias = $mock + } + + # Check if alias is disabled + if(! $alias.Enabled){ + throw ("Alias command is disabled: $alias.Alias") + } + + return $cmd + } +} +function Update-CommandWithParameter{ + [CmdletBinding()] + [OutputType([string])] + param( + [Parameter(Mandatory,ValueFromPipeline,Position=0)][string]$Command, + [Parameter()][hashtable]$Parameters + ) + process { # Replace parameters on command if($Parameters){ foreach($key in $Parameters.Keys){ - $cmd = $cmd.Replace("{"+$key+"}",$Parameters[$key]) + $Command = $Command.Replace("{"+$key+"}",$Parameters[$key]) } } - # Resolve again checking for full command mocks - $cmd = Resolve-InvokeCommandAlias -Alias $cmd - - return $cmd + return $Command } } + function New-ScriptBlock{ [CmdletBinding()] [OutputType([ScriptBlock])] diff --git a/public/InvokeCommandList.ps1 b/public/InvokeCommandList.ps1 index cefb49e..c45ba24 100644 --- a/public/InvokeCommandList.ps1 +++ b/public/InvokeCommandList.ps1 @@ -9,11 +9,17 @@ function Set-InvokeCommandAlias{ [CmdletBinding(SupportsShouldProcess)] param( [Parameter(Mandatory,ValueFromPipeline,Position=0)][string]$Alias, - [Parameter(Mandatory,ValueFromPipeline,Position=1)][string]$Command + [Parameter(Mandatory,ValueFromPipeline,Position=1)][string]$Command, + [Parameter(Position=2)][string]$Tag ) process { if ($PSCmdlet.ShouldProcess("CommandList", "Set $Alias = $Command")) { - $InvokeCommandList[$Alias] = $Command + $InvokeCommandList[$Alias] = [PSCustomObject]@{ + Alias = $Alias + Command = $Command + Tag = $Tag + Enabled = $true + } } } } Export-ModuleMember -Function Set-InvokeCommandAlias @@ -22,7 +28,7 @@ function Set-InvokeCommandAlias{ .SYNOPSIS Get the Command list active in the module #> -function Get-InvokeCommandAlias{ +function Get-InvokeCommandAliasList{ [CmdletBinding()] [OutputType([hashtable])] param() @@ -33,7 +39,7 @@ function Get-InvokeCommandAlias{ return $script:InvokeCommandList } -} Export-ModuleMember -Function Get-InvokeCommandAlias +} Export-ModuleMember -Function Get-InvokeCommandAliasList function Test-InvokeCommandAlias{ [CmdletBinding()] @@ -45,22 +51,13 @@ function Test-InvokeCommandAlias{ } } -function Resolve-InvokeCommandAlias{ +function Get-InvokeCommandAlias{ [CmdletBinding()] param( - [Parameter(Mandatory,ValueFromPipeline,Position=0)][string]$Alias + [Parameter(Mandatory,Position=0)][string]$Alias ) - process { - if(Test-InvokeCommandAlias -Alias $Alias){ - $cmd = $InvokeCommandList[$Alias] - # Recursive just in case we have mock the command behind the alias - $cmd = Resolve-InvokeCommandAlias -Alias $cmd - } else { - $cmd = $Alias - } - return $cmd - } + return $InvokeCommandList[$Alias] } <# @@ -69,10 +66,21 @@ Reset Command list #> function Reset-InvokeCommandAlias{ [CmdletBinding(SupportsShouldProcess)] - param() + param( + [Parameter()][string]$Tag + ) + process { + + $newInvokeCommandList = @{} + + if(-Not [string]::IsNullOrWhiteSpace($Tag)){ + $validKeys = $script:InvokeCommandList.Keys | Where-Object { $script:InvokeCommandList.$_.Tag -ne $Tag } + $validKeys | ForEach-Object { $newInvokeCommandList[$_] = $script:InvokeCommandList[$_] } + } + if ($PSCmdlet.ShouldProcess("CommandList", "Reset")) { - $script:InvokeCommandList = @{} + $script:InvokeCommandList = $newInvokeCommandList } "$script:InvokeCommandList" | Write-Verbose @@ -81,4 +89,38 @@ function Reset-InvokeCommandAlias{ } Export-ModuleMember -Function Reset-InvokeCommandAlias # Initilize $InvokeCommandList -Reset-InvokeCommandAlias \ No newline at end of file +Reset-InvokeCommandAlias + +<# +.SYNOPSIS +Disable a set of command alias by tag +#> +function Disable-InvokeCommandAlias{ + [CmdletBinding()] + param( + [Parameter()][string]$Tag + ) + + Foreach ($key in $InvokeCommandList.Keys) { + if($InvokeCommandList[$key].Tag -eq $Tag){ + $InvokeCommandList[$key].Enabled = $false + } + } +} Export-ModuleMember -Function Disable-InvokeCommandAlias + +<# +.SYNOPSIS +Enable a set of command alias by tag +#> +function Enable-InvokeCommandAlias{ + [CmdletBinding()] + param( + [Parameter()][string]$Tag + ) + + Foreach ($key in $InvokeCommandList.Keys) { + if($InvokeCommandList[$key].Tag -eq $Tag){ + $InvokeCommandList[$key].Enabled = $true + } + } +} Export-ModuleMember -Function Enable-InvokeCommandAlias \ No newline at end of file