mirror of
https://github.com/PowerShell/PowerShell
synced 2026-06-08 12:12:50 +00:00
* Capitalize 'Should' command and fix whitespace ```powershell $_ -ireplace '\s?\|\s?should\b',' | Should' ``` * Capitalise and apply hyphen to 'Not' parameter ```powershell $_ -ireplace '(\| Should) not\b','$1 -Not' ``` * Capitalise and apply hyphen to 'Be' parameter ```powershell $_ -ireplace '(\| Should(?: -Not)?) -?be\b','$1 -Be' ``` * Capitalise and apply hyphen to 'BeExactly' parameter $_ -ireplace '(\| Should(?: -Not)?) -?beexactly\b','$1 -BeExactly' * Capitalise and apply hyphen to 'BeGreaterThan' parameter ```powershell $_ -ireplace '(\| Should(?: -Not)?) -?begreaterthan\b','$1 -BeGreaterThan' ``` * Use 'BeTrue' parameter ```powershell $_ -ireplace '(\| Should(?: -Not)?) -Be\s\$?true\b','$1 -BeTrue' ``` * Use 'BeFalse' parameter ```powershell $_ -ireplace '(\| Should(?: -Not)?) -Be\s\$?false\b','$1 -BeFalse' ``` * Capitalise and apply hyphen to 'Match' parameter ```powershell $_ -ireplace '(\| Should(?: -Not)?)\s-?match\b','$1 -Match' ``` * Capitalise and apply hyphen to 'Throw' parameter ```powershell $_ -ireplace '(\| Should(?: -Not)?)\s-?throw\b','$1 -Throw' ``` * Capitalise and apply hyphen to 'BeNullOrEmpty' parameter ```powershell $_ -ireplace '(\| Should(?: -Not)?)\s-?benullorempty\b','$1 -BeNullOrEmpty' ``` * Capitalise 'Because' parameter ```powershell $_ -ireplace '\s-because\b',' -Because' ``` * Fix 'BeNullOrEmpty'
76 lines
2.9 KiB
PowerShell
76 lines
2.9 KiB
PowerShell
# Copyright (c) Microsoft Corporation. All rights reserved.
|
|
# Licensed under the MIT License.
|
|
Describe "Test restricted language check method on scriptblocks" -Tags "CI" {
|
|
BeforeAll {
|
|
set-strictmode -v 2
|
|
function list {
|
|
|
|
$l = [System.Collections.Generic.List[String]]::new()
|
|
$args | ForEach-Object {$l.Add($_)}
|
|
, $l
|
|
}
|
|
}
|
|
AfterAll {
|
|
Set-StrictMode -Off
|
|
}
|
|
|
|
It 'Check basic expressions' {
|
|
|
|
{{2+2}.CheckRestrictedLanguage($null, $null, $false) } | Should -Not -Throw # Succeed with no variables
|
|
}
|
|
|
|
It 'Check default variables' {
|
|
|
|
{{ $PSCulture, $PSUICulture, $true, $false, $null}.CheckRestrictedLanguage($null, $null, $false) } | Should -Not -Throw
|
|
}
|
|
|
|
It 'Check default variables' {
|
|
{ {2+$a}.CheckRestrictedLanguage($null, $null, $false) } | Should -Throw -ErrorId 'ParseException'
|
|
}
|
|
|
|
It 'Check union of default + one allowed variables' {
|
|
|
|
{ { 2 + $a }.CheckRestrictedLanguage($null, (list a), $false) } | Should -Not -Throw # succeed
|
|
}
|
|
|
|
It 'Check union of default + two allowed variables' {
|
|
|
|
{ { $a + $b }.CheckRestrictedLanguage($null, (list a b), $false) } | Should -Not -Throw # succeed
|
|
}
|
|
|
|
It 'Check union of default + allowed variables' {
|
|
|
|
{ { $PSCulture, $PSUICulture, $true, $false, $null, $a, $b}.CheckRestrictedLanguage($null, (list a b), $false) } | Should -Not -Throw
|
|
}
|
|
|
|
It 'Check union of default + one disallowed variables' {
|
|
{ { $a + $b + $c }.CheckRestrictedLanguage($null, (list a b), $false) } | Should -Throw -ErrorId 'ParseException'
|
|
}
|
|
|
|
It 'Check union of default + one allowed variable and but not allow environment variable' {
|
|
{ { 2 + $a + $env:foo }.CheckRestrictedLanguage($null, (list a), $false) } | Should -Throw -ErrorId 'ParseException'
|
|
}
|
|
|
|
It 'Check union of default + one allowed variable name and allow environment variable ' {
|
|
|
|
{{ 2 + $a + $env:foo }.CheckRestrictedLanguage($null, (list a), $true)} | Should -Not -Throw # succeed
|
|
}
|
|
|
|
It 'Check that wildcard allows env even if the flag is set to false' {
|
|
|
|
{ { 2 + $a + $b + $c + $env:foo }.CheckRestrictedLanguage($null, (list *), $false)} | Should -Not -Throw # succeed
|
|
}
|
|
|
|
It 'Check for restricted commands' {
|
|
{ {get-date}.CheckRestrictedLangauge($null, $null, $false) } | Should -Throw -ErrorId 'MethodNotFound'
|
|
}
|
|
|
|
It 'Check for allowed commands and variables' {
|
|
|
|
{ { get-process | where name -Match $pattern | foreach $prop }.CheckRestrictedLanguage(
|
|
(list get-process where foreach),
|
|
(list prop pattern)
|
|
, $false) } | Should -Not -Throw
|
|
}
|
|
}
|