diff --git a/CHANGELOG.md b/CHANGELOG.md index 72953aa7..dbbde23c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ ### Tests - **test-bin:** Only write output file in CI and fix trailing whitespaces ([#4613](https://github.com/ScoopInstaller/Scoop/issues/4613)) +- **manifest:** Fix manifests validation ([#4620](https://github.com/ScoopInstaller/Scoop/issues/4620)) ### Documentation diff --git a/bin/test.ps1 b/bin/test.ps1 index 33bc2f85..3e2cc6fc 100644 --- a/bin/test.ps1 +++ b/bin/test.ps1 @@ -1 +1 @@ -invoke-pester "$psscriptroot\..\test" +. "$PSScriptRoot\..\test\bin\test.ps1" diff --git a/test/00-Project.Tests.ps1 b/test/00-Project.Tests.ps1 index bcc35cdb..dba62f50 100644 --- a/test/00-Project.Tests.ps1 +++ b/test/00-Project.Tests.ps1 @@ -3,9 +3,9 @@ $repo_dir = (Get-Item $MyInvocation.MyCommand.Path).Directory.Parent.FullName $repo_files = @( Get-ChildItem $repo_dir -File -Recurse -Force ) $project_file_exclusions = @( - $([regex]::Escape($repo_dir) + '(\\|/).git(\\|/).*$'), - '.sublime-workspace$', - '.DS_Store$', + '[\\/]\.git[\\/]', + '\.sublime-workspace$', + '\.DS_Store$', 'supporting(\\|/)validator(\\|/)packages(\\|/)*', 'supporting(\\|/)shimexe(\\|/)packages(\\|/)*' ) diff --git a/test/Import-Bucket-Tests.ps1 b/test/Import-Bucket-Tests.ps1 index 1520e654..ade3da02 100644 --- a/test/Import-Bucket-Tests.ps1 +++ b/test/Import-Bucket-Tests.ps1 @@ -1,26 +1,27 @@ -if ([String]::IsNullOrEmpty($MyInvocation.PSScriptRoot)) { - Write-Error 'This script should not be called directly! It has to be imported from a buckets test file!' - exit 1 -} +[CmdletBinding()] +param( + [ValidateNotNullOrEmpty()] + [String] + $repo_dir = (Get-Item $MyInvocation.PSScriptRoot).FullName +) . "$psscriptroot\Scoop-TestLib.ps1" . "$psscriptroot\..\lib\core.ps1" . "$psscriptroot\..\lib\manifest.ps1" . "$psscriptroot\..\lib\unix.ps1" -$repo_dir = (Get-Item $MyInvocation.PSScriptRoot).FullName - $repo_files = @(Get-ChildItem $repo_dir -File -Recurse) $project_file_exclusions = @( - $([regex]::Escape($repo_dir) + '(\\|/).git(\\|/).*$'), + '[\\/]\.git[\\/]', '.sublime-workspace$', - '.DS_Store$', - 'supporting(\\|/)validator(\\|/)packages(\\|/)*' + '.DS_Store$' ) $bucketdir = $repo_dir -if (Test-Path("$repo_dir\bucket")) { +if (Test-Path("$repo_dir\..\bucket")) { + $bucketdir = "$repo_dir\..\bucket" +} elseif (Test-Path("$repo_dir\bucket")) { $bucketdir = "$repo_dir\bucket" } diff --git a/test/Scoop-Manifest.Tests.ps1 b/test/Scoop-Manifest.Tests.ps1 index 99c87ca6..e9035c94 100644 --- a/test/Scoop-Manifest.Tests.ps1 +++ b/test/Scoop-Manifest.Tests.ps1 @@ -1,15 +1,15 @@ -param($bucketdir = "$psscriptroot\..\bucket\") -. "$psscriptroot\Scoop-TestLib.ps1" -. "$psscriptroot\..\lib\core.ps1" -. "$psscriptroot\..\lib\manifest.ps1" +param($bucketdir = "$PSScriptRoot\..\bucket") +. "$PSScriptRoot\Scoop-TestLib.ps1" +. "$PSScriptRoot\..\lib\core.ps1" +. "$PSScriptRoot\..\lib\manifest.ps1" -Describe -Tag 'Manifests' 'manifest-validation' { +Describe 'Manifest Validator' -Tag 'Validator' { BeforeAll { $working_dir = setup_working 'manifest' - $schema = "$psscriptroot/../schema.json" - 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" + $schema = "$PSScriptRoot/../schema.json" + 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' { @@ -43,58 +43,55 @@ Describe -Tag 'Manifests' 'manifest-validation' { $validator.Errors | Select-Object -Last 1 | Should -Match 'Required properties are missing from object: version, description\.' } } +} - Context 'manifest validates against the schema' { - BeforeAll { - if ($null -eq $bucketdir) { - $bucketdir = "$psscriptroot\..\bucket\" - } - $changed_manifests = @() - if ($env:CI -eq $true) { - $commit = if ($env:APPVEYOR_PULL_REQUEST_HEAD_COMMIT) { $env:APPVEYOR_PULL_REQUEST_HEAD_COMMIT } else { $env:APPVEYOR_REPO_COMMIT } - $changed_manifests = (Get-GitChangedFile -Include '*.json' -Commit $commit) - } - $manifest_files = Get-ChildItem $bucketdir *.json - $validator = New-Object Scoop.Validator($schema, $true) +Describe 'manifest validates against the schema' -Tag 'Manifests' { + BeforeAll { + $changed_manifests = @() + if ($env:CI -eq $true) { + $commit = if ($env:APPVEYOR_PULL_REQUEST_HEAD_COMMIT) { $env:APPVEYOR_PULL_REQUEST_HEAD_COMMIT } else { $env:APPVEYOR_REPO_COMMIT } + $changed_manifests = (Get-GitChangedFile -Include 'bucket\*.json' -Commit $commit) } + $manifest_files = Get-ChildItem $bucketdir *.json + $validator = New-Object Scoop.Validator($schema, $true) + } - $quota_exceeded = $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 { - $file = $_ # exception handling may overwrite $_ + $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 { + $file = $_ # exception handling may overwrite $_ - if (!($quota_exceeded)) { - try { - $validator.Validate($file.fullname) + 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 - } + 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 } } - - $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 } + + $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 } } } diff --git a/test/bin/test.ps1 b/test/bin/test.ps1 index d700b0dc..51b7cc9a 100644 --- a/test/bin/test.ps1 +++ b/test/bin/test.ps1 @@ -7,8 +7,8 @@ param( ) $splat = @{ - Path = $TestPath - PassThru = $true + Path = $TestPath + PassThru = $true } if ($env:CI -eq $true) { @@ -51,15 +51,20 @@ if ($env:CI -eq $true) { $excludes += 'Manifests' } - $changed_manifests = (Get-GitChangedFile -Include '*.json' -Commit $commit) + $changed_manifests = (Get-GitChangedFile -Include 'bucket\*.json' -Commit $commit) if (!$changed_manifests) { Write-Warning "Skipping tests and validation for manifest files because they didn't change" $excludes += 'Manifests' } +} - if ($excludes.Length -gt 0) { - $splat.ExcludeTag = $excludes - } +if (!(Test-Path "$PSScriptRoot\..\..\bucket")) { + Write-Warning 'Skipping tests and validation for manifest files because there is no bucket' + $excludes += 'Manifests' +} + +if ($excludes.Length -gt 0) { + $splat.ExcludeTag = $excludes } Write-Host 'Invoke-Pester' @splat