-
Notifications
You must be signed in to change notification settings - Fork 382
/
Copy pathReviewUnusedParameter.tests.ps1
71 lines (61 loc) · 3.44 KB
/
ReviewUnusedParameter.tests.ps1
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
Describe "ReviewUnusedParameter" {
BeforeAll {
$RuleName = 'PSReviewUnusedParameter'
$RuleSeverity = "Warning"
}
Context "When there are violations" {
It "has 1 violation - function with 1 unused parameter" {
$ScriptDefinition = 'function BadFunc1 { param ($Param1, $Param2) $Param1}'
$Violations = Invoke-ScriptAnalyzer -ScriptDefinition $ScriptDefinition -IncludeRule $RuleName
$Violations.Count | Should -Be 1
}
It "has 2 violations - function with 2 unused parameters" {
$ScriptDefinition = 'function BadFunc1 { param ($Param1, $Param2) }'
$Violations = Invoke-ScriptAnalyzer -ScriptDefinition $ScriptDefinition -IncludeRule $RuleName
$Violations.Count | Should -Be 2
}
It "has 1 violation - scriptblock with 1 unused parameter" {
$ScriptDefinition = '{ param ($Param1) }'
$Violations = Invoke-ScriptAnalyzer -ScriptDefinition $ScriptDefinition -IncludeRule $RuleName
$Violations.Count | Should -Be 1
}
It "doesn't traverse scriptblock scope" {
$ScriptDefinition = '{ param ($Param1) }; { $Param1 }'
$Violations = Invoke-ScriptAnalyzer -ScriptDefinition $ScriptDefinition -IncludeRule $RuleName
$Violations.Count | Should -Be 1
}
It "violations have correct rule and severity" {
$ScriptDefinition = 'function BadFunc1 { param ($Param1, $Param2) $Param1}'
$Violations = Invoke-ScriptAnalyzer -ScriptDefinition $ScriptDefinition -IncludeRule $RuleName
$Violations.Severity | Select-Object -Unique | Should -Be $RuleSeverity
$Violations.RuleName | Select-Object -Unique | Should -Be $RuleName
}
}
Context "When there are no violations" {
It "has no violations - function that uses all parameters" {
$ScriptDefinition = 'function GoodFunc1 { param ($Param1, $Param2) $Param1; $Param2}'
$Violations = Invoke-ScriptAnalyzer -ScriptDefinition $ScriptDefinition -IncludeRule $RuleName
$Violations.Count | Should -Be 0
}
It "has no violations - function with splatting" {
$ScriptDefinition = 'function GoodFunc1 { param ($Param1) $Splat = @{InputObject = $Param1}}'
$Violations = Invoke-ScriptAnalyzer -ScriptDefinition $ScriptDefinition -IncludeRule $RuleName
$Violations.Count | Should -Be 0
}
It "has no violations when using PSBoundParameters" {
$ScriptDefinition = 'function Bound { param ($Param1) Get-Foo @PSBoundParameters }'
$Violations = Invoke-ScriptAnalyzer -ScriptDefinition $ScriptDefinition -IncludeRule $RuleName
$Violations.Count | Should -Be 0
}
It "has no violations when parameter is called in child scope" -skip {
$ScriptDefinition = 'function foo { param ($Param1) function Child { $Param1 } }'
$Violations = Invoke-ScriptAnalyzer -ScriptDefinition $ScriptDefinition -IncludeRule $RuleName
$Violations.Count | Should -Be 0
}
It "has no violations when case of parameter and variable usage do not match" -skip {
$ScriptDefinition = 'function foo { param ($Param1, $param2) $param1; $Param2}'
$Violations = Invoke-ScriptAnalyzer -ScriptDefinition $ScriptDefinition -IncludeRule $RuleName
$Violations.Count | Should -Be 0
}
}
}