Skip to content

Commit

Permalink
Fix tests for Pester 5
Browse files Browse the repository at this point in the history
  • Loading branch information
niheaven committed Oct 24, 2022
1 parent 77e3263 commit 6ee8c09
Show file tree
Hide file tree
Showing 16 changed files with 403 additions and 450 deletions.
3 changes: 0 additions & 3 deletions PSScriptAnalyzerSettings.psd1
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
# The PowerShell Script Analyzer will generate a warning
# diagnostic record for this file due to a bug -
# https://github.com/PowerShell/PSScriptAnalyzer/issues/472
@{
# Only diagnostic records of the specified severity will be generated.
# Uncomment the following line if you only want Errors and Warnings but
Expand Down
74 changes: 0 additions & 74 deletions test/00-Project.Tests.ps1

This file was deleted.

158 changes: 47 additions & 111 deletions test/Import-Bucket-Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,127 +1,63 @@
#Requires -Version 5.0
#Requires -Modules @{ ModuleName = 'Pester'; RequiredVersion = '4.10.1' }
#Requires -Version 5.1
#Requires -Modules @{ ModuleName = 'Pester'; ModuleVersion = '5.2.0' }
param(
[ValidateNotNullOrEmpty()]
[String]
$repo_dir = (Split-Path -Path $MyInvocation.PSCommandPath -Parent)
[String] $BucketPath = $MyInvocation.PSScriptRoot
)

. "$PSScriptRoot\Scoop-TestLib.ps1"
. "$PSScriptRoot\..\lib\manifest.ps1"
. "$PSScriptRoot\..\lib\unix.ps1"
. "$PSScriptRoot\Scoop-00File.Tests.ps1" -TestPath $BucketPath

$bucketdir = $repo_dir
if (Test-Path("$repo_dir\..\bucket")) {
$bucketdir = "$repo_dir\..\bucket"
} elseif (Test-Path("$repo_dir\bucket")) {
$bucketdir = "$repo_dir\bucket"
}

# Tests for non manifest files
$repo_files = @(Get-ChildItem -Path $repo_dir -File -Recurse)
$project_file_exclusions = @(
'[\\/]\.git[\\/]',
'.sublime-workspace$',
'.DS_Store$'
)
. "$PSScriptRoot\Import-File-Tests.ps1"

# Tests for manifest files
Describe 'Manifest Validator' -Tag 'Validator' {
BeforeAll {
$schema = "$PSScriptRoot\..\schema.json"
$working_dir = setup_working 'manifest'
Add-Type -Path "$PSScriptRoot\..\supporting\validator\bin\Newtonsoft.Json.dll"
Add-Type -Path "$PSScriptRoot\..\supporting\validator\bin\Newtonsoft.Json.Schema.dll"
Add-Type -Path "$PSScriptRoot\..\supporting\validator\bin\Scoop.Validator.dll"
}

It 'Scoop.Validator is available' {
([System.Management.Automation.PSTypeName]'Scoop.Validator').Type | Should -Be 'Scoop.Validator'
}

Context 'parse_json function' {
It 'fails with invalid json' {
{ parse_json "$working_dir\broken_wget.json" } | Should -Throw
Describe 'Manifest validates against the schema' {
BeforeDiscovery {
$bucketDir = if (Test-Path "$BucketPath\bucket") {
"$BucketPath\bucket"
} else {
$BucketPath
}
}

Context 'schema validation' {
It 'fails with broken schema' {
$validator = New-Object Scoop.Validator("$working_dir\broken_schema.json", $true)
$validator.Validate("$working_dir\wget.json") | Should -BeFalse
$validator.Errors.Count | Should -Be 1
$validator.Errors | Select-Object -First 1 | Should -Match 'broken_schema.*(line 6).*(position 4)'
}
It 'fails with broken manifest' {
$validator = New-Object Scoop.Validator($schema, $true)
$validator.Validate("$working_dir\broken_wget.json") | Should -BeFalse
$validator.Errors.Count | Should -Be 1
$validator.Errors | Select-Object -First 1 | Should -Match 'broken_wget.*(line 5).*(position 4)'
if ($env:CI -eq $true) {
$commit = if ($env:GITHUB_SHA) {
# GitHub Actions
$env:GITHUB_SHA
} elseif ($env:APPVEYOR_PULL_REQUEST_HEAD_COMMIT) {
# AppVeyor
$env:APPVEYOR_PULL_REQUEST_HEAD_COMMIT
} else {
$env:APPVEYOR_REPO_COMMIT
}
if ($commit) {
$changedManifests = @(Get-GitChangedFile -Path $bucketDir -Include '*.json' -Commit $commit)
}
}
It 'fails with invalid manifest' {
$validator = New-Object Scoop.Validator($schema, $true)
$validator.Validate("$working_dir\invalid_wget.json") | Should -BeFalse
$validator.Errors.Count | Should -Be 16
$validator.Errors | Select-Object -First 1 | Should -Match "Property 'randomproperty' has not been defined and the schema does not allow additional properties\."
$validator.Errors | Select-Object -Last 1 | Should -Match 'Required properties are missing from object: version\.'
$manifestFiles = (Get-ChildItem $bucketDir -Filter '*.json' -Recurse).FullName
if ($changedManifests) {
$manifestFiles = $manifestFiles | Where-Object { $_ -in $changedManifests }
}
}
}
Describe 'manifest validates against the schema' -Tag 'Manifests' {
BeforeAll {
$schema = "$PSScriptRoot\..\schema.json"
$changed_manifests = @()
if ($env:CI -eq $true) {
# AppVeyor
$commit = if ($env:APPVEYOR_PULL_REQUEST_HEAD_COMMIT) { $env:APPVEYOR_PULL_REQUEST_HEAD_COMMIT } else { $env:APPVEYOR_REPO_COMMIT }

# GitHub Actions
if ($env:GITHUB_SHA) {
$commit = $env:GITHUB_SHA
}
$changed_manifests = (Get-GitChangedFile -Path $repo_dir -Include '*.json' -Commit $commit)
}
$manifest_files = Get-ChildItem $bucketdir -Filter '*.json' -Recurse
$validator = New-Object Scoop.Validator($schema, $true)
Add-Type -Path "$PSScriptRoot\..\supporting\validator\bin\Scoop.Validator.dll"
# Could not use backslash '\' in Linux/macOS for .NET object 'Scoop.Validator'
$validator = New-Object Scoop.Validator("$PSScriptRoot/../schema.json", $true)
$global:quotaExceeded = $false
}

$quota_exceeded = $false

$manifest_files | ForEach-Object {
$skip_manifest = ($changed_manifests -inotcontains $_.FullName)
if ($env:CI -ne $true -or $changed_manifests -imatch 'schema.json') {
$skip_manifest = $false
}
It "$_" -Skip:$skip_manifest {
It '<_>' -TestCases $manifestFiles {
if ($global:quotaExceeded) {
Set-ItResult -Skipped -Because 'Schema validation limit exceeded.'
} else {
$file = $_ # exception handling may overwrite $_

if (!($quota_exceeded)) {
try {
$validator.Validate($file.fullname)

if ($validator.Errors.Count -gt 0) {
Write-Host -f red " [-] $_ has $($validator.Errors.Count) Error$(If($validator.Errors.Count -gt 1) { 's' })!"
Write-Host -f yellow $validator.ErrorsAsString
}
$validator.Errors.Count | Should -Be 0
} catch {
if ($_.exception.message -like '*The free-quota limit of 1000 schema validations per hour has been reached.*') {
$quota_exceeded = $true
Write-Host -f darkyellow 'Schema validation limit exceeded. Will skip further validations.'
} else {
throw
}
try {
$validator.Validate($file)
if ($validator.Errors.Count -gt 0) {
Write-Host " [-] $_ has $($validator.Errors.Count) Error$(If($validator.Errors.Count -gt 1) { 's' })!" -ForegroundColor Red
Write-Host $validator.ErrorsAsString -ForegroundColor Yellow
}
$validator.Errors.Count | Should -Be 0
} catch {
if ($_.Exception.Message -like '*The free-quota limit of 1000 schema validations per hour has been reached.*') {
$global:quotaExceeded = $true
Set-ItResult -Skipped -Because 'Schema validation limit exceeded.'
} else {
throw
}
}

$manifest = parse_json $file.fullname
$url = arch_specific 'url' $manifest '32bit'
$url64 = arch_specific 'url' $manifest '64bit'
if (!$url) {
$url = $url64
}
$url | Should -Not -BeNullOrEmpty
}
}
}
134 changes: 0 additions & 134 deletions test/Import-File-Tests.ps1

This file was deleted.

Loading

0 comments on commit 6ee8c09

Please sign in to comment.