Skip to content

Commit

Permalink
Merge pull request #188 from dlwyatt/BetterErrorMessages
Browse files Browse the repository at this point in the history
Better error messages when exported commands are called out of order
  • Loading branch information
dlwyatt committed Aug 14, 2014
2 parents 0698efc + d3faab3 commit c982a15
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 4 deletions.
1 change: 1 addition & 0 deletions Functions/Assertions/Should.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ function New-ShouldException ($Message,$Line) {

function Should {
begin {
Assert-DescribeInProgress -CommandName Should
$parsedArgs = Parse-ShouldArgs $args
}

Expand Down
2 changes: 2 additions & 0 deletions Functions/Context.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ param(
[ValidateNotNull()]
[ScriptBlock] $fixture = $(Throw "No test script block is provided. (Have you put the open curly brace on the next line?)")
)
Assert-DescribeInProgress -CommandName Context

$Pester.EnterContext($name)
$TestDriveContent = Get-TestDriveChildItem

Expand Down
8 changes: 8 additions & 0 deletions Functions/Describe.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,11 @@ param(
$Pester.LeaveDescribe()
}

function Assert-DescribeInProgress
{
param ($CommandName)
if ($null -eq $pester -or [string]::IsNullOrEmpty($pester.CurrentDescribe))
{
throw "The $CommandName command may only be used inside a Describe block."
}
}
1 change: 1 addition & 0 deletions Functions/In.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ param(
$path,
[ScriptBlock] $execute
)
Assert-DescribeInProgress -CommandName In

$old_pwd = $pwd
pushd $path
Expand Down
2 changes: 2 additions & 0 deletions Functions/It.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ about_should
[ScriptBlock] $test = $(Throw "No test script block is provided. (Have you put the open curly brace on the next line?)")
)

Assert-DescribeInProgress -CommandName It

$Pester.EnterTest($name)
Invoke-SetupBlocks

Expand Down
7 changes: 7 additions & 0 deletions Functions/Mock.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,8 @@ about_Mocking
[string]$ModuleName
)

Assert-DescribeInProgress -CommandName Mock

$filterTest = Test-ParameterFilter -ScriptBlock $ParameterFilter

if ($filterTest -isnot [bool]) { throw [System.Management.Automation.PSArgumentException] 'The Parameter Filter must return a boolean' }
Expand Down Expand Up @@ -309,6 +311,8 @@ Assert-VerifiableMocks
This will not throw an exception because the mock was invoked.
#>
Assert-DescribeInProgress -CommandName Assert-VerifiableMocks

$unVerified=@{}
$mockTable.Keys | % {
$m=$_; $mockTable[$m].blocks | ? { $_.Verifiable } | % { $unVerified[$m]=$_ }
Expand Down Expand Up @@ -454,6 +458,9 @@ param(
[ValidateSet('Describe','Context','It')]
[string] $Scope
)

Assert-DescribeInProgress -CommandName Assert-MockCalled

if (-not $PSBoundParameters.ContainsKey('ModuleName') -and $null -ne $pester.SessionState.Module)
{
$ModuleName = $pester.SessionState.Module.Name
Expand Down
11 changes: 9 additions & 2 deletions Functions/SetupTeardown.ps1
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
function BeforeEach { }
function AfterEach { }
function BeforeEach
{
Assert-DescribeInProgress -CommandName BeforeEach
}

function AfterEach
{
Assert-DescribeInProgress -CommandName AfterEach
}

function Clear-SetupAndTeardown
{
Expand Down
7 changes: 5 additions & 2 deletions Functions/TestDrive.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,9 @@ function New-RandomTempDirectory {
function Get-TestDriveItem {
#moved here from Pester.psm1
param( [string]$Path )
$result = Get-Item $(Join-Path $TestDrive $Path )
return $result

Assert-DescribeInProgress -CommandName Get-TestDriveItem
Get-Item $(Join-Path $TestDrive $Path )
}

function Get-TestDriveChildItem {
Expand Down Expand Up @@ -98,6 +99,8 @@ function Setup {
[switch]$PassThru
)

Assert-DescribeInProgress -CommandName Setup

$TestDriveName = Get-PSDrive TestDrive | Select -ExpandProperty Root

if ($Dir) {
Expand Down

0 comments on commit c982a15

Please sign in to comment.