-
Notifications
You must be signed in to change notification settings - Fork 384
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
Fix Pester v4 installation for Visual Studio 2017
image and use Pester v4 assertion operator syntax
#892
Fix Pester v4 installation for Visual Studio 2017
image and use Pester v4 assertion operator syntax
#892
Conversation
Visual Studio 2017
image and use Pester v4 assertion operator syntax
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.
First, this is awesome! Thanks a bunch going through all these and updating the tests. There's really only one thing that has to be fixed: ${$_}
refers to the variable literally named $_
so this would rather be ${_}
. But since you're going to change this, it's an opportunity to update the test to use testcases which might be a better way to go!
{ | ||
nuget install Pester -Version 4.1.1 -source https://www.powershellgallery.com/api/v2 -outputDirectory "$Env:ProgramFiles\WindowsPowerShell\Modules\." -ExcludeVersion | ||
if ($null -eq (Get-Module -ListAvailable PowershellGet)) |
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.
Wouldn't it be possible to use Install-Module
for either case? We'll find the latest version of Pester when we import.
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.
No because Install-Module
(which is part of PowerShellGet) only shipped starting with WMF5, hence the special treatment for installing Pester in the WMF4 build.
appveyor.yml
Outdated
@@ -37,6 +46,7 @@ install: | |||
build_script: | |||
- ps: | | |||
$PSVersionTable | |||
(Get-Command Invoke-Pester).Version |
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.
this is part of the log, correct? Would get-module -listavailable pester
provide more utility than a version being output?
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.
Yes, it is part of the log. If I remember correctly, Get-Module Pester -ListAvailable
told me that Pester v3 and v4 was installed but only Get-Command Invoke-Pester
gave me the 'proof' that it was still using v3 for some reason. Maybe using both commands is the best conclusion.
Tests/Engine/Settings.tests.ps1
Outdated
@@ -39,7 +39,7 @@ Describe "Settings Class" { | |||
|
|||
'IncludeRules', 'ExcludeRules', 'Severity', 'RuleArguments' | ForEach-Object { | |||
It ("Should return empty {0} property" -f $_) { | |||
$settings.${$_}.Count | Should Be 0 | |||
$settings.${$_}.Count | Should -Be 0 |
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.
there's a problem with ${$_}
which is not quite right as it is referencing the variable literally named $_
, let's use the test case functionality of pester:
It "Should return empty <name> property" -TestCases @(
@{ Name = "IncludeRules" }
@{ Name = "ExcludeRules" }
@{ Name = "Severity" }
@{ Name = "RuleArguments" }
) {
${settings}.${Name}.Count | Should -Be 0
}
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.
Well spotted. When applying the syntax changes, I did not pay much attention to the test cases themselves but I will certainly improve this one.
Tests/Engine/Settings.tests.ps1
Outdated
@@ -91,27 +91,27 @@ Describe "Settings Class" { | |||
} | |||
|
|||
It "Should return 2 IncludeRules" { |
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.
hmmm, this doesn't seem right, I assume the number is right so the title of these should probably change
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.
I agree. I will make it a variable to future proof.
$settings.CustomRulePath.Count | Should Be $rulePaths.Count | ||
0..($rulePaths.Count - 1) | ForEach-Object { $settings.CustomRulePath[$_] | Should be $rulePaths[$_] } | ||
$settings.CustomRulePath.Count | Should -Be $rulePaths.Count | ||
0..($rulePaths.Count - 1) | ForEach-Object { $settings.CustomRulePath[$_] | Should -Be $rulePaths[$_] } |
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.
wouldn't this be more easily expressed as:
for($i = 0; $i -lt $rulePaths.Count; $i++ ) {
$settings.CustomRulePath[$i] | Should -Be $rulePaths[$i]
}
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.
I find the current way actually quite neat and reasonably readable. If I had written it then it would look like something:
foreach ($i in 0..($rulePaths.Count-1)) {
$settings.CustomRulePath[$i] | Should -Be $rulePaths[$i]
}
At the end I don't mind too much but I would rather not touch existing tests too much if they are generally fine. But up to you whatever you prefer. If you want to make the style of all tests consistent, then this work should probably be a separate PR.
$violations[0].SuggestedCorrections[0].Text | Should Match $expectedText | ||
Get-ExtentText $violations[0].SuggestedCorrections[0] $violationFilepath | Should Match "" | ||
$violations[0].SuggestedCorrections[0].Text | Should -Match $expectedText | ||
Get-ExtentText $violations[0].SuggestedCorrections[0] $violationFilepath | Should -Match "" |
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.
isn't this Should -BeNullOrEmpty
?
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.
Lol. Absolutely correct. As I said, I mainly applied some replace algorithms and checked at the end only that it corrects the right operators without looking too much at the tests. Will definitely fix that one
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.
the test case example just needs a param statement in order to work.
@{ Name = "ExcludeRules" } | ||
@{ Name = "Severity" } | ||
@{ Name = "RuleArguments" } | ||
) { |
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.
this needs a param statement to work, i believe
{
param ( $name )
${settings}.${Name}.Count | Should -Be 0
}
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.
yes, you are right. thanks for pointing it out, I should've spotted that myself instead of just copy-pasting your example. Why did we not get a NullReferenceException on .Count
then?
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.
it's because property references don't provide null reference exceptions unless you're in strict mode. [int]$null.count
will return 0 ($null.count
will be null, and [int]$null
is 0
), but $null.GetType()
will throw because it's a method invocation. Strict mode will keep that from happening.
PS> Set-StrictMode -Version 2
PS> $null.count
The property 'count' cannot be found on this object. Verify that the property exists.
At line:1 char:1
+ $null.count
+ ~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], PropertyNotFoundException
+ FullyQualifiedErrorId : PropertyNotFoundStrict
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.
LGTM!
PR Summary
Closes #890
VS 2017
image (got installed but was still using v3)-
) toBe
,Not
,Throw
, andMatch
operators (by going through all operators in the documentation of Pester'sShould
documentation.PR Checklist
Note: Tick the boxes below that apply to this pull request by putting an
x
between the square brackets. Please mark anything not applicable to this PRNA
.WIP:
to the beginning of the title and remove the prefix when the PR is ready