Skip to content

Refactor pester script detection #638

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

Merged
merged 4 commits into from
Mar 26, 2018

Conversation

rjmholt
Copy link
Contributor

@rjmholt rjmholt commented Mar 21, 2018

Fixes vscode-powershell #1223.

Previously, Pester test parsing by PSES has made a couple of assumptions that didn't hold and as a result it wouldn't recognise tests with tags.

Now we do all of that, and are even set up to add something like tag-awareness in future more easily.

Copy link
Contributor

@rkeithhill rkeithhill left a comment

Choose a reason for hiding this comment

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

You might think about adding a test around here - https://github.com/PowerShell/PowerShellEditorServices/blob/master/test/PowerShellEditorServices.Test/Language/LanguageServiceTests.cs#L305

I always like to see the test fail first, then after I fix the issue the test should pass.

@rjmholt
Copy link
Contributor Author

rjmholt commented Mar 21, 2018

@rkeithhill Perfect! Didn't realise we had testing for this! I will write a couple of tests and open a PR on that.

Copy link
Member

@TylerLeonhardt TylerLeonhardt left a comment

Choose a reason for hiding this comment

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

Just a couple of questions, otherwise, LGTM! Awesome that you fixed this :) Let's give @SeeminglyScience a day to see if he wants to look at this since I know ASTs are his thing.

// Find plausible Pester commands
IEnumerable<Ast> commandAsts = scriptFile.ScriptAst.FindAll(IsNamedCommandWithArguments, true);

return commandAsts.OfType<CommandAst>()
Copy link
Member

Choose a reason for hiding this comment

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

can commandAsts ever be null on accident? It looks like there was that commandant != null check previously

Copy link
Contributor Author

Choose a reason for hiding this comment

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

FindAll promises to return a possibly empty collection, but not null


return
commandAst != null &&
ast is CommandAst commandAst &&
Copy link
Member

@TylerLeonhardt TylerLeonhardt Mar 22, 2018

Choose a reason for hiding this comment

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

Can ast ever been null on accident? This one I'm pretty sure is no but figured I'd ask.

Copy link
Collaborator

@SeeminglyScience SeeminglyScience left a comment

Choose a reason for hiding this comment

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

LGTM, just a couple optional suggestions.

}
// Ensure the first word is a Pester keyword
if (!(commandAst.CommandElements[0] is StringConstantExpressionAst pesterKeywordAst &&
PesterSymbolReference.PesterKeywords.ContainsKey(pesterKeywordAst.Value)))
Copy link
Collaborator

Choose a reason for hiding this comment

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

CommandAst.GetCommandName() might be a little cleaner here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes I just saw that on something else. Will change it over

if (stringAst == null)
// Search for a name for the test
string testName = null;
for (int i = 1; i < pesterCommandAst.CommandElements.Count; i++)
Copy link
Collaborator

Choose a reason for hiding this comment

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

StaticParameterBinder.BindCommand() is excellent for this type of analysis. Might not be available in PowerShell 3 though.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is there an easy way to find out? I don't really know where I could get a PowerShell v3 instance to run

Copy link
Collaborator

Choose a reason for hiding this comment

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

Is there an easy way to find out?

It'll fail to compile when it tries to build using the v3 reference libs. It won't show any syntax markers because of the compiler directives though, and that's a huge pain.

I don't really know where I could get a PowerShell v3 instance to run

I just have a Windows 7 VM set up in my home lab that I manually upgraded from 2 to 3. It's annoying, but works.

Copy link
Member

Choose a reason for hiding this comment

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

@rjmholt I've got a Windows Server VM for each major version of PowerShell on my Azure subscription. They're all turned off and deallocated (saving that $$$) until needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@tylerl0706 Excellent!

Copy link
Member

Choose a reason for hiding this comment

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

If you need me to test something, I can 😄 just give me the script to run. Otherwise, tomorrow I'll add you to those VMs so we can just share them.

@rjmholt
Copy link
Contributor Author

rjmholt commented Mar 26, 2018

Ok, I'm going to merge this one

@rjmholt rjmholt merged commit 9d5a2da into PowerShell:master Mar 26, 2018
TylerLeonhardt pushed a commit to TylerLeonhardt/PowerShellEditorServices that referenced this pull request Feb 26, 2019
Enable debugging of untitled script files
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.

4 participants