8000 Move test "No Exception!" pattern to 'ShouldBeErrorId' by iSazonov · Pull Request #3161 · PowerShell/PowerShell · GitHub
[go: up one dir, main page]

Skip to content

Move test "No Exception!" pattern to 'ShouldBeErrorId' #3161

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

Closed
wants to merge 6 commits into from

Conversation

iSazonov
Copy link
Collaborator
@iSazonov iSazonov commented Feb 16, 2017

It turns out we have a excellent function 'ShouldBeErrorId' in
'Common\Test.Helpers.psm1'

I slightly modified this function to be able to passthrow the exception
result for later analysis.

I searched throw [",'] in test files for replacing "No Exception!" pattern
with 'ShouldBeErrorId'.

I did not touch some files that contains their own specific version "No
Exception!" pattern or contains specific tests (ex., *-Variable cmdlets
with scopes).
Also I was forced to resolve some aliases and sometimes fix formatting.


This change is Reviewable

It turns out we have a excellent function 'ShouldBeErrorId' in
'Common\Test.Helpers.psm1'

I slightly modified this function to be able to passthrow the exception
result for later analysis.

I searched `throw [",']` in test files for replacing "No Exception!" pattern
with 'ShouldBeErrorId'.

I did not touch some files that contains their own specific version "No
Exception!" pattern or contains specific tests (ex., *-Variable cmdlets
with scopes).
Also I was forced to resolve some aliases and sometimes fix formatting.
@iSazonov
Copy link
Collaborator Author

Class tests is failed on IOS 😕

@TravisEz13
Copy link
Member

restarted iOS

@TravisEz13
Copy link
Member

Using reviewable because it helps me track my progress reviewing a PR. Not done, but here are my comments so far.


Reviewed 7 of 87 files at r1.
Review status: 7 of 87 files reviewed at latest revision, 3 unresolved discussions.


test/powershell/Common/Test.Helpers.psm1, line 61 at r1 (raw file):

        {
            & $sb | Out-Null
            Throw "No Exception!"

Is this verbose enough?


test/powershell/Common/Test.Helpers.psm1, line 66 at r1 (raw file):

        {
            $_.FullyQualifiedErrorId | Should Be $FullyQualifiedErrorId | Out-Null
            Write-Output $PSItem

Why are we writing an exception to the output stream?


test/powershell/engine/Remoting/RoleCapabilityFiles.Tests.ps1, line 60 at r1 (raw file):

            $iss = [initialsessionstate]::CreateFromSessionConfigurationFile($PSSessionConfigFile, { $true })
        } | ShouldBeErrorId "PSInvalidOperationException"
        $exc.Exception.InnerException.ErrorRecord.FullyQualifiedErrorId | Should Be "InvalidRoleCapabilityFileExtension"

should use single quotes unless you need to expand the string (same in other locations)


Comments from Reviewable

@iSazonov
Copy link
Collaborator Author

It seems the Reviewable is inconvenient to reply.

Is this verbose enough?

Yes, see #2973 (comment)

Why are we writing an exception to the output stream?

Before the change we was writing true/false to the output stream. Now an exception because sometimes we should check not only FullyQualifiedErrorId but also other properties (it is used in the PR).

should use single quotes unless you need to expand the string (same in other locations)

Original code is almost always uses double quotes and this pattern is from our guide (Why do we need a guide if we do not comply with it?)
I agree that the single quotes are more properly. If it will be approved, then we should change our pattern and make changes in the PR.

@TravisEz13
Copy link
Member

Why are we writing an exception to the output stream?

Before the change we was writing true/false to the output stream. Now an exception because sometimes we should check not only FullyQualifiedErrorId but also other properties (it is used in the PR).

Can you add a comment to the code to indicate this why we are writing the error to the output stream?


Reviewed 1 of 87 files at r1.
Review status: 8 of 87 files reviewed at latest revision, 3 unresolved discussions.


Comments from Reviewable

@TravisEz13
Copy link
Member

Yes, Reviewable is inconvenient to reply, unless you use reviewable. I don't like it unless I'm reviewing a very large PR like this one. It keeps track of this status:

Review status: 8 of 87 files reviewed at latest revision, 3 unresolved discussions.
So I know I still have 79 files to review and in the reviewable UI... which files they are.

@iSazonov
Copy link
Collaborator Author

@JamesWTruher From #3157 (comment)

I have a bit of a problem with this. The functionality of checking the FullyQualifiedErrorId rather than the exception message should be in Pester rather than our environment. Putting this code in our own environment does not help the community, but only increases the burden on our code maintenance.

New Pester version (V4) already has improved support for exceptions and offers a similar style. Only while it is still not ported on Unix.
You can see from this PR there is a great variety in such code, and even errors. Now we could bring all the same pattern. This code is more compact (less than 1100 lines), it is clearer and corresponds to the Pester style. The new pattern will allow us much easier to migrate to Pester V4.

@TravisEz13 I added the comment about writing an exception to output.

Copy link
Contributor
@lzybkr lzybkr left a comment

Choose a reason for hiding this comment

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

Can you fix the merge conflict?

I do like the change. I think it should be fine even if we move to a newer version of Pester, and if it isn't, we can help improve Pester based on our experience with this pattern.

I only sampled the changes - I'll let Travis finish his review.

@@ -1,5 +1,7 @@
using namespace System.Diagnostics

Import-Module $PSScriptRoot\..\Common\Test.Helpers.psm1
Copy link
Contributor

Choose a reason for hiding this comment

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

It would be nice if this could be less fragile to directory structure - you couldn't move Test.Helpers.psm1 without making many changes. On the other hand, one obvious alternative Import-Module $TestHelpers or something like that would require running something first, like Start-PSPester, which we don't need to do today, you can just run the test directly, like .\Foo.Tests.ps1.

It would be even nicer if it was not necessary.

If $env:PSModulePath is set correctly and the module is a more proper module (in a directory named Test.Helpers), then we could rely on module auto-loading to load the module, except maybe in a test specific to module auto-loading.

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 don't know how to make this a universal.
Can we rely only on CI and Start-PSPester?

Copy link
Contributor

Choose a reason for hiding this comment

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

My thinking:
say we have the module structure:

PowerShell
  | test
    | Modules
      | TestHelpers
        | TestHelpers.psd1
        | TestHelpers.psm1

Then, in Start-PSPester, ensure PowerShell\test\Modules is in $env:PSModulePath.
As long as the commands are exported properly, then the module will be auto-loaded.
Developers not wanting to run Start-PSPester would just ensure they have their module path set correctly.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Currently our helpers modules is in test\powershell\Common\ folder - maybe we leave them there?

Copy link
Contributor

Choose a reason for hiding this comment

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

I suggested higher up because shared tools may be useful for I feel like the directory structure is already a bit too deep and not that intuitive. At any rate, tools feel like they shouldn't been too hidden.

Copy link
Collaborator
@JamesWTruher JamesWTruher Feb 23, 2017

Choose a reason for hiding this comment

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

in what way is our structure not intuitive? In what way is our structure too deep? I don't agree with that at all. I certainly agree that our tools should not be hidden, and don't see how having globally applicable test tools in test/PowerShell/common equates to hiding.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Clear.
I have a couple more questions.

  1. We have a few more helpers modules (ex., for parser). Should we move them also in the new shared folder?
  2. Can we make these modules as submodules general module and load all the modules with a single command? What is the point of importing them in multiple test files?

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe it's just me noticing the carryo 8000 ver from sharing 1 test tree for many products, I'd just prefer a flatter structure and/or one that is easier to find corresponding tests, maybe something like:

test
  | Tools
    | Modules
      | TestHelper.psm1
  | System.Management.Automation
    | Microsoft.PowerShell.Commands
      | Get-Variable.tests.ps1
    | Language (or System.Management.Automation.Language)
      | Parser.tests.ps1
    | Remoting
      | Serialization.tests.ps1   
  | Microsoft.PowerShell.Commands.Utility
    | Object (grouping by noun, grouping by verb might make sense too)
      | Measure-Object.tests.ps1
      | New-Object.tests.ps1
  | Microsoft.PowerShell.Commands.Management
  | PSReadline
etc.

I realize some tests don't fall neatly into testing an api - and at least System.Management.Automation namespace is overly large, so requires some additional structure, but this gives an overview on what I'd find more intuitive.

Copy link
Collaborator Author
@iSazonov iSazonov Feb 24, 2017

Choose a reason for hiding this comment

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

In the PR I moved Test.Helpers.psm1 to test\Tools\Modules folder and modifyed Start-PSPester to load the module.

To change the whole structure we need to open new Issue. @lzybkr Could you move your proposal in this new Issue?

@iSazonov
Copy link
Collaborator Author

@TravisEz13 Could you continue? There are too many files and we get merge conflicts often.

@daxian-dbw
Copy link
Member
daxian-dbw commented Feb 28, 2017

I'm a little worried about using this function, as I won't be able to directly run Invoke-Pester .\CmsMessage.Tests.ps1 anymore, unless all test files that use this function explicitly import the helper module at the beginning of the file.

@iSazonov
Copy link
Collaborator Author

@daxian-dbw
Preferred way to run tests is to use Start-PSPester. Currently it compile our C# test helper tools. In the PR I added that Test.helper.psm1 is loaded too. Later we can do the same for other test helper modules.
Also see #3161 (comment)

@lzybkr
Copy link
Contributor
lzybkr commented Feb 28, 2017

One common workflow is to make many changes to code and a specific test (say Foo.tests.ps1). It's very convenient to just run .\Foo.tests.ps1 without Start-PSPester or Invoke-Pester.

One solution is to just import Test.helper.psm1 in build.psm1.
Another is to set $env:PSModulePath so that Test.helper.psm1 available for auto-loading.

@iSazonov
Copy link
Collaborator Author

Yes it is convenient. But we can run Start-PSPester once per session to configure environment.
Start-PSPester performs some special settings. If we use it, we carry out tests in exactly the same way as CIs.
I don't like the autoload because if we need to specify the path to the modules, we quickly load them all at once.
We could move the code into the Build module root to run when it loads but then we will have to reload the entire module if we need to apply any updates.

@TravisEz13
Copy link
Member

@iSazonov I think this idea needs more discussion. It may be better to file an issue and discuss the work. Then do the work in stages to avoid having a large PR which will constantly get merge conflicts.

Here is a way you break this PR up:

  1. Update test Helpers to new pattern
  2. Work needed to use pattern broadly (probably need an issue to discuss what this looks like)
  3. Update tests to use new pattern

@TravisEz13 TravisEz13 removed the Review - Needed The PR is being reviewed label Mar 13, 2017
@TravisEz13
Copy link
Member

Per our discussion I think this PR can be closed?

@iSazonov
Copy link
Collaborator Author

Let's make it a little later. This will help me move the commits to new PRs.

@TravisEz13
Copy link
Member

Can we close this now?

@iSazonov iSazonov closed this May 11, 2017
@iSazonov iSazonov deleted the tests-no-exception branch October 25, 2017 11:23
@TravisEz13 TravisEz13 added Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept and removed Review - Waiting on Author labels May 27, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Waiting on Author The PR was reviewed and requires changes or comments from the author before being accept
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants
0