WSL/tools/test/test-setup.ps1
WSL Team 697572d664 Initial open source commit for WSL.
Many Microsoft employees have contributed to the Windows Subsystem for Linux, this commit is the result of their work since 2016.

The entire history of the Windows Subsystem for Linux can't be shared here, but here's an overview of WSL's history after it moved to it own repository in 2021:

Number of commits on the main branch: 2930
Number of contributors: 31

Head over https://github.com/microsoft/WSL/releases for a more detailed history of the features added to WSL since 2021.
2025-05-15 12:09:45 -07:00

128 lines
4.1 KiB
PowerShell

<#
.SYNOPSIS
Runs all WSL tests; optionally sets-up a WSL distribution and environment prior to running the tests.
.PARAMETER Version
The version of WSL to run the tests in.
.PARAMETER DistroPath
Path to a .tar/.tar.gz file of the distro to be imported to run the tests with, if specified.
.PARAMETER DistroName
The name to be given to the imported distro.
.PARAMETER Package
Path to the wsl.msix package to install. Optional.
.PARAMETER UnitTestsPath
Path to the linux/unit_tests directory to copy and install the unit tests. Optional.
.PARAMETER PostInstallCommand
Command to run post-installation and set-up of the WSL distro used for testing, if specified.
.PARAMETER AllowUnsigned
Imports .\private-wsl.cert to the LocalMachine\Root store.
#>
[CmdletBinding()]
Param (
[Parameter(Mandatory = $true)]$Version,
$DistroPath,
$DistroName,
$Package = $null,
$UnitTestsPath = $null,
$PostInstallCommand = $null,
[switch]$AllowUnsigned)
$ErrorActionPreference = "Stop"
Set-StrictMode -Version Latest
function Run {
[CmdletBinding()]
param([ScriptBlock]$cmd)
Invoke-Command -ScriptBlock $cmd
if ($lastexitcode -ne 0) {
throw ("$cmd failed with exit code: " + $lastexitcode)
}
}
if ($Package) {
$installedPackage = Get-AppxPackage MicrosoftCorporationII.WindowsSubsystemforLinux -AllUsers
if ($installedPackage) {
Write-Host "Removing previous package installation"
Remove-AppxPackage $installedPackage -AllUsers
}
$installedMsi = (Get-ItemProperty -Path "HKLM:Software\Microsoft\Windows\CurrentVersion\Lxss\MSI" -Name ProductCode -ErrorAction Ignore)
if ($installedMsi)
{
Write-Host "Removing MSI package: $($installedMsi.ProductCode)"
$MSIArguments = @(
"/norestart"
"/qn"
"/x"
"$($installedMsi.ProductCode)"
)
$exitCode = (Start-Process -Wait "msiexec.exe" -ArgumentList $MSIArguments -NoNewWindow -PassThru).ExitCode
if ($exitCode -Ne 0)
{
Write-Host "Failed to remove package: $exitCode"
exit 1
}
}
Write-Host "Installing package: $Package"
try {
if ($AllowUnsigned)
{
# unfortunately -AllowUnsigned isn't supported on vb so we need to manually import the certificate and trust it.
(Get-AuthenticodeSignature $Package).SignerCertificate | Export-Certificate -FilePath private-wsl.cert | Out-Null
try
{
Import-Certificate -FilePath .\private-wsl.cert -CertStoreLocation Cert:\LocalMachine\Root | Out-Null
}
finally
{
Remove-Item -Path .\private-wsl.cert
}
}
Add-AppxPackage $Package
}
catch {
Write-Host $_
Get-AppPackageLog -All
exit 1
}
}
# Disable OOBE during testing
$UserLxssRegistryPath = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Lxss"
If (-NOT (Test-Path $UserLxssRegistryPath))
{
New-Item -Path $UserLxssRegistryPath -Force | Out-Null
}
New-ItemProperty -Path $UserLxssRegistryPath -Name "OOBEComplete" -Value "1" -PropertyType DWORD -Force | Out-Null
if ($DistroPath)
{
& wsl.exe --unregister "$DistroName" # Ignore non-zero return for this call
Run { wsl.exe --import "$DistroName" "$env:LocalAppData\lxss" "$DistroPath" --version "$Version" }
Run { wsl.exe --set-default "$DistroName" }
}
if ($UnitTestsPath) {
# get wslpath to unit tests
$WslUnitTestPath = Run { wsl.exe --exec wslpath "$UnitTestsPath" }
# set-up the folder structure for the tests and copy them into the linux distro
Run { wsl.exe --exec bash -c "umask 0; mkdir -p /data;" }
Run { wsl.exe --exec bash -c "umask 0; cp -r $WslUnitTestPath /data/test" }
Run { wsl.exe --exec bash -c "umask 0; mkdir -p /data/test/log" }
# ensure that /etc/fstab exists for the unit tests that expect it
Run { wsl.exe --exec bash -c "( [ -e `"/etc/fstab`" ] || touch `"/etc/fstab`" )" }
}
if ($PostInstallCommand) {
Run { wsl.exe "$PostInstallCommand" }
}