Run-TestsInPackage.ps1 (4849B)
1 <# 2 .SYNOPSIS 3 Runs the AppInstallerCLI tests within the packaged context. 4 .DESCRIPTION 5 Registers the loose files generated by the AppInstallerCLIPackage project, then runs the 6 existing tests from "within" this context. 7 .PARAMETER BuildRoot 8 The root of the build output directory. If not provided, assumed to be the local default 9 location relative to this script. 10 .PARAMETER PackageRoot 11 The root of the package build output directory. If not provided, assumed to be the local default 12 location relative to this script. 13 .PARAMETER LogTarget 14 The file path to log to. 15 .PARAMETER TestResultsTarget 16 The file path to place the test result file in. 17 .PARAMETER Args 18 Additional args to pass to the tests. 19 .PARAMETER Wait 20 Have the test process wait for user input before exiting. 21 .PARAMETER ScriptWait 22 Have the script wait for the output files to be freed before exiting. 23 #> 24 param( 25 [Parameter(Mandatory=$false)] 26 [string]$BuildRoot, 27 28 [Parameter(Mandatory=$false)] 29 [string]$PackageRoot, 30 31 [Parameter(Mandatory=$false)] 32 [string]$LogTarget, 33 34 [Parameter(Mandatory=$false)] 35 [string]$TestResultsTarget, 36 37 [Parameter(Mandatory=$false)] 38 [string]$Args, 39 40 [switch]$Wait, 41 42 [switch]$ScriptWait 43 ) 44 45 function Wait-ForFileClose([string]$Path) 46 { 47 $Local:FileInfo = [System.IO.FileInfo]::new($Path) 48 $Local:SleepCount = 0 49 $Local:SleepCountMax = 600 50 51 while ($Local:SleepCount -lt $Local:SleepCountMax) 52 { 53 try 54 { 55 [System.IO.FileStream] $Local:Stream = $Local:FileInfo.OpenWrite() 56 $Local:Stream.Dispose() 57 break 58 } 59 catch 60 { 61 Start-Sleep 1 62 $Local:SleepCount = $Local:SleepCount + 1 63 } 64 } 65 66 if ($Local:SleepCount -ge $Local:SleepCountMax) 67 { 68 throw "Timed out waiting for file close: $Path" 69 } 70 } 71 72 if ([String]::IsNullOrEmpty($BuildRoot)) 73 { 74 $BuildRoot = Split-Path -Parent $PSCommandPath; 75 $BuildRoot = Join-Path $BuildRoot "..\x64\Debug"; 76 } 77 $BuildRoot = Resolve-Path $BuildRoot 78 Write-Host "Using BuildRoot = $BuildRoot" 79 80 if ([String]::IsNullOrEmpty($PackageRoot)) 81 { 82 $PackageRoot = Split-Path -Parent $PSCommandPath; 83 $PackageRoot = Join-Path $PackageRoot "..\AppInstallerCLIPackage\bin\x64\Debug"; 84 } 85 $PackageRoot = Resolve-Path $PackageRoot 86 Write-Host "Using PackageRoot = $PackageRoot" 87 88 if (![String]::IsNullOrEmpty($LogTarget)) 89 { 90 $Local:temp = Split-Path -Parent $LogTarget 91 $Local:temp = Resolve-Path $Local:temp 92 $LogTarget = Join-Path $Local:temp (Split-Path -Leaf $LogTarget) 93 Write-Host "Using LogTarget = $LogTarget" 94 } 95 96 if (![String]::IsNullOrEmpty($TestResultsTarget)) 97 { 98 $Local:temp = Split-Path -Parent $TestResultsTarget 99 $Local:temp = Resolve-Path $Local:temp 100 $TestResultsTarget = Join-Path $Local:temp (Split-Path -Leaf $TestResultsTarget) 101 Write-Host "Using TestResultsTarget = $TestResultsTarget" 102 } 103 104 # Register the package; this requires the local package to have been deployed at least once or it won't be built. 105 $Local:ManifestPath = Join-Path $PackageRoot "AppxManifest.xml" 106 if (-not (Test-Path $Local:ManifestPath)) 107 { 108 $Local:ManifestPath = Join-Path $PackageRoot "AppX\AppxManifest.xml" 109 } 110 Write-Host "Registering manifest at path: $Local:ManifestPath" 111 Add-AppxPackage -Register $Local:ManifestPath 112 113 # Execute the tests from within the package's runtime. 114 $Local:TestExePath = Join-Path $BuildRoot "AppInstallerCLITests\AppInstallerCLITests.exe" 115 $Local:TestArgs = $Args 116 117 if ([String]::IsNullOrEmpty($LogTarget)) 118 { 119 $Local:TestArgs = $Local:TestArgs + " -log" 120 } 121 else 122 { 123 $Local:TestArgs = $Local:TestArgs + " -logto ""$LogTarget""" 124 } 125 126 if (![String]::IsNullOrEmpty($TestResultsTarget)) 127 { 128 $Local:TestArgs = $Local:TestArgs + " -s -r junit -o ""$TestResultsTarget""" 129 } 130 131 if ($Wait) 132 { 133 $Local:TestArgs = $Local:TestArgs + " -wait" 134 } 135 136 Write-Host "Executing tests at path: $Local:TestExePath" 137 Write-Host "Executing tests with args: $Local:TestArgs" 138 Invoke-CommandInDesktopPackage -PackageFamilyName WinGetDevCLI_8wekyb3d8bbwe -AppId WinGetDev -Command $Local:TestExePath -Args $Local:TestArgs 139 140 if ($ScriptWait) 141 { 142 try 143 { 144 Write-Host "Waiting for output files to be closed..." 145 Start-Sleep 5 146 if (![String]::IsNullOrEmpty($LogTarget)) 147 { 148 Wait-ForFileClose $LogTarget 149 } 150 151 if (![String]::IsNullOrEmpty($TestResultsTarget)) 152 { 153 Wait-ForFileClose $TestResultsTarget 154 } 155 Write-Host "Done" 156 } 157 finally 158 { 159 Write-Host "Remove registered package" 160 Get-AppxPackage WinGetDevCLI | Remove-AppxPackage 161 } 162 }