forked from PowerShell/PSScriptAnalyzer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUseCompatibleCmdlets.tests.ps1
More file actions
57 lines (49 loc) · 2.05 KB
/
UseCompatibleCmdlets.tests.ps1
File metadata and controls
57 lines (49 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
$ruleName = "PSUseCompatibleCmdlets"
$directory = Split-Path $MyInvocation.MyCommand.Path -Parent
$testRootDirectory = Split-Path -Parent $directory
$ruleTestDirectory = Join-Path $directory 'UseCompatibleCmdlets'
Import-Module (Join-Path $testRootDirectory 'PSScriptAnalyzerTestHelper.psm1')
Describe "UseCompatibleCmdlets" {
Context "script has violation" {
It "detects violation" {
$violationFilePath = Join-Path $ruleTestDirectory 'ScriptWithViolation.ps1'
$settingsFilePath = [System.IO.Path]::Combine($ruleTestDirectory, 'PSScriptAnalyzerSettings.psd1');
$diagnosticRecords = Invoke-ScriptAnalyzer -Path $violationFilePath -IncludeRule $ruleName -Settings $settingsFilePath
$diagnosticRecords.Count | Should -Be 1
}
}
Function Test-Command
{
param (
[Parameter(ValueFromPipeline)]
$command,
$settings,
$expectedViolations
)
process
{
It ("found {0} violations for '{1}'" -f $expectedViolations, $command) {
Invoke-ScriptAnalyzer -ScriptDefinition $command -IncludeRule $ruleName -Settings $settings | `
Get-Count | `
Should -Be $expectedViolations
}
}
}
$settings = @{rules=@{PSUseCompatibleCmdlets=@{compatibility=@("core-6.0.2-windows")}}}
Context "Microsoft.PowerShell.Core" {
@('Enter-PSSession', 'Foreach-Object', 'Get-Command') | `
Test-Command -Settings $settings -ExpectedViolations 0
}
Context "Non-builtin commands" {
@('get-foo', 'get-bar', 'get-baz') | `
Test-Command -Settings $settings -ExpectedViolations 0
}
Context "Aliases" {
@('where', 'select', 'cd') | `
Test-Command -Settings $settings -ExpectedViolations 0
}
Context "Commands present in reference platform but not in target platform" {
@("Start-VM", "New-SmbShare", "Get-Disk") | `
Test-Command -Settings $settings -ExpectedViolations 1
}
}