Skip to content

Commit

Permalink
fix bug with unbound params and fix line number inaccuracy when the t…
Browse files Browse the repository at this point in the history
…est file is not at the top of the stack
  • Loading branch information
mwrock committed Oct 29, 2012
1 parent e56db10 commit 6c4b25e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
12 changes: 10 additions & 2 deletions Functions/It.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,10 @@ param(
temp
$testResult.success = $true
} catch {
$results.FailedTestsCount += 1
$failure_message = $_.toString() -replace "Exception calling", "Assert failed on"
$temp_line_number = $_.InvocationInfo.ScriptLineNumber - 2
$temp_line_number = Get-LineNumber($_.ScriptStackTrace)
$failure_line_number = $start_line_position + $temp_line_number
$results.FailedTestsCount += 1
$testResult.failureMessage = $failure_message
$testResult.stackTrace = "at line: $failure_line_number in $test_file"
}
Expand Down Expand Up @@ -121,3 +121,11 @@ $test
}
"@ | out-file $TestDrive\temp.ps1
}

function Get-LineNumber($stackTrace) {
$find="pester\temp.ps1: line "
$idx = $stackTrace.IndexOf($find)

This comment has been minimized.

Copy link
@rismoney

rismoney Nov 23, 2012

Contributor

@{should=System.Object}You cannot call a method on a null-valued expression.
At C:\Tools\Pester.1.1.0\tools\Functions\It.ps1:127 char:31

  • $idx = $stackTrace.IndexOf <<<< ($find)
    
    • CategoryInfo : InvalidOperation: (IndexOf:String) [], RuntimeException
    • FullyQualifiedErrorId : InvokeMethodOnNull

This is what I get when I run any failed test
It "abc" {
$letters=abc
$letters.should.be("def")
}

$idx2 = $stackTrace.IndexOf("`r`n", $idx)
$start = $idx + $find.Length
return $stackTrace.Substring($start, $idx2-$start)-2
}
6 changes: 3 additions & 3 deletions Functions/Mock.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ param(
Microsoft.PowerShell.Management\Set-Item Function:\Pester_TempParamTest -value "$($mock.CmdLet) `r`n param ( $($mock.Params) ) `r`n$parameterFilter"
$cmd=(Microsoft.PowerShell.Core\Get-Command Pester_TempParamTest)
$qualifiedCalls = @()
$global:mockCallHistory | ? {$_.CommandName -eq $commandName} | ? {$p=$_.BoundParams;$a=$_.Args;&($cmd) $a @p} | %{ $qualifiedCalls += $_}
$global:mockCallHistory | ? {$_.CommandName -eq $commandName} | ? {$p=$_.BoundParams;$a=$_.Args;&($cmd) @a @p} | %{ $qualifiedCalls += $_}
Microsoft.PowerShell.Management\Remove-Item Function:\Pester_TempParamTest
if($qualifiedCalls.Length -ne $times -and ($Exactly -or ($times -eq 0))) {
throw "Expected $commandName to be called $times times exactly but was called $($qualifiedCalls.Length.ToString()) times"
Expand Down Expand Up @@ -292,9 +292,9 @@ function MockPrototype {
$mock=$mockTable.$functionName
$idx=$mock.blocks.Length
while(--$idx -ge 0) {
if(&($mock.blocks[$idx].Filter) $args @PSBoundParameters) {
if(&($mock.blocks[$idx].Filter) @args @PSBoundParameters) {
$mock.blocks[$idx].Verifiable=$false
&($mockTable.$functionName.blocks[$idx].mock) $args @PSBoundParameters
&($mockTable.$functionName.blocks[$idx].mock) @args @PSBoundParameters
return
}
}
Expand Down

11 comments on commit 6c4b25e

@scottmuc
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No tests? ;-)

@mwrock
Copy link
Member Author

@mwrock mwrock commented on 6c4b25e Oct 29, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for keeping me real. Just changed the tests so they would catch the unbound param bug. The line number is tough because I'm not sure how exactly to test It. Looks like there are no tests for It.ps1.

@scottmuc
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, It.ps1 sort of blew up at one point. I would like to repair those broken windows and I think that can be done extracting out a lot of the functionality into separate functions and test those in isolation. It.ps1 will become an integrating function.

@mwrock
Copy link
Member Author

@mwrock mwrock commented on 6c4b25e Nov 29, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Rich. Could you include the entire Describe block you are using? I'm not able to repro using:

Describe "fgdf" {
    $letters="abc"
    It "abc" {
    $letters.should.be("def")
    }
}

Tried in both PS 2 and 3.

@rismoney
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

did you build from source?

@mwrock
Copy link
Member Author

@mwrock mwrock commented on 6c4b25e Nov 29, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

build from source? not sure i follow. Are you saying this occurs when running Pester's "./build.bat"?

@rismoney
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to let you know i am using the most recent version of pester, with your commits.

the pester i am using i run pester's build.bat and then i drop the nupkg and chocolateyinstall on my internal repo...
so the version of pester includes your commits...

When I run the describe

Describe "fgdf" {
    $letters="abc"
    It "abc" {
    $letters.should.be("def")
    }
}

I get:

@{should=System.Object}You cannot call a method on a null-valued expression.
At C:\Tools\pester\Functions\It.ps1:127 char:31
+     $idx = $stackTrace.IndexOf <<<< ($find)
    + CategoryInfo          : InvalidOperation: (IndexOf:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

[-]   abc (32ms)

@rismoney
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PS C:\test> $host.Version

Major Minor Build Revision


2 0 -1 -1

@rismoney
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I comment out #126-130 I get:

[-]   abc (13ms)
  Assert failed on "be" with "1" argument(s): "Expected: def. But was: abc"
  at line: 3 in  C:\test\alpha.Tests.ps1

It doesn't look like $stacktrace has a value if I output it... Is L95 passing the proper variable?

@mwrock
Copy link
Member Author

@mwrock mwrock commented on 6c4b25e Nov 29, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Argh. It is a ps 3 breaking ps 2 issue. I forgot that running pester.bat in ps 2.0 on win 8 will just open a new 3.0 shell. Will fix now. Thanks!!

@mwrock
Copy link
Member Author

@mwrock mwrock commented on 6c4b25e Nov 29, 2012

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok. the fix is in and I like the solution much more than the original. Thanks for catching this rich!

Please sign in to comment.