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
32 changes: 18 additions & 14 deletions Test/include/parameter.test.helper.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,27 @@
#
# sample usage:
#
# function Get-SomeCommand {
# [CmdletBinding()]
# param()
#
# Write-Verbose "this is a verbose message"
# }
#
# $result = Get-SomeCommand @ VerboseParameters
# Assert-Contains -Expected "this is a verbose message" -Presented $verboseVar
# function Get-DummyFunction{
# [CmdletBinding()]
# param()

# Verbose parameters will now work.to capture the verbose outptut pipe 4>&1 and capture the output
# [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments','',Scope='function')]
# $VerboseParameters =@{
# VerboseAction = 'SilentlyContinue'
# VerboseVariable = 'verboseVar'
# Write-Error "Error Message from dummyFunction"
# Write-Verbose "Verbose Message from dummyFunction"
# Write-Warning "Warning Message from dummyFunction"
# Write-Information "Information Message from dummyFunction"
# Write-host "Host Message from dummyFunction"

# return $true
# }

# $result = Get-DummyFunction @ErrorParameters @WarningParameters @InfoParameters

# Assert-IsTrue -Condition $result
# Assert-Contains -Expected "Error Message from dummyFunction" -Presented $errorVar[0].exception.Message
# Assert-Contains -Expected "Warning Message from dummyFunction" -Presented $warningVar
# Assert-Contains -Expected "Information Message from dummyFunction" -Presented $infoVar
# Assert-Contains -Expected "Host Message from dummyFunction" -Presented $infoVar

[Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseDeclaredVarsMoreThanAssignments','',Scope='function')]
$WarningParameters = @{
WarningAction = 'SilentlyContinue'
Expand Down
26 changes: 24 additions & 2 deletions Test/include/transcriptHelp.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,30 @@
}

function Stop-MyTranscript {
Stop-Transcript

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace
$null = Stop-Transcript

$transcriptContent = Get-Content -Path $TEST_TRANSCRIPT_FILE
Remove-Item -Path $TEST_TRANSCRIPT_FILE
return $transcriptContent

$ret = Export-MyTranscript -transcriptContent $transcriptContent

return $ret
}

function Export-MyTranscript {
[CmdletBinding()]
param (
[Parameter(Mandatory, Position = 0)]
[string[]]$transcriptContent
)

$i = 0..($transcriptContent.Count - 1) | Where-Object { $transcriptContent[$_] -eq "**********************" }

$firstLine = $i[1] + 1
$lastLine = $i[2] - 1

$retlist = $transcriptContent[$firstLine..$lastLine]

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace
return $retlist
}
27 changes: 27 additions & 0 deletions Test/public/MyWrite.test.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
function Test_WriteMyHost_Singleline {

Start-MyTranscript

Invoke-PrivateContext {
Write-MyHost -Message "This is a test transcript."
}

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace
$result = Stop-MyTranscript

Assert-AreEqual -Expected "This is a test transcript." -Presented $result

}

function Test_WriteMyHost_Multiline {

Start-MyTranscript

Invoke-PrivateContext {
Write-MyHost -Message "This is a test transcript 0"
Write-MyHost -Message "This is a test transcript 1"
}
$result = Stop-MyTranscript

Assert-AreEqual -Expected "This is a test transcript 0" -Presented $result[0]
Assert-AreEqual -Expected "This is a test transcript 1" -Presented $result[1]
}
59 changes: 38 additions & 21 deletions Test/public/dependencies.test.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@
Mock_ImportModule -Name $name -Folder $modulesFolder

#Act
$result = Import-Dependency -Name $name -Verbose -Confirm:$false 4>&1 # pipe verbose stream to standard output
Set-IncludeHelperVerbose
Start-MyTranscript
$result = Import-Dependency -Name $name -Verbose -Confirm:$false
$tt = Stop-MyTranscript
Clear-IncludeHelperVerbose

#Assert verbose message
Assert-IsNotNull -Object $result
Assert-Contains -Presented $result -Expected "Module [$Name] imported from own module"
# Assert module output
$module = $result | where-object {$_.GetType().Name -eq "PSCustomObject"}
Assert-AreEqual -Expected $name -Presented $module.Name
Assert-AreEqual -Expected $name -Presented $result.Name
Assert-Contains -Presented $tt -Expected "Module [$Name] imported from own module"

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace
}

function Test_ImportDepepency_SideBySide{
Expand All @@ -41,14 +42,17 @@
Mock_ImportModule -Name $name -Folder $modulesFolder

# Act
$result = Import-Dependency -Name $name -Verbose -Confirm:$false 4>&1 # pipe verbose stream to standard output
Set-IncludeHelperVerbose
Start-MyTranscript
$result = Import-Dependency -Name $name -Verbose -Confirm:$false
$tt = Stop-MyTranscript
Clear-IncludeHelperVerbose

#Assert verbose message
Assert-IsNotNull -Object $result
Assert-Contains -Presented $result -Expected "Module [$Name] imported from side by side path"
# Assert module output
$module = $result | where-object {$_.GetType().Name -eq "PSCustomObject"}
Assert-AreEqual -Expected $name -Presented $module.Name
Assert-AreEqual -Expected $name -Presented $result.Name
#Assert verbose message
Assert-AreEqual -Presented $tt -Expected "Module [$Name] imported from side by side path"

}

function Test_ImportDepepency_Import_From_Module_Manager{
Expand All @@ -73,13 +77,16 @@


#Act
$result = Import-Dependency -Name $name -Verbose -Confirm:$false 4>&1 # pipe verbose stream to standard output
Set-IncludeHelperVerbose
Start-MyTranscript
$output = Import-Dependency -Name $name -Verbose -Confirm:$false
$result = Stop-MyTranscript
Clear-IncludeHelperVerbose

#Assert verbose message
Assert-AreEqual -Expected $name -Presented $output.Name
Assert-Contains -Presented $result -Expected "Module [$Name] imported from Powershell Module Manager"
# Assert module output
$module = $result | where-object {$_.GetType().Name -eq "PSCustomObject"}
Assert-AreEqual -Expected $name -Presented $module.Name

}

function Test_ImportDepepency_Install_From_Gallery{
Expand All @@ -106,15 +113,20 @@
return $null
'@
$expression = $expression -replace "{alias}", "Get-Module -Name $name -ListAvailable"
$expression = $expression -replace "{command}", 'New-Object -TypeName PSCustomObject -Property @{ Name = "$name" }'
$expression = $expression -replace "{command}", 'New-Object -TypeName PSCustomObject -Property @{ Name = "{name}" }'
$expression = $expression -replace "{name}", $name
$expression = $expression -replace "{tag}", $MODULE_INVOKATION_TAG_MOCK
MockCallExpression -Command "Install-Module -Name $name -AllowPrerelease -Force" -Expression $expression

#Act
$result = Import-Dependency -Name $name -Verbose -Confirm:$false 4>&1 # pipe verbose stream to standard output
Set-IncludeHelperVerbose
Start-MyTranscript
$output = Import-Dependency -Name $name -Verbose -Confirm:$false
$result = Stop-MyTranscript
Clear-IncludeHelperVerbose

#Assert verbose message
Assert-IsNotNull -Object $result
Assert-AreEqual -Expected $name -Presented $output.Name
Assert-Contains -Presented $result -Expected "Module [$Name] installed from PowerShell Gallery"
}

Expand Down Expand Up @@ -142,10 +154,15 @@
Mock_ImportModule -Name $name -Folder $modulesFolder


$result = Import-Dependency -Name $name -Verbose -Confirm:$false 4>&1 # pipe verbose stream to standard output
Set-IncludeHelperVerbose
Start-MyTranscript
$output = Import-Dependency -Name $name -Verbose -Confirm:$false
$result = Stop-MyTranscript
Clear-IncludeHelperVerbose

#Assert verbose message
Assert-IsNotNull -Object $result
Assert-AreEqual -Expected $name -Presented $output.Name

Assert-Contains -Presented $result -Expected "Module [$Name] cloned from GitHub repository"
Assert-Contains -Presented $result -Expected "Module [$Name] imported from GitHub repository"
}
Expand Down
51 changes: 35 additions & 16 deletions Test/public/parameter.test.helper.test.ps1
Original file line number Diff line number Diff line change
@@ -1,38 +1,57 @@

function Test_parameterstest{

$result = Get-DummyFunction @WarningParameters @InfoParameters @ErrorParameters

Set-IncludeHelperVerbose
Start-MyTranscript
$result = Get-DummyFunction @ErrorParameters @WarningParameters @InfoParameters
$tt = Stop-MyTranscript
Clear-IncludeHelperVerbose

# Assert result
Assert-IsTrue -Condition $result
Assert-Contains -Expected "Error Message from dummyFunction" -Presented $errorVar[0].exception.Message
Assert-Contains -Expected "Warning Message from dummyFunction" -Presented $warningVar
Assert-Contains -Expected "Information Message from dummyFunction" -Presented $infoVar
Assert-Contains -Expected "Error Message from dummyFunction" -Presented $errorVar
Assert-Contains -Expected "Information Message from dummyFunction" -Presented $infoVar

# Not displaied
Assert-NotContains -Presented $tt -Expected "Error Message from dummyFunction"
Assert-NotContains -Presented $tt -Expected "Warning Message from dummyFunction"
Assert-NotContains -Presented $tt -Expected "Information Message from dummyFunction"

# Host message
Assert-Contains -Presented $tt -Expected "Host Message from dummyFunction"
}

function Test_parameterstest_Error{
function Test_parameterstest_Verbose{

# $ErrorActionPreference = 'SilentlyContinue'
$result = Get-DummyFunction @ErrorParameters @WarningParameters @InfoParameters

Set-IncludeHelperVerbose
Start-MyTranscript
$result = Get-DummyFunction @ErrorParameters @WarningParameters @InfoParameters -Verbose
$tt = Stop-MyTranscript
Clear-IncludeHelperVerbose

# Assert result
Assert-IsTrue -Condition $result
Assert-Contains -Expected "Error Message from dummyFunction" -Presented $errorVar[0].exception.Message
Assert-Contains -Expected "Warning Message from dummyFunction" -Presented $warningVar
Assert-Contains -Expected "Information Message from dummyFunction" -Presented $infoVar
}
Assert-Contains -Expected "Information Message from dummyFunction" -Presented $infoVar

function Test_parameterstest_Verbose{
# Not displaied
Assert-NotContains -Presented $tt -Expected "Error Message from dummyFunction"
Assert-NotContains -Presented $tt -Expected "Warning Message from dummyFunction"
Assert-NotContains -Presented $tt -Expected "Information Message from dummyFunction"

$result = Get-DummyFunction -Verbose 4>&1 @ErrorParameters @WarningParameters @InfoParameters
Assert-Contains -Presented $result -Expected "True"
Assert-Contains -Presented $result -Expected "Verbose Message from dummyFunction"
# Host message
Assert-Contains -Presented $tt -Expected "VERBOSE: Verbose Message from dummyFunction"
Assert-Contains -Presented $tt -Expected "Host Message from dummyFunction"
}

function Get-DummyFunction{
[CmdletBinding()]
param()

Assert-AreEqual -Expected "SilentlyContinue" -Presented $ErrorActionPreference
Assert-AreEqual -Expected "SilentlyContinue" -Presented $WarningPreference
Assert-AreEqual -Expected "SilentlyContinue" -Presented $InformationPreference

Write-Error "Error Message from dummyFunction"
Write-Verbose "Verbose Message from dummyFunction"
Write-Warning "Warning Message from dummyFunction"
Expand Down
22 changes: 22 additions & 0 deletions Test/public/transcriptHelp.test.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,26 @@
Assert-Contains -Expected "This is a test transcript." -Presented $result
Assert-Contains -Expected "WARNING: This is a warning message." -Presented $result
# Assert-Contains -Expected " | This is an error message." -Presented $result
}

function Test_ExportTranscriptLines{

# Act
Start-MyTranscript

Write-Host "Line 1"

Check warning

Code scanning / PSScriptAnalyzer

File 'transcriptHelp.test.ps1' uses Write-Host. Avoid using Write-Host because it might not work in all hosts, does not work when there is no host, and (prior to PS 5.0) cannot be suppressed, captured, or redirected. Instead, use Write-Output, Write-Verbose, or Write-Information. Warning

File 'transcriptHelp.test.ps1' uses Write-Host. Avoid using Write-Host because it might not work in all hosts, does not work when there is no host, and (prior to PS 5.0) cannot be suppressed, captured, or redirected. Instead, use Write-Output, Write-Verbose, or Write-Information.
Write-Host "Line 2"

Check warning

Code scanning / PSScriptAnalyzer

File 'transcriptHelp.test.ps1' uses Write-Host. Avoid using Write-Host because it might not work in all hosts, does not work when there is no host, and (prior to PS 5.0) cannot be suppressed, captured, or redirected. Instead, use Write-Output, Write-Verbose, or Write-Information. Warning

File 'transcriptHelp.test.ps1' uses Write-Host. Avoid using Write-Host because it might not work in all hosts, does not work when there is no host, and (prior to PS 5.0) cannot be suppressed, captured, or redirected. Instead, use Write-Output, Write-Verbose, or Write-Information.
Write-Host "Line 3"

Check warning

Code scanning / PSScriptAnalyzer

File 'transcriptHelp.test.ps1' uses Write-Host. Avoid using Write-Host because it might not work in all hosts, does not work when there is no host, and (prior to PS 5.0) cannot be suppressed, captured, or redirected. Instead, use Write-Output, Write-Verbose, or Write-Information. Warning

File 'transcriptHelp.test.ps1' uses Write-Host. Avoid using Write-Host because it might not work in all hosts, does not work when there is no host, and (prior to PS 5.0) cannot be suppressed, captured, or redirected. Instead, use Write-Output, Write-Verbose, or Write-Information.

$result = Stop-MyTranscript

# Assert
$expectedLines = @(
"Line 1",
"Line 2",
"Line 3"
)
for($i=0; $i -lt $expectedLines.Count; $i++){
Assert-AreEqual -Expected $expectedLines[$i] -Presented $result[$i]
}
}
18 changes: 13 additions & 5 deletions helper/invokeCommand.helper.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,24 @@
[Parameter(Mandatory,Position=1)][string]$Command
)

# throw if MODULE_INVOKATION_TAG is not set or is "MyModuleModule"
if (-not $MODULE_INVOKATION_TAG) {
throw "MODULE_INVOKATION_TAG is not set. Please set it to a unique value before calling Set-MyInvokeCommandAlias."
}

if ($PSCmdlet.ShouldProcess("InvokeCommandAliasList", ("Add Command Alias [{0}] = [{1}]" -f $Alias, $Command))) {
InvokeHelper\Set-InvokeCommandAlias -Alias $Alias -Command $Command -Tag $MODULE_INVOKATION_TAG
}
}

function Invoke-MyCommand{
[CmdletBinding()]
param(
[Parameter(Mandatory,ValueFromPipeline,Position=0)][string]$Command,

Check warning

Code scanning / PSScriptAnalyzer

Command accepts pipeline input but has not defined a process block. Warning

Command accepts pipeline input but has not defined a process block.
[Parameter(Position=1)][hashtable]$Parameters
)

Write-MyDebug "[invoke] $Command" $Parameters

return InvokeHelper\Invoke-MyCommand -Command $Command -Parameters $Parameters
}


function Reset-MyInvokeCommandAlias{
[CmdletBinding(SupportsShouldProcess)]
param()
Expand Down
Loading