|
1 |
| -[CmdletBinding()] |
| 1 | +#Requires -Version 5.0 |
| 2 | +#Requires -Modules @{ ModuleName = 'Pester'; RequiredVersion = '4.10.1' } |
2 | 3 | param(
|
3 | 4 | [ValidateNotNullOrEmpty()]
|
4 | 5 | [String]
|
5 |
| - $repo_dir = (Get-Item $MyInvocation.PSScriptRoot).FullName |
| 6 | + $repo_dir = (Split-Path -Path $MyInvocation.PSCommandPath -Parent) |
6 | 7 | )
|
7 | 8 |
|
8 |
| -. "$psscriptroot\Scoop-TestLib.ps1" |
9 |
| -. "$psscriptroot\..\lib\core.ps1" |
10 |
| -. "$psscriptroot\..\lib\manifest.ps1" |
11 |
| -. "$psscriptroot\..\lib\unix.ps1" |
| 9 | +. "$PSScriptRoot\Scoop-TestLib.ps1" |
| 10 | +. "$PSScriptRoot\..\lib\manifest.ps1" |
| 11 | +. "$PSScriptRoot\..\lib\unix.ps1" |
12 | 12 |
|
13 |
| -$repo_files = @(Get-ChildItem $repo_dir -File -Recurse) |
| 13 | +$bucketdir = $repo_dir |
| 14 | +if (Test-Path("$repo_dir\..\bucket")) { |
| 15 | + $bucketdir = "$repo_dir\..\bucket" |
| 16 | +} elseif (Test-Path("$repo_dir\bucket")) { |
| 17 | + $bucketdir = "$repo_dir\bucket" |
| 18 | +} |
14 | 19 |
|
| 20 | +# Tests for non manifest files |
| 21 | +$repo_files = @(Get-ChildItem -Path $repo_dir -File -Recurse) |
15 | 22 | $project_file_exclusions = @(
|
16 | 23 | '[\\/]\.git[\\/]',
|
17 | 24 | '.sublime-workspace$',
|
18 | 25 | '.DS_Store$'
|
19 | 26 | )
|
| 27 | +. "$PSScriptRoot\Import-File-Tests.ps1" |
20 | 28 |
|
21 |
| -$bucketdir = $repo_dir |
22 |
| -if (Test-Path("$repo_dir\..\bucket")) { |
23 |
| - $bucketdir = "$repo_dir\..\bucket" |
24 |
| -} elseif (Test-Path("$repo_dir\bucket")) { |
25 |
| - $bucketdir = "$repo_dir\bucket" |
| 29 | +# Tests for manifest files |
| 30 | +Describe 'Manifest Validator' -Tag 'Validator' { |
| 31 | + BeforeAll { |
| 32 | + $schema = "$PSScriptRoot\..\schema.json" |
| 33 | + $working_dir = setup_working 'manifest' |
| 34 | + Add-Type -Path "$PSScriptRoot\..\supporting\validator\bin\Newtonsoft.Json.dll" |
| 35 | + Add-Type -Path "$PSScriptRoot\..\supporting\validator\bin\Newtonsoft.Json.Schema.dll" |
| 36 | + Add-Type -Path "$PSScriptRoot\..\supporting\validator\bin\Scoop.Validator.dll" |
| 37 | + } |
| 38 | + |
| 39 | + It 'Scoop.Validator is available' { |
| 40 | + ([System.Management.Automation.PSTypeName]'Scoop.Validator').Type | Should -Be 'Scoop.Validator' |
| 41 | + } |
| 42 | + |
| 43 | + Context 'parse_json function' { |
| 44 | + It 'fails with invalid json' { |
| 45 | + { parse_json "$working_dir\broken_wget.json" } | Should -Throw |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + Context 'schema validation' { |
| 50 | + It 'fails with broken schema' { |
| 51 | + $validator = New-Object Scoop.Validator("$working_dir\broken_schema.json", $true) |
| 52 | + $validator.Validate("$working_dir\wget.json") | Should -BeFalse |
| 53 | + $validator.Errors.Count | Should -Be 1 |
| 54 | + $validator.Errors | Select-Object -First 1 | Should -Match 'broken_schema.*(line 6).*(position 4)' |
| 55 | + } |
| 56 | + It 'fails with broken manifest' { |
| 57 | + $validator = New-Object Scoop.Validator($schema, $true) |
| 58 | + $validator.Validate("$working_dir\broken_wget.json") | Should -BeFalse |
| 59 | + $validator.Errors.Count | Should -Be 1 |
| 60 | + $validator.Errors | Select-Object -First 1 | Should -Match 'broken_wget.*(line 5).*(position 4)' |
| 61 | + } |
| 62 | + It 'fails with invalid manifest' { |
| 63 | + $validator = New-Object Scoop.Validator($schema, $true) |
| 64 | + $validator.Validate("$working_dir\invalid_wget.json") | Should -BeFalse |
| 65 | + $validator.Errors.Count | Should -Be 16 |
| 66 | + $validator.Errors | Select-Object -First 1 | Should -Match "Property 'randomproperty' has not been defined and the schema does not allow additional properties\." |
| 67 | + $validator.Errors | Select-Object -Last 1 | Should -Match 'Required properties are missing from object: version, description\.' |
| 68 | + } |
| 69 | + } |
| 70 | +} |
| 71 | +Describe 'manifest validates against the schema' -Tag 'Manifests' { |
| 72 | + BeforeAll { |
| 73 | + $schema = "$PSScriptRoot\..\schema.json" |
| 74 | + $changed_manifests = @() |
| 75 | + if ($env:CI -eq $true) { |
| 76 | + # AppVeyor |
| 77 | + $commit = if ($env:APPVEYOR_PULL_REQUEST_HEAD_COMMIT) { $env:APPVEYOR_PULL_REQUEST_HEAD_COMMIT } else { $env:APPVEYOR_REPO_COMMIT } |
| 78 | + |
| 79 | + # GitHub Actions |
| 80 | + if ($env:GITHUB_SHA) { |
| 81 | + $commit = $env:GITHUB_SHA |
| 82 | + } |
| 83 | + $changed_manifests = (Get-GitChangedFile -Path $repo_dir -Include '*.json' -Commit $commit) |
| 84 | + } |
| 85 | + $manifest_files = Get-ChildItem $bucketdir *.json |
| 86 | + $validator = New-Object Scoop.Validator($schema, $true) |
| 87 | + } |
| 88 | + |
| 89 | + $quota_exceeded = $false |
| 90 | + |
| 91 | + $manifest_files | ForEach-Object { |
| 92 | + $skip_manifest = ($changed_manifests -inotcontains $_.FullName) |
| 93 | + if ($env:CI -ne $true -or $changed_manifests -imatch 'schema.json') { |
| 94 | + $skip_manifest = $false |
| 95 | + } |
| 96 | + It "$_" -Skip:$skip_manifest { |
| 97 | + $file = $_ # exception handling may overwrite $_ |
| 98 | + |
| 99 | + if (!($quota_exceeded)) { |
| 100 | + try { |
| 101 | + $validator.Validate($file.fullname) |
| 102 | + |
| 103 | + if ($validator.Errors.Count -gt 0) { |
| 104 | + Write-Host -f red " [-] $_ has $($validator.Errors.Count) Error$(If($validator.Errors.Count -gt 1) { 's' })!" |
| 105 | + Write-Host -f yellow $validator.ErrorsAsString |
| 106 | + } |
| 107 | + $validator.Errors.Count | Should -Be 0 |
| 108 | + } catch { |
| 109 | + if ($_.exception.message -like '*The free-quota limit of 1000 schema validations per hour has been reached.*') { |
| 110 | + $quota_exceeded = $true |
| 111 | + Write-Host -f darkyellow 'Schema validation limit exceeded. Will skip further validations.' |
| 112 | + } else { |
| 113 | + throw |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + $manifest = parse_json $file.fullname |
| 119 | + $url = arch_specific 'url' $manifest '32bit' |
| 120 | + $url64 = arch_specific 'url' $manifest '64bit' |
| 121 | + if (!$url) { |
| 122 | + $url = $url64 |
| 123 | + } |
| 124 | + $url | Should -Not -BeNullOrEmpty |
| 125 | + } |
| 126 | + } |
26 | 127 | }
|
27 | 128 |
|
28 |
| -. "$psscriptroot\Import-File-Tests.ps1" |
29 |
| -. "$psscriptroot\Scoop-Manifest.Tests.ps1" -bucketdir $bucketdir |
|
0 commit comments