2020-03-25 02:08:37 +08:00
|
|
|
# Copyright (c) Microsoft Corporation.
|
2018-04-03 01:47:29 +08:00
|
|
|
# Licensed under the MIT License.
|
|
|
|
|
2020-05-23 21:24:53 +08:00
|
|
|
Describe -Name "Windows MSI" -Fixture {
|
|
|
|
BeforeAll {
|
|
|
|
function Test-Elevated {
|
|
|
|
[CmdletBinding()]
|
|
|
|
[OutputType([bool])]
|
|
|
|
Param()
|
2018-04-03 01:47:29 +08:00
|
|
|
|
2020-05-23 21:24:53 +08:00
|
|
|
# if the current Powershell session was called with administrator privileges,
|
|
|
|
# the Administrator Group's well-known SID will show up in the Groups for the current identity.
|
|
|
|
# Note that the SID won't show up unless the process is elevated.
|
|
|
|
return (([Security.Principal.WindowsIdentity]::GetCurrent()).Groups -contains "S-1-5-32-544")
|
|
|
|
}
|
2018-04-03 01:47:29 +08:00
|
|
|
|
2020-05-23 21:24:53 +08:00
|
|
|
function Invoke-Msiexec {
|
|
|
|
param(
|
|
|
|
[Parameter(ParameterSetName = 'Install', Mandatory)]
|
|
|
|
[Switch]$Install,
|
2018-04-03 01:47:29 +08:00
|
|
|
|
2020-05-23 21:24:53 +08:00
|
|
|
[Parameter(ParameterSetName = 'Uninstall', Mandatory)]
|
|
|
|
[Switch]$Uninstall,
|
2018-04-03 01:47:29 +08:00
|
|
|
|
2020-05-23 21:24:53 +08:00
|
|
|
[Parameter(Mandatory)]
|
|
|
|
[ValidateScript({Test-Path -Path $_})]
|
|
|
|
[String]$MsiPath,
|
2018-04-03 01:47:29 +08:00
|
|
|
|
2020-05-23 21:24:53 +08:00
|
|
|
[Parameter(ParameterSetName = 'Install')]
|
|
|
|
[HashTable] $Properties
|
2018-04-03 01:47:29 +08:00
|
|
|
|
2020-05-23 21:24:53 +08:00
|
|
|
)
|
|
|
|
$action = "$($PSCmdlet.ParameterSetName)ing"
|
|
|
|
if ($Install.IsPresent) {
|
|
|
|
$switch = '/I'
|
|
|
|
} else {
|
|
|
|
$switch = '/x'
|
|
|
|
}
|
2018-04-03 01:47:29 +08:00
|
|
|
|
2020-05-23 21:24:53 +08:00
|
|
|
$additionalOptions = @()
|
|
|
|
if ($Properties) {
|
|
|
|
foreach ($key in $Properties.Keys) {
|
|
|
|
$additionalOptions += "$key=$($Properties.$key)"
|
|
|
|
}
|
|
|
|
}
|
2018-04-03 01:47:29 +08:00
|
|
|
|
2020-05-23 21:24:53 +08:00
|
|
|
$argumentList = "$switch $MsiPath /quiet /l*vx $msiLog $additionalOptions"
|
|
|
|
$msiExecProcess = Start-Process msiexec.exe -Wait -ArgumentList $argumentList -NoNewWindow -PassThru
|
|
|
|
if ($msiExecProcess.ExitCode -ne 0) {
|
|
|
|
$exitCode = $msiExecProcess.ExitCode
|
|
|
|
throw "$action MSI failed and returned error code $exitCode."
|
|
|
|
}
|
|
|
|
}
|
2018-04-03 01:47:29 +08:00
|
|
|
|
|
|
|
$msiX64Path = $env:PsMsiX64Path
|
2021-02-09 05:25:14 +08:00
|
|
|
$channel = $env:PSMsiChannel
|
|
|
|
$runtime = $env:PSMsiRuntime
|
2018-04-03 01:47:29 +08:00
|
|
|
|
2019-05-11 06:34:27 +08:00
|
|
|
# Get any existing powershell in the path
|
2018-04-03 01:47:29 +08:00
|
|
|
$beforePath = @(([System.Environment]::GetEnvironmentVariable('PATH', 'MACHINE')) -split ';' |
|
|
|
|
Where-Object {$_ -like '*files\powershell*'})
|
|
|
|
|
|
|
|
$msiLog = Join-Path -Path $TestDrive -ChildPath 'msilog.txt'
|
|
|
|
|
|
|
|
foreach ($pathPart in $beforePath) {
|
|
|
|
Write-Warning "Found existing PowerShell path: $pathPart"
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(Test-Elevated)) {
|
|
|
|
Write-Warning "Tests must be elevated"
|
|
|
|
}
|
|
|
|
$uploadedLog = $false
|
|
|
|
}
|
|
|
|
BeforeEach {
|
2020-01-14 03:19:39 +08:00
|
|
|
$error.Clear()
|
2018-04-03 01:47:29 +08:00
|
|
|
}
|
|
|
|
AfterEach {
|
2020-01-14 03:19:39 +08:00
|
|
|
if ($error.Count -ne 0 -and !$uploadedLog) {
|
2019-02-09 02:57:41 +08:00
|
|
|
Copy-Item -Path $msiLog -Destination $env:temp -Force
|
|
|
|
Write-Verbose "MSI log is at $env:temp\msilog.txt" -Verbose
|
2019-05-11 06:34:27 +08:00
|
|
|
$uploadedLog = $true
|
2018-04-03 01:47:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-30 05:13:10 +08:00
|
|
|
Context "Upgrade code" {
|
|
|
|
BeforeAll {
|
2021-02-09 05:25:14 +08:00
|
|
|
Write-Verbose "cr-$channel-$runtime" -Verbose
|
|
|
|
switch ("$channel-$runtime") {
|
|
|
|
"preview-win7-x64" {
|
|
|
|
$msiUpgradeCode = '39243d76-adaf-42b1-94fb-16ecf83237c8'
|
|
|
|
}
|
|
|
|
"stable-win7-x64" {
|
|
|
|
$msiUpgradeCode = '31ab5147-9a97-4452-8443-d9709f0516e1'
|
|
|
|
}
|
|
|
|
"preview-win7-x86" {
|
|
|
|
$msiUpgradeCode = '86abcfbd-1ccc-4a88-b8b2-0facfde29094'
|
|
|
|
}
|
|
|
|
"stable-win7-x86" {
|
|
|
|
$msiUpgradeCode = '1d00683b-0f84-4db8-a64f-2f98ad42fe06'
|
|
|
|
}
|
|
|
|
default {
|
|
|
|
throw "'$_' not a valid channel runtime combination"
|
|
|
|
}
|
|
|
|
}
|
2020-06-30 05:13:10 +08:00
|
|
|
}
|
|
|
|
|
2021-02-09 05:25:14 +08:00
|
|
|
It "$Channel MSI should not be installed before test" -Skip:(!(Test-Elevated)) {
|
|
|
|
$result = @(Get-CimInstance -Query "SELECT Value FROM Win32_Property WHERE Property='UpgradeCode' and Value = '{$msiUpgradeCode}'")
|
|
|
|
$result.Count | Should -Be 0 -Because "Query should return nothing if $channel x64 is not installed"
|
2020-06-30 05:13:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
It "MSI should install without error" -Skip:(!(Test-Elevated)) {
|
|
|
|
{
|
|
|
|
Invoke-MsiExec -Install -MsiPath $msiX64Path -Properties @{ADD_PATH = 1}
|
|
|
|
} | Should -Not -Throw
|
|
|
|
}
|
|
|
|
|
|
|
|
It "Upgrade code should be correct" -Skip:(!(Test-Elevated)) {
|
2021-02-09 05:25:14 +08:00
|
|
|
$result = @(Get-CimInstance -Query "SELECT Value FROM Win32_Property WHERE Property='UpgradeCode' and Value = '{$msiUpgradeCode}'")
|
|
|
|
$result.Count | Should -Be 1 -Because "Query should return 1 result if Upgrade code is for x64 $channel"
|
2020-06-30 05:13:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
It "MSI should uninstall without error" -Skip:(!(Test-Elevated)) {
|
|
|
|
{
|
|
|
|
Invoke-MsiExec -Uninstall -MsiPath $msiX64Path
|
|
|
|
} | Should -Not -Throw
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-03 01:47:29 +08:00
|
|
|
Context "Add Path disabled" {
|
|
|
|
It "MSI should install without error" -Skip:(!(Test-Elevated)) {
|
|
|
|
{
|
|
|
|
Invoke-MsiExec -Install -MsiPath $msiX64Path -Properties @{ADD_PATH = 0}
|
|
|
|
} | Should -Not -Throw
|
|
|
|
}
|
|
|
|
|
|
|
|
It "MSI should have not be updated path" -Skip:(!(Test-Elevated)) {
|
|
|
|
$psPath = ([System.Environment]::GetEnvironmentVariable('PATH', 'MACHINE')) -split ';' |
|
2021-02-09 05:25:14 +08:00
|
|
|
Where-Object { $_ -like '*files\powershell*' -and $_ -notin $beforePath }
|
2018-04-03 01:47:29 +08:00
|
|
|
|
|
|
|
$psPath | Should -BeNullOrEmpty
|
|
|
|
}
|
|
|
|
|
|
|
|
It "MSI should uninstall without error" -Skip:(!(Test-Elevated)) {
|
|
|
|
{
|
|
|
|
Invoke-MsiExec -Uninstall -MsiPath $msiX64Path
|
|
|
|
} | Should -Not -Throw
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Context "Add Path enabled" {
|
|
|
|
It "MSI should install without error" -Skip:(!(Test-Elevated)) {
|
|
|
|
{
|
|
|
|
Invoke-MsiExec -Install -MsiPath $msiX64Path -Properties @{ADD_PATH = 1}
|
|
|
|
} | Should -Not -Throw
|
|
|
|
}
|
|
|
|
|
|
|
|
It "MSI should have updated path" -Skip:(!(Test-Elevated)) {
|
2021-02-09 05:25:14 +08:00
|
|
|
if ($channel -eq 'preview') {
|
|
|
|
$pattern = '*files*\powershell*\preview*'
|
|
|
|
} else {
|
|
|
|
$pattern = '*files*\powershell*'
|
|
|
|
}
|
|
|
|
|
2018-04-03 01:47:29 +08:00
|
|
|
$psPath = ([System.Environment]::GetEnvironmentVariable('PATH', 'MACHINE')) -split ';' |
|
2021-02-09 05:25:14 +08:00
|
|
|
Where-Object { $_ -like $pattern -and $_ -notin $beforePath }
|
|
|
|
|
|
|
|
if (!$psPath) {
|
|
|
|
([System.Environment]::GetEnvironmentVariable('PATH', 'MACHINE')) -split ';' |
|
|
|
|
Where-Object { $_ -notin $beforePath } |
|
|
|
|
ForEach-Object { Write-Verbose -Verbose $_ }
|
|
|
|
}
|
2018-04-03 01:47:29 +08:00
|
|
|
|
|
|
|
$psPath | Should -Not -BeNullOrEmpty
|
|
|
|
}
|
|
|
|
|
|
|
|
It "MSI should uninstall without error" -Skip:(!(Test-Elevated)) {
|
|
|
|
{
|
|
|
|
Invoke-MsiExec -Uninstall -MsiPath $msiX64Path
|
|
|
|
} | Should -Not -Throw
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|