From 2014ce66c17a02dceb2291eccf1297c602295976 Mon Sep 17 00:00:00 2001 From: xtqqczze <45661989+xtqqczze@users.noreply.github.com> Date: Mon, 3 Feb 2020 17:44:10 +0000 Subject: [PATCH] Set `StrictMode` version `3.0` (#11563) --- .../InstallPSCorePolicyDefinitions.ps1 | 2 +- build.psm1 | 89 +++++++++++-------- .../RegisterManifest.ps1 | 2 +- .../Install-PowerShellRemoting.ps1 | 4 +- test/powershell/Host/Logging.Tests.ps1 | 2 +- .../TabCompletion/TabCompletion.Tests.ps1 | 2 +- .../Scripting.Classes.BasicParsing.Tests.ps1 | 2 +- .../Interop/DotNet/DotNetInterop.Tests.ps1 | 8 +- .../engine/Help/HelpSystem.Tests.ps1 | 2 +- .../HelpersDebugger/HelpersDebugger.psm1 | 2 +- test/tools/Modules/PSSysLog/PSSysLog.psm1 | 8 +- tools/ci.psm1 | 10 ++- tools/install-powershell.ps1 | 2 +- 13 files changed, 77 insertions(+), 58 deletions(-) diff --git a/assets/GroupPolicy/InstallPSCorePolicyDefinitions.ps1 b/assets/GroupPolicy/InstallPSCorePolicyDefinitions.ps1 index 9634059bec..ee3c725b1b 100644 --- a/assets/GroupPolicy/InstallPSCorePolicyDefinitions.ps1 +++ b/assets/GroupPolicy/InstallPSCorePolicyDefinitions.ps1 @@ -16,7 +16,7 @@ param [ValidateNotNullOrEmpty()] [string] $Path = $PSScriptRoot ) -Set-StrictMode -Version Latest +Set-StrictMode -Version 3.0 $ErrorActionPreference = 'Stop' function Test-Elevated diff --git a/build.psm1 b/build.psm1 index 6e01afea3e..1d2bd93763 100644 --- a/build.psm1 +++ b/build.psm1 @@ -1,9 +1,12 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. +Set-StrictMode -Version 3.0 + # On Unix paths is separated by colon # On Windows paths is separated by semicolon $script:TestModulePathSeparator = [System.IO.Path]::PathSeparator +$script:Options = $null $dotnetCLIChannel = 'release' $dotnetCLIRequiredVersion = $(Get-Content $PSScriptRoot/global.json | ConvertFrom-Json).Sdk.Version @@ -292,7 +295,7 @@ function Start-PSBuild { $PSModuleRestore = $true } - if ($Runtime -eq "linux-arm" -and -not $environment.IsUbuntu) { + if ($Runtime -eq "linux-arm" -and $environment.IsLinux -and -not $environment.IsUbuntu) { throw "Cross compiling for linux-arm is only supported on Ubuntu environment" } @@ -499,32 +502,34 @@ Fix steps: $psVersion = git --git-dir="$PSScriptRoot/.git" describe } - if ($environment.IsRedHatFamily -or $environment.IsDebian) { - # Symbolic links added here do NOT affect packaging as we do not build on Debian. - # add two symbolic links to system shared libraries that libmi.so is dependent on to handle - # platform specific changes. This is the only set of platforms needed for this currently - # as Ubuntu has these specific library files in the platform and macOS builds for itself - # against the correct versions. + if ($environment.IsLinux) { + if ($environment.IsRedHatFamily -or $environment.IsDebian) { + # Symbolic links added here do NOT affect packaging as we do not build on Debian. + # add two symbolic links to system shared libraries that libmi.so is dependent on to handle + # platform specific changes. This is the only set of platforms needed for this currently + # as Ubuntu has these specific library files in the platform and macOS builds for itself + # against the correct versions. - if ($environment.IsDebian10 -or $environment.IsDebian11){ - $sslTarget = "/usr/lib/x86_64-linux-gnu/libssl.so.1.1" - $cryptoTarget = "/usr/lib/x86_64-linux-gnu/libcrypto.so.1.1" - } - elseif ($environment.IsDebian9){ - # NOTE: Debian 8 doesn't need these symlinks - $sslTarget = "/usr/lib/x86_64-linux-gnu/libssl.so.1.0.2" - $cryptoTarget = "/usr/lib/x86_64-linux-gnu/libcrypto.so.1.0.2" - } - else { #IsRedHatFamily - $sslTarget = "/lib64/libssl.so.10" - $cryptoTarget = "/lib64/libcrypto.so.10" - } + if ($environment.IsDebian10 -or $environment.IsDebian11){ + $sslTarget = "/usr/lib/x86_64-linux-gnu/libssl.so.1.1" + $cryptoTarget = "/usr/lib/x86_64-linux-gnu/libcrypto.so.1.1" + } + elseif ($environment.IsDebian9){ + # NOTE: Debian 8 doesn't need these symlinks + $sslTarget = "/usr/lib/x86_64-linux-gnu/libssl.so.1.0.2" + $cryptoTarget = "/usr/lib/x86_64-linux-gnu/libcrypto.so.1.0.2" + } + else { #IsRedHatFamily + $sslTarget = "/lib64/libssl.so.10" + $cryptoTarget = "/lib64/libcrypto.so.10" + } - if ( ! (test-path "$publishPath/libssl.so.1.0.0")) { - $null = New-Item -Force -ItemType SymbolicLink -Target $sslTarget -Path "$publishPath/libssl.so.1.0.0" -ErrorAction Stop - } - if ( ! (test-path "$publishPath/libcrypto.so.1.0.0")) { - $null = New-Item -Force -ItemType SymbolicLink -Target $cryptoTarget -Path "$publishPath/libcrypto.so.1.0.0" -ErrorAction Stop + if ( ! (test-path "$publishPath/libssl.so.1.0.0")) { + $null = New-Item -Force -ItemType SymbolicLink -Target $sslTarget -Path "$publishPath/libssl.so.1.0.0" -ErrorAction Stop + } + if ( ! (test-path "$publishPath/libcrypto.so.1.0.0")) { + $null = New-Item -Force -ItemType SymbolicLink -Target $cryptoTarget -Path "$publishPath/libcrypto.so.1.0.0" -ErrorAction Stop + } } } @@ -579,6 +584,7 @@ Fix steps: function Restore-PSPackage { + [CmdletBinding()] param( [ValidateNotNullOrEmpty()] [Parameter()] @@ -618,7 +624,7 @@ function Restore-PSPackage $RestoreArguments = @("/property:SDKToUse=$sdkToUse", "--verbosity") } - if ($PSCmdlet.MyInvocation.BoundParameters["Verbose"].IsPresent) { + if ($VerbosePreference -eq 'Continue') { $RestoreArguments += "detailed" } else { $RestoreArguments += "quiet" @@ -837,7 +843,7 @@ function Get-PSOptions { $DefaultToNew ) - if(!$script:Options -and $DefaultToNew.IsPresent) + if (!$script:Options -and $DefaultToNew.IsPresent) { return New-PSOptions } @@ -876,7 +882,14 @@ function Get-PesterTag { $fullname = $_.fullname $tok = $err = $null $ast = [System.Management.Automation.Language.Parser]::ParseFile($FullName, [ref]$tok,[ref]$err) - $des = $ast.FindAll({$args[0] -is "System.Management.Automation.Language.CommandAst" -and $args[0].CommandElements[0].Value -eq "Describe"},$true) + $des = $ast.FindAll({ + $args[0] -is [System.Management.Automation.Language.CommandAst] ` + -and $args[0].CommandElements.GetType() -in @( + [System.Management.Automation.Language.StringConstantExpressionAst], + [System.Management.Automation.Language.ExpandableStringExpressionAst] + ) ` + -and $args[0].CommandElements[0].Value -eq "Describe" + }, $true) foreach( $describe in $des) { $elements = $describe.CommandElements $lineno = $elements[0].Extent.StartLineNumber @@ -1079,7 +1092,7 @@ function Start-PSPester { $publishArgs = @{ } # if we are building for Alpine, we must include the runtime as linux-x64 # will not build runnable test tools - if ( $environment.IsAlpine ) { + if ( $environment.IsLinux -and $environment.IsAlpine ) { $publishArgs['runtime'] = 'alpine-x64' } Publish-PSTestTools @publishArgs | ForEach-Object {Write-Host $_} @@ -1615,7 +1628,7 @@ function Install-Dotnet { $curl = Get-Command -Name curl -CommandType Application -TotalCount 1 -ErrorAction Stop # Uninstall all previous dotnet packages - $uninstallScript = if ($environment.IsUbuntu) { + $uninstallScript = if ($environment.IsLinux -and $environment.IsUbuntu) { "dotnet-uninstall-debian-packages.sh" } elseif ($environment.IsMacOS) { "dotnet-uninstall-pkgs.sh" @@ -1687,14 +1700,14 @@ function Start-PSBootstrap { # Note that when it is null, Invoke-Expression (but not &) must be used to interpolate properly $sudo = if (!$NoSudo) { "sudo" } - if ($BuildLinuxArm -and -not $environment.IsUbuntu) { + if ($BuildLinuxArm -and $environment.IsLinux -and -not $environment.IsUbuntu) { Write-Error "Cross compiling for linux-arm is only supported on Ubuntu environment" return } # Install ours and .NET's dependencies $Deps = @() - if ($environment.IsUbuntu) { + if ($environment.IsLinux -and $environment.IsUbuntu) { # Build tools $Deps += "curl", "g++", "cmake", "make" @@ -1724,7 +1737,7 @@ function Start-PSBootstrap { # change the apt frontend back to the original $env:DEBIAN_FRONTEND=$originalDebianFrontEnd } - } elseif ($environment.IsRedHatFamily) { + } elseif ($environment.IsLinux -and $environment.IsRedHatFamily) { # Build tools $Deps += "which", "curl", "gcc-c++", "cmake", "make" @@ -1748,7 +1761,7 @@ function Start-PSBootstrap { Start-NativeExecution { Invoke-Expression "$baseCommand $Deps" } - } elseif ($environment.IsSUSEFamily) { + } elseif ($environment.IsLinux -and $environment.IsSUSEFamily) { # Build tools $Deps += "gcc", "cmake", "make" @@ -1784,7 +1797,7 @@ function Start-PSBootstrap { # Install dependencies # ignore exitcode, because they may be already installed Start-NativeExecution ([ScriptBlock]::Create("$PackageManager install $Deps")) -IgnoreExitcode - } elseif ($environment.IsAlpine) { + } elseif ($environment.IsLinux -and $environment.IsAlpine) { $Deps += 'libunwind', 'libcurl', 'bash', 'cmake', 'clang', 'build-base', 'git', 'curl' Start-NativeExecution { @@ -2105,8 +2118,8 @@ function script:Start-NativeExecution [switch]$IgnoreExitcode, [switch]$VerboseOutputOnError ) - $backupEAP = $script:ErrorActionPreference - $script:ErrorActionPreference = "Continue" + $backupEAP = $ErrorActionPreference + $ErrorActionPreference = "Continue" try { if($VerboseOutputOnError.IsPresent) { @@ -2139,7 +2152,7 @@ function script:Start-NativeExecution throw "Execution of {$sb} failed with exit code $LASTEXITCODE" } } finally { - $script:ErrorActionPreference = $backupEAP + $ErrorActionPreference = $backupEAP } } diff --git a/src/PowerShell.Core.Instrumentation/RegisterManifest.ps1 b/src/PowerShell.Core.Instrumentation/RegisterManifest.ps1 index 44a39ed088..6221691d85 100644 --- a/src/PowerShell.Core.Instrumentation/RegisterManifest.ps1 +++ b/src/PowerShell.Core.Instrumentation/RegisterManifest.ps1 @@ -24,7 +24,7 @@ param [switch] $Unregister ) -Set-StrictMode -Version Latest +Set-StrictMode -Version 3.0 $ErrorActionPreference = 'Stop' function Start-NativeExecution([scriptblock]$sb, [switch]$IgnoreExitcode) diff --git a/src/powershell-native/Install-PowerShellRemoting.ps1 b/src/powershell-native/Install-PowerShellRemoting.ps1 index afdac53df2..5162d1dccb 100644 --- a/src/powershell-native/Install-PowerShellRemoting.ps1 +++ b/src/powershell-native/Install-PowerShellRemoting.ps1 @@ -26,7 +26,7 @@ param $PowerShellHome ) -Set-StrictMode -Version Latest +Set-StrictMode -Version 3.0 if (! ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { @@ -123,7 +123,7 @@ function Install-PluginEndpoint { # Install the plugin # # # ###################### - + if (-not [String]::IsNullOrEmpty($PowerShellHome)) { $targetPsHome = $PowerShellHome diff --git a/test/powershell/Host/Logging.Tests.ps1 b/test/powershell/Host/Logging.Tests.ps1 index 36ff5feb52..56a60f4798 100644 --- a/test/powershell/Host/Logging.Tests.ps1 +++ b/test/powershell/Host/Logging.Tests.ps1 @@ -2,7 +2,7 @@ # Licensed under the MIT License. using namespace System.Text -Set-StrictMode -Version Latest +Set-StrictMode -Version 3.0 $ErrorActionPreference = 'Stop' Import-Module HelpersCommon diff --git a/test/powershell/Host/TabCompletion/TabCompletion.Tests.ps1 b/test/powershell/Host/TabCompletion/TabCompletion.Tests.ps1 index b97516406f..d75510eb1e 100644 --- a/test/powershell/Host/TabCompletion/TabCompletion.Tests.ps1 +++ b/test/powershell/Host/TabCompletion/TabCompletion.Tests.ps1 @@ -131,7 +131,7 @@ Describe "TabCompletion" -Tags CI { } It 'Should complete "Get-Process -Id " with Id and name in tooltip' { - Set-StrictMode -Version latest + Set-StrictMode -Version 3.0 $cmd = 'Get-Process -Id ' [System.Management.Automation.CommandCompletion]$res = TabExpansion2 -inputScript $cmd -cursorColumn $cmd.Length $res.CompletionMatches[0].CompletionText -match '^\d+$' | Should -BeTrue diff --git a/test/powershell/Language/Classes/Scripting.Classes.BasicParsing.Tests.ps1 b/test/powershell/Language/Classes/Scripting.Classes.BasicParsing.Tests.ps1 index a3dae18f94..3641323018 100644 --- a/test/powershell/Language/Classes/Scripting.Classes.BasicParsing.Tests.ps1 +++ b/test/powershell/Language/Classes/Scripting.Classes.BasicParsing.Tests.ps1 @@ -887,7 +887,7 @@ class A [int] GetX([Foo.Bar]$bar) { - Set-StrictMode -Version latest + Set-StrictMode -Version 3.0 return $bar.x } } diff --git a/test/powershell/Language/Interop/DotNet/DotNetInterop.Tests.ps1 b/test/powershell/Language/Interop/DotNet/DotNetInterop.Tests.ps1 index 5a89e65db4..c7d63a1352 100644 --- a/test/powershell/Language/Interop/DotNet/DotNetInterop.Tests.ps1 +++ b/test/powershell/Language/Interop/DotNet/DotNetInterop.Tests.ps1 @@ -164,7 +164,7 @@ namespace DotNetInterop param($Script) try { - Set-StrictMode -Version latest + Set-StrictMode -Version 3.0 & $Script | Should -Be $null } finally { Set-StrictMode -Off @@ -225,7 +225,7 @@ namespace DotNetInterop It "Get access of an indexer that returns ByRef-like type should fail gracefully in strict mode" { try { - Set-StrictMode -Version latest + Set-StrictMode -Version 3.0 { $testObj[1] } | Should -Throw -ErrorId "CannotIndexWithByRefLikeReturnType" } finally { Set-StrictMode -Off @@ -242,7 +242,7 @@ namespace DotNetInterop } Context "Passing value that is implicitly/explicitly castable to ByRef-like parameter in method invocation" { - + BeforeAll { $ps = [powershell]::Create() @@ -302,7 +302,7 @@ namespace DotNetInterop It "Return null for getter access of a CodeProperty that returns a ByRef-like type, even in strict mode" { $result = $ps.AddScript( - 'try { Set-StrictMode -Version latest; $test.TestName } finally { Set-StrictMode -Off }').Invoke() + 'try { Set-StrictMode -Version 3.0; $test.TestName } finally { Set-StrictMode -Off }').Invoke() $ps.Commands.Clear() $result.Count | Should -Be 1 $result[0] | Should -Be $null diff --git a/test/powershell/engine/Help/HelpSystem.Tests.ps1 b/test/powershell/engine/Help/HelpSystem.Tests.ps1 index 79475b8ff6..03ed622ddb 100644 --- a/test/powershell/engine/Help/HelpSystem.Tests.ps1 +++ b/test/powershell/engine/Help/HelpSystem.Tests.ps1 @@ -59,7 +59,7 @@ Describe "Validate that the Help function can Run in strict mode" -Tags @('CI') $help = & { # run in nested scope to keep strict mode from affecting other tests - Set-StrictMode -Version Latest + Set-StrictMode -Version 3.0 Help } # the help function renders the help content as text so just verify that there is content diff --git a/test/tools/Modules/HelpersDebugger/HelpersDebugger.psm1 b/test/tools/Modules/HelpersDebugger/HelpersDebugger.psm1 index 83a8cfd247..2a0f8508bc 100644 --- a/test/tools/Modules/HelpersDebugger/HelpersDebugger.psm1 +++ b/test/tools/Modules/HelpersDebugger/HelpersDebugger.psm1 @@ -5,7 +5,7 @@ trap {throw $_} # Strict mode FTW. -Set-StrictMode -Version Latest +Set-StrictMode -Version 3.0 # Enable explicit export so that there are no surprises with commands exported from the module. Export-ModuleMember diff --git a/test/tools/Modules/PSSysLog/PSSysLog.psm1 b/test/tools/Modules/PSSysLog/PSSysLog.psm1 index 70a7d963f6..92f93ea27e 100644 --- a/test/tools/Modules/PSSysLog/PSSysLog.psm1 +++ b/test/tools/Modules/PSSysLog/PSSysLog.psm1 @@ -1,7 +1,7 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. -Set-StrictMode -Version Latest +Set-StrictMode -Version 3.0 <# os_log notes: @@ -187,7 +187,7 @@ class PSLogItem hidden static [int] GetMonth([string] $value) { - Set-StrictMode -Version Latest + Set-StrictMode -Version 3.0 for ($x = 0; $x -lt [PSLogItem]::monthNames.Count; $x++) { [string] $monthName = [PSLogItem]::monthNames[$x] @@ -201,7 +201,7 @@ class PSLogItem static [PSLogItem] ConvertSysLog([string] $content, [string] $id, [Nullable[DateTime]] $after) { - Set-StrictMode -Version Latest + Set-StrictMode -Version 3.0 <# MMM dd HH:MM:SS machinename id[PID]: (commitid:TID:CHANNEL) [EventName] Message Expecting split to return @@ -339,7 +339,7 @@ class PSLogItem static [object] ConvertOsLog([string] $content, [string] $id, [Nullable[DateTime]] $after) { - Set-StrictMode -Version Latest + Set-StrictMode -Version 3.0 <# Expecting split to return 0: date 2018-02-07 diff --git a/tools/ci.psm1 b/tools/ci.psm1 index 871faf24b6..66df43551e 100644 --- a/tools/ci.psm1 +++ b/tools/ci.psm1 @@ -1,6 +1,8 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. +Set-StrictMode -Version 3.0 + $ErrorActionPreference = 'continue' $repoRoot = Join-Path $PSScriptRoot '..' $script:administratorsGroupSID = "S-1-5-32-544" @@ -479,7 +481,7 @@ function Invoke-CIFinish { $null = $artifacts.Add($package) } - elseif($package -is [pscustomobject] -and $package.msi) + elseif($package -is [pscustomobject] -and $package.psobject.Properties['msi']) { $null = $artifacts.Add($package.msi) $null = $artifacts.Add($package.wixpdb) @@ -497,7 +499,7 @@ function Invoke-CIFinish $packagingTestResult = Invoke-Pester -Script (Join-Path $repoRoot '.\test\packaging\windows\') -PassThru # fail the CI job if the tests failed, or nothing passed - if($packagingTestResult.FailedCount -ne 0 -or !$packagingTestResult.PassedCount) + if(-not $packagingTestResult -is [pscustomobject] -or $packagingTestResult.FailedCount -ne 0 -or $packagingTestResult.PassedCount -eq 0) { throw "Packaging tests failed ($($packagingTestResult.FailedCount) failed/$($packagingTestResult.PassedCount) passed)" } @@ -579,6 +581,10 @@ function Invoke-LinuxTestsCore $testResultsNoSudo = "$PWD/TestResultsNoSudo.xml" $testResultsSudo = "$PWD/TestResultsSudo.xml" $testExcludeTag = $ExcludeTag + 'RequireSudoOnUnix' + $pesterPassThruNoSudoObject = $null + $pesterPassThruSudoObject = $null + $noSudoResultsWithExpFeatures = $null + $sudoResultsWithExpFeatures = $null $noSudoPesterParam = @{ 'BinDir' = $output diff --git a/tools/install-powershell.ps1 b/tools/install-powershell.ps1 index 5eadc64d98..80c9d86dd8 100644 --- a/tools/install-powershell.ps1 +++ b/tools/install-powershell.ps1 @@ -54,7 +54,7 @@ param( [switch] $Preview ) -Set-StrictMode -Version latest +Set-StrictMode -Version 3.0 $ErrorActionPreference = "Stop" $IsLinuxEnv = (Get-Variable -Name "IsLinux" -ErrorAction Ignore) -and $IsLinux