Skip to content

Commit

Permalink
Add integration tests (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
johlju authored Aug 2, 2024
1 parent 9ad45fc commit 42526bf
Show file tree
Hide file tree
Showing 50 changed files with 2,919 additions and 210 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/code-analysis-built-module.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
- name: Install GitVersion
shell: pwsh
run: |
dotnet tool install --global GitVersion.Tool
dotnet tool install --global GitVersion.Tool --version 5.*
- name: Run GitVersion
shell: pwsh
run: |
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/code-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
- name: Install GitVersion
shell: pwsh
run: |
dotnet tool install --global GitVersion.Tool
dotnet tool install --global GitVersion.Tool --version 5.*
- name: Run GitVersion
shell: pwsh
run: |
Expand Down
52 changes: 34 additions & 18 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,42 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Public commands:
- `Convert-PesterSyntax`
- Add support for Should operators:
- Be
- BeExactly
- BeFalse
- BeGreaterOrEqual
- BeGreaterThan
- BeIn
- BeLessOrEqual
- BeLessThan
- BeLike
- BeLikeExactly
- BeNullOrEmpty
- BeOfType
- BeTrue
- Contain
- Match
- MatchExactly
- Throw
- Add support for Should operators:
- Be
- BeExactly
- BeFalse
- BeGreaterOrEqual
- BeGreaterThan
- BeIn
- BeLessOrEqual
- BeLessThan
- BeLike
- BeLikeExactly
- BeNullOrEmpty
- BeOfType
- BeTrue
- Contain
- Match
- MatchExactly
- Throw
- Added new parameter `OutputPath` to write the resulting file to
a separate path.
- Add integration tests.

### Fixed

- Improve code to resolve ScriptAnalyzer warnings and errors.
- Localize all the strings.
- `Convert-PesterSyntax`
- The `Should` operators `BeLike` and `BeLikeExactly` was mistakenly not
calling their respectively conversion function.
- Correctly handle abbreviated named parameters.
- `Should -BeFalse`, `Should -BeTrue` and `Should -BeNullOrEmpty` are now
correctly converted when `Because` is the only positional parameter.
- Negated `Should -Not -BeLessThan` now converts to `Should-BeGreaterThanOrEqual`
to correctly handle scenario when actual value and expected value are the same.
- Negated `Should -Not -BeGreaterThan` now converts to `Should-BeLessThanOrEqual`
to correctly handle scenario when actual value and expected value are the same.
- Fix parameter name in `Convert-ShouldBeOfType`
- Minor change to `Get-AstDefinition` to handle when a file is not correctly
parsed.
108 changes: 66 additions & 42 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ variables:
defaultBranch: main
Agent.Source.Git.ShallowFetchDepth: 0

# cSpell: ignore setvariable updatebuildnumber hqrmtest quickconfig viscalyx
# cSpell: ignore setvariable updatebuildnumber hqrmtest quickconfig viscalyx psresourceget
stages:
- stage: Build
jobs:
Expand All @@ -30,7 +30,7 @@ stages:
vmImage: 'windows-latest'
steps:
- pwsh: |
dotnet tool install --global GitVersion.Tool
dotnet tool install --global GitVersion.Tool --version 5.*
$gitVersionObject = dotnet-gitversion | ConvertFrom-Json
$gitVersionObject.PSObject.Properties.ForEach{
Write-Host -Object "Setting Task Variable '$($_.Name)' with value '$($_.Value)'."
Expand Down Expand Up @@ -251,46 +251,70 @@ stages:
bash <(curl -s https://codecov.io/bash) -f "./$(buildFolderName)/$(testResultFolderName)/JaCoCo_coverage.xml" -F unit
displayName: 'Publish Code Coverage to Codecov.io'
# - job: Test_Integration
# displayName: 'Integration'
# strategy:
# matrix:
# WIN2019_PWSH:
# JOB_VMIMAGE: 'windows-2019'
# PWSH: true
# WIN2022_PWSH:
# JOB_VMIMAGE: 'windows-2022'
# PWSH: true
# UBUNTU:
# JOB_VMIMAGE: 'ubuntu-latest'
# PWSH: true
# MACOS:
# JOB_VMIMAGE: 'macos-latest'
# PWSH: true
# pool:
# vmImage: $(JOB_VMIMAGE)
# timeoutInMinutes: 0
# steps:
# - task: DownloadPipelineArtifact@2
# displayName: 'Download Build Artifact'
# inputs:
# buildType: 'current'
# artifactName: $(buildArtifactName)
# targetPath: '$(Build.SourcesDirectory)/$(buildFolderName)'
# - task: PowerShell@2
# name: test
# displayName: 'Run Integration Test'
# inputs:
# filePath: './build.ps1'
# arguments: "-Tasks test -CodeCoverageThreshold 0 -PesterPath 'tests/Integration' -PesterExcludeTag @('BootstrapScript','DSC')"
# pwsh: $(PWSH)
# - task: PublishTestResults@2
# displayName: 'Publish Test Results'
# condition: succeededOrFailed()
# inputs:
# testResultsFormat: 'NUnit'
# testResultsFiles: '$(buildFolderName)/$(testResultFolderName)/NUnit*.xml'
# testRunTitle: 'Integration ($(Agent.JobName))'
- job: Test_Integration
displayName: 'Integration'
strategy:
matrix:
WINDOWS:
JOB_VMIMAGE: 'windows-latest'
UBUNTU:
JOB_VMIMAGE: 'ubuntu-latest'
MACOS:
JOB_VMIMAGE: 'macos-latest'
pool:
vmImage: $(JOB_VMIMAGE)
timeoutInMinutes: 0
steps:
- task: DownloadPipelineArtifact@2
displayName: 'Download Build Artifact'
inputs:
buildType: 'current'
artifactName: $(buildArtifactName)
targetPath: '$(Build.SourcesDirectory)/$(buildFolderName)'
- pwsh: |
./build.ps1 -Tasks noop
New-Item -ItemType Directory -Path (Get-PSModulePath -Scope CurrentUser) -Force
name: currentUserModulePath
displayName: 'Validate CurrentUser Module Path'
- pwsh: |
& ([ScriptBlock]::Create((iwr 'bit.ly/psresourceget'))) -Force -Verbose
Get-Module -Name Microsoft.PowerShell.PSResourceGet -ListAvailable
name: bootstrapPSResourceGet
displayName: 'Bootstrap PSResourceGet'
- pwsh: |
Install-PSResource -Name 'Pester' -Version '[5.0,6.0)' -TrustRepository -ErrorAction 'Stop' -Confirm:$false -PassThru
Import-Module -Name Pester -MaximumVersion '5.*' -ErrorAction 'Stop' -PassThru
Invoke-Pester -Path '.\tests\Integration\Syntax\v5' -Output Detailed
name: validatePesterSyntax_v5
displayName: 'Validate Pester 5 Syntax'
- pwsh: |
./build.ps1 -Tasks noop
Import-Module -Name PesterConverter -PassThru
New-Item -ItemType Directory -Path './ConversionTest/v6' -Force
Convert-PesterSyntax -Path (Get-ChildItem -Path './tests/Integration/Syntax/v5') -OutputPath './ConversionTest/v6' -Force -Verbose
name: convert_v5_to_v6
displayName: 'Convert v5 syntax to v6'
- task: PublishPipelineArtifact@1
displayName: 'Publish Converted v6 Artifact'
inputs:
targetPath: './ConversionTest/v6'
artifact: 'ConvertedTests_v6_$(System.JobName)_Attempt_$(System.JobAttempt)'
publishLocation: 'pipeline'
parallel: true
- pwsh: |
Install-PSResource -Name 'Pester' -Prerelease -TrustRepository -ErrorAction 'Stop' -Confirm:$false -PassThru
Import-Module -Name Pester -MaximumVersion '6.*' -ErrorAction 'Stop' -PassThru
Invoke-Pester -Path './ConversionTest/v6' -Output Detailed
name: validatePesterSyntax_v6
displayName: 'Validate Pester 6 Syntax'
## Currently there are no test result to upload since we are not using the normal test pipeline.
# - task: PublishTestResults@2
# displayName: 'Publish Test Results'
# condition: succeededOrFailed()
# inputs:
# testResultsFormat: 'NUnit'
# testResultsFiles: '$(buildFolderName)/$(testResultFolderName)/NUnit*.xml'
# testRunTitle: 'Integration ($(Agent.JobName))'

- stage: Deploy
dependsOn: Test
Expand Down
23 changes: 18 additions & 5 deletions source/Private/Convert-ShouldBeFalse.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -119,19 +119,32 @@ function Convert-ShouldBeFalse
If a previous positional parameter is missing then the ones behind
it cannot be set to positional.
#>
if ($commandParameters.ActualValue)
if ($commandParameters.Because)
{
$commandParameters.ActualValue.Positional = $true
$commandParameters.Because.Positional = $true

if ($commandParameters.Because)
if ($commandParameters.ActualValue)
{
$commandParameters.Because.Positional = $true
$commandParameters.ActualValue.Positional = $true
}
}
}

$newExtentText += $commandParameters.ActualValue.Positional ? (' {0}' -f $commandParameters.ActualValue.ExtentText) : ''
$newExtentText += $commandParameters.Because.Positional ? (' {0}' -f $commandParameters.Because.ExtentText) : ''

if ($commandParameters.Because -and $commandParameters.Because.Positional)
{
# Only add second positional if the first positional was present.
if ($commandParameters.ActualValue.Positional)
{
$newExtentText += ' {0}' -f $commandParameters.Because.ExtentText
}
else
{
# First positional parameter was not present, so set the second to named parameter.
$commandParameters.Because.Positional = $false
}
}

# Holds the new parameter names so they can be added in alphabetical order.
$parameterNames = @()
Expand Down
12 changes: 10 additions & 2 deletions source/Private/Convert-ShouldBeGreaterThan.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,19 @@
Pester 6 Syntax:
Should-BeGreaterThan [[-Actual] <Object>] [-Expected] <Object> [-Because <String>]
Should-BeLessThan [[-Actual] <Object>] [-Expected] <Object> [-Because <String>]
Should-BeLessThanOrEqual [[-Actual] <Object>] [-Expected] <Object> [-Because <String>]
Positional parameters:
Position 1: Expected
Position 2: Actual
Conversion notes:
If the command is negated, the `Should-BeLessThanOrEqual` command is used.
Assume the actual value is 2 and the expected value should not be greater
than 2, then we need to use the `Should-BeLessThanOrEqual` command for
the logic to be the same:
Pester 5: 2 | Should -Not -BeGreaterThan 2
Pester 6: 2 | Should-BeLessThanOrEqual 2
#>
function Convert-ShouldBeGreaterThan
{
Expand Down Expand Up @@ -88,7 +96,7 @@ function Convert-ShouldBeGreaterThan
# Add the correct Pester command based on negation
if ($isNegated)
{
$newExtentText = 'Should-BeLessThan'
$newExtentText = 'Should-BeLessThanOrEqual'
}
else
{
Expand Down
12 changes: 10 additions & 2 deletions source/Private/Convert-ShouldBeLessThan.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,19 @@
Pester 6 Syntax:
Should-BeLessThan [[-Actual] <Object>] [-Expected] <Object> [-Because <String>]
Should-BeGreaterThan [[-Actual] <Object>] [-Expected] <Object> [-Because <String>]
Should-BeGreaterThanOrEqual [[-Actual] <Object>] [-Expected] <Object> [-Because <String>]
Positional parameters:
Position 1: Expected
Position 2: Actual
Conversion notes:
If the command is negated, the `Should-BeGreaterThanOrEqual` command is
used. Assume the actual value is 2 and the expected value should not be
less than 2, then we need to use the `Should-BeLessThanOrEqual` command
for the logic to be the same:
Pester 5: 2 | Should -Not -BeLessThan 2
Pester 6: 2 | Should-BeGreaterThanOrEqual 2
#>
function Convert-ShouldBeLessThan
{
Expand Down Expand Up @@ -88,7 +96,7 @@ function Convert-ShouldBeLessThan
# Add the correct Pester command based on negation
if ($isNegated)
{
$newExtentText = 'Should-BeGreaterThan'
$newExtentText = 'Should-BeGreaterThanOrEqual'
}
else
{
Expand Down
15 changes: 14 additions & 1 deletion source/Private/Convert-ShouldBeNullOrEmpty.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,20 @@ function Convert-ShouldBeNullOrEmpty
}

$newExtentText += $commandParameters.ActualValue.Positional ? (' {0}' -f $commandParameters.ActualValue.ExtentText) : ''
$newExtentText += $commandParameters.Because.Positional ? (' {0}' -f $commandParameters.Because.ExtentText) : ''

if ($commandParameters.Because -and $commandParameters.Because.Positional)
{
# Only add second positional if the first positional was present.
if ($commandParameters.ActualValue.Positional)
{
$newExtentText += ' {0}' -f $commandParameters.Because.ExtentText
}
else
{
# First positional parameter was not present, so set the second to named parameter.
$commandParameters.Because.Positional = $false
}
}

# Holds the new parameter names so they can be added in alphabetical order.
$parameterNames = @()
Expand Down
20 changes: 10 additions & 10 deletions source/Private/Convert-ShouldBeOfType.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
Should -BeOfType [[-ActualValue] <Object>] [[-ExpectedType] <Object>] [[-Because] <string>] [-Not]
Positional parameters:
Position 1: ExpectedValue
Position 1: ExpectedType
Position 2: Because
Pester 6 Syntax:
Expand Down Expand Up @@ -117,7 +117,7 @@ function Convert-ShouldBeOfType
'Not'
)
PositionalParameter = @(
'ExpectedValue'
'ExpectedType'
'Because'
)
NamedParameter = @(
Expand Down Expand Up @@ -147,9 +147,9 @@ function Convert-ShouldBeOfType
If a previous positional parameter is missing then the ones behind
it cannot be set to positional.
#>
if ($commandParameters.ExpectedValue)
if ($commandParameters.ExpectedType)
{
$commandParameters.ExpectedValue.Positional = $true
$commandParameters.ExpectedType.Positional = $true

if ($commandParameters.ActualValue)
{
Expand All @@ -159,11 +159,11 @@ function Convert-ShouldBeOfType
}

# '[System.String]' -match '^\[.+\]$'
if ($commandParameters.ExpectedValue.Positional)
if ($commandParameters.ExpectedType.Positional)
{
# Add the expected value in parenthesis only if the extent text is a type defined in square brackets.
$extentTextFormat = $commandParameters.ExpectedValue.ExtentText -match '^\[.+\]$' ? ' ({0})' : ' {0}'
$newExtentText += $extentTextFormat -f $commandParameters.ExpectedValue.ExtentText
$extentTextFormat = $commandParameters.ExpectedType.ExtentText -match '^\[.+\]$' ? ' ({0})' : ' {0}'
$newExtentText += $extentTextFormat -f $commandParameters.ExpectedType.ExtentText
}

$newExtentText += $commandParameters.ActualValue.Positional ? (' {0}' -f $commandParameters.ActualValue.ExtentText) : ''
Expand All @@ -189,10 +189,10 @@ function Convert-ShouldBeOfType
break
}

'ExpectedValue'
'ExpectedType'
{
$parameterNames += @{
Expected = 'ExpectedValue'
Expected = 'ExpectedType'
}

break
Expand All @@ -215,7 +215,7 @@ function Convert-ShouldBeOfType
$originalParameterName = $parameterNames.$currentParameter

# Add the expected value in parenthesis only if the extent text is a type defined in square brackets.
$extentTextFormat = $originalParameterName -eq 'ExpectedValue' -and $commandParameters.$originalParameterName.ExtentText -match '^\[.+\]$' ? '({1})' : '{1}'
$extentTextFormat = $originalParameterName -eq 'ExpectedType' -and $commandParameters.$originalParameterName.ExtentText -match '^\[.+\]$' ? '({1})' : '{1}'

$newExtentText += " -{0} $extentTextFormat" -f $currentParameter, $commandParameters.$originalParameterName.ExtentText
}
Expand Down
Loading

0 comments on commit 42526bf

Please sign in to comment.