Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 54 additions & 59 deletions test/powershell/engine/Basic/DefaultCommands.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,19 @@
# Licensed under the MIT License.
Describe "Verify approved aliases list" -Tags "CI" {
BeforeAll {
function ConvertTo-Hashtable {
[CmdletBinding()]
param ([Parameter(ValueFromPipeline=$true)][psobject]$o)
PROCESS {
$pNames = $o.psobject.properties.name
$ht = @{}
foreach($pName in $pNames) {
$ht[$pName] = $o.$pName
}
$ht
}
}

$FullCLR = !$IsCoreCLR
$CoreWindows = $IsCoreCLR -and $IsWindows
$CoreUnix = $IsCoreCLR -and !$IsWindows
Expand Down Expand Up @@ -368,8 +381,8 @@ Describe "Verify approved aliases list" -Tags "CI" {
"Cmdlet", "New-FileCatalog", "", $($FullCLR -or $CoreWindows ), "", "", "Medium"
"Cmdlet", "New-GUID", "", $( $CoreWindows -or $CoreUnix), "", "", "None"
"Cmdlet", "New-Item", "", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", "Medium"
"Cmdlet", "New-ItemProperty", "", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", "None"
"Cmdlet", "New-Module", "", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", "Medium"
"Cmdlet", "New-ItemProperty", "", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", "Medium"
"Cmdlet", "New-Module", "", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", "None"
"Cmdlet", "New-ModuleManifest", "", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", "Low"
"Cmdlet", "New-Object", "", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", "None"
"Cmdlet", "New-PSDrive", "", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", "Low"
Expand All @@ -387,7 +400,7 @@ Describe "Verify approved aliases list" -Tags "CI" {
"Cmdlet", "New-WSManInstance", "", $($FullCLR -or $CoreWindows ), "", "", "None"
"Cmdlet", "New-WSManSessionOption", "", $($FullCLR -or $CoreWindows ), "", "", "None"
"Cmdlet", "Out-Default", "", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", "None"
"Cmdlet", "Out-File", "", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", "None"
"Cmdlet", "Out-File", "", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", "Medium"
"Cmdlet", "Out-GridView", "", $($FullCLR -or $CoreWindows ), "", "", "None"
"Cmdlet", "Out-Host", "", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", "None"
"Cmdlet", "Out-LineOutput", "", $($FullCLR ), "", "", ""
Expand Down Expand Up @@ -458,7 +471,7 @@ Describe "Verify approved aliases list" -Tags "CI" {
"Cmdlet", "Set-Service", "", $($FullCLR -or $CoreWindows ), "", "", "Medium"
"Cmdlet", "Set-StrictMode", "", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", "None"
"Cmdlet", "Set-TimeZone", "", $($FullCLR -or $CoreWindows ), "", "", "Medium"
"Cmdlet", "Set-TraceSource", "", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", "Medium"
"Cmdlet", "Set-TraceSource", "", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", "None"
"Cmdlet", "Set-Variable", "", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", "Medium"
"Cmdlet", "Set-WmiInstance", "", $($FullCLR ), "", "", ""
"Cmdlet", "Set-WSManInstance", "", $($FullCLR -or $CoreWindows ), "", "", "None"
Expand All @@ -474,7 +487,7 @@ Describe "Verify approved aliases list" -Tags "CI" {
"Cmdlet", "Start-Service", "", $($FullCLR -or $CoreWindows ), "", "", "Medium"
"Cmdlet", "Start-Sleep", "", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", "None"
"Cmdlet", "Start-Transaction", "", $($FullCLR ), "", "", ""
"Cmdlet", "Start-Transcript", "", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", "None"
"Cmdlet", "Start-Transcript", "", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", "Medium"
"Cmdlet", "Stop-Computer", "", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", "Medium"
"Cmdlet", "Stop-Job", "", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", "Medium"
"Cmdlet", "Stop-Process", "", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", "Medium"
Expand All @@ -483,7 +496,7 @@ Describe "Verify approved aliases list" -Tags "CI" {
"Cmdlet", "Suspend-Job", "", $($FullCLR ), "", "", ""
"Cmdlet", "Suspend-Service", "", $($FullCLR -or $CoreWindows ), "", "", "Medium"
"Cmdlet", "Switch-Process", "", $( $CoreUnix), "", "", "None"
"Cmdlet", "Tee-Object", "", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", "Medium"
"Cmdlet", "Tee-Object", "", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", "None"
"Cmdlet", "Test-Connection", "", $( $CoreWindows -or $CoreUnix), "", "", "None"
"Cmdlet", "Test-ComputerSecureChannel", "", $($FullCLR ), "", "", ""
"Cmdlet", "Test-FileCatalog", "", $($FullCLR -or $CoreWindows ), "", "", "Medium"
Expand Down Expand Up @@ -519,9 +532,18 @@ Describe "Verify approved aliases list" -Tags "CI" {
"Cmdlet", "Write-Warning", "", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", "None"
"@

# We control only default engine aliases (Source -eq "") and aliases from following default loaded modules
# We control only default engine aliases (Source -eq "") and aliases from following default loaded modules "
# We control only default engine Cmdlets (Source -eq "") and Cmdlets from following default loaded modules
$moduleList = @("Microsoft.PowerShell.Utility", "Microsoft.PowerShell.Management", "Microsoft.PowerShell.Security", "Microsoft.PowerShell.Host", "Microsoft.PowerShell.Diagnostics", "Microsoft.WSMan.Management", "Microsoft.PowerShell.Core", "CimCmdlets")
$moduleList = @(
"Microsoft.PowerShell.Utility",
"Microsoft.PowerShell.Management",
"Microsoft.PowerShell.Security",
"Microsoft.PowerShell.Host",
"Microsoft.PowerShell.Diagnostics",
"Microsoft.WSMan.Management",
"Microsoft.PowerShell.Core",
"CimCmdlets"
)
$getAliases = {
param($moduleList)

Expand Down Expand Up @@ -557,7 +579,12 @@ Describe "Verify approved aliases list" -Tags "CI" {
}

$commandList = $commandString | ConvertFrom-Csv -Delimiter ","
$commandHashTableList = $commandList.Where({$_.Present -eq "True" -and $_.CommandType -eq "Cmdlet"}) | ConvertTo-Hashtable

$aliasFullList = $commandList | Where-Object { $_.Present -eq "True" -and $_.CommandType -eq "Alias" }

$AllScopeOption = [System.Management.Automation.ScopedItemOptions]::AllScope
$ReadOnlyOption = [System.Management.Automation.ScopedItemOptions]::ReadOnly
}

AfterAll {
Expand All @@ -567,68 +594,36 @@ Describe "Verify approved aliases list" -Tags "CI" {
}

It "All approved aliases present (no new aliases added, no aliases removed)" {
$currentDisplayNameAliasList = $currentAliasList | Select-Object -ExpandProperty DisplayName
$aliasDisplayNameAliasList = $aliasFullList | ForEach-Object { "{0} -> {1}" -f $_.Name, $_.Definition}

$result = Compare-Object -ReferenceObject $currentDisplayNameAliasList -DifferenceObject $aliasDisplayNameAliasList

# Below 'Should Be' don't show full list wrong aliases so we output them explicitly
# if all aliases is Ok we output nothing
$result | Write-Host
$result | Should -BeNullOrEmpty
$observedAliases = $currentAliasList.ForEach({"{0}:{1}" -f $_.Name,$_.Definition}) | Sort-Object
$expectedAliases = $aliasFullList.ForEach({"{0}:{1}" -f $_.Name, $_.Definition}) | Sort-Object
$observedAliases | Should -Be $expectedAliases
}

It "All approved aliases have the correct 'AllScope' option" {
$aliasAllScopeOptionList = $aliasFullList | Where-Object { $_.AllScopeOption -eq "AllScope"} | ForEach-Object { "{0} -> {1}" -f $_.Name, $_.Definition}
$currentAllScopeOptionList = $currentAliasList | Where-Object { $_.Options -band [System.Management.Automation.ScopedItemOptions]::AllScope } | Select-Object -ExpandProperty DisplayName

$result = Compare-Object -ReferenceObject $currentAllScopeOptionList -DifferenceObject $aliasAllScopeOptionList

# Below 'Should Be' don't show full list wrong aliases so we output them explicitly
# if all aliases is Ok we output nothing
$result | Write-Host
$result | Should -BeNullOrEmpty
$expectedAllScopeAliases = $aliasFullList.Where({$_.AllScopeOption -eq "AllScope"}).ForEach({"{0}:{1}" -f $_.Name, $_.Definition}) | Sort-Object
$observedAllScopeAliases = $currentAliasList.Where({($_.Options -as [System.Management.Automation.ScopedItemOptions]) -band $AllScopeOption}).Foreach({"{0}:{1}" -f $_.Name, $_.Definition}) | Sort-Object
$observedAllScopeAliases | Should -Be $expectedAllScopeAliases
}

It "All approved aliases have the correct 'ReadOnly' option" {
$aliasReadOnlyOptionList = $aliasFullList | Where-Object { $_.ReadOnlyOption -eq "ReadOnly"} | ForEach-Object { "{0} -> {1}" -f $_.Name, $_.Definition}
$currentReadOnlyOptionList = $currentAliasList | Where-Object { $_.Options -band [System.Management.Automation.ScopedItemOptions]::ReadOnly } | Select-Object -ExpandProperty DisplayName

$result = Compare-Object -ReferenceObject $currentReadOnlyOptionList -DifferenceObject $aliasReadOnlyOptionList

# Below 'Should Be' don't show full list wrong aliases so we output them explicitly
# if all aliases is Ok we output nothing
$result | Write-Host
$result | Should -BeNullOrEmpty
$expectedReadOnlyAliases = $aliasFullList.Where({$_.ReadOnlyOption -eq "ReadOnly"}).ForEach({"{0}:{1}" -f $_.Name, $_.Definition}) | Sort-Object
$observedReadOnlyAliases = $currentAliasList.Where({($_.Options -as [System.Management.Automation.ScopedItemOptions]) -band $ReadOnlyOption}).ForEach({"{0}:{1}" -f $_.Name, $_.Definition}) | Sort-Object
$observedReadOnlyAliases | Should -Be $expectedReadOnlyAliases
}

It "All approved Cmdlets present (no new Cmdlets added, no Cmdlets removed)" {
$cmdletList = $commandList | Where-Object { $_.Present -eq "True" -and $_.CommandType -eq "Cmdlet" } | Select-Object -ExpandProperty Name

$result = (Compare-Object -ReferenceObject $currentCmdletList.Name -DifferenceObject $cmdletList).InputObject
$result | Should -BeNullOrEmpty
$expectedCmdletList = $commandList.Where({$_.Present -eq "True" -and $_.CommandType -eq "Cmdlet"}).Name | Sort-Object
$observedCmdletList = $currentCmdletList.Name | Sort-Object
$observedCmdletList | Should -Be $expectedCmdletList
}

It "All present Cmdlets should have the correct ConfirmImpact" {
$CmdletList = $commandList |
Where-Object { $_.Present -eq "True" -and $_.CommandType -eq "Cmdlet"} |
Select-Object -Property Name, ConfirmImpact

# if Preview, $currentCmdletList is deserialized, so we re-hydrate them so comparison succeeds
$currentCmdletList = $currentCmdletList | ForEach-Object { Get-Command $_.Name } |
Where-Object { $moduleList -contains $_.Source -and $null -ne $_.ImplementingType } |
Select-Object -Property Name, @{
Name = 'ConfirmImpact'
Expression = {
if (($t = $_.ImplementingType)) {
$t.GetCustomAttributes($true).Where{$_.TypeId.Name -eq 'CmdletAttribute'}.ConfirmImpact
}
}
}

# -PassThru is provided to give meaningful output when differences arise
$result = Compare-Object -ReferenceObject $currentCmdletList -DifferenceObject $CmdletList -Property ConfirmImpact -PassThru
$result | Should -BeNullOrEmpty
It "'<Name>' Cmdlet should have the correct ConfirmImpact '<ConfirmImpact>'" -TestCases $commandHashtableList {
param ( $Name, $ConfirmImpact )
# retrieve again because we may have serialized the commandinfo
$cmdlet = Get-Command $Name
$cmdletAttribute = $cmdlet.ImplementingType.GetCustomAttributes($true).Where({$_ -is [System.Management.Automation.CmdletAttribute]})
$impact = $cmdletAttribute.ConfirmImpact
$impact | Should -Be $ConfirmImpact
}
}

Expand Down