From af420ac46b27868eca92350e7cf44625ab627f86 Mon Sep 17 00:00:00 2001 From: Maria Romero Date: Wed, 24 May 2017 12:48:54 -0700 Subject: [PATCH 1/2] add tab completion for -FileFormat parameter --- .../ExportCounterCommand.cs | 21 +++++++------------ .../resources/GetEventResources.resx | 3 --- .../resources/GetEventResources.txt | 1 - 3 files changed, 8 insertions(+), 17 deletions(-) diff --git a/src/Microsoft.PowerShell.Commands.Diagnostics/ExportCounterCommand.cs b/src/Microsoft.PowerShell.Commands.Diagnostics/ExportCounterCommand.cs index ea9f382c114..8ef8fa3d49a 100644 --- a/src/Microsoft.PowerShell.Commands.Diagnostics/ExportCounterCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Diagnostics/ExportCounterCommand.cs @@ -65,12 +65,13 @@ public string Path ValueFromPipelineByPropertyName = false, HelpMessageBaseName = "GetEventResources")] [ValidateNotNull] + [ValidateSet("blg", "csv", "tsv")] public string FileFormat { get { return _format; } set { _format = value; } } - private string _format = "BLG"; + private string _format = "blg"; @@ -180,9 +181,9 @@ protected override void BeginProcessing() _resourceMgr = Microsoft.PowerShell.Commands.Diagnostics.Common.CommonUtilities.GetResourceManager(); // - // Validate the Format and CounterSamples arguments + // Set output format (log file type) // - ValidateFormat(); + SetOutputFormat(); if (Circular.IsPresent && _maxSize == 0) { @@ -311,26 +312,20 @@ protected override void ProcessRecord() } } - // ValidateFormat() helper. - // Validates Format argument: only "BLG", "TSV" and "CSV" are valid strings (case-insensitive) + // Determines Log File Type based on FileFormat parameter // - private void ValidateFormat() + private void SetOutputFormat() { switch (_format.ToLowerInvariant()) { - case "blg": - _outputFormat = PdhLogFileType.PDH_LOG_TYPE_BINARY; - break; case "csv": _outputFormat = PdhLogFileType.PDH_LOG_TYPE_CSV; break; case "tsv": _outputFormat = PdhLogFileType.PDH_LOG_TYPE_TSV; break; - default: - string msg = string.Format(CultureInfo.InvariantCulture, _resourceMgr.GetString("CounterInvalidFormat"), _format); - Exception exc = new Exception(msg); - ThrowTerminatingError(new ErrorRecord(exc, "CounterInvalidFormat", ErrorCategory.InvalidArgument, null)); + default: // By default file format is blg + _outputFormat = PdhLogFileType.PDH_LOG_TYPE_BINARY; break; } } diff --git a/src/Microsoft.PowerShell.Commands.Diagnostics/resources/GetEventResources.resx b/src/Microsoft.PowerShell.Commands.Diagnostics/resources/GetEventResources.resx index 6afe6db6ee0..57308452ee6 100644 --- a/src/Microsoft.PowerShell.Commands.Diagnostics/resources/GetEventResources.resx +++ b/src/Microsoft.PowerShell.Commands.Diagnostics/resources/GetEventResources.resx @@ -216,9 +216,6 @@ The defined template is following: Event cannot be written because the specified version {0} for event {1} is not defined for the provider {2}. Please correct the version and try again. - - The {0} format type is not a valid format for a counter file. Possible values for the Format parameter are .blg, .csv, or .tsv. - You cannot import more than 32 .blg counter log files in each command. diff --git a/src/Microsoft.PowerShell.Commands.Diagnostics/resources/GetEventResources.txt b/src/Microsoft.PowerShell.Commands.Diagnostics/resources/GetEventResources.txt index 8b9d6f3467e..751578835cd 100644 --- a/src/Microsoft.PowerShell.Commands.Diagnostics/resources/GetEventResources.txt +++ b/src/Microsoft.PowerShell.Commands.Diagnostics/resources/GetEventResources.txt @@ -52,7 +52,6 @@ CounterExportSampleNotInInitialSet=The {0} performance counter cannot be exporte CounterPathTranslationFailed=The default values for this command failed. Error: {0:x8}. CounterContinuousOrMaxSamples=The Continuous parameter and the MaxSamples parameter cannot be used in the same command. CounterSampleDataInvalid=The data in one of the performance counter samples is not valid. View the Status property for each PerformanceCounterSample object to make sure it contains valid data. -CounterInvalidFormat=The {0} format type is not a valid format for a counter file. Possible values for the Format parameter are .blg, .csv, or .tsv. CounterCircularNoMaxSize=The Circular parameter will be ignored unless the MaxSize parameter is also specified. ExportCtrWin7Required=This cmdlet can be run only on Microsoft Windows 7 and above. FileOpenFailed=Unable to open the {0} file for writing. From 45a5520569b8358a780b361c4d0524db5d6704df Mon Sep 17 00:00:00 2001 From: Maria Romero Date: Wed, 24 May 2017 12:50:28 -0700 Subject: [PATCH 2/2] update expected error for invalid -FileFormat test case --- .../TabCompletion/TabCompletion.Tests.ps1 | 30 ++++++++++++++----- .../Export-Counter.Tests.ps1 | 5 ---- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/test/powershell/Host/TabCompletion/TabCompletion.Tests.ps1 b/test/powershell/Host/TabCompletion/TabCompletion.Tests.ps1 index dd164aee76a..652ca27a053 100644 --- a/test/powershell/Host/TabCompletion/TabCompletion.Tests.ps1 +++ b/test/powershell/Host/TabCompletion/TabCompletion.Tests.ps1 @@ -33,7 +33,8 @@ Describe "TabCompletion" -Tags CI { It 'Should complete format-table hashtable' { $res = TabExpansion2 -inputScript 'Get-ChildItem | Format-Table @{ ' -cursorColumn 'Get-ChildItem | Format-Table @{ '.Length $res.CompletionMatches.Count | Should Be 5 - $res.CompletionMatches.Foreach{$_.CompletionText -in 'Label', 'Width', 'Alignment', 'Expression', 'FormatString' | Should Be $true} + $completionText = $res.CompletionMatches.CompletionText | Sort-Object + $completionText -join ' ' | Should Be 'Alignment Expression FormatString Label Width' } @@ -46,37 +47,43 @@ Describe "TabCompletion" -Tags CI { param($cmd) $res = TabExpansion2 -inputScript "Get-ChildItem | $cmd -GroupBy @{ " -cursorColumn "Get-ChildItem | $cmd -GroupBy @{ ".Length $res.CompletionMatches.Count | Should Be 3 - $res.CompletionMatches.Foreach{$_.CompletionText -in 'Label', 'Expression', 'FormatString' | Should Be $true} + $completionText = $res.CompletionMatches.CompletionText | Sort-Object + $completionText -join ' ' | Should Be 'Expression FormatString Label' } It 'Should complete format-list hashtable' { $res = TabExpansion2 -inputScript 'Get-ChildItem | Format-List @{ ' -cursorColumn 'Get-ChildItem | Format-List @{ '.Length $res.CompletionMatches.Count | Should Be 3 - $res.CompletionMatches.Foreach{$_.CompletionText -in 'Label', 'Expression', 'FormatString' | Should Be $true} + $completionText = $res.CompletionMatches.CompletionText | Sort-Object + $completionText -join ' ' | Should Be 'Expression FormatString Label' } It 'Should complete format-wide hashtable' { $res = TabExpansion2 -inputScript 'Get-ChildItem | Format-Wide @{ ' -cursorColumn 'Get-ChildItem | Format-Wide @{ '.Length $res.CompletionMatches.Count | Should Be 2 - $res.CompletionMatches.Foreach{$_.CompletionText -in 'Expression', 'FormatString' | Should Be $true} + $completionText = $res.CompletionMatches.CompletionText | Sort-Object + $completionText -join ' ' | Should Be 'Expression FormatString' } It 'Should complete format-custom hashtable' { $res = TabExpansion2 -inputScript 'Get-ChildItem | Format-Custom @{ ' -cursorColumn 'Get-ChildItem | Format-Custom @{ '.Length $res.CompletionMatches.Count | Should Be 2 - $res.CompletionMatches.Foreach{$_.CompletionText -in 'Expression', 'Depth' | Should Be $true} + $completionText = $res.CompletionMatches.CompletionText | Sort-Object + $completionText -join ' ' | Should Be 'Depth Expression' } It 'Should complete Select-Object hashtable' { $res = TabExpansion2 -inputScript 'Get-ChildItem | Select-Object @{ ' -cursorColumn 'Get-ChildItem | Select-Object @{ '.Length $res.CompletionMatches.Count | Should Be 2 - $res.CompletionMatches.Foreach{$_.CompletionText -in 'Name', 'Expression' | Should Be $true} + $completionText = $res.CompletionMatches.CompletionText | Sort-Object + $completionText -join ' '| Should Be 'Expression Name' } It 'Should complete Sort-Object hashtable' { $res = TabExpansion2 -inputScript 'Get-ChildItem | Sort-Object @{ ' -cursorColumn 'Get-ChildItem | Sort-Object @{ '.Length $res.CompletionMatches.Count | Should Be 3 - $res.CompletionMatches.Foreach{$_.CompletionText -in 'Expression', 'Ascending', 'Descending' | Should Be $true} + $completionText = $res.CompletionMatches.CompletionText | Sort-Object + $completionText -join ' '| Should Be 'Ascending Descending Expression' } It 'Should complete New-Object hashtable' { @@ -87,7 +94,7 @@ Describe "TabCompletion" -Tags CI { } $res = TabExpansion2 -inputScript 'New-Object -TypeName X -Property @{ ' -cursorColumn 'New-Object -TypeName X -Property @{ '.Length $res.CompletionMatches.Count | Should Be 3 - $res.CompletionMatches.Foreach{$_.CompletionText -in 'A', 'B', 'C' | Should Be $true} + $res.CompletionMatches.CompletionText -join ' ' | Should Be 'A B C' } It 'Should complete "Get-Process -Id " with Id and name in tooltip' { @@ -103,4 +110,11 @@ Describe "TabCompletion" -Tags CI { $res = TabExpansion2 -inputScript 'using nam' -cursorColumn 'using nam'.Length $res.CompletionMatches[0].CompletionText | Should be 'namespace' } + + It 'Should complete "Export-Counter -FileFormat" with available output formats'-Skip:(!$IsWindows) { + $res = TabExpansion2 -inputScript 'Export-Counter -FileFormat ' -cursorColumn 'Export-Counter -FileFormat '.Length + $res.CompletionMatches.Count | Should Be 3 + $completionText = $res.CompletionMatches.CompletionText | Sort-Object + $completionText -join ' '| Should Be 'blg csv tsv' + } } diff --git a/test/powershell/Modules/Microsoft.PowerShell.Diagnostics/Export-Counter.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Diagnostics/Export-Counter.Tests.ps1 index 99cbe149a25..3bcd095c2ee 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Diagnostics/Export-Counter.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Diagnostics/Export-Counter.Tests.ps1 @@ -177,11 +177,6 @@ Describe "Feature tests for Export-Counter cmdlet" -Tags "Feature" { Parameters = "-MaxSize -2" ExpectedErrorId = "CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.ExportCounterCommand" } - @{ - Name = "Fails when given invalid file format" - FileFormat = "dat" - ExpectedErrorId = "CounterInvalidFormat,Microsoft.PowerShell.Commands.ExportCounterCommand" - } ) foreach ($testCase in $testCases)