Add validation for hiding all the profiles (#2800)

fixes #2794
This commit is contained in:
Mike Griese 2019-09-19 11:54:36 -05:00 committed by msftbot[bot]
parent a62c6cd22b
commit 7128e873a4
5 changed files with 85 additions and 3 deletions

View file

@ -47,6 +47,7 @@ namespace TerminalAppLocalTests
TEST_METHOD(TestReorderingWithoutGuid);
TEST_METHOD(TestLayeringNameOnlyProfiles);
TEST_METHOD(TestExplodingNameOnlyProfiles);
TEST_METHOD(TestHideAllProfiles);
TEST_CLASS_SETUP(ClassSetup)
{
@ -1125,4 +1126,69 @@ namespace TerminalAppLocalTests
VERIFY_ARE_EQUAL(L"Windows PowerShell", settings._profiles.at(3)._name);
VERIFY_ARE_EQUAL(L"cmd", settings._profiles.at(4)._name);
}
void SettingsTests::TestHideAllProfiles()
{
const std::string settingsWithProfiles{ R"(
{
"profiles": [
{
"name" : "profile0",
"hidden": false
},
{
"name" : "profile1",
"hidden": true
}
]
})" };
const std::string settingsWithoutProfiles{ R"(
{
"profiles": [
{
"name" : "profile0",
"hidden": true
},
{
"name" : "profile1",
"hidden": true
}
]
})" };
VerifyParseSucceeded(settingsWithProfiles);
VerifyParseSucceeded(settingsWithoutProfiles);
{
// Case 1: Good settings
CascadiaSettings settings;
settings._ParseJsonString(settingsWithProfiles, false);
settings.LayerJson(settings._userSettings);
settings._RemoveHiddenProfiles();
Log::Comment(NoThrowString().Format(
L"settingsWithProfiles successfully parsed and validated"));
VERIFY_ARE_EQUAL(1u, settings._profiles.size());
}
{
// Case 2: Bad settings
CascadiaSettings settings;
settings._ParseJsonString(settingsWithoutProfiles, false);
settings.LayerJson(settings._userSettings);
bool caughtExpectedException = false;
try
{
settings._RemoveHiddenProfiles();
}
catch (const ::TerminalApp::SettingsException& ex)
{
VERIFY_IS_TRUE(ex.Error() == ::TerminalApp::SettingsLoadErrors::AllProfilesHidden);
caughtExpectedException = true;
}
VERIFY_IS_TRUE(caughtExpectedException);
}
}
}

View file

@ -30,8 +30,9 @@ static const std::array<std::wstring_view, 2> settingsLoadWarningsLabels {
L"MissingDefaultProfileText",
L"DuplicateProfileText"
};
static const std::array<std::wstring_view, 1> settingsLoadErrorsLabels {
L"NoProfilesText"
static const std::array<std::wstring_view, 2> settingsLoadErrorsLabels {
L"NoProfilesText",
L"AllProfilesHiddenText"
};
// clang-format on

View file

@ -355,4 +355,14 @@ void CascadiaSettings::_RemoveHiddenProfiles()
_profiles.end(),
[](auto&& profile) { return profile.IsHidden(); }),
_profiles.end());
// Ensure that we still have some profiles here. If we don't, then throw an
// exception, so the app can use the defaults.
const bool hasProfiles = !_profiles.empty();
if (!hasProfiles)
{
// Throw an exception. This is an invalid state, and we want the app to
// be able to gracefully use the default settings.
throw ::TerminalApp::SettingsException(::TerminalApp::SettingsLoadErrors::AllProfilesHidden);
}
}

View file

@ -131,6 +131,10 @@
</data>
<data name="NoProfilesText" xml:space="preserve">
<value>No profiles were found in your settings.
</value>
</data>
<data name="AllProfilesHiddenText" xml:space="preserve">
<value>All profiles were hidden in your settings. You must have at least one non-hidden profile.
</value>
</data>
<data name="ReloadJsonParseErrorText" xml:space="preserve">

View file

@ -29,7 +29,8 @@ namespace TerminalApp
// that we could not recover from.
enum class SettingsLoadErrors : uint32_t
{
NoProfiles = 0
NoProfiles = 0,
AllProfilesHidden = 1
};
// This is a helper class to wrap up a SettingsLoadErrors into a proper