Skip to content

Commit d9f55a3

Browse files
authored
refactor(ci,tests): Support both AppVeyor and GitHub Actions (#4655)
1 parent d7fb97f commit d9f55a3

File tree

10 files changed

+257
-214
lines changed

10 files changed

+257
-214
lines changed

.github/workflows/ci.yml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
name: Tests
2+
3+
on:
4+
pull_request:
5+
types: [ opened ]
6+
push:
7+
workflow_dispatch:
8+
9+
jobs:
10+
test_powershell:
11+
name: WindowsPowerShell
12+
runs-on: windows-latest
13+
steps:
14+
- name: Checkout
15+
uses: actions/checkout@v2
16+
with:
17+
fetch-depth: 2
18+
- name: Init and Test
19+
shell: powershell
20+
run: |
21+
.\test\bin\init.ps1
22+
.\test\bin\test.ps1
23+
test_pwsh:
24+
name: PowerShell
25+
runs-on: windows-latest
26+
steps:
27+
- name: Checkout
28+
uses: actions/checkout@v2
29+
with:
30+
fetch-depth: 2
31+
- name: Init and Test
32+
shell: pwsh
33+
run: |
34+
.\test\bin\init.ps1
35+
.\test\bin\test.ps1

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
- **checkver:** Fix output with '-Version' ([#3774](https://github.com/ScoopInstaller/Scoop/issues/3774))
2828
- **schema:** Add '$schema' property ([#4623](https://github.com/ScoopInstaller/Scoop/issues/4623))
2929
- **schema:** Add explicit escape to opening bracket matcher in jp/jsonpath regex ([#3719](https://github.com/ScoopInstaller/Scoop/issues/3719))
30+
- **tests:** Support both AppVeyor and GitHub Actions ([#4655](https://github.com/ScoopInstaller/Scoop/issues/4655))
3031
- **vscode-settings:** Remove 'formatOnSave' trigger ([#4635](https://github.com/ScoopInstaller/Scoop/issues/4635))
3132

3233
### Styles

PSScriptAnalyzerSettings.psd1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# Only diagnostic records of the specified severity will be generated.
66
# Uncomment the following line if you only want Errors and Warnings but
77
# not Information diagnostic records.
8-
Severity = @('Error','Warning')
8+
Severity = @('Error')
99

1010
# Analyze **only** the following rules. Use IncludeRules when you want
1111
# to invoke only a small subset of the defualt rules.

appveyor.yml

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,32 @@ branches:
44
- gh-pages
55
build: false
66
deploy: false
7-
clone_depth: 49
8-
image: Visual Studio 2019
7+
clone_depth: 2
8+
image: Visual Studio 2022
99
environment:
10-
scoop: C:\projects\scoop
11-
scoop_home: C:\projects\scoop
1210
matrix:
1311
- PowerShell: 5
14-
- PowerShell: 6
15-
cache:
16-
- '%USERPROFILE%\Documents\WindowsPowerShell\Modules -> appveyor.yml, test\bin\*.ps1'
17-
- C:\projects\helpers -> appveyor.yml, test\bin\*.ps1
12+
- PowerShell: 7
1813
matrix:
1914
fast_finish: true
2015
for:
2116
- matrix:
2217
only:
2318
- PowerShell: 5
19+
cache:
20+
- '%USERPROFILE%\Documents\WindowsPowerShell\Modules -> appveyor.yml, test\bin\*.ps1'
21+
- C:\projects\helpers -> appveyor.yml, test\bin\*.ps1
2422
install:
25-
- ps: . "$env:SCOOP_HOME\test\bin\init.ps1"
23+
- ps: .\test\bin\init.ps1
2624
test_script:
27-
- ps: . "$env:SCOOP_HOME\test\bin\test.ps1" -TestPath "$env:APPVEYOR_BUILD_FOLDER"
25+
- ps: .\test\bin\test.ps1
2826
- matrix:
2927
only:
30-
- PowerShell: 6
28+
- PowerShell: 7
29+
cache:
30+
- '%USERPROFILE%\Documents\PowerShell\Modules -> appveyor.yml, test\bin\*.ps1'
31+
- C:\projects\helpers -> appveyor.yml, test\bin\*.ps1
3132
install:
32-
- pwsh: . "$env:SCOOP_HOME\test\bin\init.ps1"
33+
- pwsh: .\test\bin\init.ps1
3334
test_script:
34-
- pwsh: . "$env:SCOOP_HOME\test\bin\test.ps1" -TestPath "$env:APPVEYOR_BUILD_FOLDER"
35+
- pwsh: .\test\bin\test.ps1

test/Import-Bucket-Tests.ps1

Lines changed: 113 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,128 @@
1-
[CmdletBinding()]
1+
#Requires -Version 5.0
2+
#Requires -Modules @{ ModuleName = 'Pester'; RequiredVersion = '4.10.1' }
23
param(
34
[ValidateNotNullOrEmpty()]
45
[String]
5-
$repo_dir = (Get-Item $MyInvocation.PSScriptRoot).FullName
6+
$repo_dir = (Split-Path -Path $MyInvocation.PSCommandPath -Parent)
67
)
78

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"
1212

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+
}
1419

20+
# Tests for non manifest files
21+
$repo_files = @(Get-ChildItem -Path $repo_dir -File -Recurse)
1522
$project_file_exclusions = @(
1623
'[\\/]\.git[\\/]',
1724
'.sublime-workspace$',
1825
'.DS_Store$'
1926
)
27+
. "$PSScriptRoot\Import-File-Tests.ps1"
2028

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+
}
26127
}
27128

28-
. "$psscriptroot\Import-File-Tests.ps1"
29-
. "$psscriptroot\Scoop-Manifest.Tests.ps1" -bucketdir $bucketdir

test/Scoop-Alias.Tests.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Describe 'add_alias' -Tag 'Scoop' {
3232
}
3333
}
3434

35-
Describe 'rm_alias' {
35+
Describe 'rm_alias' -Tag 'Scoop' {
3636
Mock shimdir { 'TestDrive:\shim' }
3737
Mock set_config { }
3838
Mock get_config { @{} }

test/Scoop-Linting.Tests.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
$repo_dir = (Get-Item $MyInvocation.MyCommand.Path).directory.parent.FullName
22

3-
Describe -Tag 'Linter' 'PSScriptAnalyzer' {
3+
Describe 'PSScriptAnalyzer' -Tag 'Linter' {
44
$scoop_modules = Get-ChildItem $repo_dir -Recurse -Include *.psd1, *.psm1, *.ps1
55
$scoop_modules = $scoop_modules | Where-Object { $_.DirectoryName -notlike '*\supporting*' -and $_.DirectoryName -notlike '*\test*' }
66
$scoop_modules = $scoop_modules | Select-Object -ExpandProperty Directory -Unique

test/Scoop-Manifest.Tests.ps1

Lines changed: 0 additions & 97 deletions
This file was deleted.

test/bin/init.ps1

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,6 @@
1+
#Requires -Version 5.0
12
Write-Host "PowerShell: $($PSVersionTable.PSVersion)"
23
Write-Host (7z.exe | Select-String -Pattern '7-Zip').ToString()
3-
Write-Host 'Install dependencies ...'
4+
Write-Host 'Install testsuite dependencies ...'
45
Install-Module -Repository PSGallery -Scope CurrentUser -Force -Name Pester -RequiredVersion 4.10.1 -SkipPublisherCheck
56
Install-Module -Repository PSGallery -Scope CurrentUser -Force -Name PSScriptAnalyzer, BuildHelpers
6-
7-
if ($env:CI -eq $true) {
8-
Write-Host "Load 'BuildHelpers' environment variables ..."
9-
Set-BuildEnvironment -Force
10-
}
11-
12-
$buildVariables = ( Get-ChildItem -Path 'Env:' ).Where( { $_.Name -match '^(?:BH|CI(?:_|$)|APPVEYOR)' } )
13-
$buildVariables += ( Get-Variable -Name 'CI_*' -Scope 'Script' )
14-
$details = $buildVariables |
15-
Where-Object -FilterScript { $_.Name -notmatch 'EMAIL' } |
16-
Sort-Object -Property 'Name' |
17-
Format-Table -AutoSize -Property 'Name', 'Value' |
18-
Out-String
19-
Write-Host 'CI variables:'
20-
Write-Host $details -ForegroundColor DarkGray

0 commit comments

Comments
 (0)