Skip to content

Commit

Permalink
🩹 [Patch]: Don't require source code to be in a module folder. (#45)
Browse files Browse the repository at this point in the history
## Description

- Don't require source code in a module folder.
- Add param for TestsPath.
- Add docs for outputs and TestsPath = 'tests'.

## Type of change

<!-- Use the check-boxes [x] on the options that are relevant. -->

- [ ] 📖 [Docs]
- [ ] 🪲 [Fix]
- [x] 🩹 [Patch]
- [ ] ⚠️ [Security fix]
- [ ] 🚀 [Feature]
- [ ] 🌟 [Breaking change]

## Checklist

<!-- Use the check-boxes [x] on the options that are relevant. -->

- [x] I have performed a self-review of my own code
- [x] I have commented my code, particularly in hard-to-understand areas
  • Loading branch information
MariusStorhaug authored Mar 29, 2024
1 parent fc14511 commit 276b770
Show file tree
Hide file tree
Showing 23 changed files with 39 additions and 10 deletions.
21 changes: 14 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,21 @@ The action runs the following the Pester test framework:
- [PSScriptAnalyzer tests](https://learn.microsoft.com/en-us/powershell/utility-modules/psscriptanalyzer/rules/readme?view=ps-modules)
- [PSModule framework tests](#psmodule-tests)
- If `TestType` is set to `Module`:
- The module manifest is:
- tested using [Test-ModuleManifest](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/test-modulemanifest).
- temporarily altered to version `999.0.0` to avoid version conflicts when running for dependencies of the framework.
- The module manifest is tested using [Test-ModuleManifest](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/test-modulemanifest).
- The module is imported.
- Custom module tests from the `tests` directory in the module repository are run.
- CodeCoverage is calculated.
- The following reports are calculated and uploaded as artifacts:
- Test suite results.
- Code coverage results.
- If `TestType` is set to `SourceCode`:
- The source code is tested with:
- `PSScriptAnalyzer` for best practices, using custom settings.
- `PSModule.SourceCode` for other PSModule standards.
- The action returns a `passed` output that is `true` if all tests pass, else `false`.
- The following reports are calculated and uploaded as artifacts:
- Test suite results.
- Code coverage results.

The action fails if any of the tests fail or it fails to run the tests.
This is mitigated by the `continue-on-error` option in the workflow.

## How to use it

Expand Down Expand Up @@ -97,11 +97,18 @@ jobs:

| Name | Description | Required | Default |
| ---- | ----------- | -------- | ------- |
| `Path` | The path to the module to test. | `true` | |
| `Path` | The path to the code to test. | `true` | |
| `TestType` | The type of tests to run. Can be either `Module` or `SourceCode`. | `true` | |
| `Name` | The name of the module to test. The name of the repository is used if not specified. | `false` | |
| `TestsPath` | The path to the tests to run. | `false` | `tests` |
| `Shell` | The shell to use for running the tests. | `false` | `pwsh` |

### Outputs

| Name | Description | Possible values |
| ---- | ----------- | --------------- |
| `passed` | If the tests passed. | `true`, `false` |

## PSModule tests

The [PSModule framework tests](https://github.com/PSModule/Test-PSModule/blob/main/scripts/tests/PSModule/PSModule.Tests.ps1) verifies the following coding practices that the framework enforces:
Expand Down
7 changes: 6 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@ inputs:
description: The name of the module to test. The name of the repository is used if not specified.
required: false
Path:
description: The path to the module to test.
description: The path to the code to test.
required: true
TestType:
description: The type of tests to run. Can be either 'Module' or 'SourceCode'.
required: true
TestsPath:
description: The path to the tests to run.
required: false
default: tests
Shell:
description: The shell to use for running the tests.
required: false
Expand All @@ -35,6 +39,7 @@ runs:
GITHUB_ACTION_INPUT_Name: ${{ inputs.Name }}
GITHUB_ACTION_INPUT_Path: ${{ inputs.Path }}
GITHUB_ACTION_INPUT_TestType: ${{ inputs.TestType }}
GITHUB_ACTION_INPUT_TestsPath: ${{ inputs.TestsPath }}
run: |
# Test-PSModule
$passed = . "$env:GITHUB_ACTION_PATH\scripts\main.ps1" -Verbose
Expand Down
8 changes: 6 additions & 2 deletions scripts/helpers/Test-PSModule.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,17 @@ function Test-PSModule {
# Run module tests.
[Parameter()]
[ValidateSet('SourceCode', 'Module')]
[string] $TestType = 'SourceCode'
[string] $TestType = 'SourceCode',

# Path to the folder where the tests are located.
[Parameter()]
[string] $TestsPath = 'tests'
)

$moduleName = Split-Path -Path $Path -Leaf
$testSourceCode = $TestType -eq 'SourceCode'
$testModule = $TestType -eq 'Module'
$moduleTestsPath = Join-Path $env:GITHUB_WORKSPACE 'tests'
$moduleTestsPath = Join-Path -Path $env:GITHUB_WORKSPACE -ChildPath $TestsPath

#region Get test kit versions
Start-LogGroup 'Get test kit versions'
Expand Down
13 changes: 13 additions & 0 deletions scripts/main.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,30 @@ $moduleName = if ($env:GITHUB_ACTION_INPUT_Name | IsNullOrEmpty) { $env:GITHUB_R
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
}

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]"

$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
TestsPath = $testsPath
}
$results = Test-PSModule @params

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 276b770

Please sign in to comment.