Skip to content

Commit

Permalink
test(manifest): Fix manifests validation (ScoopInstaller#4620)
Browse files Browse the repository at this point in the history
  • Loading branch information
niheaven authored and se35710 committed Mar 8, 2022
1 parent 94da31a commit 53e1eba
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 71 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion bin/test.ps1
Original file line number Diff line number Diff line change
@@ -1 +1 @@
invoke-pester "$psscriptroot\..\test"
. "$PSScriptRoot\..\test\bin\test.ps1"
6 changes: 3 additions & 3 deletions test/00-Project.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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(\\|/)*'
)
Expand Down
21 changes: 11 additions & 10 deletions test/Import-Bucket-Tests.ps1
Original file line number Diff line number Diff line change
@@ -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"
}

Expand Down
99 changes: 48 additions & 51 deletions test/Scoop-Manifest.Tests.ps1
Original file line number Diff line number Diff line change
@@ -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' {
Expand Down Expand Up @@ -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
}
}
}
17 changes: 11 additions & 6 deletions test/bin/test.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ param(
)

$splat = @{
Path = $TestPath
PassThru = $true
Path = $TestPath
PassThru = $true
}

if ($env:CI -eq $true) {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 53e1eba

Please sign in to comment.