forked from OctopusDeploy/OctopusDSC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OctopusDSC.Tests.ps1
78 lines (65 loc) · 3.61 KB
/
OctopusDSC.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
Describe "PSScriptAnalyzer" {
Import-Module PSScriptAnalyzer
$excludedRules = @(
'PSUseShouldProcessForStateChangingFunctions'
)
$excludedRules | % { Write-Warning "Excluding Rule $_" }
Write-Output "Running PsScriptAnalyzer against ./OctopusDSC/DSCResources"
$results = @(Invoke-ScriptAnalyzer ./OctopusDSC/DSCResources -recurse -exclude $excludedRules)
$results | Out-File PsScriptAnalyzer.log
Write-Output "Running PsScriptAnalyzer against ./OctopusDSC/Tests"
$results += @(Invoke-ScriptAnalyzer ./OctopusDSC/Tests -recurse -exclude $excludedRules)
$results | Out-File PsScriptAnalyzer.log -Append
# it'd be nice to run the PsScriptAnalyzer on `./OctopusDSC/Examples`, but I couldn't get it to detect the DSCModule on mac nor on linux
Write-Output ($results | ConvertTo-Json)
It "Should have zero PSScriptAnalyzer issues" {
$results.length | Should Be 0
}
}
Describe "OctopusDSC.psd1" {
It "Should be valid" {
$modules = Get-ChildItem ./OctopusDSC/DSCResources -Directory
Import-LocalizedData -BaseDirectory ./OctopusDSC -FileName OctopusDSC.psd1 -BindingVariable Data
$expected = ($Data.DscResourcesToExport | Sort-Object) -join ","
$actual = (($modules | Sort-Object) -join ",")
$actual | should be $expected
}
}
Describe "Mandatory Parameters" {
It "All required or key properties in the mof file should be marked with [Parameter(Mandatory)} in the psm1 file" {
$schemaMofFiles = Get-ChildItem ./OctopusDSC/DSCResources -Recurse -Filter *.mof
foreach ($schemaMofFile in $schemaMofFiles) {
$schemaMofFileContent = Get-Content $schemaMofFile.FullName
$moduleFile = Get-Item ($schemaMofFile.FullName -replace ".schema.mof", ".psm1")
$tokens = $null;
$parseErrors = $null;
$ast = [System.Management.Automation.Language.Parser]::ParseFile($moduleFile.FullName, [ref] $tokens, [ref] $parseErrors);
$parseErrors | should be $null
foreach($line in $schemaMofFileContent) {
if ($line -match "\s*(\[.*?(Required|Key).*\])\s*(.*) (.*);") {
$propertyName = $matches[4];
$filter = {
($args[0] -is [System.Management.Automation.Language.ParameterAst]) -and
($args[0].Name -eq $propertyName)
}
$result = $ast.FindAll($filter, $true);
foreach($param in $result) {
$function = $param.Parent.Parent.Parent
if ($function -is [System.Management.Automation.Language.FunctionDefinitionAst]) {
$functionName = ([System.Management.Automation.Language.FunctionDefinitionAst]$function).Name
if (($functionName -like "Get-TargetResource") -or
($functionName -like "Set-TargetResource") -or
($functionName -like "Test-TargetResource")) {
$parameterAttributes = $param.Attributes.Extent.Text
if ($parameterAttributes -notcontains "[Parameter(Mandatory)]") {
$filename = $moduleFile.Name -replace $moduleFile.Extension, ''
throw "$filename.$functionName.$($param.Name) should be marked as mandatory, as they are marked as required/key in the mof file"
}
}
}
}
}
}
}
}
}