diff --git a/TestingHelperTest/public/deploy.Tests.ps1 b/TestingHelperTest/public/deploy.Tests.ps1 index 6f5cd15..b6516cf 100644 --- a/TestingHelperTest/public/deploy.Tests.ps1 +++ b/TestingHelperTest/public/deploy.Tests.ps1 @@ -215,7 +215,6 @@ function Assert-Manifest{ $manifest = Import-PowerShellDataFile -Path $manifestPath - Assert-AreEqual -Expected $version -Presented $manifest.ModuleVersion -Comment "Expected[$version] Presented[$($manifest.ModuleVersion)]] - $Comment" # If preRelease is not present in the manifest, then we expect null diff --git a/deploy.ps1 b/deploy.ps1 index bf66cfb..3b87477 100644 --- a/deploy.ps1 +++ b/deploy.ps1 @@ -53,6 +53,11 @@ if ($DependencyInjection) { . $DependencyInjection } +## Install delendecies as Publish-Module +# will check if dependecies are installed locally due to a bug +# in Test-ModuleManifest +Install-Dependencies -ModuleManifestPath $MODULE_PSD1 + # Process Tag if($VersionTag){ diff --git a/private/templates/template.v3.deploy.Helper.ps1 b/private/templates/template.v3.deploy.Helper.ps1 index 16eeea5..0645d26 100644 --- a/private/templates/template.v3.deploy.Helper.ps1 +++ b/private/templates/template.v3.deploy.Helper.ps1 @@ -60,10 +60,34 @@ function Invoke-DeployModuleToPSGallery{ "Deploying {0} {1} {2} to PSGallery ..." -f $($psd1.RootModule), $($psd1.ModuleVersion), $($psd1.PrivateData.pSData.Prerelease) | Write-Information # During testing we should use -WhatIf paarmetre when calling for deploy. # Just reach this point when testing call failure + + # Inport-PsModule will run Test-Module before and will fail if all dependencies are not available localy + Install-Dependencies -ModuleManifestPath $ModuleManifestPath + Invoke-DeployModule -Name $psdPath -NuGetApiKey $NuGetApiKey -Force:$ForceDeploy } } +function Install-Dependencies{ + [CmdletBinding()] + param( + [Parameter(Mandatory)] [string]$ModuleManifestPath + ) + + $requiredModules = $ModuleManifestPath | Get-Item | Import-PowerShellDataFile | Select-Object -ExpandProperty requiredModules + + foreach ($requiredModule in $requiredModules) { + $module = Import-Module -Name $requiredModule -PassThru -ErrorAction SilentlyContinue + + if ($null -eq $module) { + "Installing module $requiredModule" | Write-Host -ForegroundColor DarkGray + Install-Module -Name $requiredModule -Force -AllowPrerelease + $module = Import-Module -Name $requiredModule -PassThru + "Loaded module Name[$($module.Name)] Version[$($module.Version)]" | Write-Host -ForegroundColor DarkGray + } + } +} + function Update-DeployModuleManifest { [CmdletBinding(SupportsShouldProcess)] param( diff --git a/private/templates/template.v3.deploy.ps1 b/private/templates/template.v3.deploy.ps1 index bf66cfb..3b87477 100644 --- a/private/templates/template.v3.deploy.ps1 +++ b/private/templates/template.v3.deploy.ps1 @@ -53,6 +53,11 @@ if ($DependencyInjection) { . $DependencyInjection } +## Install delendecies as Publish-Module +# will check if dependecies are installed locally due to a bug +# in Test-ModuleManifest +Install-Dependencies -ModuleManifestPath $MODULE_PSD1 + # Process Tag if($VersionTag){ diff --git a/private/templates/template.v3.test.ps1 b/private/templates/template.v3.test.ps1 index ff4e3bb..0e1fff0 100644 --- a/private/templates/template.v3.test.ps1 +++ b/private/templates/template.v3.test.ps1 @@ -7,44 +7,87 @@ Using TestingHelper this script will search for a Test module and run the tests This script will be referenced from launch.json to run the tests on VSCode .LINK - https://raw.githubusercontent.com/rulasg/DemoPsModule/main/test.ps1 + https://raw.githubusercontent.com/rulasg/StagingModule/main/test.ps1 .EXAMPLE > ./test.ps1 #> [CmdletBinding()] param ( - #Switch ShowTestErrors [Parameter()][switch]$ShowTestErrors ) -function Import-TestingHelper{ +function Set-TestName{ [CmdletBinding()] + [Alias("st")] param ( + [Parameter(Position=0,ValueFromPipeline)][string]$TestName + ) + + process{ + $global:TestName = $TestName + } +} + +function Get-TestName{ + [CmdletBinding()] + [Alias("gt")] + param ( + ) + + return $global:TestName +} + +function Clear-TestName{ + [CmdletBinding()] + [Alias("ct")] + param ( + ) + + $global:TestName = $null +} + +function Import-RequiredModules{ + [CmdletBinding()] + param ( + [Parameter(Mandatory,ValueFromPipeline,ValueFromPipelineByPropertyName)][string]$Name, [Parameter()][string]$Version, [Parameter()][switch]$AllowPrerelease, [Parameter()][switch]$PassThru ) - - if ($Version) { - $V = $Version.Split('-') - $semVer = $V[0] - $AllowPrerelease = ($AllowPrerelease -or ($null -ne $V[1])) - } - - $module = Import-Module TestingHelper -PassThru -ErrorAction SilentlyContinue -RequiredVersion:$semVer - - if ($null -eq $module) { - $installed = Install-Module -Name TestingHelper -Force -AllowPrerelease:$AllowPrerelease -passThru -RequiredVersion:$Version - $module = Import-Module -Name $installed.Name -RequiredVersion ($installed.Version.Split('-')[0]) -Force -PassThru - } + process{ + "Importing module Name[{0}] Version[{1}] AllowPrerelease[{2}]" -f $Name, $Version, $AllowPrerelease | Write-Host -ForegroundColor DarkGray - if ($PassThru) { - $module + if ($Version) { + $V = $Version.Split('-') + $semVer = $V[0] + $AllowPrerelease = ($AllowPrerelease -or ($null -ne $V[1])) + } + + $module = Import-Module $Name -PassThru -ErrorAction SilentlyContinue -RequiredVersion:$semVer + + if ($null -eq $module) { + "Installing module Name[{0}] Version[{1}] AllowPrerelease[{2}]" -f $Name, $Version, $AllowPrerelease | Write-Host -ForegroundColor DarkGray + $installed = Install-Module -Name $Name -Force -AllowPrerelease:$AllowPrerelease -passThru -RequiredVersion:$Version + $module = Import-Module -Name $installed.Name -RequiredVersion ($installed.Version.Split('-')[0]) -Force -PassThru + } + + if ($PassThru) { + $module + } } } -Import-TestingHelper -AllowPrerelease +# TestingHelper +Import-RequiredModules -Name TestingHelper -AllowPrerelease + +# Required Modules +$manifest = $PSScriptRoot | Join-Path -child "*.psd1" | Get-Item | Import-PowerShellDataFile +$requiredModule = $manifest.RequiredModules +$requiredModule | Import-RequiredModules -AllowPrerelease -# Run test by PSD1 file -Invoke-TestingHelper -ShowTestErrors:$ShowTestErrors \ No newline at end of file +if($TestName){ + Invoke-TestingHelper -TestName $TestName -ShowTestErrors:$ShowTestErrors +} else { + Invoke-TestingHelper -ShowTestErrors:$ShowTestErrors +} diff --git a/test.ps1 b/test.ps1 index 6379ca5..0e1fff0 100644 --- a/test.ps1 +++ b/test.ps1 @@ -29,6 +29,15 @@ function Set-TestName{ } } +function Get-TestName{ + [CmdletBinding()] + [Alias("gt")] + param ( + ) + + return $global:TestName +} + function Clear-TestName{ [CmdletBinding()] [Alias("ct")] @@ -38,40 +47,47 @@ function Clear-TestName{ $global:TestName = $null } -function Import-TestingHelper{ +function Import-RequiredModules{ [CmdletBinding()] param ( + [Parameter(Mandatory,ValueFromPipeline,ValueFromPipelineByPropertyName)][string]$Name, [Parameter()][string]$Version, [Parameter()][switch]$AllowPrerelease, [Parameter()][switch]$PassThru ) - - if ($Version) { - $V = $Version.Split('-') - $semVer = $V[0] - $AllowPrerelease = ($AllowPrerelease -or ($null -ne $V[1])) - } - - $module = Import-Module TestingHelper -PassThru -ErrorAction SilentlyContinue -RequiredVersion:$semVer - - if ($null -eq $module) { - $installed = Install-Module -Name TestingHelper -Force -AllowPrerelease:$AllowPrerelease -passThru -RequiredVersion:$Version - $module = Import-Module -Name $installed.Name -RequiredVersion ($installed.Version.Split('-')[0]) -Force -PassThru - } + process{ + "Importing module Name[{0}] Version[{1}] AllowPrerelease[{2}]" -f $Name, $Version, $AllowPrerelease | Write-Host -ForegroundColor DarkGray - if ($PassThru) { - $module + if ($Version) { + $V = $Version.Split('-') + $semVer = $V[0] + $AllowPrerelease = ($AllowPrerelease -or ($null -ne $V[1])) + } + + $module = Import-Module $Name -PassThru -ErrorAction SilentlyContinue -RequiredVersion:$semVer + + if ($null -eq $module) { + "Installing module Name[{0}] Version[{1}] AllowPrerelease[{2}]" -f $Name, $Version, $AllowPrerelease | Write-Host -ForegroundColor DarkGray + $installed = Install-Module -Name $Name -Force -AllowPrerelease:$AllowPrerelease -passThru -RequiredVersion:$Version + $module = Import-Module -Name $installed.Name -RequiredVersion ($installed.Version.Split('-')[0]) -Force -PassThru + } + + if ($PassThru) { + $module + } } } -Import-TestingHelper -AllowPrerelease +# TestingHelper +Import-RequiredModules -Name TestingHelper -AllowPrerelease -# Run test by PSD1 file -# Test-ModulelocalPSD1 -ShowTestErrors:$ShowTestErrors -# Test-ModulelocalPSD1 -ShowTestErrors:$ShowTestErrors -TestName StagingModuleTest_* +# Required Modules +$manifest = $PSScriptRoot | Join-Path -child "*.psd1" | Get-Item | Import-PowerShellDataFile +$requiredModule = $manifest.RequiredModules +$requiredModule | Import-RequiredModules -AllowPrerelease if($TestName){ - Invoke-TestingHelper -TestName $TestName + Invoke-TestingHelper -TestName $TestName -ShowTestErrors:$ShowTestErrors } else { - Invoke-TestingHelper -} \ No newline at end of file + Invoke-TestingHelper -ShowTestErrors:$ShowTestErrors +} diff --git a/tools/deploy.Helper.ps1 b/tools/deploy.Helper.ps1 index 9742c33..6abef59 100644 --- a/tools/deploy.Helper.ps1 +++ b/tools/deploy.Helper.ps1 @@ -61,10 +61,37 @@ function Invoke-DeployModuleToPSGallery{ "Deploying {0} {1} {2} to PSGallery ..." -f $($psd1.RootModule), $($psd1.ModuleVersion), $($psd1.PrivateData.pSData.Prerelease) | Write-Information # During testing we should use -WhatIf paarmetre when calling for deploy. # Just reach this point when testing call failure + + # Inport-PsModule will run Test-Module before and will fail if all dependencies are not available localy + Install-Dependencies -ModuleManifestPath $ModuleManifestPath + + # Invoke to deploy the module Invoke-DeployModule -Name $psdPath -NuGetApiKey $NuGetApiKey -Force:$ForceDeploy } } +function Install-Dependencies{ + [CmdletBinding()] + param( + [Parameter(Mandatory)] [string]$ModuleManifestPath + ) + + $manifest = $ModuleManifestPath | Get-Item | Import-PowerShellDataFile + + $requiredModules = $manifest.RequiredModules + + foreach ($requiredModule in $requiredModules) { + $module = Import-Module -Name $requiredModule -PassThru -ErrorAction SilentlyContinue + + if ($null -eq $module) { + "Installing module $requiredModule" | Write-Host -ForegroundColor DarkGray + Install-Module -Name $requiredModule -Force -AllowPrerelease + $module = Import-Module -Name $requiredModule -PassThru + "Loaded module Name[$($module.Name)] Version[$($module.Version)]" | Write-Host -ForegroundColor DarkGray + } + } +} + function Update-DeployModuleManifest { [CmdletBinding(SupportsShouldProcess)] param(