Skip to content

Commit 886f51f

Browse files
🚀 [Feature]: Add passed output and continue if fails (#39)
## Description - Add output `passed`, containing if the tests passed. - Add `Resolve-PSModuleDependency` to install and import all requirements before testing the module. ## Type of change <!-- Use the check-boxes [x] on the options that are relevant. --> - [ ] 📖 [Docs] - [ ] 🪲 [Fix] - [ ] 🩹 [Patch] - [ ] ⚠️ [Security fix] - [x] 🚀 [Feature] - [ ] 🌟 [Breaking change] ## Checklist <!-- Use the check-boxes [x] on the options that are relevant. --> - [ ] I have performed a self-review of my own code - [ ] I have commented my code, particularly in hard-to-understand areas
1 parent 4c10a36 commit 886f51f

File tree

5 files changed

+103
-10
lines changed

5 files changed

+103
-10
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#Documentation: https://github.com/PowerShell/PSScriptAnalyzer/blob/master/docs/Cmdlets/Invoke-ScriptAnalyzer.md#-settings
2+
@{
3+
#CustomRulePath='path\to\CustomRuleModule.psm1'
4+
#RecurseCustomRulePath='path\of\customrules'
5+
#Severity = @(
6+
# 'Error'
7+
# 'Warning'
8+
#)
9+
#IncludeDefaultRules=${true}
10+
ExcludeRules = @(
11+
'PSAvoidUsingWriteHost'
12+
)
13+
#IncludeRules = @(
14+
# 'PSAvoidUsingWriteHost',
15+
# 'MyCustomRuleName'
16+
#)
17+
}

action.yml

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,29 +20,40 @@ inputs:
2020
required: false
2121
default: pwsh
2222

23+
outputs:
24+
passed:
25+
description: If the tests passed.
26+
value: ${{ steps.test.outputs.passed }}
27+
2328
runs:
2429
using: composite
2530
steps:
2631
- name: Run Test-PSModule
32+
id: test
2733
shell: ${{ inputs.Shell }}
2834
env:
2935
GITHUB_ACTION_INPUT_Name: ${{ inputs.Name }}
3036
GITHUB_ACTION_INPUT_Path: ${{ inputs.Path }}
3137
GITHUB_ACTION_INPUT_TestType: ${{ inputs.TestType }}
3238
run: |
3339
# Test-PSModule
34-
. "$env:GITHUB_ACTION_PATH\scripts\main.ps1" -Verbose
40+
$passed = . "$env:GITHUB_ACTION_PATH\scripts\main.ps1" -Verbose
41+
"passed=$passed" | Out-File -FilePath $env:GITHUB_OUTPUT -Append
42+
43+
if (-not $passed) {
44+
exit 1
45+
}
3546
3647
- name: Upload test results
3748
uses: actions/upload-artifact@v4
38-
if: ${{ inputs.TestType == 'Module' }}
49+
if: ${{ inputs.TestType == 'Module' && (success() || failure()) }}
3950
with:
4051
name: ${{ runner.os }}-${{ inputs.Shell }}-Test-Report
4152
path: ${{ github.workspace }}/outputs/Test-Report.xml
4253

4354
- name: Upload code coverage report
4455
uses: actions/upload-artifact@v4
45-
if: ${{ inputs.TestType == 'Module' }}
56+
if: ${{ inputs.TestType == 'Module' && (success() || failure()) }}
4657
with:
4758
name: ${{ runner.os }}-${{ inputs.Shell }}-CodeCoverage-Report
4859
path: ${{ github.workspace }}/outputs/CodeCoverage-Report.xml
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
function Resolve-PSModuleDependency {
2+
<#
3+
.SYNOPSIS
4+
Resolve dependencies for a module based on the manifest file.
5+
6+
.DESCRIPTION
7+
Resolve dependencies for a module based on the manifest file, following PSModuleInfo structure
8+
9+
.EXAMPLE
10+
Resolve-PSModuleDependency -Path 'C:\MyModule\MyModule.psd1'
11+
12+
Installs all modules defined in the manifest file, following PSModuleInfo structure.
13+
14+
.NOTES
15+
Should later be adapted to support both pre-reqs, and dependencies.
16+
Should later be adapted to take 4 parameters sets: specific version ("requiredVersion" | "GUID"), latest version ModuleVersion,
17+
and latest version within a range MinimumVersion - MaximumVersion.
18+
#>
19+
[Alias('Resolve-PSModuleDependencies')]
20+
[CmdletBinding()]
21+
param(
22+
# The path to the manifest file.
23+
[Parameter(Mandatory)]
24+
[string] $ManifestFilePath
25+
)
26+
27+
Write-Verbose 'Resolving dependencies'
28+
29+
$manifest = Import-PowerShellDataFile -Path $ManifestFilePath
30+
Write-Verbose "Reading [$ManifestFilePath]"
31+
Write-Verbose "Found [$($manifest.RequiredModules.Count)] modules to install"
32+
33+
foreach ($requiredModule in $manifest.RequiredModules) {
34+
$installParams = @{}
35+
36+
if ($requiredModule -is [string]) {
37+
$installParams.Name = $requiredModule
38+
} else {
39+
$installParams.Name = $requiredModule.ModuleName
40+
$installParams.MinimumVersion = $requiredModule.ModuleVersion
41+
$installParams.RequiredVersion = $requiredModule.RequiredVersion
42+
$installParams.MaximumVersion = $requiredModule.MaximumVersion
43+
}
44+
$installParams.Force = $true
45+
$installParams.Verbose = $false
46+
47+
Write-Verbose "[$($installParams.Name)] - Installing module"
48+
$VerbosePreferenceOriginal = $VerbosePreference
49+
$VerbosePreference = 'SilentlyContinue'
50+
Install-Module @installParams -AllowPrerelease:$false
51+
$VerbosePreference = $VerbosePreferenceOriginal
52+
Write-Verbose "[$($installParams.Name)] - Importing module"
53+
$VerbosePreferenceOriginal = $VerbosePreference
54+
$VerbosePreference = 'SilentlyContinue'
55+
Import-Module @installParams
56+
$VerbosePreference = $VerbosePreferenceOriginal
57+
Write-Verbose "[$($installParams.Name)] - Done"
58+
}
59+
Write-Verbose 'Resolving dependencies - Done'
60+
}

scripts/helpers/Test-PSModule.ps1

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,7 @@ function Test-PSModule {
111111
$containerParams = @{
112112
Path = $moduleTestsPath
113113
Data = @{
114-
Path = $Path
115-
Verbose = $true
114+
Path = $Path
116115
}
117116
}
118117
Write-Verbose 'ContainerParams:'
@@ -127,6 +126,11 @@ function Test-PSModule {
127126

128127
#region Import module
129128
if ((Test-Path -Path $moduleTestsPath) -and $testModule) {
129+
Start-LogGroup 'Install module dependencies'
130+
$moduleManifestPath = Join-Path -Path $Path -ChildPath "$moduleName.psd1"
131+
Resolve-PSModuleDependency -ManifestFilePath $moduleManifestPath
132+
Stop-LogGroup
133+
130134
Start-LogGroup "Importing module: $moduleName"
131135
Add-PSModulePath -Path (Split-Path $Path -Parent)
132136
Get-Module -Name $moduleName -ListAvailable | Remove-Module -Force

scripts/main.ps1

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,14 @@ Stop-LogGroup
3434
$failedTests = $results.FailedCount
3535

3636
if ($failedTests -gt 0) {
37-
Write-Output '::error::❌ Some tests failed.'
38-
exit $failedTests
37+
Write-Host '::error::❌ Some tests failed.'
38+
return $false
3939
}
4040
if ($results.Result -ne 'Passed') {
41-
Write-Output '::error::❌ Some tests failed.'
42-
exit 1
41+
Write-Host '::error::❌ Some tests failed.'
42+
return $false
4343
}
4444
if ($failedTests -eq 0) {
45-
Write-Output '::notice::✅ All tests passed.'
45+
Write-Host '::notice::✅ All tests passed.'
46+
return $true
4647
}

0 commit comments

Comments
 (0)