Skip to content
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

Merged
merged 17 commits into from
Feb 20, 2018

Conversation

bergmeister
Copy link
Collaborator

@bergmeister bergmeister commented Feb 16, 2018

PR Summary

Closes #890

  • Fix Pester v4 installation that did not work for VS 2017 image (got installed but was still using v3)
  • Change assertion operators to Pester 4 syntax by pre-pending the dash (-) to Be, Not, Throw, and Match operators (by going through all operators in the documentation of Pester's Should 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 PR NA.

  • PR has a meaningful title
    • Use the present tense and imperative mood when describing your changes
  • Summarized changes
  • NA User facing documentation needed
  • Change is not breaking
  • NA Make sure you've added a new test if existing tests do not effectively test the code changed
  • This PR is ready to merge and is not work in progress :
    • If the PR is work in progress, please add the prefix WIP: to the beginning of the title and remove the prefix when the PR is ready

@bergmeister bergmeister self-assigned this Feb 16, 2018
@bergmeister bergmeister changed the title Use Pester v4 assertion operator syntax WIP Use Pester v4 assertion operator syntax Feb 16, 2018
@bergmeister bergmeister reopened this Feb 16, 2018
@bergmeister bergmeister changed the title WIP Use Pester v4 assertion operator syntax Fix Pester v4 installation for Visual Studio 2017 image and use Pester v4 assertion operator syntax Feb 17, 2018
Copy link
Contributor

@JamesWTruher JamesWTruher left a 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))
Copy link
Contributor

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.

Copy link
Collaborator Author

@bergmeister bergmeister Feb 20, 2018

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
Copy link
Contributor

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?

Copy link
Collaborator Author

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.

@@ -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
Copy link
Contributor

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
}

Copy link
Collaborator Author

@bergmeister bergmeister Feb 20, 2018

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.

@@ -91,27 +91,27 @@ Describe "Settings Class" {
}

It "Should return 2 IncludeRules" {
Copy link
Contributor

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

Copy link
Collaborator Author

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[$_] }
Copy link
Contributor

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]
}

Copy link
Collaborator Author

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 ""
Copy link
Contributor

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?

Copy link
Collaborator Author

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

Copy link
Contributor

@JamesWTruher JamesWTruher left a 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" }
) {
Copy link
Contributor

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
}

Copy link
Collaborator Author

@bergmeister bergmeister Feb 20, 2018

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?

Copy link
Contributor

@JamesWTruher JamesWTruher Feb 20, 2018

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

Copy link
Contributor

@JamesWTruher JamesWTruher left a comment

Choose a reason for hiding this comment

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

LGTM!

@JamesWTruher JamesWTruher merged commit 6aeb4f6 into PowerShell:development Feb 20, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

update tests to use new Pester 4 syntax
2 participants