Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Unit Tests and Official Releases #1854

Merged
merged 6 commits into from
Apr 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 33 additions & 3 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,42 @@ jobs:
build-runspace:
runs-on: windows-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
with:
ref: ${{ github.head_ref }}
- name: Create local changes
shell: pwsh
run: |
powershell.exe -f Compile.ps1
- uses: stefanzweifel/git-auto-commit-action@v4.16.0
Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process
./Compile.ps1
continue-on-error: true
- name: Check for failure in the previous step
if: failure()
run: |
echo "Compile.ps1 failed to execute properly."
exit 1
- uses: stefanzweifel/git-auto-commit-action@v5
with:
commit_message: Compile Winutil
if: success()

create-release:
needs: build-runspace
runs-on: windows-latest
if: github.ref == 'refs/heads/main'
steps:
- uses: actions/checkout@v4
- name: Get current date (PowerShell)
id: date
run: echo "CURRENT_DATE=$(powershell (Get-Date -Format 'yyyy-MM-dd').ToString())" >> $GITHUB_ENV
- name: Create Release and Upload Asset
id: create_release
uses: softprops/action-gh-release@v2
with:
tag_name: ${{ env.CURRENT_DATE }}
name: Release ${{ env.CURRENT_DATE }}
draft: false
prerelease: false
files: ./winutil.ps1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
2 changes: 1 addition & 1 deletion .github/workflows/unittests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:
- name: Run Pester tests
run: |
Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process
Invoke-Pester -Path 'pester/*.Tests.ps1' -EnableExit
Invoke-Pester -Path 'pester/*.Tests.ps1' -Output Detailed

shell: pwsh
env:
Expand Down
75 changes: 43 additions & 32 deletions pester/functions.Tests.ps1
Original file line number Diff line number Diff line change
@@ -1,44 +1,55 @@
#===========================================================================
# Tests - Functions
#===========================================================================

# Get all .ps1 files in the functions folder
$ps1Files = Get-ChildItem -Path ./functions -Filter *.ps1

# Loop through each file
foreach ($file in $ps1Files) {
# Define the test name
$testName = "Syntax check for $($file.Name)"

# Define the test script
$testScript = {
# Import the script
. $file.FullName

# Check if any errors occurred
$scriptError = $error[0]
$scriptError | Should -Be $null
Describe "Comprehensive Checks for PS1 Files in Functions Folder" {
BeforeAll {
# Get all .ps1 files in the functions folder
$ps1Files = Get-ChildItem -Path ./functions -Filter *.ps1 -Recurse
}

# Add the test to the Pester test suite
Describe $testName $testScript
}
foreach ($file in $ps1Files) {
Context "Checking $($file.Name)" {
It "Should import without errors" {
{ . $file.FullName } | Should -Not -Throw
}

Describe "Functions"{
It "Should have no syntax errors" {
$syntaxErrors = $null
$null = [System.Management.Automation.PSParser]::Tokenize((Get-Content -Path $file.FullName -Raw), [ref]$syntaxErrors)
$syntaxErrors.Count | Should -Be 0
}

It "Should not use deprecated cmdlets or aliases" {
$content = Get-Content -Path $file.FullName -Raw
# Example check for a known deprecated cmdlet or alias
$content | Should -Not -Match 'DeprecatedCmdlet'
# Add more checks as needed
}

Get-ChildItem .\functions -Recurse -File | ForEach-Object {
It "Should follow naming conventions for functions" {
$functions = (Get-Command -Path $file.FullName).Name
foreach ($function in $functions) {
$function | Should -Match '^[a-z]+(-[a-z]+)*$' # Enforce lower-kebab-case
}
}

context "$($psitem.BaseName)" {
BeforeEach -Scriptblock {
. $psitem.FullName
It "Should define mandatory parameters for all functions" {
. $file.FullName
$functions = (Get-Command -Path $file.FullName).Name
foreach ($function in $functions) {
$parameters = (Get-Command -Name $function).Parameters.Values
$mandatoryParams = $parameters | Where-Object { $_.Attributes.Mandatory -eq $true }
$mandatoryParams.Count | Should -BeGreaterThan 0
}
}

It "Imports with no errors" -TestCases @{
basename = $($psitem.BaseName)
fullname = $psitem.FullName
} {
Get-ChildItem function:\$basename | Should -Not -BeNullOrEmpty
}
It "Should have all functions available after import" {
. $file.FullName
$functions = (Get-Command -Path $file.FullName).Name
foreach ($function in $functions) {
{ Get-Command -Name $function -CommandType Function } | Should -Not -BeNullOrEmpty
}
}
}
}
}
}
Loading