Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 2014ce6

Browse files
authoredFeb 3, 2020
Set StrictMode version 3.0 (PowerShell#11563)
1 parent a34d0f3 commit 2014ce6

File tree

13 files changed

+78
-59
lines changed

13 files changed

+78
-59
lines changed
 

‎assets/GroupPolicy/InstallPSCorePolicyDefinitions.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ param
1616
[ValidateNotNullOrEmpty()]
1717
[string] $Path = $PSScriptRoot
1818
)
19-
Set-StrictMode -Version Latest
19+
Set-StrictMode -Version 3.0
2020
$ErrorActionPreference = 'Stop'
2121

2222
function Test-Elevated

‎build.psm1

Lines changed: 52 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
# Copyright (c) Microsoft Corporation. All rights reserved.
22
# Licensed under the MIT License.
33

4+
Set-StrictMode -Version 3.0
5+
46
# On Unix paths is separated by colon
57
# On Windows paths is separated by semicolon
68
$script:TestModulePathSeparator = [System.IO.Path]::PathSeparator
9+
$script:Options = $null
710

811
$dotnetCLIChannel = 'release'
912
$dotnetCLIRequiredVersion = $(Get-Content $PSScriptRoot/global.json | ConvertFrom-Json).Sdk.Version
@@ -292,7 +295,7 @@ function Start-PSBuild {
292295
$PSModuleRestore = $true
293296
}
294297

295-
if ($Runtime -eq "linux-arm" -and -not $environment.IsUbuntu) {
298+
if ($Runtime -eq "linux-arm" -and $environment.IsLinux -and -not $environment.IsUbuntu) {
296299
throw "Cross compiling for linux-arm is only supported on Ubuntu environment"
297300
}
298301

@@ -499,32 +502,34 @@ Fix steps:
499502
$psVersion = git --git-dir="$PSScriptRoot/.git" describe
500503
}
501504

502-
if ($environment.IsRedHatFamily -or $environment.IsDebian) {
503-
# Symbolic links added here do NOT affect packaging as we do not build on Debian.
504-
# add two symbolic links to system shared libraries that libmi.so is dependent on to handle
505-
# platform specific changes. This is the only set of platforms needed for this currently
506-
# as Ubuntu has these specific library files in the platform and macOS builds for itself
507-
# against the correct versions.
508-
509-
if ($environment.IsDebian10 -or $environment.IsDebian11){
510-
$sslTarget = "/usr/lib/x86_64-linux-gnu/libssl.so.1.1"
511-
$cryptoTarget = "/usr/lib/x86_64-linux-gnu/libcrypto.so.1.1"
512-
}
513-
elseif ($environment.IsDebian9){
514-
# NOTE: Debian 8 doesn't need these symlinks
515-
$sslTarget = "/usr/lib/x86_64-linux-gnu/libssl.so.1.0.2"
516-
$cryptoTarget = "/usr/lib/x86_64-linux-gnu/libcrypto.so.1.0.2"
517-
}
518-
else { #IsRedHatFamily
519-
$sslTarget = "/lib64/libssl.so.10"
520-
$cryptoTarget = "/lib64/libcrypto.so.10"
521-
}
505+
if ($environment.IsLinux) {
506+
if ($environment.IsRedHatFamily -or $environment.IsDebian) {
507+
# Symbolic links added here do NOT affect packaging as we do not build on Debian.
508+
# add two symbolic links to system shared libraries that libmi.so is dependent on to handle
509+
# platform specific changes. This is the only set of platforms needed for this currently
510+
# as Ubuntu has these specific library files in the platform and macOS builds for itself
511+
# against the correct versions.
512+
513+
if ($environment.IsDebian10 -or $environment.IsDebian11){
514+
$sslTarget = "/usr/lib/x86_64-linux-gnu/libssl.so.1.1"
515+
$cryptoTarget = "/usr/lib/x86_64-linux-gnu/libcrypto.so.1.1"
516+
}
517+
elseif ($environment.IsDebian9){
518+
# NOTE: Debian 8 doesn't need these symlinks
519+
$sslTarget = "/usr/lib/x86_64-linux-gnu/libssl.so.1.0.2"
520+
$cryptoTarget = "/usr/lib/x86_64-linux-gnu/libcrypto.so.1.0.2"
521+
}
522+
else { #IsRedHatFamily
523+
$sslTarget = "/lib64/libssl.so.10"
524+
$cryptoTarget = "/lib64/libcrypto.so.10"
525+
}
522526

523-
if ( ! (test-path "$publishPath/libssl.so.1.0.0")) {
524-
$null = New-Item -Force -ItemType SymbolicLink -Target $sslTarget -Path "$publishPath/libssl.so.1.0.0" -ErrorAction Stop
525-
}
526-
if ( ! (test-path "$publishPath/libcrypto.so.1.0.0")) {
527-
$null = New-Item -Force -ItemType SymbolicLink -Target $cryptoTarget -Path "$publishPath/libcrypto.so.1.0.0" -ErrorAction Stop
527+
if ( ! (test-path "$publishPath/libssl.so.1.0.0")) {
528+
$null = New-Item -Force -ItemType SymbolicLink -Target $sslTarget -Path "$publishPath/libssl.so.1.0.0" -ErrorAction Stop
529+
}
530+
if ( ! (test-path "$publishPath/libcrypto.so.1.0.0")) {
531+
$null = New-Item -Force -ItemType SymbolicLink -Target $cryptoTarget -Path "$publishPath/libcrypto.so.1.0.0" -ErrorAction Stop
532+
}
528533
}
529534
}
530535

@@ -579,6 +584,7 @@ Fix steps:
579584

580585
function Restore-PSPackage
581586
{
587+
[CmdletBinding()]
582588
param(
583589
[ValidateNotNullOrEmpty()]
584590
[Parameter()]
@@ -618,7 +624,7 @@ function Restore-PSPackage
618624
$RestoreArguments = @("/property:SDKToUse=$sdkToUse", "--verbosity")
619625
}
620626

621-
if ($PSCmdlet.MyInvocation.BoundParameters["Verbose"].IsPresent) {
627+
if ($VerbosePreference -eq 'Continue') {
622628
$RestoreArguments += "detailed"
623629
} else {
624630
$RestoreArguments += "quiet"
@@ -837,7 +843,7 @@ function Get-PSOptions {
837843
$DefaultToNew
838844
)
839845

840-
if(!$script:Options -and $DefaultToNew.IsPresent)
846+
if (!$script:Options -and $DefaultToNew.IsPresent)
841847
{
842848
return New-PSOptions
843849
}
@@ -876,7 +882,14 @@ function Get-PesterTag {
876882
$fullname = $_.fullname
877883
$tok = $err = $null
878884
$ast = [System.Management.Automation.Language.Parser]::ParseFile($FullName, [ref]$tok,[ref]$err)
879-
$des = $ast.FindAll({$args[0] -is "System.Management.Automation.Language.CommandAst" -and $args[0].CommandElements[0].Value -eq "Describe"},$true)
885+
$des = $ast.FindAll({
886+
$args[0] -is [System.Management.Automation.Language.CommandAst] `
887+
-and $args[0].CommandElements.GetType() -in @(
888+
[System.Management.Automation.Language.StringConstantExpressionAst],
889+
[System.Management.Automation.Language.ExpandableStringExpressionAst]
890+
) `
891+
-and $args[0].CommandElements[0].Value -eq "Describe"
892+
}, $true)
880893
foreach( $describe in $des) {
881894
$elements = $describe.CommandElements
882895
$lineno = $elements[0].Extent.StartLineNumber
@@ -1079,7 +1092,7 @@ function Start-PSPester {
10791092
$publishArgs = @{ }
10801093
# if we are building for Alpine, we must include the runtime as linux-x64
10811094
# will not build runnable test tools
1082-
if ( $environment.IsAlpine ) {
1095+
if ( $environment.IsLinux -and $environment.IsAlpine ) {
10831096
$publishArgs['runtime'] = 'alpine-x64'
10841097
}
10851098
Publish-PSTestTools @publishArgs | ForEach-Object {Write-Host $_}
@@ -1615,7 +1628,7 @@ function Install-Dotnet {
16151628
$curl = Get-Command -Name curl -CommandType Application -TotalCount 1 -ErrorAction Stop
16161629

16171630
# Uninstall all previous dotnet packages
1618-
$uninstallScript = if ($environment.IsUbuntu) {
1631+
$uninstallScript = if ($environment.IsLinux -and $environment.IsUbuntu) {
16191632
"dotnet-uninstall-debian-packages.sh"
16201633
} elseif ($environment.IsMacOS) {
16211634
"dotnet-uninstall-pkgs.sh"
@@ -1687,14 +1700,14 @@ function Start-PSBootstrap {
16871700
# Note that when it is null, Invoke-Expression (but not &) must be used to interpolate properly
16881701
$sudo = if (!$NoSudo) { "sudo" }
16891702

1690-
if ($BuildLinuxArm -and -not $environment.IsUbuntu) {
1703+
if ($BuildLinuxArm -and $environment.IsLinux -and -not $environment.IsUbuntu) {
16911704
Write-Error "Cross compiling for linux-arm is only supported on Ubuntu environment"
16921705
return
16931706
}
16941707

16951708
# Install ours and .NET's dependencies
16961709
$Deps = @()
1697-
if ($environment.IsUbuntu) {
1710+
if ($environment.IsLinux -and $environment.IsUbuntu) {
16981711
# Build tools
16991712
$Deps += "curl", "g++", "cmake", "make"
17001713

@@ -1724,7 +1737,7 @@ function Start-PSBootstrap {
17241737
# change the apt frontend back to the original
17251738
$env:DEBIAN_FRONTEND=$originalDebianFrontEnd
17261739
}
1727-
} elseif ($environment.IsRedHatFamily) {
1740+
} elseif ($environment.IsLinux -and $environment.IsRedHatFamily) {
17281741
# Build tools
17291742
$Deps += "which", "curl", "gcc-c++", "cmake", "make"
17301743

@@ -1748,7 +1761,7 @@ function Start-PSBootstrap {
17481761
Start-NativeExecution {
17491762
Invoke-Expression "$baseCommand $Deps"
17501763
}
1751-
} elseif ($environment.IsSUSEFamily) {
1764+
} elseif ($environment.IsLinux -and $environment.IsSUSEFamily) {
17521765
# Build tools
17531766
$Deps += "gcc", "cmake", "make"
17541767

@@ -1784,7 +1797,7 @@ function Start-PSBootstrap {
17841797
# Install dependencies
17851798
# ignore exitcode, because they may be already installed
17861799
Start-NativeExecution ([ScriptBlock]::Create("$PackageManager install $Deps")) -IgnoreExitcode
1787-
} elseif ($environment.IsAlpine) {
1800+
} elseif ($environment.IsLinux -and $environment.IsAlpine) {
17881801
$Deps += 'libunwind', 'libcurl', 'bash', 'cmake', 'clang', 'build-base', 'git', 'curl'
17891802

17901803
Start-NativeExecution {
@@ -2105,8 +2118,8 @@ function script:Start-NativeExecution
21052118
[switch]$IgnoreExitcode,
21062119
[switch]$VerboseOutputOnError
21072120
)
2108-
$backupEAP = $script:ErrorActionPreference
2109-
$script:ErrorActionPreference = "Continue"
2121+
$backupEAP = $ErrorActionPreference
2122+
$ErrorActionPreference = "Continue"
21102123
try {
21112124
if($VerboseOutputOnError.IsPresent)
21122125
{
@@ -2139,7 +2152,7 @@ function script:Start-NativeExecution
21392152
throw "Execution of {$sb} failed with exit code $LASTEXITCODE"
21402153
}
21412154
} finally {
2142-
$script:ErrorActionPreference = $backupEAP
2155+
$ErrorActionPreference = $backupEAP
21432156
}
21442157
}
21452158

‎src/PowerShell.Core.Instrumentation/RegisterManifest.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ param
2424

2525
[switch] $Unregister
2626
)
27-
Set-StrictMode -Version Latest
27+
Set-StrictMode -Version 3.0
2828
$ErrorActionPreference = 'Stop'
2929

3030
function Start-NativeExecution([scriptblock]$sb, [switch]$IgnoreExitcode)

‎src/powershell-native/Install-PowerShellRemoting.ps1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ param
2626
$PowerShellHome
2727
)
2828

29-
Set-StrictMode -Version Latest
29+
Set-StrictMode -Version 3.0
3030

3131
if (! ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))
3232
{
@@ -123,7 +123,7 @@ function Install-PluginEndpoint {
123123
# Install the plugin #
124124
# #
125125
######################
126-
126+
127127
if (-not [String]::IsNullOrEmpty($PowerShellHome))
128128
{
129129
$targetPsHome = $PowerShellHome

‎test/powershell/Host/Logging.Tests.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# Licensed under the MIT License.
33
using namespace System.Text
44

5-
Set-StrictMode -Version Latest
5+
Set-StrictMode -Version 3.0
66
$ErrorActionPreference = 'Stop'
77

88
Import-Module HelpersCommon

‎test/powershell/Host/TabCompletion/TabCompletion.Tests.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ Describe "TabCompletion" -Tags CI {
131131
}
132132

133133
It 'Should complete "Get-Process -Id " with Id and name in tooltip' {
134-
Set-StrictMode -Version latest
134+
Set-StrictMode -Version 3.0
135135
$cmd = 'Get-Process -Id '
136136
[System.Management.Automation.CommandCompletion]$res = TabExpansion2 -inputScript $cmd -cursorColumn $cmd.Length
137137
$res.CompletionMatches[0].CompletionText -match '^\d+$' | Should -BeTrue

‎test/powershell/Language/Classes/Scripting.Classes.BasicParsing.Tests.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -887,7 +887,7 @@ class A
887887
888888
[int] GetX([Foo.Bar]$bar)
889889
{
890-
Set-StrictMode -Version latest
890+
Set-StrictMode -Version 3.0
891891
return $bar.x
892892
}
893893
}

‎test/powershell/Language/Interop/DotNet/DotNetInterop.Tests.ps1

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ namespace DotNetInterop
164164
param($Script)
165165

166166
try {
167-
Set-StrictMode -Version latest
167+
Set-StrictMode -Version 3.0
168168
& $Script | Should -Be $null
169169
} finally {
170170
Set-StrictMode -Off
@@ -225,7 +225,7 @@ namespace DotNetInterop
225225

226226
It "Get access of an indexer that returns ByRef-like type should fail gracefully in strict mode" {
227227
try {
228-
Set-StrictMode -Version latest
228+
Set-StrictMode -Version 3.0
229229
{ $testObj[1] } | Should -Throw -ErrorId "CannotIndexWithByRefLikeReturnType"
230230
} finally {
231231
Set-StrictMode -Off
@@ -242,7 +242,7 @@ namespace DotNetInterop
242242
}
243243

244244
Context "Passing value that is implicitly/explicitly castable to ByRef-like parameter in method invocation" {
245-
245+
246246
BeforeAll {
247247
$ps = [powershell]::Create()
248248

@@ -302,7 +302,7 @@ namespace DotNetInterop
302302

303303
It "Return null for getter access of a CodeProperty that returns a ByRef-like type, even in strict mode" {
304304
$result = $ps.AddScript(
305-
'try { Set-StrictMode -Version latest; $test.TestName } finally { Set-StrictMode -Off }').Invoke()
305+
'try { Set-StrictMode -Version 3.0; $test.TestName } finally { Set-StrictMode -Off }').Invoke()
306306
$ps.Commands.Clear()
307307
$result.Count | Should -Be 1
308308
$result[0] | Should -Be $null

‎test/powershell/engine/Help/HelpSystem.Tests.ps1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ Describe "Validate that the Help function can Run in strict mode" -Tags @('CI')
5959

6060
$help = & {
6161
# run in nested scope to keep strict mode from affecting other tests
62-
Set-StrictMode -Version Latest
62+
Set-StrictMode -Version 3.0
6363
Help
6464
}
6565
# the help function renders the help content as text so just verify that there is content

‎test/tools/Modules/HelpersDebugger/HelpersDebugger.psm1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
trap {throw $_}
66

77
# Strict mode FTW.
8-
Set-StrictMode -Version Latest
8+
Set-StrictMode -Version 3.0
99

1010
# Enable explicit export so that there are no surprises with commands exported from the module.
1111
Export-ModuleMember

‎test/tools/Modules/PSSysLog/PSSysLog.psm1

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Copyright (c) Microsoft Corporation. All rights reserved.
22
# Licensed under the MIT License.
33

4-
Set-StrictMode -Version Latest
4+
Set-StrictMode -Version 3.0
55

66
<#
77
os_log notes:
@@ -187,7 +187,7 @@ class PSLogItem
187187

188188
hidden static [int] GetMonth([string] $value)
189189
{
190-
Set-StrictMode -Version Latest
190+
Set-StrictMode -Version 3.0
191191
for ($x = 0; $x -lt [PSLogItem]::monthNames.Count; $x++)
192192
{
193193
[string] $monthName = [PSLogItem]::monthNames[$x]
@@ -201,7 +201,7 @@ class PSLogItem
201201

202202
static [PSLogItem] ConvertSysLog([string] $content, [string] $id, [Nullable[DateTime]] $after)
203203
{
204-
Set-StrictMode -Version Latest
204+
Set-StrictMode -Version 3.0
205205
<#
206206
MMM dd HH:MM:SS machinename id[PID]: (commitid:TID:CHANNEL) [EventName] Message
207207
Expecting split to return
@@ -339,7 +339,7 @@ class PSLogItem
339339

340340
static [object] ConvertOsLog([string] $content, [string] $id, [Nullable[DateTime]] $after)
341341
{
342-
Set-StrictMode -Version Latest
342+
Set-StrictMode -Version 3.0
343343
<#
344344
Expecting split to return
345345
0: date 2018-02-07

‎tools/ci.psm1

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Copyright (c) Microsoft Corporation. All rights reserved.
22
# Licensed under the MIT License.
33

4+
Set-StrictMode -Version 3.0
5+
46
$ErrorActionPreference = 'continue'
57
$repoRoot = Join-Path $PSScriptRoot '..'
68
$script:administratorsGroupSID = "S-1-5-32-544"
@@ -479,7 +481,7 @@ function Invoke-CIFinish
479481
{
480482
$null = $artifacts.Add($package)
481483
}
482-
elseif($package -is [pscustomobject] -and $package.msi)
484+
elseif($package -is [pscustomobject] -and $package.psobject.Properties['msi'])
483485
{
484486
$null = $artifacts.Add($package.msi)
485487
$null = $artifacts.Add($package.wixpdb)
@@ -497,7 +499,7 @@ function Invoke-CIFinish
497499
$packagingTestResult = Invoke-Pester -Script (Join-Path $repoRoot '.\test\packaging\windows\') -PassThru
498500

499501
# fail the CI job if the tests failed, or nothing passed
500-
if($packagingTestResult.FailedCount -ne 0 -or !$packagingTestResult.PassedCount)
502+
if(-not $packagingTestResult -is [pscustomobject] -or $packagingTestResult.FailedCount -ne 0 -or $packagingTestResult.PassedCount -eq 0)
501503
{
502504
throw "Packaging tests failed ($($packagingTestResult.FailedCount) failed/$($packagingTestResult.PassedCount) passed)"
503505
}
@@ -579,6 +581,10 @@ function Invoke-LinuxTestsCore
579581
$testResultsNoSudo = "$PWD/TestResultsNoSudo.xml"
580582
$testResultsSudo = "$PWD/TestResultsSudo.xml"
581583
$testExcludeTag = $ExcludeTag + 'RequireSudoOnUnix'
584+
$pesterPassThruNoSudoObject = $null
585+
$pesterPassThruSudoObject = $null
586+
$noSudoResultsWithExpFeatures = $null
587+
$sudoResultsWithExpFeatures = $null
582588

583589
$noSudoPesterParam = @{
584590
'BinDir' = $output

0 commit comments

Comments
 (0)
Please sign in to comment.