1+ Import-Module - Verbose PSScriptAnalyzer
2+
3+ $currentPath = Split-Path - Parent $MyInvocation.MyCommand.Path
4+ $ruleName = " PSDSCDscTestsPresent"
5+
6+ Describe " DscTestsPresent rule in class based resource" {
7+
8+ $testsPath = " $currentPath \DSCResources\MyDscResource\Tests"
9+ $classResourcePath = " $currentPath \DSCResources\MyDscResource\MyDscResource.psm1"
10+
11+ Context " When tests absent" {
12+
13+ $violations = Invoke-ScriptAnalyzer - ErrorAction SilentlyContinue $classResourcePath | Where-Object {$_.RuleName -eq $ruleName }
14+ $violationMessage = " No tests found for resource 'FileResource'"
15+
16+ It " has 1 missing test violation" {
17+ $violations.Count | Should Be 1
18+ }
19+
20+ It " has the correct description message" {
21+ $violations [0 ].Message | Should Match $violationMessage
22+ }
23+ }
24+
25+ Context " When tests present" {
26+ New-Item - Path $testsPath - ItemType Directory
27+ New-Item - Path " $testsPath \FileResource_Test.psm1" - ItemType File
28+
29+ $noViolations = Invoke-ScriptAnalyzer - ErrorAction SilentlyContinue $classResourcePath | Where-Object {$_.RuleName -eq $ruleName }
30+
31+ It " returns no violations" {
32+ $noViolations.Count | Should Be 0
33+ }
34+
35+ Remove-Item - Path $testsPath - Recurse - Force
36+ }
37+ }
38+
39+ Describe " DscTestsPresent rule in regular (non-class) based resource" {
40+
41+ $testsPath = " $currentPath \Tests"
42+ $resourcePath = " $currentPath \DSCResources\MSFT_WaitForAll\MSFT_WaitForAll.psm1"
43+
44+ Context " When tests absent" {
45+
46+ $violations = Invoke-ScriptAnalyzer - ErrorAction SilentlyContinue $resourcePath | Where-Object {$_.RuleName -eq $ruleName }
47+ $violationMessage = " No tests found for resource 'MSFT_WaitForAll'"
48+
49+ It " has 1 missing tests violation" {
50+ $violations.Count | Should Be 1
51+ }
52+
53+ It " has the correct description message" {
54+ $violations [0 ].Message | Should Match $violationMessage
55+ }
56+ }
57+
58+ Context " When tests present" {
59+ New-Item - Path $testsPath - ItemType Directory
60+ New-Item - Path " $testsPath \MSFT_WaitForAll_Test.psm1" - ItemType File
61+
62+ $noViolations = Invoke-ScriptAnalyzer - ErrorAction SilentlyContinue $resourcePath | Where-Object {$_.RuleName -eq $ruleName }
63+
64+ It " returns no violations" {
65+ $noViolations.Count | Should Be 0
66+ }
67+
68+ Remove-Item - Path $testsPath - Recurse - Force
69+ }
70+ }
0 commit comments