Skip to content

Fix GetType() bad pattern and related issues in tests#3134

Merged
daxian-dbw merged 3 commits into
PowerShell:masterfrom
iSazonov:gettypenullreference
Feb 16, 2017
Merged

Fix GetType() bad pattern and related issues in tests#3134
daxian-dbw merged 3 commits into
PowerShell:masterfrom
iSazonov:gettypenullreference

Conversation

@iSazonov
Copy link
Copy Markdown
Collaborator

$var.GetType() can raise an exception in tests so we should check $var
before make the call. A large part of the tests does not make this
check.
I start with searching ".GetType()" but discovered many related issues
in tests (reduntant and unneeded tests, "throw" bad pattens, bugs,
formattings (sorry!) and so on) - I had to fix them too.

According to our test roadmap, we must increase the code coverage from 45 up to 90% i.e. twice. Bad patterns and bad formatting will slow down our movement. I hope the team will support me in this PR.

@PowerShellTeam PowerShellTeam added the Review - Needed The PR is being reviewed label Feb 13, 2017
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

pipe will unwrap the input collection, so this will become checking the type of individual elements instead of the collection as a whole. Better keep what it was in this case.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Fixed.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

can this be $_.Exception | Should BeOfType $exception?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Don't work 😕

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I see what is happening. $exception is point to internal types, and thus Pester cannot resolve. #Closed

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

`$result.GetType().BaseType | Should Be 'System.Object' seems not interesting, maybe remove it?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Done.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is it failing? If it is we better have an issue tracking it.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

OsLocalDateTime return empty to me. And the test is wrong. Yes, we should open a new Issue...

Copy link
Copy Markdown
Member

@daxian-dbw daxian-dbw Feb 14, 2017

Choose a reason for hiding this comment

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

Yup, the test doesn't do anything about OsLocalDateTime. Please open an issue.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Done #3146

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can this line be removed?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I believe that the test is similar to the following after it. So it is better to leave.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

#Closed

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

if any of those aliases are removed, these tests can catch it. So maybe we should keep these 3 tests?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

We have separate files with tests for aliases. If we need control a list of approved aliases we should make the test there. If it is we should open a new Issue.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yes, please open an issue to verify the approved aliases exist in powershell. The test can be added to Microsoft.PowerShell.Utility\Default-Aliases.Tests.ps1.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Done #3145

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

indentation. there are similar indentation problems down in the changes.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I reformat the file.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is it failing on Linux? I don't see this failure in our daily build ... Can you open an issue for it?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I only add the comment for you. I don't mark it as pending.
The code don't raise an exception as expected in the test (on windows). So we should open a new Issue.

Copy link
Copy Markdown
Member

@daxian-dbw daxian-dbw Feb 14, 2017

Choose a reason for hiding this comment

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

Weird, it pass in daily build, but no exception is thrown when I run it locally.
@PaulHigin can you please take a look?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

?? This test runs fine on Linux for me. The CI test failures only indicate that the class inheritance tests fail. Why do you think this test is failing?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I copy-paste the code

[System.Management.Automation.Runspaces.SSHConnectionInfo]::new(
                    [System.Management.Automation.Internal.AutomationNull]::Value,
                    "localhost",
                    [System.Management.Automation.Internal.AutomationNull]::Value)

to interactive session (on Windows) and it don't raise exception.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Oh I see now. Yes, this test should be removed because the code now gets the current user name if no user name is specified. Good find.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Clear. Thanks! I'll remove the test.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@PaulHigin Why doesn't it fail in our daily test run then? There is no current user name when running daily test pass in AppVeyor builder?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Not sure. But the test is incorrect (no longer needed) because a null UserName parameter is now allowed, and the test should be removed.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

The test is removed.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You use $result | Should BeOfType in some places, and $result | Should Not BeNullOrEmpty then $result.Exception.GetType | Should Be in some other places. Why don't you unify them with a single pattern?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Sometimes it is clear from above code that '$result' is not empty. Ex. $result.Count | Should Be 2

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I see, if it's obvious $result is not null, you will go with $result | should BeOfType. If not, you will check $result | Should Not BeNullOrEmpty first.
But after checking "Not BeNullOrEmpty", why not also use $result | should BeOfType? I see in some places you just continue to use $result.GetType() | Should Be.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

$ev will always be an arraylist, but it might be empty. I think here you want to test $ev[0] | Should Not BeNullOrEmpty.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

We should not trust a cmdlet which we check.
If $ev != Null then $ev[0] never raise.
So I suppose $ev | Should Not BeNullOrEmpty is correct.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Understood. #Closed

Copy link
Copy Markdown
Collaborator

@JamesWTruher JamesWTruher Feb 14, 2017

Choose a reason for hiding this comment

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

this extra test doesn't really get you that much. If $HtAst is null, or if GetType is null, or $HtAst.SafeGetValue() doesn't return a hashtable, the test will fail regardless.
Doesn't just $HtAst.SafeGetValue() | should beoftype hashtable suffice?
most of the validating that the return value isn't null doesn't provide that much value (as the test will fail when the method is called), and in the case that it is not a failure, only adds time to the test.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I removed "GetType().Name" However, previously we need to check that $HtAst is not Null to exclude exception.

$var.GetType() can raise an exception in tests so we should check $var
before make the call. A large part of the tests does not make this
check.
I start with searching ".GetType()" but discovered many related issues
in tests (reduntant and unneeded tests, "throw" bad pattens, bugs,
formattings (sorry!) and so on) - I had to fix them too.
@iSazonov iSazonov force-pushed the gettypenullreference branch 2 times, most recently from 2ca9f96 to c442923 Compare February 15, 2017 13:35
Removed 'GetType().Name' patterns.
@iSazonov iSazonov force-pushed the gettypenullreference branch from c442923 to 28eee62 Compare February 15, 2017 14:10
@iSazonov
Copy link
Copy Markdown
Collaborator Author

Sorry I accidentally made a rebase 😕

@daxian-dbw daxian-dbw merged commit 409ab74 into PowerShell:master Feb 16, 2017
@iSazonov
Copy link
Copy Markdown
Collaborator Author

@daxian-dbw Thanks for the very fast code review! 👍

@iSazonov iSazonov deleted the gettypenullreference branch February 16, 2017 03:12
@iSazonov iSazonov removed the Review - Needed The PR is being reviewed label Mar 27, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants