From d54ef12472672c577c5b0b40699b68ba83aff910 Mon Sep 17 00:00:00 2001 From: Ryan Johnson Date: Sat, 23 Sep 2023 02:25:41 -0400 Subject: [PATCH] chore(ci): update tests (#234) - Updates the basic tests to be generic and discoverable for pull requests. - Tests that the module can be imported and that the manifest is well formed. - Updated to Pester 5 syntax with detailed output. Signed-off-by: Ryan Johnson --- .ci/pester.tests.ps1 | 10 ++--- .github/workflows/ci-module.yml | 74 ++++++++++++++------------------- .github/workflows/tests.yml | 42 +++++++++++++++++++ 3 files changed, 76 insertions(+), 50 deletions(-) create mode 100644 .github/workflows/tests.yml diff --git a/.ci/pester.tests.ps1 b/.ci/pester.tests.ps1 index ab332f21..dbfa8c5c 100644 --- a/.ci/pester.tests.ps1 +++ b/.ci/pester.tests.ps1 @@ -1,16 +1,12 @@ -BeforeAll { - Import-Module -Name "$PSScriptRoot/../PowerVCF.psd1" -Force -ErrorAction Stop -} - -Describe -Tag:('ModuleValidation') 'Module Baseline Validation' { +Describe -Tag:('ModuleValidation') 'Module Basic Tests' { It 'is present' { - $module = Get-Module PowerVCF + $module = Get-Module -Name $moduleName $module | Should -Be $true } It ('passes Test-ModuleManifest') { - Test-ModuleManifest -Path:("$PSScriptRoot/../PowerVCF.psd1") | Should -Not -BeNullOrEmpty + Test-ModuleManifest -Path $moduleManifest | Should -Not -BeNullOrEmpty $? | Should -Be $true } } diff --git a/.github/workflows/ci-module.yml b/.github/workflows/ci-module.yml index bbf71890..4878e45d 100644 --- a/.github/workflows/ci-module.yml +++ b/.github/workflows/ci-module.yml @@ -1,54 +1,42 @@ ---- -name: Module CI +name: Tests on: pull_request: - branches: [main] + branches: [develop] paths: - - '**.psm1' - - '**.psd1' + - "**.psm1" + - "**.psd1" jobs: - test-pwshcore-linux: + basic_tests: strategy: matrix: platform: [ubuntu-latest] runs-on: ${{ matrix.platform }} steps: - - uses: actions/checkout@3df4ab11eba7bda6032a0b82a6bb43b11571feac # v3.5.3v - - name: Run Pester Tests (pwsh) - run: | - Write-Output $PSVersionTable.PSVersion.Major $PSVersionTable.PSRemotingProtocolVersion.Minor - Set-PSRepository psgallery -InstallationPolicy trusted - Install-Module -Name VMware.PowerCLI -confirm:$false -Force - Install-Module -Name Pester -confirm:$false -Force - Invoke-Pester -Path "./.ci/pester.tests.ps1" -EnableExit - shell: pwsh - - # test-pwshcore-windows: - # strategy: - # matrix: - # platform: [windows-latest] - # runs-on: ${{ matrix.platform }} - # steps: - # - uses: actions/checkout@v3 - # - name: Run Pester Tests (pwsh) - # run: | - # Write-Output $PSVersionTable.PSVersion.Major $PSVersionTable.PSRemotingProtocolVersion.Minor - # Set-PSRepository psgallery -InstallationPolicy trusted - # Install-Module -Name Pester -confirm:$false -Force - # Invoke-Pester -Path "./.ci/pester.test.ps1" -EnableExit - # shell: pwsh - - # test-powershell-windows: - # runs-on: windows-latest - # steps: - # - uses: actions/checkout@v3 - # - name: Run Pester Tests (Windows PowerShell) - # run: | - # Write-Output $PSVersionTable.PSVersion.Major $PSVersionTable.PSRemotingProtocolVersion.Minor - # Set-PSRepository psgallery -InstallationPolicy trusted - # Install-Module -Name Pester -Confirm:$false -Force - # Invoke-Pester -Path "./.ci/pester.tests.ps1" -EnableExit - # if ($Error[0].Fullyqualifiederrorid -eq 'PesterAssertionFailed') {exit 1} - # shell: powershell + - uses: actions/checkout@3df4ab11eba7bda6032a0b82a6bb43b11571feac # v4.0.0 + - name: Run Basic Tests + working-directory: ${{ github.workspace }} + run: | + $moduleManifest = (Get-ChildItem -Path $env:GITHUB_WORKSPACE -Filter *.psd1).Name + if ($moduleManifest) { + Write-Output "SUCCESS: Manifest '$moduleManifest' found in '$env:GITHUB_WORKSPACE'." + } else { + Write-Output "FAILURE: Manifest not found in '$env:GITHUB_WORKSPACE'." + } + if ($moduleManifest -match '^(.*)\.psd1$') { + $moduleName = $Matches[1] + Write-Output "SUCCESS: Determining module name from manifest'$moduleManifest'." + } else { + Write-Error "FAILED: Determining module name from manifest '$moduleManifest'." + } + Import-Module -Name (Resolve-Path $moduleManifest).Path -Force -ErrorAction Stop + if (Get-Module -Name $moduleName) { + Write-Output "SUCCESS: Module '$moduleName' was imported." + } else { + Write-Error "FAILED: Module '$moduleName' was not imported." + } + Set-PSRepository psgallery -InstallationPolicy trusted + Install-Module -Name Pester -confirm:$false -Force + Invoke-Pester -Path "./.ci/pester.tests.ps1" -Output Detailed -PassThru + shell: pwsh diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml new file mode 100644 index 00000000..4878e45d --- /dev/null +++ b/.github/workflows/tests.yml @@ -0,0 +1,42 @@ +name: Tests + +on: + pull_request: + branches: [develop] + paths: + - "**.psm1" + - "**.psd1" + +jobs: + basic_tests: + strategy: + matrix: + platform: [ubuntu-latest] + runs-on: ${{ matrix.platform }} + steps: + - uses: actions/checkout@3df4ab11eba7bda6032a0b82a6bb43b11571feac # v4.0.0 + - name: Run Basic Tests + working-directory: ${{ github.workspace }} + run: | + $moduleManifest = (Get-ChildItem -Path $env:GITHUB_WORKSPACE -Filter *.psd1).Name + if ($moduleManifest) { + Write-Output "SUCCESS: Manifest '$moduleManifest' found in '$env:GITHUB_WORKSPACE'." + } else { + Write-Output "FAILURE: Manifest not found in '$env:GITHUB_WORKSPACE'." + } + if ($moduleManifest -match '^(.*)\.psd1$') { + $moduleName = $Matches[1] + Write-Output "SUCCESS: Determining module name from manifest'$moduleManifest'." + } else { + Write-Error "FAILED: Determining module name from manifest '$moduleManifest'." + } + Import-Module -Name (Resolve-Path $moduleManifest).Path -Force -ErrorAction Stop + if (Get-Module -Name $moduleName) { + Write-Output "SUCCESS: Module '$moduleName' was imported." + } else { + Write-Error "FAILED: Module '$moduleName' was not imported." + } + Set-PSRepository psgallery -InstallationPolicy trusted + Install-Module -Name Pester -confirm:$false -Force + Invoke-Pester -Path "./.ci/pester.tests.ps1" -Output Detailed -PassThru + shell: pwsh