Set StrictMode version 3.0 (#11563)

This commit is contained in:
xtqqczze 2020-02-03 17:44:10 +00:00 committed by GitHub
parent a34d0f3e80
commit 2014ce66c1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 77 additions and 58 deletions

View File

@ -16,7 +16,7 @@ param
[ValidateNotNullOrEmpty()]
[string] $Path = $PSScriptRoot
)
Set-StrictMode -Version Latest
Set-StrictMode -Version 3.0
$ErrorActionPreference = 'Stop'
function Test-Elevated

View File

@ -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
}
}

View File

@ -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)

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -887,7 +887,7 @@ class A
[int] GetX([Foo.Bar]$bar)
{
Set-StrictMode -Version latest
Set-StrictMode -Version 3.0
return $bar.x
}
}

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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