forked from PowerShell/PSScriptAnalyzer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRuleDocumentation.tests.ps1
More file actions
58 lines (50 loc) · 2.71 KB
/
RuleDocumentation.tests.ps1
File metadata and controls
58 lines (50 loc) · 2.71 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
58
Describe "Validate rule documentation files" {
BeforeAll {
$ruleDocDirectory = Join-Path $PSScriptRoot '../../RuleDocumentation'
$docs = Get-ChildItem $ruleDocDirectory/*.md -Exclude README.md |
ForEach-Object { "PS" + $_.BaseName} | Sort-Object
$rules = Get-ScriptAnalyzerRule | ForEach-Object RuleName | Sort-Object
$readmeLinks = @{}
$readmeRules = Get-Content -LiteralPath $ruleDocDirectory/README.md |
Foreach-Object { if ($_ -match '^\s*\|\s*\[([^]]+)\]\(([^)]+)\)\s*\|') {
$ruleName = $matches[1] -replace '<sup>.</sup>$', ''
$readmeLinks["$ruleName"] = $matches[2]
"PS${ruleName}"
}} |
Sort-Object
# Remove rules from the diff list that aren't supported on old PS version
if ($PSVersionTable.PSVersion.Major -eq 4) {
$docs = $docs | Where-Object {$_ -notmatch '^PSAvoidGlobalAliases$'}
$readmeRules = $readmeRules | Where-Object { $_ -notmatch '^PSAvoidGlobalAliases$' }
}
$rulesDocsDiff = Compare-Object -ReferenceObject $rules -DifferenceObject $docs -SyncWindow 25
$rulesReadmeDiff = Compare-Object -ReferenceObject $rules -DifferenceObject $readmeRules -SyncWindow 25
}
It "Every rule must have a rule documentation file" {
$rulesDocsDiff | Where-Object SideIndicator -eq "<=" | Foreach-Object InputObject | Should -BeNullOrEmpty
}
It "Every rule documentation file must have a corresponding rule" {
$rulesDocsDiff | Where-Object SideIndicator -eq "=>" | Foreach-Object InputObject | Should -BeNullOrEmpty
}
It "Every rule must have an entry in the rule documentation README.md file" {
$rulesReadmeDiff | Where-Object SideIndicator -eq "<=" | Foreach-Object InputObject | Should -BeNullOrEmpty
}
It "Every entry in the rule documentation README.md file must correspond to a rule" {
$rulesReadmeDiff | Where-Object SideIndicator -eq "=>" | Foreach-Object InputObject | Should -BeNullOrEmpty
}
It "Every entry in the rule documentation README.md file must have a valid link to the documentation file" {
foreach ($key in $readmeLinks.Keys) {
$link = $readmeLinks[$key]
$filePath = Join-Path $ruleDocDirectory $link
$filePath | Should -Exist
}
}
It "Every rule name in the rule documentation README.md file must match the documentation file's basename" {
foreach ($key in $readmeLinks.Keys) {
$link = $readmeLinks[$key]
$filePath = Join-Path $ruleDocDirectory $link
$fileName = Split-Path $filePath -Leaf
$fileName | Should -BeExactly "${key}.md"
}
}
}