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

feat(CI): Support validation of all archived manifests #168

Merged
merged 6 commits into from
Jun 19, 2021
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## [0.6.5](https://github.com/Ash258/Scoop-Core/milestone/5)

- **CI**:
- Support basic validation of yml typed manifests
- Support validation of all archived manifests
- **scoop-cat**: Add `-f`, `--format` options
- Adopt new resolve function for parameter passing
- **scoop-home**
Expand Down
65 changes: 40 additions & 25 deletions test/Scoop-Manifest.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -20,65 +20,80 @@ Describe -Tag 'Manifests' 'manifest-validation' {
Context 'parse_json function' {
It 'fails with invalid json' {
{ parse_json "$working_dir\broken_wget.json" } | Should -Throw
{ ConvertFrom-Manifest "$working_dir\broken_wget.json" } | Should -Throw
}
}

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)'
$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)'
$validator.Errors.Count | Should -Be 1
$validator.Errors | Select-Object -First 1 | Should -Match 'broken_wget.*(line 5).*(position 4)'
}
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, description\.'
$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, description\.'
}
}

Context 'manifest validates against the schema' {
BeforeAll {
if ($null -eq $bucketdir) { $bucketdir = "$PSScriptRoot\..\bucket\" }
if (!(Test-Path $bucketdir)) { New-Item $bucketdir -ItemType 'Directory' -Force }

$changed_manifests = @()
$manifest_files = @()
$allFiles = Get-ChildItem $bucketdir -Recurse

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)
if ($commit) {
$allChanged = Get-GitChangedFile -Commit $commit

# Filter out valid manifests
foreach ($ext in $ALLOWED_MANIFEST_EXTENSION) {
$changed_manifests += $allChanged | Where-Object { $_ -like "*.$ext" }
}
}
}

# Filter out valid manifests
foreach ($ext in $ALLOWED_MANIFEST_EXTENSION) {
$manifest_files += $allFiles | Where-Object { $_ -like "*.$ext" }
}
$manifest_files = Get-ChildItem $bucketdir *.json

$validator = New-Object Scoop.Validator($schema, $true)
}

$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 $_
foreach ($file in $manifest_files) {
$skip_manifest = ($changed_manifests -inotcontains $file.FullName)
if (($env:CI -ne $true) -or ($changed_manifests -imatch 'schema.json')) { $skip_manifest = $false }

if (!($quota_exceeded)) {
It "$file" -Skip:$skip_manifest {
# TODO: Skip yml for now for schema validation
if (!$quota_exceeded -and ($file.Extension -notmatch '\.ya?ml$')) {
try {
$validator.Validate($file.fullname)
$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 red " [-] $file has $($validator.Errors.Count) Error$(If($validator.Errors.Count -gt 1) { 's' })!"
Write-Host -f yellow $validator.ErrorsAsString
}
$validator.Errors.Count | Should -be 0
$validator.Errors.Count | Should -Be 0
} catch {
if ($_.exception.message -like '*The free-quota limit of 1000 schema validations per hour has been reached.*') {
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 {
Expand All @@ -87,12 +102,12 @@ Describe -Tag 'Manifests' 'manifest-validation' {
}
}

$manifest = parse_json $file.fullname
$manifest = ConvertFrom-Manifest -LiteralPath $file.FullName
$url = arch_specific 'url' $manifest '32bit'
$url64 = arch_specific 'url' $manifest '64bit'
if (!$url) {
$url = $url64
}

if (!$url) { $url = $url64 }

$url | Should -Not -BeNullOrEmpty
}
}
Expand Down