Skip to content

Commit

Permalink
✅ Update tests for Pester v5
Browse files Browse the repository at this point in the history
 - Update sanity tests
 - Update private function tests
 - Update public function tests

:white_check_mark: Skip Measure-Koan tests

These tests are currently unstable and prone to corrupting Pester's
internal state. Without a better option at the moment, we'll need to
skip these tests for the time being.
The function has been manually verified and seems to be more accurate
than the original version.
  • Loading branch information
vexx32 committed Jun 18, 2020
1 parent 50e9bca commit 03c19c8
Show file tree
Hide file tree
Showing 23 changed files with 1,423 additions and 1,345 deletions.
95 changes: 50 additions & 45 deletions Tests/Functions/Private/Assert-UnblockedFile.Tests.ps1
@@ -1,59 +1,64 @@
#region Header
if (-not (Get-Module PSKoans)) {
$moduleBase = Join-Path -Path $psscriptroot.Substring(0, $psscriptroot.IndexOf('\Tests')) -ChildPath 'PSKoans'
#Requires -Modules PSKoans

Import-Module $moduleBase -Force
}
#endregion

if ($PSVersionTable.PSEdition -eq 'Desktop' -or $PSVersionTable.Platform -eq 'Win32NT') {
InModuleScope PSKoans {
Describe Assert-UnblockedFile {
BeforeAll {
$defaultParams = @{
FileInfo = [System.IO.FileInfo](Join-Path -Path $TestDrive -ChildPath 'AboutSomething.Koans.ps1')
PassThru = $true
}
}
#region Discovery
$SkipTests = $PSVersionTable.PSEdition -ne 'Desktop' -or $PSVersionTable.Platform -ne 'Win32NT'
#endregion Discovery

BeforeEach {
Set-Content -Path $defaultParams.FileInfo.FullName -Value @'
using module PSKoans
Describe 'Assert-UnblockedFile' -Skip:$SkipTests {

[Koan(Position = 1)]
param ( )
BeforeAll {
$defaultParams = @{
FileInfo = [System.IO.FileInfo](Join-Path -Path $TestDrive -ChildPath 'AboutSomething.Koans.ps1')
PassThru = $true
}
}

Describe 'About something' {
It 'Has examples' {
$true | Should -BeTrue
}
}
'@
}
BeforeEach {
Set-Content -Path $defaultParams.FileInfo.FullName -Value @'
using module PSKoans
[Koan(Position = 1)]
param()
Describe 'About something' {
AfterEach {
Remove-Item -Path $defaultParams.FileInfo.FullName
It 'Has examples' {
$true | Should -BeTrue
}
}
'@
}

Context 'File is blocked' {
BeforeEach {
Set-Content -Path $defaultParams.FileInfo.FullName -Stream Zone.Identifier -Value @'
[ZoneTransfer]
ZoneId=3
ReferrerUrl=C:\Downloads\File.zip
AfterEach {
Remove-Item -Path $defaultParams.FileInfo.FullName
}

Context 'File With External Zone Identifier' {

BeforeEach {
Set-Content -Path $defaultParams.FileInfo.FullName -Stream Zone.Identifier -Value @'
[ZoneTransfer]
ZoneId=3
ReferrerUrl=C:\Downloads\File.zip
'@
}
}

It 'should throw a terminating error if the file is blocked' {
{ Assert-UnblockedFile @defaultParams } | Should -Throw -ErrorId 'PSKoans.KoanFileIsBlocked'
It 'should throw a terminating error if the file is blocked' {
{
InModuleScope 'PSKoans' -Parameters @{ Params = $defaultParams } {
param($Params)
Assert-UnblockedFile @Params
}
}
} | Should -Throw -ErrorId 'PSKoans.KoanFileIsBlocked'
}
}

Context 'File is not blocked' {
It 'returns the original object with -PassThru if the file is not blocked' {
Assert-UnblockedFile @defaultParams | Should -BeOfType [System.IO.FileInfo]
}
}
Context 'File Without Zone Identifier' {

It 'returns the original object with -PassThru if the file is not blocked' {
InModuleScope 'PSKoans' -Parameters @{ Params = $defaultParams } {
param($Params)
Assert-UnblockedFile @Params
} | Should -BeOfType [System.IO.FileInfo]
}
}
}
39 changes: 18 additions & 21 deletions Tests/Functions/Private/ConvertFrom-WildcardPattern.Tests.ps1
@@ -1,29 +1,26 @@
#Requires -Modules PSKoans

InModuleScope 'PSKoans' {
Describe 'ConvertFrom-WildcardPattern' {
Describe 'ConvertFrom-WildcardPattern' {

It 'adds start and end of string anchors to an explicit value' {
ConvertFrom-WildcardPattern 'AboutArrays' | Should -Be '^AboutArrays$'
}

It 'joins multiple expressions with or' {
ConvertFrom-WildcardPattern 'AboutArrays', 'AboutComparison' | Should -Be '^AboutArrays$|^AboutComparison$'
}
It 'adds start and end of string anchors to an explicit value' {
InModuleScope 'PSKoans' { ConvertFrom-WildcardPattern 'AboutArrays' } | Should -Be '^AboutArrays$'
}

It 'joins multiple expressions with |' {
InModuleScope 'PSKoans' { ConvertFrom-WildcardPattern 'AboutArrays', 'AboutComparison' } |
Should -Be '^AboutArrays$|^AboutComparison$'
}

It 'Replaces wildcard characters with regex equivalent' -TestCases @(
@{ Pattern = 'About*son'; Expected = '^About.*son$' }
@{ Pattern = 'AboutArrays*'; Expected = '^AboutArrays' }
@{ Pattern = '*Arrays'; Expected = 'Arrays$' }
@{ Pattern = '*Array*'; Expected = 'Array' }
) {
param (
$Pattern,
$Expected
)
It 'replaces wildcard characters with regex equivalent' -TestCases @(
@{ Pattern = 'About*son'; Expected = '^About.*son$' }
@{ Pattern = 'AboutArrays*'; Expected = '^AboutArrays' }
@{ Pattern = '*Arrays'; Expected = 'Arrays$' }
@{ Pattern = '*Array*'; Expected = 'Array' }
) {
InModuleScope 'PSKoans' -Parameters @{ Pattern = $Pattern } {
param($Pattern)

ConvertFrom-WildcardPattern $Pattern | Should -Be $Expected
}
ConvertFrom-WildcardPattern $Pattern
} | Should -Be $Expected
}
}
93 changes: 51 additions & 42 deletions Tests/Functions/Private/Get-KoanAst.Tests.ps1
@@ -1,57 +1,66 @@
#region Header
if (-not (Get-Module PSKoans)) {
$moduleBase = Join-Path -Path $psscriptroot.Substring(0, $psscriptroot.IndexOf('\Tests')) -ChildPath 'PSKoans'
#Requires -Modules PSKoans

Import-Module $moduleBase -Force
}
#endregion

InModuleScope PSKoans {
Describe Get-KoanAst {
BeforeAll {
$path = Join-Path $TestDrive 'AboutSomething.Koans.ps1'

Set-Content -Path $path -Value @'
using module PSKoans
[Koan(Position = 1)]
param()
<#
About Something
#>
Describe 'Something' {
It 'Has some examples' {
$true | Should -BeTrue
}
Describe 'Get-KoanAst' {

BeforeAll {
$path = Join-Path $TestDrive 'AboutSomething.Koans.ps1'

Set-Content -Path $path -Value @'
using module PSKoans
[Koan(Position = 1)]
param()
<#
About Something
#>
Describe 'Something' {
It 'Has some examples' {
$true | Should -BeTrue
}
}
'@
}

It 'excludes the "using module PSKoans" statement from the AST' {
$ast = InModuleScope 'PSKoans' -Parameters @{ Path = $path } {
param($Path)
Get-KoanAst -Path $Path
}

It 'Excludes the "using module PSKoans" statement from the AST' {
$ast = Get-KoanAst -Path $path
$ast.UsingStatements | Should -BeNullOrEmpty
}

$ast.UsingStatements | Should -BeNullOrEmpty
}
It 'maintains consistency of position data when reading and modifying the source' {
$tokens = $errors = $null
$originalAst = [System.Management.Automation.Language.Parser]::ParseFile(
$path,
[Ref]$tokens,
[Ref]$errors
)

It 'When reading and modifying the source, position data is consistent' {
$tokens = $errors = @()
$originalAst = [System.Management.Automation.Language.Parser]::ParseFile(
$path,
[Ref]$tokens,
[Ref]$errors
)
$originalItBlock = $originalAst.Find( {
$originalItBlock = $originalAst.Find(
{
$args[0] -is [System.Management.Automation.Language.CommandAst] -and
$args[0].GetCommandName() -eq 'It'
}, $true)
},
$true
)

$modifiedAst = Get-KoanAst -Path $path
$modifiedItBlock = $modifiedAst.Find( {
$modifiedAst = InModuleScope 'PSKoans' -Parameters @{ Path = $path } {
param($Path)
Get-KoanAst -Path $Path
}

$modifiedItBlock = $modifiedAst.Find(
{
$args[0] -is [System.Management.Automation.Language.CommandAst] -and
$args[0].GetCommandName() -eq 'It'
}, $true)
},
$true
)

$modifiedItBlock.Extent.StartOffset | Should -Be $originalItBlock.Extent.StartOffset
$modifiedItBlock.Extent.EndOffset | Should -Be $originalItBlock.Extent.EndOffset
}
$modifiedItBlock.Extent.StartOffset |
Should -Be $originalItBlock.Extent.StartOffset -Because 'the start offsets should match'
$modifiedItBlock.Extent.EndOffset |
Should -Be $originalItBlock.Extent.EndOffset -Because 'the end offsets should match'
}
}

0 comments on commit 03c19c8

Please sign in to comment.