Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 18 additions & 15 deletions .github/workflows/powershell.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,23 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- name: Run PSScriptAnalyzer
uses: microsoft/psscriptanalyzer-action@6b2948b1944407914a58661c49941824d149734f
with:
# Check https://github.com/microsoft/action-psscriptanalyzer for more info about the options.
# The below set up runs PSScriptAnalyzer to your entire repository and runs some basic security rules.
path: .\
recurse: true
# Include your own basic security rules. Removing this option will run all the rules
#includeRule: '"PSAvoidGlobalAliases", "PSAvoidUsingConvertToSecureStringWithPlainText"'
output: results.sarif

# Upload the SARIF file generated in the previous step
- name: Upload SARIF results file
uses: github/codeql-action/upload-sarif@v2
ref: ${{ github.head_ref }}
- name: Pester
shell: pwsh
run: |
$container = New-PesterContainer -Path "utilities\new-genericpestertest.ps1" -Data @{folder = "Powershell/Scripts"}
invoke-pester -container $container -CI
- name: findresults
if: always()
shell: pwsh
run: |
(gci -Filter "*result*" -recurse).FullName
- name: pester-tests-report
if: always()
uses: zyborg/pester-tests-report@v1.5.0
with:
sarif_file: results.sarif
test_results_path: testResults.xml
report_name: Results
report_title: Copy of Subset of Tests
github_token: ${{ secrets.GITHUB_TOKEN }}
2 changes: 1 addition & 1 deletion utilities/Generate-githubTOC.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,4 @@ PROCESS {
}
END {
return $TOC
}
}
95 changes: 95 additions & 0 deletions utilities/new-genericpestertest.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
param (
[Parameter(Mandatory = $false)]
[string] $folder = "\"
)

BeforeDiscovery {
function Get-PSRuleAnalyzerArray($a) {
$array = @()
$a | ForEach-Object { $array += @{
rulename = $_.RuleName
} }
return $array
}

$powershellScripts = Get-ChildItem $folder -Recurse -Filter "*.ps1"

$RawPSRuleDefinitions = Get-ScriptAnalyzerRule
$PSRuleDefinitions = Get-PSRuleAnalyzerArray $RawPSRuleDefinitions
$files = @()

foreach ($powershellScript in $powershellScripts) {
$help = Get-Help $powershellScript -Detailed

$parameters = @()
foreach ($param in $help.parameters.parameter) {
$parameters += @{
name = $param.Name
description = $param.description.Text
}
}

$examples = @()
foreach ($example in $help.examples.example) {
$examples += @{
title = $example.title
code = $example.code
}
}


$helpDefinition = @{
help = $help
parameters = $parameters
examples = $examples
}

$files += @{
name = $powershellScript.Name
fullName = $powershellScript.FullName
helpDefinition = $helpDefinition
analyzerTests = $PSRuleDefinitions
}
}
}

Describe "<name>" -ForEach $files {
BeforeEach {
$scriptanalyzerResults = Invoke-ScriptAnalyzer -Path $fullName
}
Context "Help" {
It "should have synopsis defined" {
$helpDefinition.help.synopsis | Should -Not -BeNullOrEmpty
}

It "should have synopsis with minimal length of 40" {
$helpDefinition.help.synopsis.Length | Should -BeGreaterOrEqual 40
}

It "Should have a description" {
$helpDefinition.help.description | Should -Not -BeNullOrEmpty
}

It "Should have a description with minimal length of 120" {
$helpDefinition.help.description[0].Text.Length | Should -BeGreaterOrEqual 120
}

It "Should have an example" {
$helpDefinition.help.examples.example.count | Should -BeGreaterOrEqual 1
}

It "'<title>' should be defined" -ForEach $helpDefinition.examples {
$code | Should -Not -BeNullOrEmpty
}

It "Synopsis Parameter '<name>' should be described" -ForEach $helpDefinition.parameters {
$description | Should -Not -BeNullOrEmpty
}
}

Context -Name "PSScriptAnalyzer" {
It " - '<RuleName>' should pass" -ForEach $analyzerTests {
$scriptanalyzerResults.RuleName -contains $rulename | Should -Be $false
}
}
}