From 7d2b62df8f3ebff29bba7599e23be4d6fa498e3b Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Mon, 19 Aug 2024 19:10:56 +0200 Subject: [PATCH 1/5] test --- action.yml | 2 ++ scripts/main.ps1 | 45 ++++++++++++++++++++++----------------------- 2 files changed, 24 insertions(+), 23 deletions(-) diff --git a/action.yml b/action.yml index dc26050b..41290371 100644 --- a/action.yml +++ b/action.yml @@ -46,6 +46,8 @@ runs: $passed = . "${{ github.action_path }}\scripts\main.ps1" -Verbose "passed=$passed" | Out-File -FilePath $env:GITHUB_OUTPUT -Append + Write-Host "Passed: [$passed]" + if (-not $passed) { exit 1 } diff --git a/scripts/main.ps1 b/scripts/main.ps1 index 659d130b..6a232111 100644 --- a/scripts/main.ps1 +++ b/scripts/main.ps1 @@ -9,31 +9,30 @@ LogGroup "Loading helper scripts from [$path]" { } } -Start-LogGroup 'Loading inputs' -$moduleName = if ($env:GITHUB_ACTION_INPUT_Name | IsNullOrEmpty) { $env:GITHUB_REPOSITORY_NAME } else { $env:GITHUB_ACTION_INPUT_Name } -Write-Verbose "Module name: [$moduleName]" +LogGroup 'Loading inputs' { + $moduleName = if ($env:GITHUB_ACTION_INPUT_Name | IsNullOrEmpty) { $env:GITHUB_REPOSITORY_NAME } else { $env:GITHUB_ACTION_INPUT_Name } + Write-Verbose "Module name: [$moduleName]" + + $codeToTest = Join-Path -Path $env:GITHUB_WORKSPACE -ChildPath "$env:GITHUB_ACTION_INPUT_Path\$moduleName" + if (Test-Path -Path $codeToTest) { + Write-Verbose "Code to test: [$codeToTest]" + } else { + $codeToTest = Join-Path -Path $env:GITHUB_WORKSPACE -ChildPath $env:GITHUB_ACTION_INPUT_Path + } -$codeToTest = Join-Path -Path $env:GITHUB_WORKSPACE -ChildPath "$env:GITHUB_ACTION_INPUT_Path\$moduleName" -if (Test-Path -Path $codeToTest) { Write-Verbose "Code to test: [$codeToTest]" -} else { - $codeToTest = Join-Path -Path $env:GITHUB_WORKSPACE -ChildPath $env:GITHUB_ACTION_INPUT_Path -} - -Write-Verbose "Code to test: [$codeToTest]" -if (-not (Test-Path -Path $codeToTest)) { - throw "Path [$codeToTest] does not exist." -} -Write-Verbose "Test type to run: [$env:GITHUB_ACTION_INPUT_TestType]" + if (-not (Test-Path -Path $codeToTest)) { + throw "Path [$codeToTest] does not exist." + } + Write-Verbose "Test type to run: [$env:GITHUB_ACTION_INPUT_TestType]" -$testsPath = $env:GITHUB_ACTION_INPUT_TestsPath -Write-Verbose "Path to tests: [$testsPath]" -if (-not (Test-Path -Path $testsPath)) { - throw "Path [$testsPath] does not exist." + $testsPath = $env:GITHUB_ACTION_INPUT_TestsPath + Write-Verbose "Path to tests: [$testsPath]" + if (-not (Test-Path -Path $testsPath)) { + throw "Path [$testsPath] does not exist." + } } -Stop-LogGroup - $params = @{ Path = $codeToTest TestType = $env:GITHUB_ACTION_INPUT_TestType @@ -41,9 +40,9 @@ $params = @{ } $results = Test-PSModule @params -Start-LogGroup 'Test results' -Write-Verbose ($results | Out-String) -Stop-LogGroup +LogGroup 'Test results' { + Write-Verbose ($results | Out-String) +} $failedTests = $results.FailedCount From 4b8d9c5cfea23f77bbc97428585946f51dfe4eac Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Mon, 19 Aug 2024 19:33:36 +0200 Subject: [PATCH 2/5] test --- .github/workflows/Action-Test-Src-Default.yml | 6 ++++++ .github/workflows/Action-Test-Src-WithManifest.yml | 6 ++++++ .github/workflows/Action-Test-outputs.yml | 6 ++++++ 3 files changed, 18 insertions(+) diff --git a/.github/workflows/Action-Test-Src-Default.yml b/.github/workflows/Action-Test-Src-Default.yml index c39a5c5a..b19719b8 100644 --- a/.github/workflows/Action-Test-Src-Default.yml +++ b/.github/workflows/Action-Test-Src-Default.yml @@ -27,9 +27,15 @@ jobs: - name: Action-Test uses: ./ + id: action-test env: GITHUB_TOKEN: ${{ github.token }} with: Name: PSModuleTest Path: tests/src TestType: SourceCode + + - name: Status + shell: pwsh + run: | + Write-Host "Passed: [${{ steps.action-test.outputs.passed }}]" diff --git a/.github/workflows/Action-Test-Src-WithManifest.yml b/.github/workflows/Action-Test-Src-WithManifest.yml index 629b5395..5259b170 100644 --- a/.github/workflows/Action-Test-Src-WithManifest.yml +++ b/.github/workflows/Action-Test-Src-WithManifest.yml @@ -27,9 +27,15 @@ jobs: - name: Action-Test uses: ./ + id: action-test env: GITHUB_TOKEN: ${{ github.token }} with: Name: PSModuleTest Path: tests/srcWithManifest TestType: SourceCode + + - name: Status + shell: pwsh + run: | + Write-Host "Passed: [${{ steps.action-test.outputs.passed }}]" diff --git a/.github/workflows/Action-Test-outputs.yml b/.github/workflows/Action-Test-outputs.yml index ce396005..ee9c6be9 100644 --- a/.github/workflows/Action-Test-outputs.yml +++ b/.github/workflows/Action-Test-outputs.yml @@ -27,9 +27,15 @@ jobs: - name: Action-Test uses: ./ + id: action-test env: GITHUB_TOKEN: ${{ github.token }} with: Name: PSModuleTest Path: tests/outputs/modules TestType: Module + + - name: Status + shell: pwsh + run: | + Write-Host "Passed: [${{ steps.action-test.outputs.passed }}]" From 0a6237e4ceb6b96eb27a8c336c71c9ef5061ebee Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Mon, 19 Aug 2024 19:43:02 +0200 Subject: [PATCH 3/5] test --- action.yml | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/action.yml b/action.yml index 41290371..c8723e52 100644 --- a/action.yml +++ b/action.yml @@ -19,10 +19,6 @@ inputs: description: The path to the tests to run. required: false default: tests - Shell: - description: The shell to use for running the tests. - required: false - default: pwsh outputs: passed: @@ -34,23 +30,25 @@ runs: steps: - name: Run Test-PSModule uses: PSModule/GitHub-Script@v1 + + - name: Run Test-PSModule + shell: pwsh id: test env: GITHUB_ACTION_INPUT_Name: ${{ inputs.Name }} GITHUB_ACTION_INPUT_Path: ${{ inputs.Path }} GITHUB_ACTION_INPUT_TestType: ${{ inputs.TestType }} GITHUB_ACTION_INPUT_TestsPath: ${{ inputs.TestsPath }} - with: - Script: | - # Test-PSModule - $passed = . "${{ github.action_path }}\scripts\main.ps1" -Verbose - "passed=$passed" | Out-File -FilePath $env:GITHUB_OUTPUT -Append + run: | + # Test-PSModule + $passed = . "${{ github.action_path }}\scripts\main.ps1" -Verbose + "passed=$passed" | Out-File -FilePath $env:GITHUB_OUTPUT -Append + + Write-Host "Passed: [$passed]" - Write-Host "Passed: [$passed]" - - if (-not $passed) { - exit 1 - } + if (-not $passed) { + exit 1 + } - name: Upload test results uses: actions/upload-artifact@v4 From eeeb0ebf2cef3d68dfc7b13f0ae5d1b3cd8a5f30 Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Mon, 19 Aug 2024 20:02:55 +0200 Subject: [PATCH 4/5] Fix output in nested call --- scripts/helpers/Test-PSModule.ps1 | 206 ++++++++++++++---------------- 1 file changed, 94 insertions(+), 112 deletions(-) diff --git a/scripts/helpers/Test-PSModule.ps1 b/scripts/helpers/Test-PSModule.ps1 index 84c82f77..198dc431 100644 --- a/scripts/helpers/Test-PSModule.ps1 +++ b/scripts/helpers/Test-PSModule.ps1 @@ -1,6 +1,4 @@ -#REQUIRES -Modules Utilities, PSScriptAnalyzer, Pester - -function Test-PSModule { +function Test-PSModule { <# .SYNOPSIS Performs tests on a module. @@ -27,156 +25,140 @@ function Test-PSModule { $testModule = $TestType -eq 'Module' $moduleTestsPath = Join-Path -Path $env:GITHUB_WORKSPACE -ChildPath $TestsPath - #region Get test kit versions - Start-LogGroup 'Get test kit versions' - $PSSAModule = Get-PSResource -Name PSScriptAnalyzer -Verbose:$false | Sort-Object Version -Descending | Select-Object -First 1 - $pesterModule = Get-PSResource -Name Pester -Verbose:$false | Sort-Object Version -Descending | Select-Object -First 1 - - Write-Verbose 'Testing with:' - Write-Verbose " PowerShell $($PSVersionTable.PSVersion.ToString())" - Write-Verbose " Pester $($pesterModule.version)" - Write-Verbose " PSScriptAnalyzer $($PSSAModule.version)" - Stop-LogGroup - #endregion - - #region Add test - Common - PSScriptAnalyzer - Start-LogGroup 'Add test - Common - PSScriptAnalyzer' - $containers = @() - $PSSATestsPath = Join-Path -Path $PSScriptRoot -ChildPath '..\tests\PSScriptAnalyzer' - $settingsFileName = if ($testModule) { 'Settings.Module.psd1' } else { 'Settings.SourceCode.psd1' } - $settingsFilePath = Join-Path -Path $PSSATestsPath -ChildPath $settingsFileName - $containerParams = @{ - Path = Join-Path $PSSATestsPath 'PSScriptAnalyzer.Tests.ps1' - Data = @{ - Path = $Path - SettingsFilePath = $settingsFilePath - Verbose = $true - } - } - Write-Verbose 'ContainerParams:' - Write-Verbose "$($containerParams | ConvertTo-Json)" - $containers += New-PesterContainer @containerParams - Stop-LogGroup - #endregion + LogGroup 'Get test kit versions' { + $PSSAModule = Get-PSResource -Name PSScriptAnalyzer -Verbose:$false | Sort-Object Version -Descending | Select-Object -First 1 + $pesterModule = Get-PSResource -Name Pester -Verbose:$false | Sort-Object Version -Descending | Select-Object -First 1 - #region Add test - Common - PSModule - Start-LogGroup 'Add test - Common - PSModule' - $containerParams = @{ - Path = Join-Path -Path $PSScriptRoot -ChildPath '..\tests\PSModule\Common.Tests.ps1' - Data = @{ - Path = $Path - Verbose = $true - } + Write-Verbose 'Testing with:' + Write-Verbose " PowerShell $($PSVersionTable.PSVersion.ToString())" + Write-Verbose " Pester $($pesterModule.version)" + Write-Verbose " PSScriptAnalyzer $($PSSAModule.version)" } - Write-Verbose 'ContainerParams:' - Write-Verbose "$($containerParams | ConvertTo-Json)" - $containers += New-PesterContainer @containerParams - Stop-LogGroup - #endregion - #region Add test - Module - PSModule - if ($testModule) { - Start-LogGroup 'Add test - Module - PSModule' + LogGroup 'Add test - Common - PSScriptAnalyzer' { + $containers = @() + $PSSATestsPath = Join-Path -Path $PSScriptRoot -ChildPath '..\tests\PSScriptAnalyzer' + $settingsFileName = if ($testModule) { 'Settings.Module.psd1' } else { 'Settings.SourceCode.psd1' } + $settingsFilePath = Join-Path -Path $PSSATestsPath -ChildPath $settingsFileName $containerParams = @{ - Path = Join-Path -Path $PSScriptRoot -ChildPath '..\tests\PSModule\Module.Tests.ps1' + Path = Join-Path $PSSATestsPath 'PSScriptAnalyzer.Tests.ps1' Data = @{ - Path = $Path - Verbose = $true + Path = $Path + SettingsFilePath = $settingsFilePath + Verbose = $true } } Write-Verbose 'ContainerParams:' Write-Verbose "$($containerParams | ConvertTo-Json)" $containers += New-PesterContainer @containerParams - Stop-LogGroup } - #endregion - #region Add test - SourceCode - PSModule - if ($testSourceCode) { - Start-LogGroup 'Add test - SourceCode - PSModule' + LogGroup 'Add test - Common - PSModule' { $containerParams = @{ - Path = Join-Path -Path $PSScriptRoot -ChildPath '..\tests\PSModule\SourceCode.Tests.ps1' + Path = Join-Path -Path $PSScriptRoot -ChildPath '..\tests\PSModule\Common.Tests.ps1' Data = @{ - Path = $Path - TestsPath = $moduleTestsPath - Verbose = $true + Path = $Path + Verbose = $true } } Write-Verbose 'ContainerParams:' Write-Verbose "$($containerParams | ConvertTo-Json)" $containers += New-PesterContainer @containerParams - Stop-LogGroup } - #endregion - #region Add test - Module - $moduleName if ($testModule) { - if (Test-Path -Path $moduleTestsPath) { - Start-LogGroup "Add test - Module - $moduleName" + LogGroup 'Add test - Module - PSModule' { + $containerParams = @{ + Path = Join-Path -Path $PSScriptRoot -ChildPath '..\tests\PSModule\Module.Tests.ps1' + Data = @{ + Path = $Path + Verbose = $true + } + } + Write-Verbose 'ContainerParams:' + Write-Verbose "$($containerParams | ConvertTo-Json)" + $containers += New-PesterContainer @containerParams + } + } + + if ($testSourceCode) { + LogGroup 'Add test - SourceCode - PSModule' { $containerParams = @{ - Path = $moduleTestsPath + Path = Join-Path -Path $PSScriptRoot -ChildPath '..\tests\PSModule\SourceCode.Tests.ps1' Data = @{ - Path = $Path + Path = $Path + TestsPath = $moduleTestsPath + Verbose = $true } } Write-Verbose 'ContainerParams:' Write-Verbose "$($containerParams | ConvertTo-Json)" $containers += New-PesterContainer @containerParams - Stop-LogGroup + } + } + + if ($testModule) { + if (Test-Path -Path $moduleTestsPath) { + LogGroup "Add test - Module - $moduleName" { + $containerParams = @{ + Path = $moduleTestsPath + Data = @{ + Path = $Path + } + } + Write-Verbose 'ContainerParams:' + Write-Verbose "$($containerParams | ConvertTo-Json)" + $containers += New-PesterContainer @containerParams + } } else { Write-Warning "⚠️ No tests found - [$moduleTestsPath]" } } - #endregion - #region Import module if ((Test-Path -Path $moduleTestsPath) -and $testModule) { - Start-LogGroup 'Install module dependencies' - $moduleManifestPath = Join-Path -Path $Path -ChildPath "$moduleName.psd1" - Resolve-PSModuleDependency -ManifestFilePath $moduleManifestPath - Stop-LogGroup + LogGroup 'Install module dependencies' { + $moduleManifestPath = Join-Path -Path $Path -ChildPath "$moduleName.psd1" + Resolve-PSModuleDependency -ManifestFilePath $moduleManifestPath + } - Start-LogGroup "Importing module: $moduleName" - Add-PSModulePath -Path (Split-Path $Path -Parent) - Get-Module -Name $moduleName -ListAvailable | Remove-Module -Force - Import-Module -Name $moduleName -Force -RequiredVersion '999.0.0' -Global - Stop-LogGroup + LogGroup "Importing module: $moduleName" { + Add-PSModulePath -Path (Split-Path $Path -Parent) + Get-Module -Name $moduleName -ListAvailable | Remove-Module -Force + Import-Module -Name $moduleName -Force -RequiredVersion '999.0.0' -Global + } } - #endregion - #region Pester config - Start-LogGroup 'Pester config' - $pesterParams = @{ - Configuration = @{ - Run = @{ - Path = $Path - Container = $containers - PassThru = $true - } - TestResult = @{ - Enabled = $testModule - OutputFormat = 'NUnitXml' - OutputPath = Join-Path -Path $env:GITHUB_WORKSPACE -ChildPath 'outputs\Test-Report.xml' - TestSuiteName = 'Unit tests' - } - CodeCoverage = @{ - Enabled = $testModule - OutputPath = Join-Path -Path $env:GITHUB_WORKSPACE -ChildPath 'outputs\CodeCoverage-Report.xml' - OutputFormat = 'JaCoCo' - OutputEncoding = 'UTF8' - CoveragePercentTarget = 75 - } - Output = @{ - CIFormat = 'Auto' - StackTraceVerbosity = 'None' - Verbosity = 'Detailed' + LogGroup 'Pester config' { + $pesterParams = @{ + Configuration = @{ + Run = @{ + Path = $Path + Container = $containers + PassThru = $true + } + TestResult = @{ + Enabled = $testModule + OutputFormat = 'NUnitXml' + OutputPath = Join-Path -Path $env:GITHUB_WORKSPACE -ChildPath 'outputs\Test-Report.xml' + TestSuiteName = 'Unit tests' + } + CodeCoverage = @{ + Enabled = $testModule + OutputPath = Join-Path -Path $env:GITHUB_WORKSPACE -ChildPath 'outputs\CodeCoverage-Report.xml' + OutputFormat = 'JaCoCo' + OutputEncoding = 'UTF8' + CoveragePercentTarget = 75 + } + Output = @{ + CIFormat = 'Auto' + StackTraceVerbosity = 'None' + Verbosity = 'Detailed' + } } } + Write-Verbose 'PesterParams:' + Write-Verbose "$($pesterParams | ConvertTo-Json -Depth 4 -WarningAction SilentlyContinue)" } - Write-Verbose 'PesterParams:' - Write-Verbose "$($pesterParams | ConvertTo-Json -Depth 4 -WarningAction SilentlyContinue)" - Stop-LogGroup - #endregion #region Run tests $verbosepref = $VerbosePreference From 61bc80372e8b203bd09e70be2ccb5d8dde1d046a Mon Sep 17 00:00:00 2001 From: Marius Storhaug Date: Mon, 19 Aug 2024 21:19:14 +0200 Subject: [PATCH 5/5] Fix tests --- .github/workflows/Action-Test-Src-Default.yml | 6 +++++- .github/workflows/Action-Test-Src-WithManifest.yml | 6 +++++- .github/workflows/Action-Test-outputs.yml | 6 +++++- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/.github/workflows/Action-Test-Src-Default.yml b/.github/workflows/Action-Test-Src-Default.yml index b19719b8..5925dfff 100644 --- a/.github/workflows/Action-Test-Src-Default.yml +++ b/.github/workflows/Action-Test-Src-Default.yml @@ -38,4 +38,8 @@ jobs: - name: Status shell: pwsh run: | - Write-Host "Passed: [${{ steps.action-test.outputs.passed }}]" + $passed = '${{ steps.action-test.outputs.passed }}' + Write-Host "Passed: [$Passed]" + if ($passed -eq 'False') { + exit 1 + } diff --git a/.github/workflows/Action-Test-Src-WithManifest.yml b/.github/workflows/Action-Test-Src-WithManifest.yml index 5259b170..925b2190 100644 --- a/.github/workflows/Action-Test-Src-WithManifest.yml +++ b/.github/workflows/Action-Test-Src-WithManifest.yml @@ -38,4 +38,8 @@ jobs: - name: Status shell: pwsh run: | - Write-Host "Passed: [${{ steps.action-test.outputs.passed }}]" + $passed = '${{ steps.action-test.outputs.passed }}' + Write-Host "Passed: [$Passed]" + if ($passed -eq 'False') { + exit 1 + } diff --git a/.github/workflows/Action-Test-outputs.yml b/.github/workflows/Action-Test-outputs.yml index ee9c6be9..030ad6a3 100644 --- a/.github/workflows/Action-Test-outputs.yml +++ b/.github/workflows/Action-Test-outputs.yml @@ -38,4 +38,8 @@ jobs: - name: Status shell: pwsh run: | - Write-Host "Passed: [${{ steps.action-test.outputs.passed }}]" + $passed = '${{ steps.action-test.outputs.passed }}' + Write-Host "Passed: [$Passed]" + if ($passed -eq 'False') { + exit 1 + }