Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return error if color property or value is invalid with Set-PSReadLineOption -Colors #1124

Merged
merged 3 commits into from
Nov 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion PSReadLine/Cmdlets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,10 @@ internal void SetColor(string property, object value)
{
setter(this, value);
}
else
{
throw new ArgumentException(string.Format(CultureInfo.CurrentUICulture, PSReadLineResources.InvalidColorProperty, property));
}
}
}

Expand Down Expand Up @@ -964,7 +968,7 @@ public static string AsEscapeSequence(object o, bool isBackground)
break;
}

throw new ArgumentException("o");
throw new ArgumentException(string.Format(CultureInfo.CurrentUICulture, PSReadLineResources.InvalidColorValue, o.ToString()));
}

public static string AsEscapeSequence(ConsoleColor fg, ConsoleColor bg)
Expand Down
18 changes: 14 additions & 4 deletions PSReadLine/PSReadLineResources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions PSReadLine/PSReadLineResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -751,9 +751,6 @@ Or not saving history with:
<data name="OopsCustomHandlerException" xml:space="preserve">
<value>An exception occurred in custom key handler, see $error for more information: {0}</value>
</data>
<data name="InvalidColorParameter" xml:space="preserve">
<value>Parameter must be a ConsoleColor, ANSI escape sequence, or RGB value with optional leading '#'.</value>
</data>
<data name="BasicGrouping" xml:space="preserve">
<value>Basic editing functions</value>
</data>
Expand All @@ -778,4 +775,10 @@ Or not saving history with:
<data name="CustomGrouping" xml:space="preserve">
<value>User defined functions</value>
</data>
<data name="InvalidColorValue" xml:space="preserve">
<value>'{0}' is not a valid color value. It must be a ConsoleColor, ANSI escape sequence, or RGB value with optional leading '#'.</value>
</data>
<data name="InvalidColorProperty" xml:space="preserve">
<value>'{0}' is not a valid color property</value>
</data>
</root>
28 changes: 28 additions & 0 deletions test/OptionsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,34 @@ public void GetKeyHandlers()
}
}

[SkippableFact]
public void SetInvalidColorOptions()
{
bool throws = false;
try
{
PSConsoleReadLine.SetOptions(new SetPSReadLineOption{
Colors = new Hashtable {
{ "InvalidProperty", ConsoleColor.Magenta }
},
});
}
catch (ArgumentException) { throws = true; }
Assert.True(throws, "Invalid color property should throw");

throws = false;
try
{
PSConsoleReadLine.SetOptions(new SetPSReadLineOption{
Colors = new Hashtable {
{ "Default", "apple" }
},
});
}
catch (ArgumentException) { throws = true; }
Assert.True(throws, "Invalid color value should throw");
}

[SkippableFact]
[ExcludeFromCodeCoverage]
public void UselessStuffForBetterCoverage()
Expand Down