Skip to content

Commit 3863bf5

Browse files
authoredSep 7, 2019
Add more script analyzer tests (#215)
To detect bad config Excludes extra tests under centos, as I cant get DSC on CentOS to detect pick up the modules.
1 parent 9f89ff8 commit 3863bf5

File tree

2 files changed

+60
-12
lines changed

2 files changed

+60
-12
lines changed
 

‎OctopusDSC/Examples/cOctopusWorkerPool.ps1

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Configuration SampleConfig
1515
Url = "https://octopus.example.com"
1616
Ensure = 'Present'
1717
WorkerPoolName = 'My Ops Worker Pool'
18-
WorkerPoolDescription "A worker pool for operational tasks"
18+
WorkerPoolDescription = "A worker pool for operational tasks"
1919
SpaceID = "spaces-1"
2020
OctopusCredentials = $creds
2121
}

‎Tests/OctopusDSC.Tests.ps1

+59-11
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,72 @@ Describe "PSScriptAnalyzer" {
44
'PSUseShouldProcessForStateChangingFunctions'
55
)
66

7-
$path = Resolve-Path "$PSCommandPath/../../OctopusDSC/DSCResources"
8-
Write-Output "Running PsScriptAnalyzer against $path"
9-
$results = @(Invoke-ScriptAnalyzer $path -recurse -exclude $excludedRules)
10-
$results | ConvertTo-Json | Out-File PsScriptAnalyzer-DSCResources.log
11-
127
It "Should have zero PSScriptAnalyzer issues in OctopusDSC/DSCResources" {
8+
$path = Resolve-Path "$PSCommandPath/../../OctopusDSC/DSCResources"
9+
Write-Output "Running PsScriptAnalyzer against $path"
10+
$results = @(Invoke-ScriptAnalyzer $path -recurse -exclude $excludedRules)
11+
$results | ConvertTo-Json | Out-File PsScriptAnalyzer-DSCResources.log
12+
1313
$results.length | Should Be 0
1414
}
1515

16-
$path = Resolve-Path "$PSCommandPath/../../OctopusDSC/Tests"
17-
Write-Output "Running PsScriptAnalyzer against $path"
18-
$results = @(Invoke-ScriptAnalyzer $path -recurse -exclude $excludedRules)
19-
$results | ConvertTo-Json | Out-File PsScriptAnalyzer-Tests.log
20-
21-
# 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
2216
It "Should have zero PSScriptAnalyzer issues in OctopusDSC/Tests" {
17+
$path = Resolve-Path "$PSCommandPath/../../OctopusDSC/Tests"
18+
Write-Output "Running PsScriptAnalyzer against $path"
19+
$results = @(Invoke-ScriptAnalyzer $path -recurse -exclude $excludedRules)
20+
$results | ConvertTo-Json | Out-File PsScriptAnalyzer-Tests.log
21+
2322
$results.length | Should Be 0
2423
}
24+
25+
#unfortunately, cant get the following tests to run on our CentOS buildagent
26+
#keep getting:
27+
# "Undefined DSC resource 'cOctopusServer'. Use Import-DSCResource to import the resource."
28+
#even though it works fine on ubuntu locally
29+
$isRunningUnderTeamCity = (Test-Path Env:\TEAMCITY_PROJECT_NAME)
30+
if (-not $isRunningUnderTeamCity)
31+
{
32+
$existingPSModulePath = $env:PSModulePath
33+
$path = Resolve-Path "$PSCommandPath/../../"
34+
if ($isLinux -or $IsMacOS) {
35+
$newPath = "$($env:PSModulePath):$path"
36+
} else {
37+
$newPath = "$($env:PSModulePath);$path"
38+
}
39+
Write-Output "Setting `$env:PSModulePath to '$newPath'"
40+
$env:PSModulePath = $newPath
41+
42+
$excludedRules += 'PSAvoidUsingConvertToSecureStringWithPlainText'
43+
44+
It "Should have zero PSScriptAnalyzer issues in Scenarios" {
45+
$path = Resolve-Path "$PSCommandPath/../../Tests/Scenarios"
46+
Write-Output "Running PsScriptAnalyzer against $path"
47+
$results = @(Invoke-ScriptAnalyzer $path -recurse -exclude $excludedRules)
48+
$results | ConvertTo-Json | Out-File PsScriptAnalyzer-Scenarios.log
49+
if ($IsLinux -or $IsMacOS) {
50+
$results = $results | where-object {
51+
# these DSC resources are available on linux
52+
($_.Message -ne "Undefined DSC resource 'LocalConfigurationManager'. Use Import-DSCResource to import the resource.") -and
53+
($_.Message -ne "Undefined DSC resource 'Script'. Use Import-DSCResource to import the resource.") -and
54+
($_.Message -ne "Undefined DSC resource 'User'. Use Import-DSCResource to import the resource.") -and
55+
($_.Message -ne "Undefined DSC resource 'Group'. Use Import-DSCResource to import the resource.") -and
56+
# it's getting confused - Group here is actually the DSC resource, not an alias.
57+
($_.Message -ne "'Group' is an alias of 'Group-Object'. Alias can introduce possible problems and make scripts hard to maintain. Please consider changing alias to its full content.")
58+
}
59+
}
60+
$results.length | Should Be 0
61+
}
62+
63+
It "Should have zero PSScriptAnalyzer issues in Examples" {
64+
$path = Resolve-Path "$PSCommandPath/../../OctopusDSC/Examples"
65+
Write-Output "Running PsScriptAnalyzer against $path"
66+
$results = @(Invoke-ScriptAnalyzer $path -recurse -exclude $excludedRules)
67+
$results | ConvertTo-Json | Out-File PsScriptAnalyzer-Examples.log
68+
69+
$results.length | Should Be 0
70+
}
71+
$env:PSModulePath = $existingPSModulePath
72+
}
2573
}
2674

2775
Describe "OctopusDSC.psd1" {

0 commit comments

Comments
 (0)
Please sign in to comment.