Skip to content

Commit c864f68

Browse files
authored
test(manifest): Fix manifests validation (#4620)
1 parent 3c5f5ff commit c864f68

6 files changed

+75
-71
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
### Tests
2020

2121
- **test-bin:** Only write output file in CI and fix trailing whitespaces ([#4613](https://github.com/ScoopInstaller/Scoop/issues/4613))
22+
- **manifest:** Fix manifests validation ([#4620](https://github.com/ScoopInstaller/Scoop/issues/4620))
2223

2324
### Documentation
2425

bin/test.ps1

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
invoke-pester "$psscriptroot\..\test"
1+
. "$PSScriptRoot\..\test\bin\test.ps1"

test/00-Project.Tests.ps1

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ $repo_dir = (Get-Item $MyInvocation.MyCommand.Path).Directory.Parent.FullName
33
$repo_files = @( Get-ChildItem $repo_dir -File -Recurse -Force )
44

55
$project_file_exclusions = @(
6-
$([regex]::Escape($repo_dir) + '(\\|/).git(\\|/).*$'),
7-
'.sublime-workspace$',
8-
'.DS_Store$',
6+
'[\\/]\.git[\\/]',
7+
'\.sublime-workspace$',
8+
'\.DS_Store$',
99
'supporting(\\|/)validator(\\|/)packages(\\|/)*',
1010
'supporting(\\|/)shimexe(\\|/)packages(\\|/)*'
1111
)

test/Import-Bucket-Tests.ps1

+11-10
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,27 @@
1-
if ([String]::IsNullOrEmpty($MyInvocation.PSScriptRoot)) {
2-
Write-Error 'This script should not be called directly! It has to be imported from a buckets test file!'
3-
exit 1
4-
}
1+
[CmdletBinding()]
2+
param(
3+
[ValidateNotNullOrEmpty()]
4+
[String]
5+
$repo_dir = (Get-Item $MyInvocation.PSScriptRoot).FullName
6+
)
57

68
. "$psscriptroot\Scoop-TestLib.ps1"
79
. "$psscriptroot\..\lib\core.ps1"
810
. "$psscriptroot\..\lib\manifest.ps1"
911
. "$psscriptroot\..\lib\unix.ps1"
1012

11-
$repo_dir = (Get-Item $MyInvocation.PSScriptRoot).FullName
12-
1313
$repo_files = @(Get-ChildItem $repo_dir -File -Recurse)
1414

1515
$project_file_exclusions = @(
16-
$([regex]::Escape($repo_dir) + '(\\|/).git(\\|/).*$'),
16+
'[\\/]\.git[\\/]',
1717
'.sublime-workspace$',
18-
'.DS_Store$',
19-
'supporting(\\|/)validator(\\|/)packages(\\|/)*'
18+
'.DS_Store$'
2019
)
2120

2221
$bucketdir = $repo_dir
23-
if (Test-Path("$repo_dir\bucket")) {
22+
if (Test-Path("$repo_dir\..\bucket")) {
23+
$bucketdir = "$repo_dir\..\bucket"
24+
} elseif (Test-Path("$repo_dir\bucket")) {
2425
$bucketdir = "$repo_dir\bucket"
2526
}
2627

test/Scoop-Manifest.Tests.ps1

+48-51
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
param($bucketdir = "$psscriptroot\..\bucket\")
2-
. "$psscriptroot\Scoop-TestLib.ps1"
3-
. "$psscriptroot\..\lib\core.ps1"
4-
. "$psscriptroot\..\lib\manifest.ps1"
1+
param($bucketdir = "$PSScriptRoot\..\bucket")
2+
. "$PSScriptRoot\Scoop-TestLib.ps1"
3+
. "$PSScriptRoot\..\lib\core.ps1"
4+
. "$PSScriptRoot\..\lib\manifest.ps1"
55

6-
Describe -Tag 'Manifests' 'manifest-validation' {
6+
Describe 'Manifest Validator' -Tag 'Validator' {
77
BeforeAll {
88
$working_dir = setup_working 'manifest'
9-
$schema = "$psscriptroot/../schema.json"
10-
Add-Type -Path "$psscriptroot\..\supporting\validator\bin\Newtonsoft.Json.dll"
11-
Add-Type -Path "$psscriptroot\..\supporting\validator\bin\Newtonsoft.Json.Schema.dll"
12-
Add-Type -Path "$psscriptroot\..\supporting\validator\bin\Scoop.Validator.dll"
9+
$schema = "$PSScriptRoot/../schema.json"
10+
Add-Type -Path "$PSScriptRoot\..\supporting\validator\bin\Newtonsoft.Json.dll"
11+
Add-Type -Path "$PSScriptRoot\..\supporting\validator\bin\Newtonsoft.Json.Schema.dll"
12+
Add-Type -Path "$PSScriptRoot\..\supporting\validator\bin\Scoop.Validator.dll"
1313
}
1414

1515
It 'Scoop.Validator is available' {
@@ -43,58 +43,55 @@ Describe -Tag 'Manifests' 'manifest-validation' {
4343
$validator.Errors | Select-Object -Last 1 | Should -Match 'Required properties are missing from object: version, description\.'
4444
}
4545
}
46+
}
4647

47-
Context 'manifest validates against the schema' {
48-
BeforeAll {
49-
if ($null -eq $bucketdir) {
50-
$bucketdir = "$psscriptroot\..\bucket\"
51-
}
52-
$changed_manifests = @()
53-
if ($env:CI -eq $true) {
54-
$commit = if ($env:APPVEYOR_PULL_REQUEST_HEAD_COMMIT) { $env:APPVEYOR_PULL_REQUEST_HEAD_COMMIT } else { $env:APPVEYOR_REPO_COMMIT }
55-
$changed_manifests = (Get-GitChangedFile -Include '*.json' -Commit $commit)
56-
}
57-
$manifest_files = Get-ChildItem $bucketdir *.json
58-
$validator = New-Object Scoop.Validator($schema, $true)
48+
Describe 'manifest validates against the schema' -Tag 'Manifests' {
49+
BeforeAll {
50+
$changed_manifests = @()
51+
if ($env:CI -eq $true) {
52+
$commit = if ($env:APPVEYOR_PULL_REQUEST_HEAD_COMMIT) { $env:APPVEYOR_PULL_REQUEST_HEAD_COMMIT } else { $env:APPVEYOR_REPO_COMMIT }
53+
$changed_manifests = (Get-GitChangedFile -Include 'bucket\*.json' -Commit $commit)
5954
}
55+
$manifest_files = Get-ChildItem $bucketdir *.json
56+
$validator = New-Object Scoop.Validator($schema, $true)
57+
}
6058

61-
$quota_exceeded = $false
59+
$quota_exceeded = $false
6260

63-
$manifest_files | ForEach-Object {
64-
$skip_manifest = ($changed_manifests -inotcontains $_.FullName)
65-
if ($env:CI -ne $true -or $changed_manifests -imatch 'schema.json') {
66-
$skip_manifest = $false
67-
}
68-
It "$_" -Skip:$skip_manifest {
69-
$file = $_ # exception handling may overwrite $_
61+
$manifest_files | ForEach-Object {
62+
$skip_manifest = ($changed_manifests -inotcontains $_.FullName)
63+
if ($env:CI -ne $true -or $changed_manifests -imatch 'schema.json') {
64+
$skip_manifest = $false
65+
}
66+
It "$_" -Skip:$skip_manifest {
67+
$file = $_ # exception handling may overwrite $_
7068

71-
if (!($quota_exceeded)) {
72-
try {
73-
$validator.Validate($file.fullname)
69+
if (!($quota_exceeded)) {
70+
try {
71+
$validator.Validate($file.fullname)
7472

75-
if ($validator.Errors.Count -gt 0) {
76-
Write-Host -f red " [-] $_ has $($validator.Errors.Count) Error$(If($validator.Errors.Count -gt 1) { 's' })!"
77-
Write-Host -f yellow $validator.ErrorsAsString
78-
}
79-
$validator.Errors.Count | Should -Be 0
80-
} catch {
81-
if ($_.exception.message -like '*The free-quota limit of 1000 schema validations per hour has been reached.*') {
82-
$quota_exceeded = $true
83-
Write-Host -f darkyellow 'Schema validation limit exceeded. Will skip further validations.'
84-
} else {
85-
throw
86-
}
73+
if ($validator.Errors.Count -gt 0) {
74+
Write-Host -f red " [-] $_ has $($validator.Errors.Count) Error$(If($validator.Errors.Count -gt 1) { 's' })!"
75+
Write-Host -f yellow $validator.ErrorsAsString
76+
}
77+
$validator.Errors.Count | Should -Be 0
78+
} catch {
79+
if ($_.exception.message -like '*The free-quota limit of 1000 schema validations per hour has been reached.*') {
80+
$quota_exceeded = $true
81+
Write-Host -f darkyellow 'Schema validation limit exceeded. Will skip further validations.'
82+
} else {
83+
throw
8784
}
8885
}
86+
}
8987

90-
$manifest = parse_json $file.fullname
91-
$url = arch_specific 'url' $manifest '32bit'
92-
$url64 = arch_specific 'url' $manifest '64bit'
93-
if (!$url) {
94-
$url = $url64
95-
}
96-
$url | Should -Not -BeNullOrEmpty
88+
$manifest = parse_json $file.fullname
89+
$url = arch_specific 'url' $manifest '32bit'
90+
$url64 = arch_specific 'url' $manifest '64bit'
91+
if (!$url) {
92+
$url = $url64
9793
}
94+
$url | Should -Not -BeNullOrEmpty
9895
}
9996
}
10097
}

test/bin/test.ps1

+11-6
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ param(
77
)
88

99
$splat = @{
10-
Path = $TestPath
11-
PassThru = $true
10+
Path = $TestPath
11+
PassThru = $true
1212
}
1313

1414
if ($env:CI -eq $true) {
@@ -51,15 +51,20 @@ if ($env:CI -eq $true) {
5151
$excludes += 'Manifests'
5252
}
5353

54-
$changed_manifests = (Get-GitChangedFile -Include '*.json' -Commit $commit)
54+
$changed_manifests = (Get-GitChangedFile -Include 'bucket\*.json' -Commit $commit)
5555
if (!$changed_manifests) {
5656
Write-Warning "Skipping tests and validation for manifest files because they didn't change"
5757
$excludes += 'Manifests'
5858
}
59+
}
5960

60-
if ($excludes.Length -gt 0) {
61-
$splat.ExcludeTag = $excludes
62-
}
61+
if (!(Test-Path "$PSScriptRoot\..\..\bucket")) {
62+
Write-Warning 'Skipping tests and validation for manifest files because there is no bucket'
63+
$excludes += 'Manifests'
64+
}
65+
66+
if ($excludes.Length -gt 0) {
67+
$splat.ExcludeTag = $excludes
6368
}
6469

6570
Write-Host 'Invoke-Pester' @splat

0 commit comments

Comments
 (0)