forked from ScoopInstaller/Scoop
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Scoop-Manifest.Tests.ps1
132 lines (114 loc) · 6.14 KB
/
Scoop-Manifest.Tests.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
param($bucketdir = "$PSScriptRoot\..\bucket\")
. "$PSScriptRoot\Scoop-TestLib.ps1"
. "$PSScriptRoot\..\lib\core.ps1"
. "$PSScriptRoot\..\lib\manifest.ps1"
Describe -Tag 'Manifests' 'manifest-validation' {
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"
}
It 'Scoop.Validator is available' {
([System.Management.Automation.PSTypeName]'Scoop.Validator').Type | Should -Be 'Scoop.Validator'
}
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)'
}
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)'
}
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\.'
}
It 'fail with nonsense in autoupdate' {
$validator = New-Object Scoop.Validator($schema, $true)
$validator.Validate("$working_dir/autoupdate_fail.json") | Should -BeFalse
$validator.Errors.Count | Should -Not -Be 0
# URL should contain URI or uri with {}, but not $(base)?url
@($validator.Errors)[0] | Should -Match 'from ''anyOf'''
@($validator.Errors)[1] | Should -Match '\$baseurl/'
# Hash has to contain $url/$baseurl or uri
@($validator.Errors)[-1] | Should -Match 'nonhttps/'
}
It 'pass with curly brackets in autoupdate' {
$validator = New-Object Scoop.Validator($schema, $true)
$validator.Validate("$working_dir/autoupdate.json") | Should -BeTrue
$validator.Errors.Count | Should -Be 0
}
}
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 }
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 -Property 'Name' -Like -Value "*.$ext"
}
$validator = New-Object Scoop.Validator($schema, $true)
}
$quota_exceeded = $false
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 }
It $file.BaseName -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)
if ($validator.Errors.Count -gt 0) {
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
} 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 = ConvertFrom-Manifest -LiteralPath $file.FullName
$url = arch_specific 'url' $manifest '32bit'
$url64 = arch_specific 'url' $manifest '64bit'
$urlarm64 = arch_specific 'url' $manifest 'arm64'
if (!$url) { $url = $url64 }
if (!$url) { $url = $urlarm64 }
$url | Should -Not -BeNullOrEmpty
}
}
}
}