Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions Test/include/invokeCommand.mock.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
#
# This includes help commands to mock invokes in a test module
#
# Set $env:TraceInvokeMockFilePath to trave Invoke dependencies
# "traceInvoke.log" | %{touch $_ ; $env:TraceInvokeMockFilePath = $_ | Resolve-Path}
#
# THIS INCLUDE REQURED module.helper.ps1
if(-not $MODULE_NAME){ throw "Missing MODULE_NAME varaible initialization. Check for module.helerp.ps1 file." }
if(-not $MODULE_ROOT_PATH){ throw "Missing MODULE_ROOT_PATH varaible initialization. Check for module.helerp.ps1 file." }
Expand All @@ -14,6 +17,28 @@ $MOCK_PATH = $testRootPath | Join-Path -ChildPath 'private' -AdditionalChildPath
$MODULE_INVOKATION_TAG = "$($MODULE_NAME)Module"
$MODULE_INVOKATION_TAG_MOCK = "$($MODULE_INVOKATION_TAG)_Mock"

function Trace-InvokeCommandAlias{
[CmdletBinding()]
param(
[Parameter(Mandatory,Position=0)][string]$Alias
)

$filePath = $env:TraceInvokeMockFilePath

if(! $filePath){ return }

if(! (Test-Path $filePath)) {return}

$content = Get-Content $filePath

$content = $content ?? @()

if($content.Contains($Alias)) { return}

$alias | Out-File $filePath -Append

}

function Set-InvokeCommandMock{
[CmdletBinding()]
param(
Expand All @@ -22,6 +47,8 @@ function Set-InvokeCommandMock{
)

InvokeHelper\Set-InvokeCommandAlias -Alias $Alias -Command $Command -Tag $MODULE_INVOKATION_TAG_MOCK

Trace-InvokeCommandAlias $alias
}

function Reset-InvokeCommandMock{
Expand Down Expand Up @@ -257,3 +284,5 @@ function Assert-MockFileNotfound{
}




33 changes: 33 additions & 0 deletions Test/public/invokeCommand.mock.test.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,37 @@
Assert-Count -Presented $result -Expected 2
Assert-Contains -Presented $result -Expected 'new string from mock 2'
Assert-Contains -Presented $result -Expected 'more things to say'
}

function Test_TraceInvokeCOmmandAlias{


Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace
$fileName = "traceInvoke.log"
Assert-ItemNotExist -Path $fileName

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace
# Set flag

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace
touch $fileName
$fileNamePath = $fileName | Resolve-Path
$env:TraceInvokeMockFilePath = $fileNamePath
Assert-ItemExist -Path $fileNamePath

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace
# Act record invoke alias
Set-InvokeCommandMock -alias "fakeAlias1" -Command 'echo "hello from fakeAlias1"'
Set-InvokeCommandMock -alias "fakeAlias2" -Command 'echo "hello from fakeAlias2"'

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace
# Assert the two new alias are recorded
$content = Get-Content $fileNamePath
Assert-Count -Expected 2 -Presented $content
Assert-Contains -Presented $content -Expected 'fakeAlias1'
Assert-Contains -Presented $content -Expected 'fakeAlias2'

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace
# Assert no duplicate alias are recorded
Set-InvokeCommandMock -alias "fakeAlias1" -Command 'echo "hello from fakeAlias1"'
$content = Get-Content $fileNamePath
Assert-Count -Expected 2 -Presented $content

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace
# Assert new alias are recorded
Set-InvokeCommandMock -alias "fakeAlias3" -Command 'echo "hello from fakeAlias1"'
$content = Get-Content $fileNamePath
Assert-Count -Expected 3 -Presented $content
}