8000 update validation of adding and removing extension to require period … · PowerShell/PowerShell@a671e7b · GitHub
[go: up one dir, main page]

8000 Skip to content

Commit a671e7b

Browse files
committed
update validation of adding and removing extension to require period prefix
1 parent 0604327 commit a671e7b

File tree

3 files changed

+19
-21
lines changed

3 files changed

+19
-21
lines changed

src/System.Management.Automation/FormatAndOutput/common/PSStyle.cs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@ public string Executable
445445
/// <summary>
446446
/// Custom dictionary handling validation of extension and content.
447447
/// </summary>
448-
public class FileExtensionDictionary : Dictionary<string, string>
448+
public sealed class FileExtensionDictionary : Dictionary<string, string>
449449
{
450450
/// <summary>
451451
/// Initializes a new instance of the <see cref="FileExtensionDictionary"/> class.
@@ -459,10 +459,9 @@ public FileExtensionDictionary() : base(StringComparer.OrdinalIgnoreCase) { }
459459
/// <param name="decoration">ANSI string value to add.</param>
460460
public new void Add(string extension, string decoration)
461461
{
462-
// if extension doesn't start with a dot, add one
463462
if (!extension.StartsWith("."))
464463
{
465-
extension = "." + extension;
464+
throw new ArgumentException(PSStyleStrings.ExtensionNotStartingWithPeriod);
466465
}
467466

468467
base.Add(extension, ValidateNoContent(decoration));
@@ -474,10 +473,9 @@ public FileExtensionDictionary() : base(StringComparer.OrdinalIgnoreCase) { }
474473
/// <param name="extension">Extension to remove.</param>
475474
public new void Remove(string extension)
476475
{
477-
// if extension doesn't start with a dot, add one
478476
if (!extension.StartsWith("."))
479477
{
480-
extension = "." + extension;
478+
throw new ArgumentException(PSStyleStrings.ExtensionNotStartingWithPeriod);
481479
}
482480

483481
base.Remove(extension);

src/System.Management.Automation/resources/PSStyleStrings.resx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,4 +64,7 @@
6464
<data name="ProgressWidthTooSmall" xml:space="preserve">
6565
<value>The MaxWidth for the Progress rendering must be at least 18 to render correctly.</value>
6666
</data>
67+
<data name="ExtensionNotStartingWithPeriod" xml:space="preserve">
68+
<value>When adding or removing extensions, the extension must start with a period.</value>
69+
</data>
6770
</root>

test/powershell/engine/Formatting/PSStyle.Tests.ps1

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -207,25 +207,22 @@ Describe 'Tests for $PSStyle automatic variable' {
207207
{ $PSStyle.FileInfo.Extension.Add('.md', 'hello') } | Should -Throw -ErrorId 'InvalidOperationException'
208208
}
209209

210-
It 'Should add and remove extension: <extension>' -TestCases @(
211-
@{ extension = '.myext' }
212-
@{ extension = 'myext' }
213-
) {
214-
param ($extension)
215-
216-
if ($extension.StartsWith('.')) {
217-
$fullExtension = $extension
218-
}
219-
else {
220-
$fullExtension = '.' + $extension
221-
}
222-
223-
$PSStyle.FileInfo.Extension.Keys | Should -Not -Contain $fullextension
210+
It 'Should add and remove extension' {
211+
$extension = '.mytest'
212+
$PSStyle.FileInfo.Extension.Keys | Should -Not -Contain $extension
224213
$PSStyle.FileInfo.Extension.Add($extension, $PSStyle.Foreground.Blue)
225214

226-
$PSStyle.FileInfo.Extension[$fullextension] | Should -Be $PSStyle.Foreground.Blue
215+
$PSStyle.FileInfo.Extension[$extension] | Should -Be $PSStyle.Foreground.Blue
227216
$PSStyle.FileInfo.Extension.Remove($extension)
228-
$PSStyle.FileInfo.Extension.Keys | Should -Not -Contain $fullextension
217+
$PSStyle.FileInfo.Extension.Keys | Should -Not -Contain $extension
218+
}
219+
220+
It 'Should fail to add extension does not start with a period' {
221+
{ $PSStyle.FileInfo.Extension.Add('mytest', $PSStyle.Foreground.Blue) } | Should -Throw -ErrorId 'ArgumentException'
222+
}
223+
224+
It 'Should fail to remove extension does not start with a period' {
225+
{ $PSStyle.FileInfo.Extension.Remove('zip') } | Should -Throw -ErrorId 'ArgumentException'
229226
}
230227

231228
It 'Should fail if MaxWidth is less than 18' {

0 commit comments

Comments
 (0)
0