Browse Source

fix(access): name a value that cannot be formatted by the short form of its type

The fallback of the message of a bad parameter value used the full name of the
type. For a generic type that spells out the assembly of every type argument,
so a LINQ iterator whose selector throws gave a message of 330 characters, where
the code before the last commit printed the short form. The fallback uses
Type.ToString now: System.Collections.Generic.List`1[System.Object].

Two rows pin the short form (a generic list of one object whose ToString
throws, for AccessType and InheritanceFlags of Add-NTFSAccess, piped). They
failed on the previous commit in all four configurations and pass now.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: AI Assistant <ai@example.com>
pull/131/head
Raimund Andree 1 week ago
parent
commit
ce34395a8b
  1. 4
      NTFSSecurity/ParameterConversion.cs
  2. 5
      Tests/ParameterSets.Access.Tests.ps1

4
NTFSSecurity/ParameterConversion.cs

@ -154,7 +154,7 @@ namespace NTFSSecurity
// The value as a message names it: a collection of one element by that element (also when it is a collection
// itself), a collection of several elements by its first ten, anything else by its text. The message is built
// inside a catch block, so this method must not fail whatever the text or the enumeration of a value does; a
// value that cannot be formatted is named by its type.
// value that cannot be formatted is named by its type, in the short form of Type.ToString.
private static string Describe(object value)
{
try
@ -164,7 +164,7 @@ namespace NTFSSecurity
catch (Exception)
{
var unwrapped = value is PSObject ? ((PSObject)value).BaseObject : value;
return unwrapped == null ? string.Empty : unwrapped.GetType().FullName;
return unwrapped == null ? string.Empty : unwrapped.GetType().ToString();
}
}

5
Tests/ParameterSets.Access.Tests.ps1

@ -1404,12 +1404,15 @@ Describe 'Invalid and valid values of AccessType, InheritanceFlags, PropagationF
}
# The message names a list by its first ten elements: ten are named in full, eleven end with an ellipsis. The
# elements of a flags parameter are converted one by one, so these numbers fail there too (4 is not a flag).
# elements of a flags parameter are converted one by one, so these numbers fail there too (4 is not a flag). A
# value whose text cannot be built is named by its type, in the short form that Type.ToString gives.
$accessInvalidParameterCases += @(
@{ Cmdlet = 'Add-NTFSAccess'; Parameter = 'AccessType'; Shape = 'ten numbers'; Value = 1..10; ValueText = '1, 2, 3, 4, 5, 6, 7, 8, 9, 10' }
@{ Cmdlet = 'Add-NTFSAccess'; Parameter = 'AccessType'; Shape = 'eleven numbers'; Value = 1..11; ValueText = '1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ...' }
@{ Cmdlet = 'Add-NTFSAccess'; Parameter = 'InheritanceFlags'; Shape = 'ten numbers'; Value = 1..10; ValueText = '1, 2, 3, 4, 5, 6, 7, 8, 9, 10' }
@{ Cmdlet = 'Add-NTFSAccess'; Parameter = 'InheritanceFlags'; Shape = 'eleven numbers'; Value = 1..11; ValueText = '1, 2, 3, 4, 5, 6, 7, 8, 9, 10, ...' }
@{ Cmdlet = 'Add-NTFSAccess'; Parameter = 'AccessType'; Shape = 'a generic list of one object whose ToString throws'; Value = [System.Collections.Generic.List[object]]::new([object[]] @($accessThrowingElement)); ValueText = 'System.Collections.Generic.List`1[System.Object]' }
@{ Cmdlet = 'Add-NTFSAccess'; Parameter = 'InheritanceFlags'; Shape = 'a generic list of one object whose ToString throws'; Value = [System.Collections.Generic.List[object]]::new([object[]] @($accessThrowingElement)); ValueText = 'System.Collections.Generic.List`1[System.Object]' }
)
$accessSdInvalidParameterCases = foreach ($cmdlet in 'Add-NTFSAccess', 'Remove-NTFSAccess') {

Loading…
Cancel
Save