sdk_client/scripts/run-wasm-tests.ps1

129 lines
5.6 KiB
PowerShell
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

$ErrorActionPreference = "Stop"
function Set-WasmToolchainEnv {
param()
$clang = $env:CC
if (-not $clang -or -not (Test-Path $clang)) {
$defaultClang = "C:\\Program Files\\LLVM\\bin\\clang.exe"
if (Test-Path $defaultClang) {
$clang = $defaultClang
} else {
$cmd = Get-Command clang.exe -ErrorAction SilentlyContinue
if ($cmd) { $clang = $cmd.Path }
}
}
if (-not $clang) { throw "Clang introuvable. Installez LLVM/Clang et relancez." }
$env:CC = $clang
$llvmBin = Split-Path $clang -Parent
$env:AR = Join-Path $llvmBin "llvm-ar.exe"
$env:NM = Join-Path $llvmBin "llvm-nm.exe"
$env:TARGET_CC = $env:CC
$env:CC_wasm32_unknown_unknown = $env:CC
$env:AR_wasm32_unknown_unknown = $env:AR
$env:NM_wasm32_unknown_unknown = $env:NM
[System.Environment]::SetEnvironmentVariable('CC_wasm32-unknown-unknown', $env:CC, 'Process')
[System.Environment]::SetEnvironmentVariable('AR_wasm32-unknown-unknown', $env:AR, 'Process')
[System.Environment]::SetEnvironmentVariable('NM_wasm32-unknown-unknown', $env:NM, 'Process')
}
function Invoke-WasmPackTests {
param(
[switch]$Chrome,
[switch]$Firefox,
[switch]$Node
)
if ($Chrome) { Ensure-WasmBindgenRunner; wasm-pack test --headless --chrome }
if ($Firefox) { Ensure-WasmBindgenRunner; wasm-pack test --headless --firefox }
if ($Node) {
# Forcer Node comme runner pour wasm-bindgen-test
$node = (Get-Command node.exe -ErrorAction SilentlyContinue).Path
if ($node) { $env:WASM_BINDGEN_TEST_RUNNER = $node } else { $env:WASM_BINDGEN_TEST_RUNNER = "node" }
$env:CARGO_TARGET_WASM32_UNKNOWN_UNKNOWN_RUNNER = "node"
wasm-pack test --node
}
}
$runnerSet = $false
function Ensure-WasmBindgenRunner {
param()
# Cherche un runner dans le cache wasm-pack
$localWp = Join-Path $env:LOCALAPPDATA ".wasm-pack"
$cachedRunner = $null
if (Test-Path $localWp) {
$candidates = Get-ChildItem -Path $localWp -Recurse -Filter "wasm-bindgen-test-runner.exe" -ErrorAction SilentlyContinue | Select-Object -First 1
if ($candidates) { $cachedRunner = $candidates.FullName }
}
if (-not $cachedRunner) {
Write-Host "Aucun runner trouvé. Téléchargement de larchive officielle (tar.gz) pour Windows..." -ForegroundColor Yellow
$wbgVersion = "0.2.100"
$arch = "x86_64-pc-windows-msvc"
$tarName = "wasm-bindgen-$wbgVersion-$arch.tar.gz"
$downloadUrl = "https://github.com/rustwasm/wasm-bindgen/releases/download/$wbgVersion/$tarName"
$destParent = $localWp
$tarPath = Join-Path $env:TEMP $tarName
try {
if (-not (Test-Path $destParent)) { New-Item -ItemType Directory -Force -Path $destParent | Out-Null }
Invoke-WebRequest -Uri $downloadUrl -OutFile $tarPath -UseBasicParsing -ErrorAction Stop
Push-Location $destParent
tar -xzf $tarPath
Pop-Location
} catch {
Write-Warning "Échec du téléchargement/extraction du runner: $($_.Exception.Message)"
} finally {
if (Test-Path $tarPath) { Remove-Item -Force $tarPath }
}
# Recherche récursive du binaire extrait
$found = Get-ChildItem -Path (Join-Path $destParent "wasm-bindgen-$wbgVersion-$arch") -Recurse -Filter "wasm-bindgen-test-runner.exe" -ErrorAction SilentlyContinue | Select-Object -First 1
if ($found) { $cachedRunner = $found.FullName }
}
if ($cachedRunner -and (Test-Path $cachedRunner)) {
$script:runnerSet = $true
$env:WASM_BINDGEN_TEST_RUNNER = $cachedRunner
$runnerDir = Split-Path $cachedRunner -Parent
if ($env:PATH -notlike "*$runnerDir*") { $env:PATH = "$runnerDir;$env:PATH" }
# Force cargo/wasm-pack à utiliser ce runner pour wasm32-unknown-unknown
[System.Environment]::SetEnvironmentVariable('CARGO_TARGET_WASM32_UNKNOWN_UNKNOWN_RUNNER', $cachedRunner, 'Process')
# Copie de secours dans les dossiers cache wasm-pack attendus (hashés)
try {
$wpDirs = Get-ChildItem -Path $localWp -Directory -Filter "wasm-bindgen-*" -ErrorAction SilentlyContinue
foreach ($d in $wpDirs) {
$destRunner = Join-Path $d.FullName "wasm-bindgen-test-runner.exe"
if (-not (Test-Path $destRunner)) {
Copy-Item -Force $cachedRunner $destRunner -ErrorAction SilentlyContinue
}
$wbExeSrc = Join-Path $runnerDir "wasm-bindgen.exe"
$wbExeDst = Join-Path $d.FullName "wasm-bindgen.exe"
if ((Test-Path $wbExeSrc) -and -not (Test-Path $wbExeDst)) {
Copy-Item -Force $wbExeSrc $wbExeDst -ErrorAction SilentlyContinue
}
}
} catch {}
Write-Host "WASM_BINDGEN_TEST_RUNNER défini vers: $cachedRunner" -ForegroundColor Green
return
}
Write-Warning "wasm-bindgen-test-runner introuvable. wasm-pack tentera de le télécharger lors de l'exécution des tests."
}
$scriptsDir = Split-Path -Parent $MyInvocation.MyCommand.Path
$repoRoot = Split-Path -Parent $scriptsDir
Push-Location $repoRoot
try {
Set-WasmToolchainEnv
# Ne préparer le runner binaire que si navigateurs utilisés (Node n'en a pas besoin)
try {
# D'abord Node (plus robuste sur Windows)
Invoke-WasmPackTests -Node
} catch {
Write-Warning "Tests Node échoués, tentative avec navigateurs headless."
Invoke-WasmPackTests -Chrome -Firefox
}
} finally {
Pop-Location
}