-
Notifications
You must be signed in to change notification settings - Fork 394
Fix settings file array parsing when no commas are present #1161
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
JamesWTruher
merged 6 commits into
PowerShell:development
from
rjmholt:fix-settings-array-reads
Mar 7, 2019
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c97fd6b
Fix settings file array parsing when no commas are present
rjmholt 51b5fe5
Add extra test
rjmholt f60e129
Improve comment
rjmholt 5047f87
Add test file
b513b37
Change test to accept null
17f552e
Fix new-item call in PS 4
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
# Copyright (c) Microsoft Corporation. All rights reserved. | ||
# Licensed under the MIT License. | ||
|
||
$script:fileContent = @' | ||
$assetDir = 'C:\ImportantFiles\' | ||
$archiveDir = 'C:\Archived\' | ||
|
||
$runningOnWindows = -not ($IsLinux -or $IsMacOS) | ||
$zipCompressionLevel = 'Optimal' | ||
$includeBaseDirInZips = $true | ||
|
||
if (-not (Test-Path $archiveDir)) | ||
{ | ||
New-Item -Type Directory $archiveDir | ||
} | ||
|
||
Import-Module -FullyQualifiedName @{ ModuleName = 'MyArchiveUtilities'; ModuleVersion = '2.0' } | ||
|
||
$sigs = [System.Collections.Generic.List[System.Management.Automation.Signature]]::new() | ||
$filePattern = [System.Management.Automation.WildcardPattern]::Get('system*') | ||
foreach ($file in Get-ChildItem $assetDir -Recurse -Depth 1) | ||
{ | ||
if (-not $filePattern.IsMatch($file.Name)) | ||
{ | ||
continue | ||
} | ||
|
||
if ($file.Name -like '*.dll') | ||
{ | ||
$sig = Get-AuthenticodeSignature $file | ||
$sigs.Add($sig) | ||
continue | ||
} | ||
|
||
if (Test-WithFunctionFromMyModule -File $file) | ||
{ | ||
$destZip = Join-Path $archiveDir $file.BaseName | ||
[System.IO.Compression.ZipFile]::CreateFromDirectory($file.FullName, "$destZip.zip", $zipCompressionLevel, $includeBaseDirInZips) | ||
} | ||
} | ||
|
||
Write-Output $sigs | ||
'@ | ||
|
||
$script:settingsContent = @' | ||
@{ | ||
Rules = @{ | ||
PSUseCompatibleCommands = @{ | ||
Enable = $true | ||
TargetProfiles = @( | ||
'win-8_x64_10.0.17763.0_5.1.17763.316_x64_4.0.30319.42000_framework' # Server 2019 - PS 5.1 (the platform it already runs on) | ||
'win-8_x64_6.2.9200.0_3.0_x64_4.0.30319.42000_framework' # Server 2012 - PS 3 | ||
'ubuntu_x64_18.04_6.1.3_x64_4.0.30319.42000_core' # Ubuntu 18.04 - PS 6.1 | ||
) | ||
} | ||
PSUseCompatibleTypes = @{ | ||
Enable = $true | ||
# Same as for command targets | ||
TargetProfiles = @( | ||
'win-8_x64_10.0.17763.0_5.1.17763.316_x64_4.0.30319.42000_framework' | ||
'win-8_x64_6.2.9200.0_3.0_x64_4.0.30319.42000_framework' | ||
'ubuntu_x64_18.04_6.1.3_x64_4.0.30319.42000_core' | ||
) | ||
} | ||
PSUseCompatibleSyntax = @{ | ||
Enable = $true | ||
TargetVersions = @('3.0', '5.1', '6.1') | ||
} | ||
} | ||
} | ||
'@ | ||
|
||
$script:expectedCommandDiagnostics = @( | ||
@{ Command = 'Import-Module'; Parameter = 'FullyQualifiedName'; Line = 13 } | ||
@{ Command = 'Get-ChildItem'; Parameter = 'Depth'; Line = 17 } | ||
@{ Command = 'Get-AuthenticodeSignature'; Line = 26 } | ||
) | ||
|
||
$script:expectedTypeDiagnostics = @( | ||
@{ Type = 'System.Management.Automation.WildcardPattern'; Member = 'Get'; Line = 16 } | ||
@{ Type = 'System.IO.Compression.ZipFile'; Line = 34 } | ||
) | ||
|
||
$script:expectedSyntaxDiagnostics = @( | ||
@{ Line = 15; CorrectionText = "New-Object 'System.Collections.Generic.List[System.Management.Automation.Signature]'" } | ||
) | ||
|
||
Describe "Running all compatibility rules with a settings file" { | ||
BeforeAll { | ||
$testFile = New-Item -Path "$TestDrive/test.ps1" -Value $script:fileContent -ItemType File | ||
$settingsFile = New-Item -Path "$TestDrive/settings.psd1" -Value $script:settingsContent -ItemType File | ||
$diagnostics = Invoke-ScriptAnalyzer -Path $testFile.FullName -Settings $settingsFile.FullName | | ||
Group-Object -Property RuleName -AsHashtable | ||
$commandDiagnostics = $diagnostics.PSUseCompatibleCommands | ||
$typeDiagnostics = $diagnostics.PSUseCompatibleTypes | ||
$syntaxDiagnostics = $diagnostics.PSUseCompatibleSyntax | ||
} | ||
|
||
It "Finds the problem with command <Command> on line <Line> in the file" -TestCases $script:expectedCommandDiagnostics { | ||
param([string]$Command, [string]$Parameter, [int]$Line) | ||
|
||
$actualDiagnostic = $commandDiagnostics | Where-Object { $_.Command -eq $Command -and $_.Line -eq $Line } | ||
$actualDiagnostic.Command | Should -BeExactly $Command | ||
$actualDiagnostic.Line | Should -Be $Line | ||
if ($Parameter) | ||
{ | ||
$actualDiagnostic.Parameter | Should -Be $Parameter | ||
} | ||
} | ||
|
||
It "Finds the problem with type <Type> on line <Line> in the file" -TestCases $script:expectedTypeDiagnostics { | ||
param([string]$Type, [string]$Member, [int]$Line) | ||
|
||
$actualDiagnostic = $typeDiagnostics | Where-Object { $_.Type -eq $Type -and $_.Line -eq $Line } | ||
$actualDiagnostic.Type | Should -BeExactly $Type | ||
$actualDiagnostic.Line | Should -Be $Line | ||
if ($Member) | ||
{ | ||
$actualDiagnostic.Member | Should -Be $Member | ||
} | ||
} | ||
|
||
It "Finds the problem with syntax on line <Line> in the file" -TestCases $script:expectedSyntaxDiagnostics { | ||
param([string]$Suggestion, [int]$Line) | ||
|
||
$actualDiagnostic = $syntaxDiagnostics | Where-Object { $_.Line -eq $Line } | ||
$actualDiagnostic.Line | Should -Be $Line | ||
if ($Suggestion) | ||
{ | ||
$actualDiagnostic.Suggestion | Should -Be $Suggestion | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Noticed this was missing and quickly added it. Makes sense from a layering perspective -- null should be parse-able but might not be allowed as a configurable value.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about just simplifying the whole code to bool.tryparse ?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If one of the possibilities is
null
, then it would probably look like:I'm not sure that's simpler though -- the switch/case has a nice flow to me, and ideally the C# compiler knows better than I do about how to make it work quickly. (Although it looks like that's not the case, since I would implement a trie for optimum speed).