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
76 changes: 51 additions & 25 deletions TestingHelperTest/public/New-ModuleV3-Add-ModuleV3.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,39 @@
function TestingHelperTest_NewModuleV3_AddModule_FailCall_NewModuleManifest {

# test when failing calling dependency Microsoft PowerShell.Core/New-ModuleManifest

$modulename = "MyModule"

# Inject depepdency
$module = Import-Module -Name $TESTED_MANIFEST_PATH -Prefix "LL_" -Force -PassThru
& $module {
. {
function script:New-MyModuleManifest {
[CmdletBinding()]
param(
[Parameter(Mandatory)][string]$Path,
[Parameter(Mandatory)][string]$RootModule
)
throw "Injected error from New-MymoduleManifest."
try {

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace

Line has trailing whitespace
$modulename = "MyModule"

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace

Line has trailing whitespace
# Inject depepdency
$module = Import-Module -Name $TESTED_MANIFEST_PATH -Prefix "LL_" -Force -PassThru
& $module {
. {
function script:New-MyModuleManifest {
[CmdletBinding()]
param(
[Parameter(Mandatory)][string]$Path,

Check warning

Code scanning / PSScriptAnalyzer

The parameter 'Path' has been declared but not used.

The parameter 'Path' has been declared but not used.
[Parameter(Mandatory)][string]$RootModule,

Check warning

Code scanning / PSScriptAnalyzer

The parameter 'RootModule' has been declared but not used.

The parameter 'RootModule' has been declared but not used.
[Parameter(Mandatory)][string]$PreRelease

Check warning

Code scanning / PSScriptAnalyzer

The parameter 'PreRelease' has been declared but not used.

The parameter 'PreRelease' has been declared but not used.
)
throw "Injected error from New-MymoduleManifest."
}
}
}

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace

Line has trailing whitespace
$result = Add-LL_ModuleV3 -Name $modulename @ErrorParameters

Check notice

Code scanning / PSScriptAnalyzer

Line has trailing whitespace

Line has trailing whitespace
Assert-IsNull -Object $result -Comment "No module is created"
Assert-IsNotNull -Object $errorVar.Exception -Comment "Error is thrown"
Assert-Contains -Expected "Injected error from New-MymoduleManifest." -Presented ($errorVar.Exception.Message)
Assert-ContainsPattern -Expected "Error creating the PSD1 file.*" -Presented ($errorVar.Exception.Message)
}
finally {
# reset module
Import-Module -Name $TESTED_MANIFEST_PATH -Prefix "TT_" -Force
}
}

$result = Add-LL_ModuleV3 -Name $modulename @ErrorParameters

Assert-IsNull -Object $result -Comment "No module is created"
Assert-IsNotNull -Object $errorVar.Exception -Comment "Error is thrown"
Assert-Contains -Expected "Injected error from New-MymoduleManifest." -Presented ($errorVar.Exception.Message)
Assert-ContainsPattern -Expected "Error creating the PSD1 file.*" -Presented ($errorVar.Exception.Message)

# reset module
Import-Module -Name $TESTED_MANIFEST_PATH -Prefix "TT_" -Force
}

function TestingHelperTest_NewModuleV3_AddModule_DefaultManifest {
Expand All @@ -39,6 +45,8 @@ function TestingHelperTest_NewModuleV3_AddModule_DefaultManifest {

$defaultsManifest = Get-DefaultsManifest

$defaultsManifest.PrivateData.PSData.Prerelease = 'dev'

Assert-AreEqualPath -Expected $moduleName -Presented $result

Assert-AddModuleV3 -Path $moduleName -Expected $defaultsManifest
Expand All @@ -48,6 +56,7 @@ function TestingHelperTest_NewModuleV3_AddModule_MyManifest {

$moduleName = "MyModule"

# Metadata is really the posible parameters for Update-ModuleManifest
$param = @{
RootModule = "MyModule.psm1"
Author = "Me"
Expand All @@ -56,11 +65,28 @@ function TestingHelperTest_NewModuleV3_AddModule_MyManifest {
Description = "MyDescription of the module"
FunctionsToExport = @("MyFunction")
CopyRight = "(c) 2020 MyCompany. All rights reserved."
Prerelease = "radompre"
}

$result = Add-TT_ModuleV3 -Name $moduleName -Metadata $param

Assert-AddModuleV3 -Path $result -Expected $param
$ExpectedMetadata = @{
RootModule = $param.RootModule
Author = $param.Author
CompanyName = $param.CompanyName
ModuleVersion = $param.ModuleVersion
Description = $param.Description
FunctionsToExport = $param.FunctionsToExport
Copyright = $param.CopyRight
}

$ExpectedMetadata.PrivateData = @{
PSData = @{
Prerelease = $param.Prerelease
}
}

Assert-AddModuleV3 -Path $result -Expected $ExpectedMetadata

}

5 changes: 3 additions & 2 deletions private/Microsoft.PowerShell.Core.Dependency.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ function script:New-MyModuleManifest {
[CmdletBinding()]
param(
[Parameter(Mandatory)][string]$Path,
[Parameter(Mandatory)][string]$RootModule
[Parameter(Mandatory)][string]$RootModule,
[Parameter(Mandatory)][string]$PreRelease
)
New-ModuleManifest -Path $Path -RootModule $RootModule
New-ModuleManifest -Path $Path -RootModule $RootModule -PreRelease $PreRelease
}

function script:Update-MyModuleManifest {
Expand Down
3 changes: 2 additions & 1 deletion public/Add-ModuleV3.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ function Add-ModuleV3 {

try {
# Create the PSD1 file
New-MyModuleManifest -Path $psd1Path -RootModule $rootModule
# Default Prerelease to 'dev' but may be overriten by $metadata
New-MyModuleManifest -Path $psd1Path -RootModule $rootModule -PreRelease "dev"

# Update with metadata
if ($Metadata.Count -gt 0) {
Expand Down
3 changes: 3 additions & 0 deletions public/Add-TestModuleV3.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ function Add-TestModuleV3 {
$manifest.Remove('Name')
$manifest.Remove('RootModule')
$manifest.Remove('GUID')

#match the prerelease field with module
$manifest.Prerelease = $manifest.PrivateData.PSData.Prerelease
}

# TODO: Not sure how to mangage Force on Add-Module functions
Expand Down