diff --git a/.github/workflows/powershell.yml b/.github/workflows/powershell.yml index 7877bb6..a458731 100644 --- a/.github/workflows/powershell.yml +++ b/.github/workflows/powershell.yml @@ -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 }} diff --git a/utilities/Generate-githubTOC.ps1 b/utilities/Generate-githubTOC.ps1 index 768dbfd..e4fcfb2 100644 --- a/utilities/Generate-githubTOC.ps1 +++ b/utilities/Generate-githubTOC.ps1 @@ -109,4 +109,4 @@ PROCESS { } END { return $TOC -} +} \ No newline at end of file diff --git a/utilities/new-genericpestertest.ps1 b/utilities/new-genericpestertest.ps1 new file mode 100644 index 0000000..e7a2de5 --- /dev/null +++ b/utilities/new-genericpestertest.ps1 @@ -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 "" -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 "'' 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 + } + } +} \ No newline at end of file