2020-03-24 19:08:37 +01:00
|
|
|
# Copyright (c) Microsoft Corporation.
|
2018-04-02 19:47:29 +02:00
|
|
|
# Licensed under the MIT License.
|
|
|
|
|
2020-05-23 15:24:53 +02:00
|
|
|
Describe -Name "Windows MSI" -Fixture {
|
|
|
|
BeforeAll {
|
|
|
|
function Test-Elevated {
|
|
|
|
[CmdletBinding()]
|
|
|
|
[OutputType([bool])]
|
|
|
|
Param()
|
2018-04-02 19:47:29 +02:00
|
|
|
|
2020-05-23 15:24:53 +02: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-02 19:47:29 +02:00
|
|
|
|
2020-05-23 15:24:53 +02:00
|
|
|
function Invoke-Msiexec {
|
|
|
|
param(
|
|
|
|
[Parameter(ParameterSetName = 'Install', Mandatory)]
|
|
|
|
[Switch]$Install,
|
2018-04-02 19:47:29 +02:00
|
|
|
|
2020-05-23 15:24:53 +02:00
|
|
|
[Parameter(ParameterSetName = 'Uninstall', Mandatory)]
|
|
|
|
[Switch]$Uninstall,
|
2018-04-02 19:47:29 +02:00
|
|
|
|
2020-05-23 15:24:53 +02:00
|
|
|
[Parameter(Mandatory)]
|
|
|
|
[ValidateScript({Test-Path -Path $_})]
|
|
|
|
[String]$MsiPath,
|
2018-04-02 19:47:29 +02:00
|
|
|
|
2020-05-23 15:24:53 +02:00
|
|
|
[Parameter(ParameterSetName = 'Install')]
|
|
|
|
[HashTable] $Properties
|
2018-04-02 19:47:29 +02:00
|
|
|
|
2020-05-23 15:24:53 +02:00
|
|
|
)
|
|
|
|
$action = "$($PSCmdlet.ParameterSetName)ing"
|
|
|
|
if ($Install.IsPresent) {
|
|
|
|
$switch = '/I'
|
|
|
|
} else {
|
|
|
|
$switch = '/x'
|
|
|
|
}
|
2018-04-02 19:47:29 +02:00
|
|
|
|
2020-05-23 15:24:53 +02:00
|
|
|
$additionalOptions = @()
|
|
|
|
if ($Properties) {
|
|
|
|
foreach ($key in $Properties.Keys) {
|
|
|
|
$additionalOptions += "$key=$($Properties.$key)"
|
|
|
|
}
|
|
|
|
}
|
2018-04-02 19:47:29 +02:00
|
|
|
|
2020-05-23 15:24:53 +02: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-02 19:47:29 +02:00
|
|
|
|
|
|
|
$msiX64Path = $env:PsMsiX64Path
|
|
|
|
|
2019-05-11 00:34:27 +02:00
|
|
|
# Get any existing powershell in the path
|
2018-04-02 19:47:29 +02: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-13 20:19:39 +01:00
|
|
|
$error.Clear()
|
2018-04-02 19:47:29 +02:00
|
|
|
}
|
|
|
|
AfterEach {
|
2020-01-13 20:19:39 +01:00
|
|
|
if ($error.Count -ne 0 -and !$uploadedLog) {
|
2019-02-08 19:57:41 +01:00
|
|
|
Copy-Item -Path $msiLog -Destination $env:temp -Force
|
|
|
|
Write-Verbose "MSI log is at $env:temp\msilog.txt" -Verbose
|
2019-05-11 00:34:27 +02:00
|
|
|
$uploadedLog = $true
|
2018-04-02 19:47:29 +02: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 ';' |
|
|
|
|
Where-Object {$_ -like '*files\powershell*' -and $_ -notin $beforePath}
|
|
|
|
|
|
|
|
$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)) {
|
|
|
|
$psPath = ([System.Environment]::GetEnvironmentVariable('PATH', 'MACHINE')) -split ';' |
|
|
|
|
Where-Object {$_ -like '*files\powershell*' -and $_ -notin $beforePath}
|
|
|
|
|
|
|
|
$psPath | Should -Not -BeNullOrEmpty
|
|
|
|
}
|
|
|
|
|
|
|
|
It "MSI should uninstall without error" -Skip:(!(Test-Elevated)) {
|
|
|
|
{
|
|
|
|
Invoke-MsiExec -Uninstall -MsiPath $msiX64Path
|
|
|
|
} | Should -Not -Throw
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|