Skip to content

Commit 09300c0

Browse files
SteveL-MSFTdaxian-dbw
authored andcommitted
Return error if color property or value is invalid with Set-PSReadLineOption -Colors (#1124)
1 parent ae467e1 commit 09300c0

File tree

4 files changed

+53
-8
lines changed

4 files changed

+53
-8
lines changed

PSReadLine/Cmdlets.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,10 @@ internal void SetColor(string property, object value)
483483
{
484484
setter(this, value);
485485
}
486+
else
487+
{
488+
throw new ArgumentException(string.Format(CultureInfo.CurrentUICulture, PSReadLineResources.InvalidColorProperty, property));
489+
}
486490
}
487491
}
488492

@@ -964,7 +968,7 @@ public static string AsEscapeSequence(object o, bool isBackground)
964968
break;
965969
}
966970

967-
throw new ArgumentException("o");
971+
throw new ArgumentException(string.Format(CultureInfo.CurrentUICulture, PSReadLineResources.InvalidColorValue, o.ToString()));
968972
}
969973

970974
public static string AsEscapeSequence(ConsoleColor fg, ConsoleColor bg)

PSReadLine/PSReadLineResources.Designer.cs

Lines changed: 14 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

PSReadLine/PSReadLineResources.resx

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -751,9 +751,6 @@ Or not saving history with:
751751
<data name="OopsCustomHandlerException" xml:space="preserve">
752752
<value>An exception occurred in custom key handler, see $error for more information: {0}</value>
753753
</data>
754-
<data name="InvalidColorParameter" xml:space="preserve">
755-
<value>Parameter must be a ConsoleColor, ANSI escape sequence, or RGB value with optional leading '#'.</value>
756-
</data>
757754
<data name="BasicGrouping" xml:space="preserve">
758755
<value>Basic editing functions</value>
759756
</data>
@@ -778,4 +775,10 @@ Or not saving history with:
778775
<data name="CustomGrouping" xml:space="preserve">
779776
<value>User defined functions</value>
780777
</data>
778+
<data name="InvalidColorValue" xml:space="preserve">
779+
<value>'{0}' is not a valid color value. It must be a ConsoleColor, ANSI escape sequence, or RGB value with optional leading '#'.</value>
780+
</data>
781+
<data name="InvalidColorProperty" xml:space="preserve">
782+
<value>'{0}' is not a valid color property</value>
783+
</data>
781784
</root>

test/OptionsTest.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,34 @@ public void GetKeyHandlers()
7878
}
7979
}
8080

81+
[SkippableFact]
82+
public void SetInvalidColorOptions()
83+
{
84+
bool throws = false;
85+
try
86+
{
87+
PSConsoleReadLine.SetOptions(new SetPSReadLineOption{
88+
Colors = new Hashtable {
89+
{ "InvalidProperty", ConsoleColor.Magenta }
90+
},
91+
});
92+
}
93+
catch (ArgumentException) { throws = true; }
94+
Assert.True(throws, "Invalid color property should throw");
95+
96+
throws = false;
97+
try
98+
{
99+
PSConsoleReadLine.SetOptions(new SetPSReadLineOption{
100+
Colors = new Hashtable {
101+
{ "Default", "apple" }
102+
},
103+
});
104+
}
105+
catch (ArgumentException) { throws = true; }
106+
Assert.True(throws, "Invalid color value should throw");
107+
}
108+
81109
[SkippableFact]
82110
[ExcludeFromCodeCoverage]
83111
public void UselessStuffForBetterCoverage()

0 commit comments

Comments
 (0)