Skip to content
Open
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
1 change: 0 additions & 1 deletion TestingHelperTest/public/deploy.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions deploy.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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){

Expand Down
24 changes: 24 additions & 0 deletions private/templates/template.v3.deploy.Helper.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
5 changes: 5 additions & 0 deletions private/templates/template.v3.deploy.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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){

Expand Down
85 changes: 64 additions & 21 deletions private/templates/template.v3.test.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
if($TestName){
Invoke-TestingHelper -TestName $TestName -ShowTestErrors:$ShowTestErrors
} else {
Invoke-TestingHelper -ShowTestErrors:$ShowTestErrors
}
62 changes: 39 additions & 23 deletions test.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ function Set-TestName{
}
}

function Get-TestName{
[CmdletBinding()]
[Alias("gt")]
param (
)

return $global:TestName
}

function Clear-TestName{
[CmdletBinding()]
[Alias("ct")]
Expand All @@ -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
}
Invoke-TestingHelper -ShowTestErrors:$ShowTestErrors
}
27 changes: 27 additions & 0 deletions tools/deploy.Helper.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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{

Check warning

Code scanning / PSScriptAnalyzer

The cmdlet 'Install-Dependencies' uses a plural noun. A singular noun should be used instead.

The cmdlet 'Install-Dependencies' uses a plural noun. A singular noun should be used instead.
[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

Check warning

Code scanning / PSScriptAnalyzer

File 'deploy.Helper.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.

File 'deploy.Helper.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.
}
}
}

function Update-DeployModuleManifest {
[CmdletBinding(SupportsShouldProcess)]
param(
Expand Down