forked from vexx32/PSKoans
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKoanValidation.Tests.ps1
126 lines (99 loc) · 4.38 KB
/
KoanValidation.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#Requires -Modules PSKoans
using namespace System.Management.Automation.Language
using namespace System.Collections.Generic
Describe 'Static Analysis: Koan Topics' {
Context 'Individual Topics' {
#region Discovery
$KoanTopics = Resolve-Path "$PSScriptRoot/../PSKoans/Koans" |
Get-ChildItem -Recurse -Filter '*.Koans.ps1' |
ForEach-Object {
$commandInfo = Get-Command -Name $_.FullName -ErrorAction SilentlyContinue
$koanAttribute = $commandInfo.ScriptBlock.Attributes.Where{ $_.TypeID -match 'Koan' }
@{
File = $_
Name = $_.BaseName -replace '\.Koans$'
Position = $koanAttribute.Position
Module = $koanAttribute.Module
}
}
#endregion Discovery
It 'has no syntax errors in <Topic>' -TestCases $KoanTopics {
$File.FullName | Should -Exist
$Errors = $Tokens = $null
[Parser]::ParseFile($file.FullName, [ref]$Tokens, [ref]$Errors) > $null
$Errors.Count | Should -Be 0
}
It 'does not have nested It blocks in <Topic>' -TestCases $KoanTopics {
function Test-ItBlock {
[CmdletBinding()]
param([Ast] $element)
if ($element -isnot [CommandAst]) {
return $false
}
$commandName = $element.GetCommandName()
if ([string]::IsNullOrEmpty($commandName)) {
return $false
}
if ($commandName -ne 'it') {
return $false
}
return $true
}
$Errors = $Tokens = $null
$Ast = [Parser]::ParseFile($file.FullName, [ref]$Tokens, [ref]$Errors)
$ParentItBlocks = [HashSet[Ast]]::new()
$null = $Ast.FindAll(
{
param([Ast] $currentAst)
if (-not (Test-ItBlock $currentAst)) {
return $false
}
for ($node = $currentAst.Parent; $null -ne $node; $node = $node.Parent) {
if (Test-ItBlock $node) {
return $ParentItBlocks.Add($node)
}
}
return $false
},
<# searchNestedScriptBlocks: #> $true
)
$ParentItBlocks | Should -BeNullOrEmpty -Because 'It blocks cannot be nested'
}
It 'has exactly one line feed at end of the <Topic> file' -TestCases $KoanTopics {
$crlf = [Regex]::Match(($File | Get-Content -Raw), '(\r?(?<lf>\n))+\Z')
$crlf.Groups['lf'].Captures.Count | Should -Be 1
}
It 'has a position number defined for <Topic>' -TestCases $KoanTopics {
param($File, $Position)
$Position | Should -Not -BeNullOrEmpty
$Position | Should -BeGreaterThan 0
}
}
Context 'Library Cleanliness' {
BeforeAll {
$KoanFolder = Resolve-Path "$PSScriptRoot/../PSKoans/Koans"
}
It 'does not have topics with duplicate Koan positions' {
$DuplicatePosition = Get-ChildItem -Path $KoanFolder -Recurse -Filter '*.Koans.ps1' |
ForEach-Object {
$commandInfo = Get-Command -name $_.FullName -ErrorAction SilentlyContinue
$koanAttribute = $commandInfo.ScriptBlock.Attributes.Where{ $_.TypeID -match 'Koan' }
[PSCustomObject]@{
File = $_
Name = $_.BaseName -replace '\.Koans$'
Position = $koanAttribute.Position
Module = $koanAttribute.Module
}
} |
Group-Object { '{0}/{1}' -f $_.Module, $_.Position } |
Where-Object Count -gt 1 |
ForEach-Object { '{0}: {1}' -f $_.Name, ($_.Group.File -join ', ') }
$DuplicatePosition | Should -BeNullOrEmpty
}
It 'does not have non-Koan Topic files in the Koans directory' {
Get-ChildItem -Path $KoanFolder -Recurse -Filter '*.ps1' |
Where-Object BaseName -notmatch '\.Koans$' |
Should -BeNullOrEmpty
}
}
}