From 74e15d2c6786fbf76fc8bc6007fe2a4e8049ba5d Mon Sep 17 00:00:00 2001 From: rulasg Date: Thu, 22 Jun 2023 23:04:27 +0200 Subject: [PATCH 01/11] fea: test result single line at the bottom --- public/Invoke-TestingHelper.ps1 | 37 +++++++++++++-------------------- 1 file changed, 15 insertions(+), 22 deletions(-) diff --git a/public/Invoke-TestingHelper.ps1 b/public/Invoke-TestingHelper.ps1 index 04b6188..65ef05f 100644 --- a/public/Invoke-TestingHelper.ps1 +++ b/public/Invoke-TestingHelper.ps1 @@ -52,22 +52,15 @@ function Test-Module { $result | Add-Member -NotePropertyName "TestsName" -NotePropertyValue $functionsTestName $result | Add-Member -NotePropertyName "Tests" -NotePropertyValue $functionsTest.Length $result | Add-Member -NotePropertyName "Time" -NotePropertyValue $time - - # Display single line result - Write-Host -ForegroundColor DarkCyan - $TestingModuleName | Write-Host -ForegroundColor Green -NoNewline - " results - " | Write-Host -ForegroundColor DarkCyan -NoNewline - Out-SingleResultData -Name "Pass" -Value $result.Pass -Color "Yellow" - Out-SingleResultData -Name "Failed" -Value $result.Failed -Color "Red" - Out-SingleResultData -Name "Skipped" -Value $result.Skipped -Color "Yellow" - Out-SingleResultData -Name "NotImplemented" -Value $result.NotImplemented -Color "Red" - Write-Host -ForegroundColor DarkCyan - - # Displayy all results strucutre - $result - - # Save result to global variable - $global:ResultTestingHelper = $result + + # Save result to global variable + $global:ResultTestingHelper = $result + + # Displayy all results strucutre + $global:ResultTestingHelper | Write-Output + + # Display single line result + Show-ResultSingleLine -Result $result # unload testing module Remove-Module -Name $TestingModuleName -Force @@ -135,14 +128,14 @@ function Invoke-TestingHelper { $result | Add-Member -NotePropertyName "Tests" -NotePropertyValue $functionsTest.Length $result | Add-Member -NotePropertyName "Time" -NotePropertyValue $time - # Display single line result - Show-ResultSingleLine -Result $result - - # Displayy all results strucutre - $result - # Save result to global variable $global:ResultTestingHelper = $result + + # Displayy all results strucutre + $global:ResultTestingHelper | Write-Output + + # Display single line result + Show-ResultSingleLine -Result $result # unload testing module Remove-Module -Name $TestingModuleName -Force From c60ba0997c67b870a62e0e65f5626747415fa811 Mon Sep 17 00:00:00 2001 From: rulasg Date: Thu, 22 Jun 2023 23:48:11 +0200 Subject: [PATCH 02/11] update templates --- private/templates/template.test.ps1 | 2 +- ...nalyzer.yml => template.v3.powershell.yml} | 0 private/templates/template.v3.sync.ps1 | 24 ++++++---- private/templates/template.v3.test.ps1 | 48 +++++++++++++++++++ public/New-ModuleV3.ps1 | 2 +- sync.ps1 | 24 ++++++---- 6 files changed, 82 insertions(+), 18 deletions(-) rename private/templates/{template.v3.PSScriptAnalyzer.yml => template.v3.powershell.yml} (100%) create mode 100644 private/templates/template.v3.test.ps1 diff --git a/private/templates/template.test.ps1 b/private/templates/template.test.ps1 index 502a526..e2ac7d7 100644 --- a/private/templates/template.test.ps1 +++ b/private/templates/template.test.ps1 @@ -45,4 +45,4 @@ function Import-TestingHelper{ Import-TestingHelper -AllowPrerelease # Run test by PSD1 file -Invoke-TestingHelper +Test-ModulelocalPSD1 diff --git a/private/templates/template.v3.PSScriptAnalyzer.yml b/private/templates/template.v3.powershell.yml similarity index 100% rename from private/templates/template.v3.PSScriptAnalyzer.yml rename to private/templates/template.v3.powershell.yml diff --git a/private/templates/template.v3.sync.ps1 b/private/templates/template.v3.sync.ps1 index 39831b4..04f60a1 100644 --- a/private/templates/template.v3.sync.ps1 +++ b/private/templates/template.v3.sync.ps1 @@ -1,21 +1,29 @@ <# .SYNOPSIS - Synchronizes TestingHelper templates files + Synchronizes with TestingHelper templates files .DESCRIPTION - Synchronizes TestingHelper templates to the local repo. + Synchronizes with TestingHelper templates to the local repo. TestingHelper uses templates to create a new module. This script will update the local module with the latest templates. .LINK - https://raw.githubusercontent.com/rulasg/DemoPsModule/main/sync.ps1 + https://raw.githubusercontent.com/rulasg/TestingHelper/main/sync.ps1 #> -[cmdletbinding()] +[cmdletbinding(SupportsShouldProcess)] param() . ($PSScriptRoot | Join-Path -ChildPath "sync-helper.ps1") -Save-UrlContentToFile -Url 'https://raw.githubusercontent.com/rulasg/DemoPsModule/main/test.ps1' -FilePath 'test.ps1' -Save-UrlContentToFile -Url 'https://raw.githubusercontent.com/rulasg/DemoPsModule/main/release.ps1' -FilePath 'release.ps1' -Save-UrlContentToFile -Url 'https://raw.githubusercontent.com/rulasg/DemoPsModule/main/deploy.ps1' -FilePath 'deploy.ps1' -Save-UrlContentToFile -Url 'https://raw.githubusercontent.com/rulasg/DemoPsModule/main/deploy-helper.ps1' -FilePath 'deploy-helper.ps1' +Save-UrlContentToFile -FilePath 'deploy_module_on_release.yml' -Url 'https://raw.githubusercontent.com/rulasg/TestingHelper/main/private/templates/template.v3.deploy_module_on_release.yml' +Save-UrlContentToFile -FilePath 'powershell.yml' -Url 'https://raw.githubusercontent.com/rulasg/TestingHelper/main/private/templates/template.v3.powershell.yml' +Save-UrlContentToFile -FilePath 'test_with_TestingHelper.yml' -Url 'https://raw.githubusercontent.com/rulasg/TestingHelper/main/private/templates/template.v3.test_with_TestingHelper.yml' + +Save-UrlContentToFile -FilePath 'deploy-helper.ps1' -Url 'https://raw.githubusercontent.com/rulasg/TestingHelper/main/private/templates/template.v3.deploy-helper.ps1' +Save-UrlContentToFile -FilePath 'deploy.ps1' -Url 'https://raw.githubusercontent.com/rulasg/TestingHelper/main/private/templates/template.v3.deploy.ps1' + +Save-UrlContentToFile -FilePath 'sync-helper.ps1' -Url 'https://raw.githubusercontent.com/rulasg/TestingHelper/main/private/templates/template.v3.sync-helper.ps1' +Save-UrlContentToFile -FilePath 'sync.ps1' -Url 'https://raw.githubusercontent.com/rulasg/TestingHelper/main/private/templates/template.v3.sync.ps1' + +Save-UrlContentToFile -FilePath 'release.ps1' -Url 'https://raw.githubusercontent.com/rulasg/TestingHelper/main/private/templates/template.v3.release.ps1' +Save-UrlContentToFile -FilePath 'test.ps1' -Url 'https://raw.githubusercontent.com/rulasg/TestingHelper/main/private/templates/template.v3.test.ps1' \ No newline at end of file diff --git a/private/templates/template.v3.test.ps1 b/private/templates/template.v3.test.ps1 new file mode 100644 index 0000000..502a526 --- /dev/null +++ b/private/templates/template.v3.test.ps1 @@ -0,0 +1,48 @@ +<# +.SYNOPSIS + Run tests +.DESCRIPTION + Run the unit test of the actual module +.NOTES + Using TestingHelper this script will search for a Test module and run the tests + This script will be referenced from launch.json to run the tests on VSCode +.LINK + https://raw.githubusercontent.com/rulasg/DemoPsModule/main/test.ps1 +.EXAMPLE + > ./test.ps1 +#> + +[CmdletBinding()] +param () + +function Import-TestingHelper{ + [CmdletBinding()] + param ( + [Parameter()][string]$Version, + [Parameter()][switch]$AllowPrerelease, + [Parameter()][switch]$PassThru + ) + + + if ($Version) { + $V = $Version.Split('-') + $semVer = $V[0] + $AllowPrerelease = ($AllowPrerelease -or ($null -ne $V[1])) + } + + $module = Import-Module TestingHelper -PassThru -ErrorAction SilentlyContinue -RequiredVersion:$semVer + + if ($null -eq $module) { + $installed = Install-Module -Name TestingHelper -Force -AllowPrerelease:$AllowPrerelease -passThru -RequiredVersion:$Version + $module = Import-Module -Name $installed.Name -RequiredVersion ($installed.Version.Split('-')[0]) -Force -PassThru + } + + if ($PassThru) { + $module + } +} + +Import-TestingHelper -AllowPrerelease + +# Run test by PSD1 file +Invoke-TestingHelper diff --git a/public/New-ModuleV3.ps1 b/public/New-ModuleV3.ps1 index a015a7b..4d99b85 100644 --- a/public/New-ModuleV3.ps1 +++ b/public/New-ModuleV3.ps1 @@ -141,7 +141,7 @@ function New-ModuleV3 { # Add PSScriptAnalyzer if($AddPSScriptAnalyzerWorkflow){ $destination = $modulePath | Join-Path -ChildPath ".github" -AdditionalChildPath "workflows" - Import-Template -Path $destination -File "PSScriptAnalyzer.yml" -Template "template.v3.PSScriptAnalyzer.yml" + Import-Template -Path $destination -File "powershell.yml" -Template "template.v3.powershell.yml" } # Add Testing diff --git a/sync.ps1 b/sync.ps1 index 39831b4..04f60a1 100644 --- a/sync.ps1 +++ b/sync.ps1 @@ -1,21 +1,29 @@ <# .SYNOPSIS - Synchronizes TestingHelper templates files + Synchronizes with TestingHelper templates files .DESCRIPTION - Synchronizes TestingHelper templates to the local repo. + Synchronizes with TestingHelper templates to the local repo. TestingHelper uses templates to create a new module. This script will update the local module with the latest templates. .LINK - https://raw.githubusercontent.com/rulasg/DemoPsModule/main/sync.ps1 + https://raw.githubusercontent.com/rulasg/TestingHelper/main/sync.ps1 #> -[cmdletbinding()] +[cmdletbinding(SupportsShouldProcess)] param() . ($PSScriptRoot | Join-Path -ChildPath "sync-helper.ps1") -Save-UrlContentToFile -Url 'https://raw.githubusercontent.com/rulasg/DemoPsModule/main/test.ps1' -FilePath 'test.ps1' -Save-UrlContentToFile -Url 'https://raw.githubusercontent.com/rulasg/DemoPsModule/main/release.ps1' -FilePath 'release.ps1' -Save-UrlContentToFile -Url 'https://raw.githubusercontent.com/rulasg/DemoPsModule/main/deploy.ps1' -FilePath 'deploy.ps1' -Save-UrlContentToFile -Url 'https://raw.githubusercontent.com/rulasg/DemoPsModule/main/deploy-helper.ps1' -FilePath 'deploy-helper.ps1' +Save-UrlContentToFile -FilePath 'deploy_module_on_release.yml' -Url 'https://raw.githubusercontent.com/rulasg/TestingHelper/main/private/templates/template.v3.deploy_module_on_release.yml' +Save-UrlContentToFile -FilePath 'powershell.yml' -Url 'https://raw.githubusercontent.com/rulasg/TestingHelper/main/private/templates/template.v3.powershell.yml' +Save-UrlContentToFile -FilePath 'test_with_TestingHelper.yml' -Url 'https://raw.githubusercontent.com/rulasg/TestingHelper/main/private/templates/template.v3.test_with_TestingHelper.yml' + +Save-UrlContentToFile -FilePath 'deploy-helper.ps1' -Url 'https://raw.githubusercontent.com/rulasg/TestingHelper/main/private/templates/template.v3.deploy-helper.ps1' +Save-UrlContentToFile -FilePath 'deploy.ps1' -Url 'https://raw.githubusercontent.com/rulasg/TestingHelper/main/private/templates/template.v3.deploy.ps1' + +Save-UrlContentToFile -FilePath 'sync-helper.ps1' -Url 'https://raw.githubusercontent.com/rulasg/TestingHelper/main/private/templates/template.v3.sync-helper.ps1' +Save-UrlContentToFile -FilePath 'sync.ps1' -Url 'https://raw.githubusercontent.com/rulasg/TestingHelper/main/private/templates/template.v3.sync.ps1' + +Save-UrlContentToFile -FilePath 'release.ps1' -Url 'https://raw.githubusercontent.com/rulasg/TestingHelper/main/private/templates/template.v3.release.ps1' +Save-UrlContentToFile -FilePath 'test.ps1' -Url 'https://raw.githubusercontent.com/rulasg/TestingHelper/main/private/templates/template.v3.test.ps1' \ No newline at end of file From 52fd612913b48ee271e1876bb2d4a693eb15dec5 Mon Sep 17 00:00:00 2001 From: rulasg Date: Fri, 23 Jun 2023 00:05:02 +0200 Subject: [PATCH 03/11] Templates and scripts improvements --- .../private/tests-Helper-New-ModuleV3.ps1 | 2 +- TestingHelperTest/public/Tests-deploy.ps1 | 2 +- deploy-helper.ps1 | 2 +- deploy.ps1 | 1 - deploy_module_on_release.yml | 48 +++++++++++++++++++ powershell.yml | 47 ++++++++++++++++++ private/templates/template.deploy.ps1 | 4 +- .../templates/template.v3.deploy-helper.ps1 | 4 +- private/templates/template.v3.sync-helper.ps1 | 7 ++- sync-helper.ps1 | 9 ++-- test.ps1 | 11 ++--- test_with_TestingHelper.yml | 45 +++++++++++++++++ 12 files changed, 159 insertions(+), 23 deletions(-) create mode 100644 deploy_module_on_release.yml create mode 100644 powershell.yml create mode 100644 test_with_TestingHelper.yml diff --git a/TestingHelperTest/private/tests-Helper-New-ModuleV3.ps1 b/TestingHelperTest/private/tests-Helper-New-ModuleV3.ps1 index 0227e28..6b2d909 100644 --- a/TestingHelperTest/private/tests-Helper-New-ModuleV3.ps1 +++ b/TestingHelperTest/private/tests-Helper-New-ModuleV3.ps1 @@ -94,7 +94,7 @@ function Assert-AddModuleV3 { # PSScriptAnalyzer if ($AddPSScriptAnalyzerWorkflow) { $destination = $Path | Join-Path -ChildPath ".github" -AdditionalChildPath "workflows" - Assert-ItemExist -Path ($destination | Join-Path -ChildPath "PSScriptAnalyzer.yml") -Comment "PSScriptAnalyzer.yml" + Assert-ItemExist -Path ($destination | Join-Path -ChildPath "powershell.yml") -Comment "powershell.yml" } # TestingWorkflow diff --git a/TestingHelperTest/public/Tests-deploy.ps1 b/TestingHelperTest/public/Tests-deploy.ps1 index 7e55b78..510c9a1 100644 --- a/TestingHelperTest/public/Tests-deploy.ps1 +++ b/TestingHelperTest/public/Tests-deploy.ps1 @@ -17,7 +17,7 @@ $SCRITPBLOCK_WITHNOEXCEPTION = { return 0 } } -$EXCEPTION_MESSAGE = 'Some throw exception comming from Deploy-Module Injection' +$EXCEPTION_MESSAGE = 'Some exception message thown on Invoke-DeployModule injected function' $SCRITPBLOCK_WITHEXCEPTION = { function Invoke-DeployModule { diff --git a/deploy-helper.ps1 b/deploy-helper.ps1 index 62a7dcd..58214b8 100644 --- a/deploy-helper.ps1 +++ b/deploy-helper.ps1 @@ -159,4 +159,4 @@ function Get-DeployModuleManifestPath { } else { $psdPath } -} +} \ No newline at end of file diff --git a/deploy.ps1 b/deploy.ps1 index fc30069..8c5fc0c 100644 --- a/deploy.ps1 +++ b/deploy.ps1 @@ -74,4 +74,3 @@ if ( [string]::IsNullOrWhiteSpace($NuGetApiKey) ) { # Deploy module to PSGallery Invoke-DeployModuleToPSGallery -NuGetApiKey $NuGetApiKey -Force - diff --git a/deploy_module_on_release.yml b/deploy_module_on_release.yml new file mode 100644 index 0000000..e73346b --- /dev/null +++ b/deploy_module_on_release.yml @@ -0,0 +1,48 @@ +name: Deploy on Release Published + +on: + workflow_dispatch: + release: + types: [published] + +permissions: + contents: read + +jobs: + deploy_to_powershellgallery: + runs-on: ubuntu-latest + environment: powershellgallery + steps: + - uses: actions/checkout@v3 + - env: + EVENT_CONTEXT: ${{ toJSON(github.event) }} + run: | + echo $EVENT_CONTEXT + + - name: deploy_ps1 + shell: pwsh + env: + NUGETAPIKEY: ${{ secrets.NUGETAPIKEY }} + EVENT_REF: ${{ github.event.ref }} + RELEASE_TAG: ${{ github.event.release.tag_name }} + RELEASE_NAME: ${{ github.event.release.name }} + run: | + $env:EVENT_REF = $env:REF + + If ([string]::IsNullOrEmpty($env:EVENT_REF)) { + # Release published trigger + $tag = $env:RELEASE_TAG + write-host -message "Release [$env:RELEASE_NAME] on tag [$tag]" + } else { + # Read Tag o Branch name + $tag = $env:EVENT_REF.Split('/')[2] + write-host "workflow_dispatch triggered on ref leaf [$tag]" + } + + If([string]::IsNullorwhitespace($tag)) { + # Tag name is empty, exit + write-error "Tag name is empty" + exit 1 + } + + ./deploy.ps1 -VersionTag $tag -NugetApiKey $env:NUGETAPIKEY diff --git a/powershell.yml b/powershell.yml new file mode 100644 index 0000000..96d32d8 --- /dev/null +++ b/powershell.yml @@ -0,0 +1,47 @@ +# This workflow uses actions that are not certified by GitHub. +# They are provided by a third-party and are governed by +# separate terms of service, privacy policy, and support +# documentation. +# +# https://github.com/microsoft/action-psscriptanalyzer +# For more information on PSScriptAnalyzer in general, see +# https://github.com/PowerShell/PSScriptAnalyzer + +name: PSScriptAnalyzer + +on: + workflow_dispatch: + pull_request: + +permissions: + contents: read + +jobs: + build: + permissions: + contents: read # for actions/checkout to fetch code + security-events: write # for github/codeql-action/upload-sarif to upload SARIF results + actions: read # only required for a private repository by github/codeql-action/upload-sarif to get the Action run status + name: PSScriptAnalyzer + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v3 + + - name: Run PSScriptAnalyzer + uses: microsoft/psscriptanalyzer-action@v1.1 + 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 + severity: 'Error' + # 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 + with: + sarif_file: results.sarif + diff --git a/private/templates/template.deploy.ps1 b/private/templates/template.deploy.ps1 index 12848fc..bf4de2f 100644 --- a/private/templates/template.deploy.ps1 +++ b/private/templates/template.deploy.ps1 @@ -70,10 +70,10 @@ if ($Force -and -not $Confirm){ } # Deploy the module with ShouldProcess (-whatif, -confirm) -if ($PSCmdlet.ShouldProcess($psdPath, "Deploy-Module")) { +if ($PSCmdlet.ShouldProcess($psdPath, "Publish-Module")) { $message ="Deploying {0} {1} {2} to PSGallery ..." -f $($psdPath.Name), $($psd1.ModuleVersion), $($psd1.PrivateData.pSData.Prerelease) # show an empty line Write-Information -InformationAction Continue -Message "" Write-Information -InformationAction Continue -Message $message - Deploy-Module -Name $psdPath -NuGetApiKey $NuGetApiKey -Force:$ForceDeploy + Publish-Module -Name $psdPath -NuGetApiKey $NuGetApiKey -Force:$ForceDeploy } \ No newline at end of file diff --git a/private/templates/template.v3.deploy-helper.ps1 b/private/templates/template.v3.deploy-helper.ps1 index 6d4e279..571efcb 100644 --- a/private/templates/template.v3.deploy-helper.ps1 +++ b/private/templates/template.v3.deploy-helper.ps1 @@ -107,7 +107,7 @@ function Invoke-DeployModule { Force = $Force } - Deploy-Module @parameters + Publish-Module-Module @parameters if($?){ Write-Information -MessageData "Deployed module [$Name] to PSGallery" @@ -159,4 +159,4 @@ function Get-DeployModuleManifestPath { } else { $psdPath } -} +} \ No newline at end of file diff --git a/private/templates/template.v3.sync-helper.ps1 b/private/templates/template.v3.sync-helper.ps1 index 2bb714b..03bd464 100644 --- a/private/templates/template.v3.sync-helper.ps1 +++ b/private/templates/template.v3.sync-helper.ps1 @@ -31,8 +31,11 @@ function Out-ContentToFile { [Parameter(Mandatory=$true)][string]$filePath ) - if ($PSCmdlet.ShouldProcess($filePath, "Save content [{0}] to file" -f $content.Length)) { - $content | Out-File -FilePath $filePath -Force + process{ + + if ($PSCmdlet.ShouldProcess($filePath, "Save content [{0}] to file" -f $content.Length)) { + $content | Out-File -FilePath $filePath -Force + } } } diff --git a/sync-helper.ps1 b/sync-helper.ps1 index b57868a..df89a42 100644 --- a/sync-helper.ps1 +++ b/sync-helper.ps1 @@ -31,11 +31,8 @@ function Out-ContentToFile { [Parameter(Mandatory=$true)][string]$filePath ) - process { - - if ($PSCmdlet.ShouldProcess($filePath, "Save content [{0}] to file" -f $content.Length)) { - $content | Out-File -FilePath $filePath -Force - } + if ($PSCmdlet.ShouldProcess($filePath, "Save content [{0}] to file" -f $content.Length)) { + $content | Out-File -FilePath $filePath -Force } } @@ -50,4 +47,4 @@ function Save-UrlContentToFile { $fileContent | Out-ContentToFile -FilePath $filePath Write-Information -MessageData "Saved content to [$filePath] from [$url]" -} +} \ No newline at end of file diff --git a/test.ps1 b/test.ps1 index a3f9122..25f6d32 100644 --- a/test.ps1 +++ b/test.ps1 @@ -13,9 +13,7 @@ #> [CmdletBinding()] -param ( - [Parameter()][switch]$ShowTestErrors -) +param () function Import-TestingHelper{ [CmdletBinding()] @@ -24,7 +22,8 @@ function Import-TestingHelper{ [Parameter()][switch]$AllowPrerelease, [Parameter()][switch]$PassThru ) - + + if ($Version) { $V = $Version.Split('-') $semVer = $V[0] @@ -46,6 +45,4 @@ function Import-TestingHelper{ Import-TestingHelper -AllowPrerelease # Run test by PSD1 file -# Test-ModulelocalPSD1 is Obsolete. -# Switch to Invoke-TestingHelper as soon as a deplyed version has this function -Test-ModulelocalPSD1 \ No newline at end of file +Invoke-TestingHelper \ No newline at end of file diff --git a/test_with_TestingHelper.yml b/test_with_TestingHelper.yml new file mode 100644 index 0000000..d08b89d --- /dev/null +++ b/test_with_TestingHelper.yml @@ -0,0 +1,45 @@ +# This is a workflow to test you PowerShell module with TestingHelper +# https://github.com/rulasg/DemoPsModule/blob/main/.github/workflows/test_with_TestingHelper.yml + +name: Test with TestingHelper + +# Controls when the workflow will run +on: + + # Run as check on pull request + pull_request: + + # Allows you to run this workflow manually from the Actions tab + workflow_dispatch: + +permissions: + # To run test we only need to read the repository + contents: read + +# A workflow run is made up of one or more jobs that can run sequentially or in parallel +jobs: + # This workflow contains a single job called "build" + test: + # The type of runner that the job will run on + runs-on: ubuntu-latest + + # Steps represent a sequence of tasks that will be executed as part of the job + steps: + # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it + - uses: actions/checkout@v3 + + # Runs a single command using the runners shell + - name: Run test.ps1 + shell: pwsh + run: | + $result = ./test.ps1 -ShowTestErrors + $result + + # Allow Not Implemented and Skipped tests to pass + $passed = $result.Tests -eq $result.Pass + $result.NotImplemented + $result.Skipped + # $passed = $result.Tests -eq $result.Pass + + if($passed) + { "All test passed" | Write-Verbose -verbose ; exit 0 } + else + { "Not all tests passed" | Write-Verbose -verbose ; exit 1 } From ab2cab8c230282df2e3d391e51b4d0bd86c661d6 Mon Sep 17 00:00:00 2001 From: rulasg Date: Fri, 23 Jun 2023 00:31:41 +0200 Subject: [PATCH 04/11] sync-helper and small template udpate --- TestingHelperTest/private/tests-Helper-New-ModuleV3.ps1 | 4 ++-- deploy-helper.ps1 | 2 +- private/templates/template.v3.deploy-helper.ps1 | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/TestingHelperTest/private/tests-Helper-New-ModuleV3.ps1 b/TestingHelperTest/private/tests-Helper-New-ModuleV3.ps1 index 6b2d909..c0f2f58 100644 --- a/TestingHelperTest/private/tests-Helper-New-ModuleV3.ps1 +++ b/TestingHelperTest/private/tests-Helper-New-ModuleV3.ps1 @@ -76,8 +76,8 @@ function Assert-AddModuleV3 { # Deploy if ($AddDeployScript) { - Assert-ItemExist -Path ($Path | Join-Path -ChildPath "deploy.ps1") -Comment "Deploy-Module.ps1" - Assert-ItemExist -Path ($Path | Join-Path -ChildPath "deploy-helper.ps1") -Comment "Deploy-Module.ps1" + Assert-ItemExist -Path ($Path | Join-Path -ChildPath "deploy.ps1") -Comment "deploy.ps1" + Assert-ItemExist -Path ($Path | Join-Path -ChildPath "deploy-helper.ps1") -Comment "deploy-helper.ps1" } # release script diff --git a/deploy-helper.ps1 b/deploy-helper.ps1 index 58214b8..cb29901 100644 --- a/deploy-helper.ps1 +++ b/deploy-helper.ps1 @@ -55,7 +55,7 @@ function Invoke-DeployModuleToPSGallery{ } # Deploy the module with ShouldProcess (-whatif, -confirm) - if ($PSCmdlet.ShouldProcess($psdPath, "Deploy-Module")) { + if ($PSCmdlet.ShouldProcess($psdPath, "Invoke-DeployModule")) { "Deploying {0} {1} {2} to PSGallery ..." -f $($psd1.RootModule), $($psd1.ModuleVersion), $($psd1.PrivateData.pSData.Prerelease) | Write-Information # During testing we should use -WhatIf paarmetre when calling for deploy. # Just reach this point when testing call failure diff --git a/private/templates/template.v3.deploy-helper.ps1 b/private/templates/template.v3.deploy-helper.ps1 index 571efcb..30bb90c 100644 --- a/private/templates/template.v3.deploy-helper.ps1 +++ b/private/templates/template.v3.deploy-helper.ps1 @@ -55,7 +55,7 @@ function Invoke-DeployModuleToPSGallery{ } # Deploy the module with ShouldProcess (-whatif, -confirm) - if ($PSCmdlet.ShouldProcess($psdPath, "Deploy-Module")) { + if ($PSCmdlet.ShouldProcess($psdPath, "Invoke-DeployModule")) { "Deploying {0} {1} {2} to PSGallery ..." -f $($psd1.RootModule), $($psd1.ModuleVersion), $($psd1.PrivateData.pSData.Prerelease) | Write-Information # During testing we should use -WhatIf paarmetre when calling for deploy. # Just reach this point when testing call failure From fe35d8521c173dc5ca3fc8f19e94c1db62a3a00b Mon Sep 17 00:00:00 2001 From: rulasg Date: Fri, 23 Jun 2023 00:36:59 +0200 Subject: [PATCH 05/11] templates improvement --- private/templates/template.v3.deploy.ps1 | 3 +-- private/templates/template.v3.powershell.yml | 2 +- private/templates/template.v3.sync-helper.ps1 | 2 +- private/templates/template.v3.sync.ps1 | 18 +++++++++--------- private/templates/template.v3.test.ps1 | 3 +-- 5 files changed, 13 insertions(+), 15 deletions(-) diff --git a/private/templates/template.v3.deploy.ps1 b/private/templates/template.v3.deploy.ps1 index fc30069..d33a969 100644 --- a/private/templates/template.v3.deploy.ps1 +++ b/private/templates/template.v3.deploy.ps1 @@ -73,5 +73,4 @@ if ( [string]::IsNullOrWhiteSpace($NuGetApiKey) ) { } # Deploy module to PSGallery -Invoke-DeployModuleToPSGallery -NuGetApiKey $NuGetApiKey -Force - +Invoke-DeployModuleToPSGallery -NuGetApiKey $NuGetApiKey -Force \ No newline at end of file diff --git a/private/templates/template.v3.powershell.yml b/private/templates/template.v3.powershell.yml index 60bdec1..5a66b46 100644 --- a/private/templates/template.v3.powershell.yml +++ b/private/templates/template.v3.powershell.yml @@ -43,4 +43,4 @@ jobs: - name: Upload SARIF results file uses: github/codeql-action/upload-sarif@v2 with: - sarif_file: results.sarif + sarif_file: results.sarif \ No newline at end of file diff --git a/private/templates/template.v3.sync-helper.ps1 b/private/templates/template.v3.sync-helper.ps1 index 03bd464..fc3e019 100644 --- a/private/templates/template.v3.sync-helper.ps1 +++ b/private/templates/template.v3.sync-helper.ps1 @@ -50,4 +50,4 @@ function Save-UrlContentToFile { $fileContent | Out-ContentToFile -FilePath $filePath Write-Information -MessageData "Saved content to [$filePath] from [$url]" -} +} \ No newline at end of file diff --git a/private/templates/template.v3.sync.ps1 b/private/templates/template.v3.sync.ps1 index 04f60a1..4db95c9 100644 --- a/private/templates/template.v3.sync.ps1 +++ b/private/templates/template.v3.sync.ps1 @@ -15,15 +15,15 @@ param() . ($PSScriptRoot | Join-Path -ChildPath "sync-helper.ps1") -Save-UrlContentToFile -FilePath 'deploy_module_on_release.yml' -Url 'https://raw.githubusercontent.com/rulasg/TestingHelper/main/private/templates/template.v3.deploy_module_on_release.yml' -Save-UrlContentToFile -FilePath 'powershell.yml' -Url 'https://raw.githubusercontent.com/rulasg/TestingHelper/main/private/templates/template.v3.powershell.yml' -Save-UrlContentToFile -FilePath 'test_with_TestingHelper.yml' -Url 'https://raw.githubusercontent.com/rulasg/TestingHelper/main/private/templates/template.v3.test_with_TestingHelper.yml' +Save-UrlContentToFile -FilePath 'deploy_module_on_release.yml' -Url 'https://raw.githubusercontent.com/rulasg/TestingHelper/main/private/templates/template.v3.deploy_module_on_release.yml' +Save-UrlContentToFile -FilePath 'powershell.yml' -Url 'https://raw.githubusercontent.com/rulasg/TestingHelper/main/private/templates/template.v3.powershell.yml' +Save-UrlContentToFile -FilePath 'test_with_TestingHelper.yml' -Url 'https://raw.githubusercontent.com/rulasg/TestingHelper/main/private/templates/template.v3.test_with_TestingHelper.yml' -Save-UrlContentToFile -FilePath 'deploy-helper.ps1' -Url 'https://raw.githubusercontent.com/rulasg/TestingHelper/main/private/templates/template.v3.deploy-helper.ps1' -Save-UrlContentToFile -FilePath 'deploy.ps1' -Url 'https://raw.githubusercontent.com/rulasg/TestingHelper/main/private/templates/template.v3.deploy.ps1' +Save-UrlContentToFile -FilePath 'deploy-helper.ps1' -Url 'https://raw.githubusercontent.com/rulasg/TestingHelper/main/private/templates/template.v3.deploy-helper.ps1' +Save-UrlContentToFile -FilePath 'deploy.ps1' -Url 'https://raw.githubusercontent.com/rulasg/TestingHelper/main/private/templates/template.v3.deploy.ps1' -Save-UrlContentToFile -FilePath 'sync-helper.ps1' -Url 'https://raw.githubusercontent.com/rulasg/TestingHelper/main/private/templates/template.v3.sync-helper.ps1' -Save-UrlContentToFile -FilePath 'sync.ps1' -Url 'https://raw.githubusercontent.com/rulasg/TestingHelper/main/private/templates/template.v3.sync.ps1' +Save-UrlContentToFile -FilePath 'sync-helper.ps1' -Url 'https://raw.githubusercontent.com/rulasg/TestingHelper/main/private/templates/template.v3.sync-helper.ps1' +Save-UrlContentToFile -FilePath 'sync.ps1' -Url 'https://raw.githubusercontent.com/rulasg/TestingHelper/main/private/templates/template.v3.sync.ps1' -Save-UrlContentToFile -FilePath 'release.ps1' -Url 'https://raw.githubusercontent.com/rulasg/TestingHelper/main/private/templates/template.v3.release.ps1' -Save-UrlContentToFile -FilePath 'test.ps1' -Url 'https://raw.githubusercontent.com/rulasg/TestingHelper/main/private/templates/template.v3.test.ps1' \ No newline at end of file +Save-UrlContentToFile -FilePath 'release.ps1' -Url 'https://raw.githubusercontent.com/rulasg/TestingHelper/main/private/templates/template.v3.release.ps1' +Save-UrlContentToFile -FilePath 'test.ps1' -Url 'https://raw.githubusercontent.com/rulasg/TestingHelper/main/private/templates/template.v3.test.ps1' \ No newline at end of file diff --git a/private/templates/template.v3.test.ps1 b/private/templates/template.v3.test.ps1 index 502a526..21151eb 100644 --- a/private/templates/template.v3.test.ps1 +++ b/private/templates/template.v3.test.ps1 @@ -22,7 +22,6 @@ function Import-TestingHelper{ [Parameter()][switch]$AllowPrerelease, [Parameter()][switch]$PassThru ) - if ($Version) { $V = $Version.Split('-') @@ -45,4 +44,4 @@ function Import-TestingHelper{ Import-TestingHelper -AllowPrerelease # Run test by PSD1 file -Invoke-TestingHelper +Invoke-TestingHelper \ No newline at end of file From 3ef019865f159addbea044f85cbbc595656a38a2 Mon Sep 17 00:00:00 2001 From: rulasg Date: Fri, 23 Jun 2023 00:37:09 +0200 Subject: [PATCH 06/11] sync-helper --- sync-helper.ps1 | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/sync-helper.ps1 b/sync-helper.ps1 index df89a42..812a71a 100644 --- a/sync-helper.ps1 +++ b/sync-helper.ps1 @@ -44,7 +44,13 @@ function Save-UrlContentToFile { ) $fileContent = Get-UrlContent -Url $url - $fileContent | Out-ContentToFile -FilePath $filePath - Write-Information -MessageData "Saved content to [$filePath] from [$url]" + # check if $fileContent length is greater than 0 + if ([string]::IsNullOrWhiteSpace($fileContent)) { + Write-Error -Message "Content from [$url] is empty" + return + } else { + $fileContent | Out-ContentToFile -FilePath $filePath + Write-Information -MessageData "Saved content to [$filePath] from [$url]" + } } \ No newline at end of file From d009679c64e7f9fb9cf9cb7580b967a967fdb792 Mon Sep 17 00:00:00 2001 From: rulasg Date: Fri, 23 Jun 2023 00:52:38 +0200 Subject: [PATCH 07/11] ShowTestErrors on test.ps1 adn templatr --- private/templates/template.v3.test.ps1 | 7 +++++-- test.ps1 | 8 +++++--- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/private/templates/template.v3.test.ps1 b/private/templates/template.v3.test.ps1 index 21151eb..ff4e3bb 100644 --- a/private/templates/template.v3.test.ps1 +++ b/private/templates/template.v3.test.ps1 @@ -13,7 +13,10 @@ #> [CmdletBinding()] -param () +param ( + #Switch ShowTestErrors + [Parameter()][switch]$ShowTestErrors +) function Import-TestingHelper{ [CmdletBinding()] @@ -44,4 +47,4 @@ function Import-TestingHelper{ Import-TestingHelper -AllowPrerelease # Run test by PSD1 file -Invoke-TestingHelper \ No newline at end of file +Invoke-TestingHelper -ShowTestErrors:$ShowTestErrors \ No newline at end of file diff --git a/test.ps1 b/test.ps1 index 25f6d32..ff4e3bb 100644 --- a/test.ps1 +++ b/test.ps1 @@ -13,7 +13,10 @@ #> [CmdletBinding()] -param () +param ( + #Switch ShowTestErrors + [Parameter()][switch]$ShowTestErrors +) function Import-TestingHelper{ [CmdletBinding()] @@ -22,7 +25,6 @@ function Import-TestingHelper{ [Parameter()][switch]$AllowPrerelease, [Parameter()][switch]$PassThru ) - if ($Version) { $V = $Version.Split('-') @@ -45,4 +47,4 @@ function Import-TestingHelper{ Import-TestingHelper -AllowPrerelease # Run test by PSD1 file -Invoke-TestingHelper \ No newline at end of file +Invoke-TestingHelper -ShowTestErrors:$ShowTestErrors \ No newline at end of file From c46020822e2b5205eb46ef713b977388ac1d4fed Mon Sep 17 00:00:00 2001 From: rulasg Date: Fri, 23 Jun 2023 01:05:54 +0200 Subject: [PATCH 08/11] Sync templates --- private/templates/template.v3.sync-helper.ps1 | 9 +++++++-- sync-helper.ps1 | 8 +++++--- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/private/templates/template.v3.sync-helper.ps1 b/private/templates/template.v3.sync-helper.ps1 index fc3e019..7fe6558 100644 --- a/private/templates/template.v3.sync-helper.ps1 +++ b/private/templates/template.v3.sync-helper.ps1 @@ -47,7 +47,12 @@ function Save-UrlContentToFile { ) $fileContent = Get-UrlContent -Url $url - $fileContent | Out-ContentToFile -FilePath $filePath - Write-Information -MessageData "Saved content to [$filePath] from [$url]" + if ([string]::IsNullOrWhiteSpace($fileContent)) { + Write-Error -Message "Content from [$url] is empty" + return + } else { + $fileContent | Out-ContentToFile -FilePath $filePath + Write-Information -MessageData "Saved content to [$filePath] from [$url]" + } } \ No newline at end of file diff --git a/sync-helper.ps1 b/sync-helper.ps1 index 812a71a..7fe6558 100644 --- a/sync-helper.ps1 +++ b/sync-helper.ps1 @@ -31,8 +31,11 @@ function Out-ContentToFile { [Parameter(Mandatory=$true)][string]$filePath ) - if ($PSCmdlet.ShouldProcess($filePath, "Save content [{0}] to file" -f $content.Length)) { - $content | Out-File -FilePath $filePath -Force + process{ + + if ($PSCmdlet.ShouldProcess($filePath, "Save content [{0}] to file" -f $content.Length)) { + $content | Out-File -FilePath $filePath -Force + } } } @@ -45,7 +48,6 @@ function Save-UrlContentToFile { $fileContent = Get-UrlContent -Url $url - # check if $fileContent length is greater than 0 if ([string]::IsNullOrWhiteSpace($fileContent)) { Write-Error -Message "Content from [$url] is empty" return From 966fe6f3b278993ab7bd8ef3946f88aefce3759d Mon Sep 17 00:00:00 2001 From: rulasg Date: Fri, 23 Jun 2023 01:15:22 +0200 Subject: [PATCH 09/11] try to fix Scpri Analyzer failing --- .github/workflows/powershell.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/powershell.yml b/.github/workflows/powershell.yml index 10c5cab..ff245c2 100644 --- a/.github/workflows/powershell.yml +++ b/.github/workflows/powershell.yml @@ -36,7 +36,7 @@ jobs: # The below set up runs PSScriptAnalyzer to your entire repository and runs some basic security rules. path: .\ recurse: true - severity: 'Error' + severity: "Error" # Include your own basic security rules. Removing this option will run all the rules # includeRule: '"PSAvoidGlobalAliases", "PSAvoidUsingConvertToSecureStringWithPlainText"' output: results.sarif @@ -45,4 +45,4 @@ jobs: - name: Upload SARIF results file uses: github/codeql-action/upload-sarif@v2 with: - sarif_file: results.sarif + sarif_file: results.sarif \ No newline at end of file From a5d6aa8e3ddfff5d10c9db1e13563b8c9ddacc07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ra=C3=BAl=20Gonz=C3=A1lez?= Date: Fri, 23 Jun 2023 01:18:54 +0200 Subject: [PATCH 10/11] Update private/templates/template.v3.deploy-helper.ps1 --- private/templates/template.v3.deploy-helper.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/private/templates/template.v3.deploy-helper.ps1 b/private/templates/template.v3.deploy-helper.ps1 index 30bb90c..cb29901 100644 --- a/private/templates/template.v3.deploy-helper.ps1 +++ b/private/templates/template.v3.deploy-helper.ps1 @@ -107,7 +107,7 @@ function Invoke-DeployModule { Force = $Force } - Publish-Module-Module @parameters + Publish-Module @parameters if($?){ Write-Information -MessageData "Deployed module [$Name] to PSGallery" From 0d3494d48ceefe2b47b79c41cb40b78f6e6454f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ra=C3=BAl=20Gonz=C3=A1lez?= Date: Fri, 23 Jun 2023 01:19:01 +0200 Subject: [PATCH 11/11] Update private/templates/template.v3.sync-helper.ps1 --- private/templates/template.v3.sync-helper.ps1 | 1 - 1 file changed, 1 deletion(-) diff --git a/private/templates/template.v3.sync-helper.ps1 b/private/templates/template.v3.sync-helper.ps1 index 7fe6558..28354cb 100644 --- a/private/templates/template.v3.sync-helper.ps1 +++ b/private/templates/template.v3.sync-helper.ps1 @@ -32,7 +32,6 @@ function Out-ContentToFile { ) process{ - if ($PSCmdlet.ShouldProcess($filePath, "Save content [{0}] to file" -f $content.Length)) { $content | Out-File -FilePath $filePath -Force }