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
28 changes: 28 additions & 0 deletions Test/include/callPrivateContext.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# CALL PRIVATE FUNCTIONS
#
# This file enables tests to invoke private (non-exported) functions
# defined inside the main module by evaluating a provided scriptblock
# in the module context.
#
# It relies on variables initialized by module.helper.ps1.
# REQUIRED:
# $MODULE_PATH - Full path to the module .psm1 file (or a file inside its folder)
#
# THIS INCLUDE REQUIRES module.helper.ps1
if(-not $MODULE_PATH){ throw "Missing MODULE_PATH variable initialization. Check for module.helper.ps1 file." }

function Invoke-PrivateContext {

Check notice

Code scanning / PSScriptAnalyzer

The cmdlet 'Invoke-PrivateContext' does not have a help comment. Note

The cmdlet 'Invoke-PrivateContext' does not have a help comment.
param (
[Parameter(Mandatory, Position = 0)]
[scriptblock]$ScriptBlock
)

$modulePath = $MODULE_PATH | Split-Path -Parent
$module = Import-Module -Name $modulePath -PassThru

if ($null -eq $module) {
throw "Failed to import the main module."
}

& $module $ScriptBlock

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace Note

Line has trailing whitespace
} Export-ModuleMember -Function Invoke-PrivateContext
27 changes: 27 additions & 0 deletions Test/public/callPrivateFunction.test.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
function Test_InvokePrivateFunction{

# First check that private function call fails
$errorStr = @"
The term 'Get-PrivateString' is not recognized as a name of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
"@

$hasthrown = $false
try{
Get-PrivateString -Param1 "TestParam"
} catch {
$hasthrown = $true
Assert-AreEqual -Expected $errorStr -Presented $_.Exception.Message
}
Assert-IsTrue -Condition $hasthrown


# Call private function through Invoke-PrivateContext
Invoke-PrivateContext {

$result = Get-PrivateString -Param1 "TestParam"

Assert-AreEqual -Expected "Private string [TestParam]" -Presented $result
}

}