Skip to content

Commit 77373f0

Browse files
committed
Add php-src repo/ref overrides for libs workflow tests
1 parent e0e0174 commit 77373f0

3 files changed

Lines changed: 99 additions & 10 deletions

File tree

php/BuildPhp/private/Add-TestRequirements.ps1

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ function Add-TestRequirements {
1212
VS Version
1313
.PARAMETER TestsDirectory
1414
Tests Directory
15+
.PARAMETER SourceRepository
16+
php-src repository to source tests from when SourceRef is provided.
17+
.PARAMETER SourceRef
18+
Optional branch, tag, or SHA in the custom php-src repository.
1519
#>
1620
[OutputType()]
1721
param (
@@ -35,7 +39,11 @@ function Add-TestRequirements {
3539
[string] $TestsDirectory,
3640
[Parameter(Mandatory = $true, Position=5, HelpMessage='Artifacts Directory')]
3741
[ValidateNotNull()]
38-
[string] $ArtifactsDirectory
42+
[string] $ArtifactsDirectory,
43+
[Parameter(Mandatory = $false, Position=6, HelpMessage='php-src repository to source tests from when SourceRef is provided')]
44+
[string] $SourceRepository = 'php/php-src',
45+
[Parameter(Mandatory = $false, Position=7, HelpMessage='Optional branch, tag, or SHA in the custom php-src repository')]
46+
[string] $SourceRef = ''
3947
)
4048
begin {
4149
}
@@ -55,6 +63,7 @@ function Add-TestRequirements {
5563

5664
$testZipFilePath = Join-Path $ArtifactsDirectory $testZipFile
5765
$testsDirectoryPath = Join-Path $currentDirectory $TestsDirectory
66+
$useCustomSource = -not [string]::IsNullOrWhiteSpace($SourceRef)
5867

5968
if(-not(Test-Path $binZipFilePath)) {
6069
Write-Host "Downloading PHP build $binZipFile..."
@@ -67,9 +76,16 @@ function Add-TestRequirements {
6776
}
6877
}
6978

70-
if(-not(Test-Path $testZipFilePath)) {
71-
Write-Host "Downloading PHP test pack $testZipFile..."
72-
Get-PhpTestPack -PhpVersion $PhpVersion -TestsDirectory $TestsDirectory
79+
if($useCustomSource -or -not(Test-Path $testZipFilePath)) {
80+
if($useCustomSource) {
81+
Write-Host "Downloading PHP tests from custom php-src source..."
82+
} else {
83+
Write-Host "Downloading PHP test pack $testZipFile..."
84+
}
85+
Get-PhpTestPack -PhpVersion $PhpVersion `
86+
-TestsDirectory $TestsDirectory `
87+
-SourceRepository $SourceRepository `
88+
-SourceRef $SourceRef
7389
} else {
7490
try {
7591
[System.IO.Compression.ZipFile]::ExtractToDirectory($testZipFilePath, $testsDirectoryPath)

php/BuildPhp/private/Get-PhpTestPack.ps1

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ function Get-PhpTestPack {
66
PHP Version
77
.PARAMETER TestsDirectory
88
Tests Directory
9+
.PARAMETER SourceRepository
10+
php-src repository to source tests from when SourceRef is provided.
11+
.PARAMETER SourceRef
12+
Optional branch, tag, or SHA in the custom php-src repository.
913
#>
1014
[OutputType()]
1115
param (
@@ -15,14 +19,70 @@ function Get-PhpTestPack {
1519
[string] $PhpVersion,
1620
[Parameter(Mandatory = $false, Position=1, HelpMessage='Tests Directory')]
1721
[ValidateLength(1, [int]::MaxValue)]
18-
[string] $TestsDirectory
22+
[string] $TestsDirectory,
23+
[Parameter(Mandatory = $false, Position=2, HelpMessage='php-src repository to source tests from when SourceRef is provided')]
24+
[string] $SourceRepository = 'php/php-src',
25+
[Parameter(Mandatory = $false, Position=3, HelpMessage='Optional branch, tag, or SHA in the custom php-src repository')]
26+
[string] $SourceRef = ''
1927
)
2028
begin {
2129
}
2230
process {
2331
Add-Type -Assembly "System.IO.Compression.Filesystem"
2432

2533
$versionInUrl = $PhpVersion
34+
$currentDirectory = (Get-Location).Path
35+
$testsDirectoryPath = Join-Path $currentDirectory $TestsDirectory
36+
$useCustomSource = -not [string]::IsNullOrWhiteSpace($SourceRef)
37+
38+
if($useCustomSource) {
39+
if([string]::IsNullOrWhiteSpace($SourceRepository)) {
40+
throw "SourceRepository must be provided to source tests from a custom php-src archive."
41+
}
42+
43+
$sourceZipFile = ("php-src-tests-{0}-{1}.zip" -f `
44+
($SourceRepository -replace '[\\/]', '-'), `
45+
($SourceRef -replace '[^0-9A-Za-z._-]', '-'))
46+
$sourceZipPath = Join-Path $currentDirectory $sourceZipFile
47+
$extractRoot = Join-Path $currentDirectory ("php-src-tests-" + [System.Guid]::NewGuid().ToString())
48+
$sourceUrl = "https://api.github.com/repos/$SourceRepository/zipball/$([System.Uri]::EscapeDataString($SourceRef))"
49+
$headers = @{
50+
'User-Agent' = 'php-windows-builder'
51+
'X-GitHub-Api-Version' = '2022-11-28'
52+
}
53+
54+
if($env:GITHUB_TOKEN) {
55+
$headers['Authorization'] = 'Bearer ' + $env:GITHUB_TOKEN
56+
} else {
57+
Write-Warning 'GITHUB_TOKEN not set. API rate limits may apply when downloading custom php-src tests.'
58+
}
59+
60+
Write-Host "Downloading PHP tests from $SourceRepository@$SourceRef..."
61+
Invoke-WebRequest -Uri $sourceUrl -Headers $headers -OutFile $sourceZipPath -UseBasicParsing
62+
63+
New-Item -Path $extractRoot -ItemType "directory" -Force > $null 2>&1
64+
try {
65+
try {
66+
[System.IO.Compression.ZipFile]::ExtractToDirectory($sourceZipPath, $extractRoot)
67+
} catch {
68+
7z x $sourceZipPath "-o$extractRoot" -y | Out-Null
69+
}
70+
71+
$sourceRoots = @(
72+
Get-ChildItem -Path $extractRoot -Directory
73+
)
74+
if($sourceRoots.Count -ne 1) {
75+
throw "Expected a single root directory in custom php-src archive, found $($sourceRoots.Count)."
76+
}
77+
78+
Move-Item -Path $sourceRoots[0].FullName -Destination $testsDirectoryPath
79+
} finally {
80+
Remove-Item -Path $extractRoot -Recurse -Force -ErrorAction Ignore
81+
}
82+
83+
return
84+
}
85+
2686
if($PhpVersion -eq 'master') {
2787
$fallbackBaseUrl = $baseUrl = "https://github.com/shivammathur/php-builder-windows/releases/download/master"
2888
$versionInUrl = "master"
@@ -45,9 +105,7 @@ function Get-PhpTestPack {
45105
}
46106
}
47107

48-
$currentDirectory = (Get-Location).Path
49108
$testZipFilePath = Join-Path $currentDirectory $testZipFile
50-
$testsDirectoryPath = Join-Path $currentDirectory $TestsDirectory
51109

52110
try {
53111
[System.IO.Compression.ZipFile]::ExtractToDirectory($testZipFilePath, $testsDirectoryPath)
@@ -57,4 +115,4 @@ function Get-PhpTestPack {
57115
}
58116
end {
59117
}
60-
}
118+
}

php/BuildPhp/public/Invoke-PhpTests.ps1

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ function Invoke-PhpTests {
1212
Specify Cache
1313
.PARAMETER TestType
1414
Test Type
15+
.PARAMETER SourceRepository
16+
php-src repository to source tests from when SourceRef is provided.
17+
.PARAMETER SourceRef
18+
Optional branch, tag, or SHA in the custom php-src repository.
1519
#>
1620
[OutputType()]
1721
param (
@@ -32,7 +36,11 @@ function Invoke-PhpTests {
3236
[string] $Opcache,
3337
[Parameter(Mandatory = $true, Position=4, HelpMessage='Test Type')]
3438
[ValidateSet('ext', 'php')]
35-
[string] $TestType
39+
[string] $TestType,
40+
[Parameter(Mandatory = $false, Position=5, HelpMessage='php-src repository to source tests from when SourceRef is provided')]
41+
[string] $SourceRepository = 'php/php-src',
42+
[Parameter(Mandatory = $false, Position=6, HelpMessage='Optional branch, tag, or SHA in the custom php-src repository')]
43+
[string] $SourceRef = ''
3644
)
3745
begin {
3846
}
@@ -57,7 +65,14 @@ function Invoke-PhpTests {
5765

5866
Set-Location "$buildDirectory"
5967

60-
Add-TestRequirements -PhpVersion $PhpVersion -Arch $Arch -Ts $Ts -VsVersion $VsData.vs -TestsDirectory $testsDirectory -ArtifactsDirectory $currentDirectory
68+
Add-TestRequirements -PhpVersion $PhpVersion `
69+
-Arch $Arch `
70+
-Ts $Ts `
71+
-VsVersion $VsData.vs `
72+
-TestsDirectory $testsDirectory `
73+
-ArtifactsDirectory $currentDirectory `
74+
-SourceRepository $SourceRepository `
75+
-SourceRef $SourceRef
6176

6277
Set-PhpIniForTests -BuildDirectory $buildDirectory -Opcache $Opcache
6378

0 commit comments

Comments
 (0)