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

Allow paths to be pipelined to Invoke-ScriptAnalyzer #1040

Merged
merged 7 commits into from
Jul 19, 2018
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion Engine/Commands/InvokeScriptAnalyzerCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -279,8 +279,9 @@ protected override void BeginProcessing()
this.SessionState,
recurseCustomRulePath);

if (IsFileParameterSet())
if (IsFileParameterSet() && Path != null)
{
// just used to obtain the directory to use to find settings below
ProcessPath();
}

Expand Down Expand Up @@ -350,6 +351,11 @@ protected override void ProcessRecord()
return;
}

if (IsFileParameterSet())
{
ProcessPath();
}

#if !PSV3
// TODO Support dependency resolution for analyzing script definitions
if (saveDscDependency)
Expand Down
4 changes: 2 additions & 2 deletions Tests/Engine/CustomizedRule.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ if (-not (Test-PSEditionCoreCLR))
$null,"Wow6432Node" | ForEach-Object {
try
{
Set-ItemProperty -Name "DisablePromptToUpdateHelp" -Path "HKLM:\SOFTWARE\$($_)\Microsoft\PowerShell" -Value 1 -Force
Set-ItemProperty -Name "DisablePromptToUpdateHelp" -Path "HKLM:\SOFTWARE\$($_)\Microsoft\PowerShell" -Value 1 -Force -ErrorAction SilentlyContinue
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you please elaborate on the error that you were getting that led you to apply this change? On which environment (platform and ps version) did you see it?

Copy link
Contributor Author

@edyoung edyoung Jul 18, 2018

Choose a reason for hiding this comment

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

running tests in non-elevated windows powershell on Windows 10. Despite the try/catch errors are shown when this fails to update HKLM key.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ok, thanks for the info. This does not happen on my machine, I am wondering if this could be caused by some group policy in your environment. Although it seems that this test code should reviewed again and possibly refactored, I think we can still accept your changes to it.

}
catch
{
Expand Down Expand Up @@ -66,7 +66,7 @@ Describe "Test importing correct customized rules" {
$null,"Wow6432Node" | ForEach-Object {
try
{
Set-ItemProperty -Name "DisablePromptToUpdateHelp" -Path "HKLM:\SOFTWARE\$($_)\Microsoft\PowerShell" -Value 1 -Force
Set-ItemProperty -Name "DisablePromptToUpdateHelp" -Path "HKLM:\SOFTWARE\$($_)\Microsoft\PowerShell" -Value 1 -Force -EA SilentlyContinue
Copy link
Collaborator

Choose a reason for hiding this comment

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

Minor nit: Use -ErrorAction SilentlyContinue for consistency, readability and to not rely on parameter aliases

}
catch
{
Expand Down
17 changes: 16 additions & 1 deletion Tests/Engine/InvokeScriptAnalyzer.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,22 @@ Describe "Test Path" {
Remove-PSDrive $freeDriveName
$numFilesResult | Should -Be $numFilesExpected
}
}
}

Context "When piping in files" {
It "Can be piped in from a string" {
$piped = ("$directory\TestScript.ps1" | Invoke-ScriptAnalyzer)
$explicit = Invoke-ScriptAnalyzer -Path $directory\TestScript.ps1

$piped.Count -eq $explicit.Count | Should -BeTrue
}

It "Can be piped from Get-ChildItem" {
$piped = ( Get-ChildItem -Path $directory -Filter TestTestPath*.ps1 | Invoke-ScriptAnalyzer)
$explicit = Invoke-ScriptAnalyzer -Path $directory\TestTestPath*.ps1
$piped.Count -eq $explicit.Count | Should -BeTrue
Copy link
Collaborator

Choose a reason for hiding this comment

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

This would result in a much better error message if the test fails:

$piped.Count  | Should -Be $explicit.Count

}
}
}

Context "When given a directory" {
Expand Down
2 changes: 1 addition & 1 deletion Tests/Engine/LibraryUsage.tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ $testingLibraryUsage = $true
$null,"Wow6432Node" | ForEach-Object {
try
{
Set-ItemProperty -Name "DisablePromptToUpdateHelp" -Path "HKLM:\SOFTWARE\$($_)\Microsoft\PowerShell" -Value 1 -Force
Set-ItemProperty -Name "DisablePromptToUpdateHelp" -Path "HKLM:\SOFTWARE\$($_)\Microsoft\PowerShell" -Value 1 -Force -ErrorAction SilentlyContinue
}
catch
{
Expand Down