-
-
Notifications
You must be signed in to change notification settings - Fork 146
/
Copy pathProject.Tests.ps1
94 lines (78 loc) · 4.68 KB
/
Project.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
BeforeDiscovery {
$script:ModuleName = 'dbachecks'
$ModuleBase = (Get-Module -Name $ModuleName -ListAvailable).ModuleBase
$commands = Get-Command -Module $ModuleName -CommandType Cmdlet, Function
}
Describe 'PSScriptAnalyzer rule-sets' -Tag Build , ScriptAnalyzer {
BeforeDiscovery {
$script:ModuleName = 'dbachecks'
$ModuleBase = (Get-Module -Name $ModuleName -ListAvailable).ModuleBase
$Rules = Get-ScriptAnalyzerRule
$scripts = Get-ChildItem $ModuleBase -Include *.ps1, *.psm1, *.psd1 -Recurse | Where-Object fullname -NotMatch 'classes'
# Get last commit that was merged from main
$lastCommit = git log --grep="Updated Version Number and docs from master" -1 --format='%H'
# Get the files that have been altered in since the last merge from master
$scripts = git diff --name-only $lastCommit HEAD | Where-Object { $psitem.EndsWith('ps1') }
# only the ones in these folders
$scripts = $scripts | Where-Object { ($_ -like 'internal*') -or ($_ -like 'functions*') -or ( $_ -like 'checks*') }
}
Context 'Checking PSScriptAnalyzer on Script <_>' -ForEach $scripts {
BeforeDiscovery {
$PsScriptAnalyzerSettings = 'PSScriptAnalyzerSettings.psd1'
$scriptpath = Join-Path -Path $ModuleBase -ChildPath $PsItem
$Tests = $rules.ForEach{
@{
scriptpath = $scriptpath
RuleName = $_.RuleName
PsScriptAnalyzerSettings = $PsScriptAnalyzerSettings
}
}
}
It 'The Script Analyzer Rule <_.RuleName> Should not fail' -ForEach $Tests {
$rulefailures = Invoke-ScriptAnalyzer -Path $PsItem.scriptpath -IncludeRule $PsItem.RuleName -Settings $PsItem.PsScriptAnalyzerSettings
$message = ($rulefailures | Select-Object Message -Unique).Message
$lines = $rulefailures.Line -join ','
$Because = 'Script Analyzer says the rules have been broken on lines {3} with Message {0} Check in VSCode Problems tab or Run Invoke-ScriptAnalyzer -Script {1} -Settings {2}' -f $message, $scriptpath, $PsScriptAnalyzerSettings, $lines
$rulefailures.Count | Should -Be 0 -Because $Because
}
}
}
Describe 'Testing help for <_.Name>' -Tag Help -ForEach $commands {
BeforeAll {
$Help = Get-Help $PsItem.Name -ErrorAction SilentlyContinue
}
Context 'General help' {
It 'Synopsis should not be auto-generated or empty' {
$Because = 'We are good citizens and write good help'
$Help.Synopsis | Should -Not -BeLike 'Short description*' -Because $Because
$Help.Synopsis[0] | Should -Not -Match '\n' -Because $Because
}
It 'Description should not be auto-generated or empty' {
$Because = 'We are good citizens and write good help'
$Help.Description | Should -Not -BeLike '*Long description*' -Because $Because
$Help.Description | Should -Not -BeNullOrEmpty -Because $Because
}
}
Context 'Examples help' {
It 'There should be more than one example' {
$Because = 'Most commands should have more than one example to explain and we are good citizens and write good help'
$Help.Examples.example.Count | Should -BeGreaterThan 1 -Because $Because
}
It 'There should be code for <_.title>' -ForEach $Help.Examples.Example {
$Because = 'All examples should have code otherwise what is the point? and we are good citizens and write good help'
$PsItem.Code | Should -Not -BeNullOrEmpty -Because $Because
$PsItem.Code | Should -Not -BeLike '*An example*' -Because $Because
}
It 'There should be remarks for <_.title>' -ForEach $Help.Examples.Example {
$Because = 'All examples should have explanations otherwise what is the point? and we are good citizens and write good help'
$PsItem.remarks[0] | Should -Not -Be '@{Text=}' -Because $Because
}
}
Context 'Parameters help' {
It 'Parameter <_.name> should have help' -ForEach ($command.ParameterSets.Parameters | Where-Object Name -NotIn 'Debug', 'ErrorAction', 'ErrorVariable', 'InformationAction', 'InformationVariable', 'OutBuffer', 'OutVariable', 'PipelineVariable', 'Verbose', 'WarningAction', 'WarningVariable', 'Confirm', 'WhatIf') {
$Because = 'Every parameter should have help and we are good citizens and write good help'
$_.Description.Text | Should -Not -BeNullOrEmpty -Because $Because
$_.Description.Text | Should -Not -Be 'Parameter description' -Because $Because
}
}
}