forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmsi.tests.ps1
More file actions
124 lines (102 loc) · 4.31 KB
/
msi.tests.ps1
File metadata and controls
124 lines (102 loc) · 4.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# Copyright (c) Microsoft Corporation.
# Licensed under the MIT License.
Describe -Name "Windows MSI" -Fixture {
BeforeAll {
function Test-Elevated {
[CmdletBinding()]
[OutputType([bool])]
Param()
# 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")
}
function Invoke-Msiexec {
param(
[Parameter(ParameterSetName = 'Install', Mandatory)]
[Switch]$Install,
[Parameter(ParameterSetName = 'Uninstall', Mandatory)]
[Switch]$Uninstall,
[Parameter(Mandatory)]
[ValidateScript({Test-Path -Path $_})]
[String]$MsiPath,
[Parameter(ParameterSetName = 'Install')]
[HashTable] $Properties
)
$action = "$($PSCmdlet.ParameterSetName)ing"
if ($Install.IsPresent) {
$switch = '/I'
} else {
$switch = '/x'
}
$additionalOptions = @()
if ($Properties) {
foreach ($key in $Properties.Keys) {
$additionalOptions += "$key=$($Properties.$key)"
}
}
$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."
}
}
$msiX64Path = $env:PsMsiX64Path
# Get any existing powershell in the path
$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 {
$error.Clear()
}
AfterEach {
if ($error.Count -ne 0 -and !$uploadedLog) {
Copy-Item -Path $msiLog -Destination $env:temp -Force
Write-Verbose "MSI log is at $env:temp\msilog.txt" -Verbose
$uploadedLog = $true
}
}
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
}
}
}