-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Make switch statement report correct error position when it fails to evaluate the condition #7151
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| # Copyright (c) Microsoft Corporation. All rights reserved. | ||
| # Licensed under the MIT License. | ||
|
|
||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why we need new file? Update: I see we have tests only for parser. Closest to this https://github.com/PowerShell/PowerShell/blob/master/test/powershell/Language/Parser/Parser.Tests.ps1#L705
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I used a new file because I couldn't find an existing test file that this category of issues can fall in. As for the test you referenced, it looks more fit in
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I don't found a file with runtime error tests too. And I wonder. If we need better code coverage perhaps we should rename the file from
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Runtime errors usually are tested along with the feature that they are associated with, no need for a separate test file only for validating runtime errors. |
||
| Describe "Error position Tests" -Tags "CI" { | ||
| It "switch condition evaluation failure should report correct error position" { | ||
| $testFile = Join-Path $TestDrive "SwitchError1.ps1" | ||
| Set-Content -Path $testFile -Encoding Ascii -Value @' | ||
| $test = 1 | ||
| switch ($null[0]) { | ||
| "a" {}; | ||
| } | ||
| '@ | ||
| try { & $testFile } catch { $errorRecord = $_ } | ||
| $errorRecord | Should -Not -BeNullOrEmpty | ||
| $errorRecord.ScriptStackTrace | Should -Match "SwitchError1.ps1: line 2" | ||
| } | ||
|
|
||
| It "switch condition MoveNext failure should report correct error position" { | ||
| $code = @' | ||
| using System; | ||
| using System.Collections.Generic; | ||
| namespace SwitchTest | ||
| { | ||
| public class Test | ||
| { | ||
| public static IEnumerable<string> GetName() | ||
| { | ||
| yield return "Hello world"; | ||
| throw new ArgumentException(); | ||
| } | ||
| } | ||
| } | ||
| '@ | ||
| $testFile = Join-Path $TestDrive "SwitchError2.ps1" | ||
| Set-Content -Path $testFile -Encoding Ascii -Value @' | ||
| $test = 1 | ||
| $enumerable = [SwitchTest.Test]::GetName() | ||
| switch ($enumerable) { | ||
| "hello world" { $test = 1; $_ } | ||
| "Yay" { $test = 2; $_ } | ||
| } | ||
| '@ | ||
| if (-not ("SwitchTest.Test" -as [type])) { | ||
| Add-Type -TypeDefinition $code | ||
| } | ||
|
|
||
| try { & $testFile > $null } catch { $errorRecord = $_ } | ||
| $errorRecord | Should -Not -BeNullOrEmpty | ||
| $errorRecord.ScriptStackTrace | Should -Match "SwitchError2.ps1: line 3" | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So simple 👍
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@daxian-dbw what was the bug here btw? (As in why does this change fix it?)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We generate code to update position in the script that is executing at various places, so when an error happens, the error can be associated with a position in the script. Here we didn't update the position for the
conditionwhen evaluating the condition from a switch statement.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting that the previous code went out of its way to only work for
foreachloopsThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I wonder why it was so? If we didn't have tests for that previously didn't we break something now?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I did break something 😄
When stepping over the script in debugging, now the debugger stops on
switch ($b)twice. I will revert this PR.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't add me to CI list 😄