From 5adf890821ae6cdc98e82dbc9e9da3b107d0af6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9on?= Date: Fri, 6 Jan 2023 14:30:03 +0100 Subject: [PATCH 01/14] added --- .github/workflows/powershell.yml | 2 +- utilities/Generate-githubTOC.ps1 | 2 +- utilities/new-genericpestertest.ps1 | 95 +++++++++++++++++++++++++++++ 3 files changed, 97 insertions(+), 2 deletions(-) create mode 100644 utilities/new-genericpestertest.ps1 diff --git a/.github/workflows/powershell.yml b/.github/workflows/powershell.yml index 7877bb6..0256d41 100644 --- a/.github/workflows/powershell.yml +++ b/.github/workflows/powershell.yml @@ -45,4 +45,4 @@ jobs: - name: Upload SARIF results file uses: github/codeql-action/upload-sarif@v2 with: - sarif_file: results.sarif + sarif_file: *testResults.xml 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 From e8f3b61bf5d448f0d8a15ff529849aee45509a71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9on?= <leonboers@gmail.com> Date: Fri, 6 Jan 2023 14:34:10 +0100 Subject: [PATCH 02/14] Update powershell.yml --- .github/workflows/powershell.yml | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/.github/workflows/powershell.yml b/.github/workflows/powershell.yml index 0256d41..f2461ac 100644 --- a/.github/workflows/powershell.yml +++ b/.github/workflows/powershell.yml @@ -30,19 +30,13 @@ jobs: 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 - + - name: Pester + shell: pwsh + run: | + $container = New-PesterContainer -Path "utilities\new-genericpestertest.ps1" -Data @{folder = "powershell\Scripts"} + invoke-pester -container $container -CI # Upload the SARIF file generated in the previous step - name: Upload SARIF results file uses: github/codeql-action/upload-sarif@v2 with: - sarif_file: *testResults.xml + sarif_file: testResults.xml From 510f154ced7a626d1a39ba19268533dae629fd35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9on?= <leonboers@gmail.com> Date: Fri, 6 Jan 2023 14:35:32 +0100 Subject: [PATCH 03/14] changed path --- .github/workflows/powershell.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/powershell.yml b/.github/workflows/powershell.yml index f2461ac..9da3eb5 100644 --- a/.github/workflows/powershell.yml +++ b/.github/workflows/powershell.yml @@ -33,7 +33,7 @@ jobs: - name: Pester shell: pwsh run: | - $container = New-PesterContainer -Path "utilities\new-genericpestertest.ps1" -Data @{folder = "powershell\Scripts"} + $container = New-PesterContainer -Path "utilities\new-genericpestertest.ps1" -Data @{folder = "Powershell/Scripts"} invoke-pester -container $container -CI # Upload the SARIF file generated in the previous step - name: Upload SARIF results file From e56e2eec8e11474349ed2ce10fce3f736c965560 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9on?= <leonboers@gmail.com> Date: Fri, 6 Jan 2023 14:42:55 +0100 Subject: [PATCH 04/14] Update powershell.yml --- .github/workflows/powershell.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/powershell.yml b/.github/workflows/powershell.yml index 9da3eb5..55c6a13 100644 --- a/.github/workflows/powershell.yml +++ b/.github/workflows/powershell.yml @@ -37,6 +37,7 @@ jobs: invoke-pester -container $container -CI # Upload the SARIF file generated in the previous step - name: Upload SARIF results file + if: always() uses: github/codeql-action/upload-sarif@v2 with: sarif_file: testResults.xml From 901d8b190d8796169f147a9a31fbe718998f3c27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9on?= <leonboers@gmail.com> Date: Fri, 6 Jan 2023 14:47:16 +0100 Subject: [PATCH 05/14] Update powershell.yml --- .github/workflows/powershell.yml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.github/workflows/powershell.yml b/.github/workflows/powershell.yml index 55c6a13..bb2533e 100644 --- a/.github/workflows/powershell.yml +++ b/.github/workflows/powershell.yml @@ -35,6 +35,11 @@ jobs: 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 *result* -recurse # Upload the SARIF file generated in the previous step - name: Upload SARIF results file if: always() From 72a059c8bef0cf16cb12a63ca5c9b824d85414f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9on?= <leonboers@gmail.com> Date: Fri, 6 Jan 2023 14:49:59 +0100 Subject: [PATCH 06/14] Update powershell.yml --- .github/workflows/powershell.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/powershell.yml b/.github/workflows/powershell.yml index bb2533e..8f4b84d 100644 --- a/.github/workflows/powershell.yml +++ b/.github/workflows/powershell.yml @@ -39,7 +39,7 @@ jobs: if: always() shell: pwsh run: | - gci *result* -recurse + (gci -Filter "*result*" -recurse).FullName # Upload the SARIF file generated in the previous step - name: Upload SARIF results file if: always() From 77fddf5cfa4bbe6ea7563fd88b5e45991d5059e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9on?= <leonboers@gmail.com> Date: Fri, 6 Jan 2023 15:29:12 +0100 Subject: [PATCH 07/14] Update powershell.yml --- .github/workflows/powershell.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/powershell.yml b/.github/workflows/powershell.yml index 8f4b84d..2ef08f4 100644 --- a/.github/workflows/powershell.yml +++ b/.github/workflows/powershell.yml @@ -43,6 +43,6 @@ jobs: # Upload the SARIF file generated in the previous step - name: Upload SARIF results file if: always() - uses: github/codeql-action/upload-sarif@v2 + uses: actions/upload-artifact@v2 with: sarif_file: testResults.xml From fd8ce340b72a3f3c1d9be9309cb3cd59fc5a90df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9on?= <leonboers@gmail.com> Date: Fri, 6 Jan 2023 15:30:32 +0100 Subject: [PATCH 08/14] Update powershell.yml --- .github/workflows/powershell.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/powershell.yml b/.github/workflows/powershell.yml index 2ef08f4..d8f468c 100644 --- a/.github/workflows/powershell.yml +++ b/.github/workflows/powershell.yml @@ -45,4 +45,4 @@ jobs: if: always() uses: actions/upload-artifact@v2 with: - sarif_file: testResults.xml + path: testResults.xml From 87dbb50127ba0a7af048615e0daf2b633613a32d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9on?= <leonboers@gmail.com> Date: Fri, 6 Jan 2023 15:39:14 +0100 Subject: [PATCH 09/14] Update powershell.yml --- .github/workflows/powershell.yml | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/.github/workflows/powershell.yml b/.github/workflows/powershell.yml index d8f468c..3759005 100644 --- a/.github/workflows/powershell.yml +++ b/.github/workflows/powershell.yml @@ -29,7 +29,8 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - + with: + ref: ${{ github.head_ref }} - name: Pester shell: pwsh run: | @@ -40,9 +41,9 @@ jobs: shell: pwsh run: | (gci -Filter "*result*" -recurse).FullName - # Upload the SARIF file generated in the previous step - - name: Upload SARIF results file - if: always() - uses: actions/upload-artifact@v2 + - name: pester-tests-report + uses: zyborg/pester-tests-report@v1.5.0 with: - path: testResults.xml + test_results_path: # optional + # Comma-separated list of test full names, or with `-like` wildcards, to restrict which tests are resolved. The default is to include every test discovered matching the other input parameters. + full_names_filters: testResults.xml From 51cc180791a31ba1af5b8a0cf85c2254d1e7b057 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9on?= <leonboers@gmail.com> Date: Fri, 6 Jan 2023 15:40:13 +0100 Subject: [PATCH 10/14] Update powershell.yml --- .github/workflows/powershell.yml | 4 ---- 1 file changed, 4 deletions(-) diff --git a/.github/workflows/powershell.yml b/.github/workflows/powershell.yml index 3759005..38dbef5 100644 --- a/.github/workflows/powershell.yml +++ b/.github/workflows/powershell.yml @@ -43,7 +43,3 @@ jobs: (gci -Filter "*result*" -recurse).FullName - name: pester-tests-report uses: zyborg/pester-tests-report@v1.5.0 - with: - test_results_path: # optional - # Comma-separated list of test full names, or with `-like` wildcards, to restrict which tests are resolved. The default is to include every test discovered matching the other input parameters. - full_names_filters: testResults.xml From 53e199f7a3b32027f938acd168b3243e63aa1665 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9on?= <leonboers@gmail.com> Date: Fri, 6 Jan 2023 15:40:32 +0100 Subject: [PATCH 11/14] Update powershell.yml --- .github/workflows/powershell.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/powershell.yml b/.github/workflows/powershell.yml index 38dbef5..e189105 100644 --- a/.github/workflows/powershell.yml +++ b/.github/workflows/powershell.yml @@ -42,4 +42,5 @@ jobs: run: | (gci -Filter "*result*" -recurse).FullName - name: pester-tests-report + if: always() uses: zyborg/pester-tests-report@v1.5.0 From fffc2439b936c3b76b0ea795738f07e77e0f8d9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9on?= <leonboers@gmail.com> Date: Fri, 6 Jan 2023 15:42:37 +0100 Subject: [PATCH 12/14] Update powershell.yml --- .github/workflows/powershell.yml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.github/workflows/powershell.yml b/.github/workflows/powershell.yml index e189105..a458731 100644 --- a/.github/workflows/powershell.yml +++ b/.github/workflows/powershell.yml @@ -44,3 +44,8 @@ jobs: - name: pester-tests-report if: always() uses: zyborg/pester-tests-report@v1.5.0 + with: + test_results_path: testResults.xml + report_name: Results + report_title: Copy of Subset of Tests + github_token: ${{ secrets.GITHUB_TOKEN }} From 56f5b55c7c91c5f5c5e04368560eb81de5d02e12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9on?= <leonboers@gmail.com> Date: Fri, 6 Jan 2023 15:44:18 +0100 Subject: [PATCH 13/14] Update powershell.yml --- .github/workflows/powershell.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/powershell.yml b/.github/workflows/powershell.yml index a458731..1bb3c59 100644 --- a/.github/workflows/powershell.yml +++ b/.github/workflows/powershell.yml @@ -48,4 +48,3 @@ jobs: test_results_path: testResults.xml report_name: Results report_title: Copy of Subset of Tests - github_token: ${{ secrets.GITHUB_TOKEN }} From b0ecc29e42803290fef70967b18214faf75dd035 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9on?= <leonboers@gmail.com> Date: Fri, 6 Jan 2023 15:49:49 +0100 Subject: [PATCH 14/14] Update powershell.yml --- .github/workflows/powershell.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/powershell.yml b/.github/workflows/powershell.yml index 1bb3c59..a458731 100644 --- a/.github/workflows/powershell.yml +++ b/.github/workflows/powershell.yml @@ -48,3 +48,4 @@ jobs: test_results_path: testResults.xml report_name: Results report_title: Copy of Subset of Tests + github_token: ${{ secrets.GITHUB_TOKEN }}