Skip to content

Commit 480b2e7

Browse files
author
Kapil Borle
committed
Add tests for settings discovery
1 parent 151e8f1 commit 480b2e7

File tree

5 files changed

+41
-0
lines changed

5 files changed

+41
-0
lines changed

Tests/Engine/Settings.tests.ps1

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
if (!(Get-Module PSScriptAnalyzer))
2+
{
3+
Import-Module PSScriptAnalyzer
4+
}
5+
6+
$directory = Split-Path $MyInvocation.MyCommand.Path
7+
Describe "Settings Precedence" {
8+
$settingsTestDirectory = [System.IO.Path]::Combine($directory, "SettingsTest")
9+
$project1Root = [System.IO.Path]::Combine($settingsTestDirectory, "Project1")
10+
$project2Root = [System.IO.Path]::Combine($settingsTestDirectory, "Project2")
11+
Context "settings object is explicit" {
12+
It "runs rules from the explicit setting file" {
13+
$settingsFilepath = [System.IO.Path]::Combine($project1Root, "ExplicitSettings.psd1")
14+
$violations = Invoke-ScriptAnalyzer -Path $project1Root -Settings $settingsFilepath -Recurse
15+
$violations.Count | Should Be 1
16+
$violations[0].RuleName | Should Be "PSAvoidUsingWriteHost"
17+
}
18+
}
19+
Context "settings file is implicit" {
20+
It "runs rules from the implicit setting file" {
21+
$violations = Invoke-ScriptAnalyzer -Path $project1Root -Recurse
22+
$violations.Count | Should Be 1
23+
$violations[0].RuleName | Should Be "PSAvoidUsingCmdletAliases"
24+
}
25+
26+
It "cannot find file if not named PSScriptAnalyzerSettings.psd1" {
27+
$violations = Invoke-ScriptAnalyzer -Path $project2Root -Recurse
28+
$violations.Count | Should Be 2
29+
}
30+
}
31+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@{
2+
"IncludeRules" = @("PSAvoidUsingWriteHost")
3+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@{
2+
"IncludeRules" = @("PSAvoidUsingCmdletAliases")
3+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
gci
2+
Write-Host "Do not use write-host"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
gci
2+
Write-Host "Do not use write-host"

0 commit comments

Comments
 (0)