From aea561f08df3cc1c6ce92135db47604868fade2c Mon Sep 17 00:00:00 2001 From: David Weber Date: Mon, 9 Oct 2017 23:19:02 -0400 Subject: [PATCH] Add better error message for empty and null -UFormat arg (#5055) Add better error message for empty and null -UFormat arg --- .../commands/utility/GetDateCommand.cs | 2 +- .../Microsoft.PowerShell.Utility/Get-Date.Tests.ps1 | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetDateCommand.cs b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetDateCommand.cs index d3205ba5e4..9aae07cf70 100644 --- a/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetDateCommand.cs +++ b/src/Microsoft.PowerShell.Commands.Utility/commands/utility/GetDateCommand.cs @@ -199,6 +199,7 @@ namespace Microsoft.PowerShell.Commands /// Unix format string /// [Parameter(ParameterSetName = "UFormat")] + [ValidateNotNullOrEmpty] public string UFormat { get; set; } @@ -332,7 +333,6 @@ namespace Microsoft.PowerShell.Commands int offset = 0; StringBuilder sb = new StringBuilder(); - // folks may include the "+" as part of the format string if (UFormat[0] == '+') { diff --git a/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Date.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Date.Tests.ps1 index f10ffadf41..9c18379671 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Date.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Utility/Get-Date.Tests.ps1 @@ -37,6 +37,14 @@ Describe "Get-Date DRT Unit Tests" -Tags "CI" { Get-date -Date 1/1/0030 -uformat %S%T%u%U%V%w%W%x%X%y%Y%% | Should be "0000:00:002012001/01/3000:00:00300030%" } + It "Passing '' to -uformat produces a descriptive error" -TestCases @( + @{ name = "`$null" ; value = $null } + @{ name = "empty string"; value = "" } + ) { + param($value) + { Get-date -Date 1/1/1970 -uformat $value -ErrorAction Stop } | ShouldBeErrorId "ParameterArgumentValidationError,Microsoft.PowerShell.Commands.GetDateCommand" + } + It "Get-date works with pipeline input" { $x = new-object System.Management.Automation.PSObject $x | add-member NoteProperty Date ([DateTime]::Now)