Merge pull request #837 from dfinke/UpdatePesterInstall

Prevent install of Pester v5
This commit is contained in:
Doug Finke
2020-05-04 18:52:27 -04:00
committed by GitHub
3 changed files with 66 additions and 64 deletions

View File

@@ -1,4 +1,4 @@
[cmdletbinding(DefaultParameterSetName='Scope')] [cmdletbinding(DefaultParameterSetName = 'Scope')]
Param( Param(
[Parameter(Mandatory = $true, ParameterSetName = 'ModulePath')] [Parameter(Mandatory = $true, ParameterSetName = 'ModulePath')]
[ValidateNotNullOrEmpty()] [ValidateNotNullOrEmpty()]
@@ -10,7 +10,7 @@ Param(
[string] [string]
$Scope = 'CurrentUser', $Scope = 'CurrentUser',
[Parameter(Mandatory=$true, ParameterSetName = 'PreCheckOnly')] [Parameter(Mandatory = $true, ParameterSetName = 'PreCheckOnly')]
[switch]$PreCheckOnly, [switch]$PreCheckOnly,
[Parameter(ParameterSetName = 'ModulePath')] [Parameter(ParameterSetName = 'ModulePath')]
[Parameter(ParameterSetName = 'Scope')] [Parameter(ParameterSetName = 'Scope')]
@@ -30,7 +30,7 @@ Param(
) )
Function Show-Warning { Function Show-Warning {
param( param(
[Parameter(Position=0,ValueFromPipeline=$true)] [Parameter(Position = 0, ValueFromPipeline = $true)]
$message $message
) )
process { process {
@@ -49,11 +49,11 @@ if (-not $psdpath -or $psdpath.count -gt 1) {
throw "Did not find a unique PSD file " throw "Did not find a unique PSD file "
} }
else { else {
try {$null = Test-ModuleManifest -Path $psdpath -ErrorAction stop} try { $null = Test-ModuleManifest -Path $psdpath -ErrorAction stop }
catch {throw $_ ; return} catch { throw $_ ; return }
$ModuleName = $psdpath.Name -replace '\.psd1$' , '' $ModuleName = $psdpath.Name -replace '\.psd1$' , ''
$Settings = $(& ([scriptblock]::Create(($psdpath | Get-Content -Raw)))) $Settings = $(& ([scriptblock]::Create(($psdpath | Get-Content -Raw))))
$approvedVerbs = Get-Verb | Select-Object -ExpandProperty verb $approvedVerbs = Get-Verb | Select-Object -ExpandProperty verb
$script:warningfile = Join-Path -Path $pwd -ChildPath "warnings.txt" $script:warningfile = Join-Path -Path $pwd -ChildPath "warnings.txt"
} }
@@ -63,7 +63,7 @@ if (-not $SkipPreChecks) {
#Check files in the manifest are present #Check files in the manifest are present
foreach ($file in $Settings.FileList) { foreach ($file in $Settings.FileList) {
if (-not (Test-Path $file)) { if (-not (Test-Path $file)) {
Show-Warning "File $file in the manifest file list is not present" Show-Warning "File $file in the manifest file list is not present"
} }
} }
@@ -71,7 +71,7 @@ if (-not $SkipPreChecks) {
# its name and any alias names in the manifest; function should have a param block and help should be in an MD file # its name and any alias names in the manifest; function should have a param block and help should be in an MD file
# We will want a regex which captures from "function verb-noun {" to its closing "}" # We will want a regex which captures from "function verb-noun {" to its closing "}"
# need to match each { to a } - $reg is based on https://stackoverflow.com/questions/7898310/using-regex-to-balance-match-parenthesis # need to match each { to a } - $reg is based on https://stackoverflow.com/questions/7898310/using-regex-to-balance-match-parenthesis
$reg = [Regex]::new(@" $reg = [Regex]::new(@"
function\s*[-\w]+\s*{ # The function name and opening '{' function\s*[-\w]+\s*{ # The function name and opening '{'
(?: (?:
[^{}]+ # Match all non-braces [^{}]+ # Match all non-braces
@@ -84,56 +84,56 @@ if (-not $SkipPreChecks) {
} # Functions closing '}' } # Functions closing '}'
"@, 57) # 57 = compile,multi-line ignore case and white space. "@, 57) # 57 = compile,multi-line ignore case and white space.
foreach ($file in (Get-Item .\Public\*.ps1)) { foreach ($file in (Get-Item .\Public\*.ps1)) {
$name = $file.name -replace(".ps1","") $name = $file.name -replace (".ps1", "")
if ($name -notmatch ("(\w+)-\w+")) {Show-Warning "$name in the public folder is not a verb-noun name"} if ($name -notmatch ("(\w+)-\w+")) { Show-Warning "$name in the public folder is not a verb-noun name" }
elseif ($Matches[1] -notin $approvedVerbs) {Show-Warning "$name in the public folder does not start with an approved verb"} elseif ($Matches[1] -notin $approvedVerbs) { Show-Warning "$name in the public folder does not start with an approved verb" }
if(-not ($Settings.FunctionsToExport -ceq $name)) { if (-not ($Settings.FunctionsToExport -ceq $name)) {
Show-Warning ('File {0} in the public folder does not match an exported function in the manifest' -f $file.name) Show-Warning ('File {0} in the public folder does not match an exported function in the manifest' -f $file.name)
} }
else { else {
$fileContent = Get-Content $file -Raw $fileContent = Get-Content $file -Raw
$m = $reg.Matches($fileContent) $m = $reg.Matches($fileContent)
if ($m.Count -eq 0) {Show-Warning ('Could not find {0} function in {1}' -f $name, $file.name); continue} if ($m.Count -eq 0) { Show-Warning ('Could not find {0} function in {1}' -f $name, $file.name); continue }
elseif ($m.Count -ge 2) {Show-Warning ('Multiple functions in {0}' -f $item.name) ; Continue} elseif ($m.Count -ge 2) { Show-Warning ('Multiple functions in {0}' -f $item.name) ; Continue }
elseif ($m[0] -imatch "^\function\s" -and elseif ($m[0] -imatch "^\function\s" -and
$m[0] -cnotmatch "^\w+\s+$name") {Show-Warning ('function name does not match file name for {0}' -f $file.name)} $m[0] -cnotmatch "^\w+\s+$name") { Show-Warning ('function name does not match file name for {0}' -f $file.name) }
#$m[0] runs form the f of function to its final } -find the section up to param, check for aliases & comment-based help #$m[0] runs form the f of function to its final } -find the section up to param, check for aliases & comment-based help
$m2 = [regex]::Match($m[0],"^.*?param",17) # 17 = multi-line, ignnore case $m2 = [regex]::Match($m[0], "^.*?param", 17) # 17 = multi-line, ignnore case
if (-not $m2.Success) {Show-Warning "function $name has no param() block"} if (-not $m2.Success) { Show-Warning "function $name has no param() block" }
else { else {
if ($m2.value -match "(?<!#\s*)\[\s*Alias\(\s*.([\w-]+).\s*\)\s*\]") { if ($m2.value -match "(?<!#\s*)\[\s*Alias\(\s*.([\w-]+).\s*\)\s*\]") {
foreach ($a in ($Matches[1] -split '\s*,\s*')) { foreach ($a in ($Matches[1] -split '\s*,\s*')) {
$a = $a -replace "'","" -replace '"','' $a = $a -replace "'", "" -replace '"', ''
if (-not ($Settings.AliasesToExport -eq $a)) { if (-not ($Settings.AliasesToExport -eq $a)) {
Show-Warning "Function $name has alias $a which is not in the manifest" Show-Warning "Function $name has alias $a which is not in the manifest"
} }
} }
} }
if ($m2.value -match "\.syopsis|\.Description|\.Example") { if ($m2.value -match "\.syopsis|\.Description|\.Example") {
Show-Warning "Function $name appears to have comment based help." Show-Warning "Function $name appears to have comment based help."
} }
} }
} }
} }
#Warn about functions which are exported but not found in public #Warn about functions which are exported but not found in public
$notFromPublic = $Settings.FunctionsToExport.Where({-not (Test-Path ".\public\$_.ps1")}) $notFromPublic = $Settings.FunctionsToExport.Where( { -not (Test-Path ".\public\$_.ps1") })
If ($notFromPublic) {Show-Warning ('Exported function(s) {0} are not loaded from the Public folder' -f ($notFromPublic -join ', '))} If ($notFromPublic) { Show-Warning ('Exported function(s) {0} are not loaded from the Public folder' -f ($notFromPublic -join ', ')) }
} }
if ($PreCheckOnly) {return} if ($PreCheckOnly) { return }
#region build, determine module path if necessary, create target directory if necessary, copy files based on manifest, build help #region build, determine module path if necessary, create target directory if necessary, copy files based on manifest, build help
try { try {
if ($ModulePath) { if ($ModulePath) {
$ModulePath = $ModulePath -replace "\\$|/$","" $ModulePath = $ModulePath -replace "\\$|/$", ""
} }
else { else {
if ($IsLinux -or $IsMacOS) {$ModulePathSeparator = ':' } if ($IsLinux -or $IsMacOS) { $ModulePathSeparator = ':' }
else {$ModulePathSeparator = ';' } else { $ModulePathSeparator = ';' }
if ($Scope -eq 'CurrentUser') {$dir = [System.Environment]::GetFolderPath([System.Environment+SpecialFolder]::UserProfile) } if ($Scope -eq 'CurrentUser') { $dir = [System.Environment]::GetFolderPath([System.Environment+SpecialFolder]::UserProfile) }
else {$dir = [System.Environment]::GetFolderPath([System.Environment+SpecialFolder]::ProgramFiles) } else { $dir = [System.Environment]::GetFolderPath([System.Environment+SpecialFolder]::ProgramFiles) }
$ModulePath = ($env:PSModulePath -split $ModulePathSeparator).where({$_ -like "$dir*"},"First",1) $ModulePath = ($env:PSModulePath -split $ModulePathSeparator).where( { $_ -like "$dir*" }, "First", 1)
$ModulePath = Join-Path -Path $ModulePath -ChildPath $ModuleName $ModulePath = Join-Path -Path $ModulePath -ChildPath $ModuleName
$ModulePath = Join-Path -Path $ModulePath -ChildPath $Settings.ModuleVersion $ModulePath = Join-Path -Path $ModulePath -ChildPath $Settings.ModuleVersion
} }
@@ -150,13 +150,13 @@ try {
$outputFile = $psdpath | Copy-Item -Destination $ModulePath -PassThru $outputFile = $psdpath | Copy-Item -Destination $ModulePath -PassThru
$outputFile.fullname $outputFile.fullname
foreach ($file in $Settings.FileList) { foreach ($file in $Settings.FileList) {
if ($file -like '.\*') { if ($file -like '.\*') {
$dest = ($file -replace '\.\\',"$ModulePath\") $dest = ($file -replace '\.\\', "$ModulePath\")
if (-not (Test-Path -PathType Container (Split-Path -Parent $dest))) { if (-not (Test-Path -PathType Container (Split-Path -Parent $dest))) {
$null = New-item -Type Directory -Path (Split-Path -Parent $dest) $null = New-item -Type Directory -Path (Split-Path -Parent $dest)
} }
} }
else {$dest = $ModulePath } else { $dest = $ModulePath }
Copy-Item -Path $file -Destination $dest -Force -Recurse Copy-Item -Path $file -Destination $dest -Force -Recurse
} }
@@ -167,19 +167,19 @@ try {
} }
$platypsInfo = Import-Module platyPS -PassThru -force $platypsInfo = Import-Module platyPS -PassThru -force
Get-ChildItem .\mdHelp -Directory | ForEach-Object { Get-ChildItem .\mdHelp -Directory | ForEach-Object {
'Building help for language ''{0}'', using {1} V{2}.' -f $_.Name,$platypsInfo.Name, $platypsInfo.Version 'Building help for language ''{0}'', using {1} V{2}.' -f $_.Name, $platypsInfo.Name, $platypsInfo.Version
$Null = New-ExternalHelp -Path $_.FullName -OutputPath (Join-Path $ModulePath $_.Name) -Force $Null = New-ExternalHelp -Path $_.FullName -OutputPath (Join-Path $ModulePath $_.Name) -Force
} }
} }
#Leave module path for things which follow. #Leave module path for things which follow.
$env:PSNewBuildModule = $ModulePath $env:PSNewBuildModule = $ModulePath
} }
catch { catch {
if ($PSScriptRoot) { Pop-Location } if ($PSScriptRoot) { Pop-Location }
throw ('Failed installing module "{0}". Error: "{1}" in Line {2}' -f $ModuleName, $_, $_.InvocationInfo.ScriptLineNumber) throw ('Failed installing module "{0}". Error: "{1}" in Line {2}' -f $ModuleName, $_, $_.InvocationInfo.ScriptLineNumber)
} }
finally { finally {
if (-not $outputFile -or -not (Test-Path $outputFile)) { throw "Failed to create module"} if (-not $outputFile -or -not (Test-Path $outputFile)) { throw "Failed to create module" }
} }
#endregion #endregion
@@ -189,16 +189,16 @@ if ($env:Build_ArtifactStagingDirectory) {
#Check valid command names, help, run script analyzer over the files in the module directory #Check valid command names, help, run script analyzer over the files in the module directory
if (-not $SkipPostChecks) { if (-not $SkipPostChecks) {
try {$outputFile | Import-Module -Force -ErrorAction stop } try { $outputFile | Import-Module -Force -ErrorAction stop }
catch { catch {
if ($PSScriptRoot) { Pop-Location } if ($PSScriptRoot) { Pop-Location }
throw "New module failed to load" throw "New module failed to load"
} }
$commands = Get-Command -Module $ModuleName -CommandType function,Cmdlet $commands = Get-Command -Module $ModuleName -CommandType function, Cmdlet
$commands.where({$_.name -notmatch "(\w+)-\w+" -or $Matches[1] -notin $approvedVerbs}) | ForEach-Object { $commands.where( { $_.name -notmatch "(\w+)-\w+" -or $Matches[1] -notin $approvedVerbs }) | ForEach-Object {
Show-Warning ('{0} does not meet the ApprovedVerb-Noun naming rules' -f $_.name) Show-Warning ('{0} does not meet the ApprovedVerb-Noun naming rules' -f $_.name)
} }
$helpless = $commands | Get-Help | Where-Object {$_.Synopsis -match "^\s+$($_.name)\s+\["} | Select-Object -ExpandProperty name $helpless = $commands | Get-Help | Where-Object { $_.Synopsis -match "^\s+$($_.name)\s+\[" } | Select-Object -ExpandProperty name
foreach ($command in $helpless ) { foreach ($command in $helpless ) {
Show-Warning ('On-line help is missing for {0}.' -f $command) Show-Warning ('On-line help is missing for {0}.' -f $command)
} }
@@ -222,10 +222,12 @@ if (-not $SkipPostChecks) {
AutoSize = $true AutoSize = $true
Activate = $true Activate = $true
PivotTableDefinition = @{BreakDown = @{ PivotTableDefinition = @{BreakDown = @{
PivotData = @{RuleName = 'Count' } PivotData = @{RuleName = 'Count' }
PivotRows = 'Severity', 'RuleName' PivotRows = 'Severity', 'RuleName'
PivotTotals = 'Rows' PivotTotals = 'Rows'
PivotChartDefinition = $chartDef }} PivotChartDefinition = $chartDef
}
}
} }
Remove-Item -Path $ExcelParams['Path'] -ErrorAction SilentlyContinue Remove-Item -Path $ExcelParams['Path'] -ErrorAction SilentlyContinue
$AnalyzerResults | Export-Excel @ExcelParams $AnalyzerResults | Export-Excel @ExcelParams
@@ -244,8 +246,8 @@ if (Test-Path $script:warningfile) {
#if there are test files, run pester (unless told not to) #if there are test files, run pester (unless told not to)
if (-not $SkipPesterTests -and (Get-ChildItem -Recurse *.tests.ps1)) { if (-not $SkipPesterTests -and (Get-ChildItem -Recurse *.tests.ps1)) {
Import-Module -Force $outputFile Import-Module -Force $outputFile
if (-not (Get-Module -ListAvailable pester | Where-Object -Property version -ge ([version]::new(4,4,1)))) { if (-not (Get-Module -ListAvailable pester | Where-Object -Property version -ge ([version]::new(4, 4, 1)))) {
Install-Module Pester -Force -SkipPublisherCheck Install-Module Pester -Force -SkipPublisherCheck -MaximumVersion 4.99.99
} }
Import-Module Pester Import-Module Pester
$PesterOutputPath = Join-Path $pwd -ChildPath ('TestResultsPS{0}.xml' -f $PSVersionTable.PSVersion) $PesterOutputPath = Join-Path $pwd -ChildPath ('TestResultsPS{0}.xml' -f $PSVersionTable.PSVersion)

View File

@@ -25,12 +25,12 @@ skip_commits:
# Scripts that run after cloning repository # Scripts that run after cloning repository
install: install:
- ps: 'Install-Module -Name Pester -Force -SkipPublisherCheck' - ps: 'Install-Module -Name Pester -Force -SkipPublisherCheck -MaximumVersion 4.99.99'
- ps: 'Install-Module -Name Assert -Force' - ps: 'Install-Module -Name Assert -Force -MaximumVersion 4.99.99'
# PowerShell Core # PowerShell Core
- ps: '& .\CI\InstallPowerShell.ps1 -Version "7.0.0-preview.4"' # Install other PowerShell Core version (Optional) - ps: '& .\CI\InstallPowerShell.ps1 -Version "7.0.0-preview.4"' # Install other PowerShell Core version (Optional)
- pwsh: 'Install-Module -Name Pester -Force' - pwsh: 'Install-Module -Name Pester -Force -MaximumVersion 4.99.99'
- pwsh: 'Install-Module -Name Assert -Force' - pwsh: 'Install-Module -Name Assert -Force -MaximumVersion 4.99.99'
# To run your custom scripts instead of automatic tests # To run your custom scripts instead of automatic tests
test_script: test_script:
@@ -56,8 +56,8 @@ for:
# Scripts that run after cloning repository # Scripts that run after cloning repository
install: install:
- pwsh: '& .\CI\CI.ps1 -Initialize' # Set AppVeyor build version - pwsh: '& .\CI\CI.ps1 -Initialize' # Set AppVeyor build version
- pwsh: 'Install-Module -Name Pester -Force' - pwsh: 'Install-Module -Name Pester -Force -MaximumVersion 4.99.99'
- pwsh: 'Install-Module -Name Assert -Force' - pwsh: 'Install-Module -Name Assert -Force -MaximumVersion 4.99.99'
# To run your custom scripts instead of automatic tests # To run your custom scripts instead of automatic tests
test_script: test_script:
- pwsh: '& .\CI\CI.ps1 -Test' - pwsh: '& .\CI\CI.ps1 -Test'

View File

@@ -20,7 +20,7 @@ jobs:
vmImage: 'windows-latest' vmImage: 'windows-latest'
steps: steps:
- powershell: 'Install-Module -Name Pester -Force -SkipPublisherCheck' - powershell: 'Install-Module -Name Pester -Force -SkipPublisherCheck -MaximumVersion 4.99.99'
displayName: 'Update Pester' displayName: 'Update Pester'
- powershell: './CI/CI.ps1 -Test' - powershell: './CI/CI.ps1 -Test'
displayName: 'Install and Test' displayName: 'Install and Test'
@@ -49,7 +49,7 @@ jobs:
vmImage: 'windows-latest' vmImage: 'windows-latest'
steps: steps:
- pwsh: 'Install-Module -Name Pester -Force' - pwsh: 'Install-Module -Name Pester -Force -MaximumVersion 4.99.99'
displayName: 'Update Pester' displayName: 'Update Pester'
- pwsh: './CI/CI.ps1 -Test' - pwsh: './CI/CI.ps1 -Test'
displayName: 'Install and Test' displayName: 'Install and Test'
@@ -65,7 +65,7 @@ jobs:
vmImage: 'ubuntu-latest' vmImage: 'ubuntu-latest'
steps: steps:
- powershell: 'Install-Module -Name Pester -Force' - powershell: 'Install-Module -Name Pester -Force -MaximumVersion 4.99.99'
displayName: 'Update Pester' displayName: 'Update Pester'
- powershell: './CI/CI.ps1 -Test' - powershell: './CI/CI.ps1 -Test'
displayName: 'Install and Test' displayName: 'Install and Test'
@@ -83,7 +83,7 @@ jobs:
steps: steps:
- script: brew install mono-libgdiplus - script: brew install mono-libgdiplus
displayName: 'Install mono-libgdiplus' displayName: 'Install mono-libgdiplus'
- powershell: 'Install-Module -Name Pester -Force' - powershell: 'Install-Module -Name Pester -Force -MaximumVersion 4.99.99'
displayName: 'Update Pester' displayName: 'Update Pester'
- powershell: './CI/CI.ps1 -Test' - powershell: './CI/CI.ps1 -Test'
displayName: 'Install and Test' displayName: 'Install and Test'