Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests(pester): Update to Pester 5 #5222

Merged
merged 8 commits into from
Oct 28, 2022
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
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Init Test Suite
uses: potatoqualitee/[email protected]
with:
modules-to-cache: PSScriptAnalyzer, BuildHelpers, Pester:4.10.1
modules-to-cache: BuildHelpers
shell: powershell
- name: Test Scoop Core
shell: powershell
Expand All @@ -32,7 +32,7 @@ jobs:
- name: Init Test Suite
uses: potatoqualitee/[email protected]
with:
modules-to-cache: PSScriptAnalyzer, BuildHelpers, Pester:4.10.1
modules-to-cache: BuildHelpers
shell: pwsh
- name: Test Scoop Core
shell: pwsh
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
### Tests

- **unix:** Fix tests in Linux and macOS ([#5179](https://github.com/ScoopInstaller/Scoop/issues/5179))
- **pester:** Update to Pester 5 ([#5222](https://github.com/ScoopInstaller/Scoop/issues/5222))

### Documentation

Expand Down
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
}
}
}
Loading