Skip to content

Commit

Permalink
Merge pull request #27 from rulasg/rulasg/issue26
Browse files Browse the repository at this point in the history
  • Loading branch information
rulasg authored Jan 10, 2024
2 parents 2fda6f4 + a178fe9 commit 0366b85
Show file tree
Hide file tree
Showing 4 changed files with 251 additions and 35 deletions.
158 changes: 150 additions & 8 deletions InvokeHelperTest/public/InvokeCommandList.test.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 1 addition & 1 deletion en-US/about_InvokeHelper.help.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ SETTING THE COMMAND LIST
```

```powershell
> Get-InvokeCommandAlias
> Get-InvokeCommandAliasList

Name Value
---- -----
Expand Down
46 changes: 39 additions & 7 deletions private/BuildCommand.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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])]
Expand Down
80 changes: 61 additions & 19 deletions public/InvokeCommandList.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand All @@ -33,7 +39,7 @@ function Get-InvokeCommandAlias{
return $script:InvokeCommandList
}

} Export-ModuleMember -Function Get-InvokeCommandAlias
} Export-ModuleMember -Function Get-InvokeCommandAliasList

function Test-InvokeCommandAlias{
[CmdletBinding()]
Expand All @@ -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]
}

<#
Expand All @@ -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
Expand All @@ -81,4 +89,38 @@ function Reset-InvokeCommandAlias{
} Export-ModuleMember -Function Reset-InvokeCommandAlias

# Initilize $InvokeCommandList
Reset-InvokeCommandAlias
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

0 comments on commit 0366b85

Please sign in to comment.