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

implement env vars in settings (#2785) #9287

Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
157 changes: 157 additions & 0 deletions src/cascadia/LocalTests_SettingsModel/ProfileTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ namespace SettingsModelLocalTests
TEST_METHOD(LayerProfileProperties);
TEST_METHOD(LayerProfileIcon);
TEST_METHOD(LayerProfilesOnArray);
TEST_METHOD(ProfileWithEnvVarSelfRefKeyThrows);
TEST_METHOD(ProfileWithEnvVarCircularRefsThrows);
TEST_METHOD(ProfileWithEnvVarNoReferences);
TEST_METHOD(ProfileWithEnvVarProcessEnv);
TEST_METHOD(ProfileWithEnvVarComplex);
TEST_METHOD(ProfileWithEnvVarWithParent);

TEST_CLASS_SETUP(ClassSetup)
{
Expand Down Expand Up @@ -307,4 +313,155 @@ namespace SettingsModelLocalTests
VERIFY_ARE_EQUAL(L"profile4", settings->_allProfiles.GetAt(0).Name());
}

void ProfileTests::ProfileWithEnvVarSelfRefKeyThrows()
{
const std::string profileString{ R"({
"name": "profile0",
"guid": "{6239a42c-0000-49a3-80bd-e8fdd045185c}",
"environment": {
"VAR_1": "${env:VAR_1}"
}
})" };
auto profile = implementation::Profile::FromJson(VerifyParseSucceeded(profileString));
VERIFY_THROWS(profile->ValidateEvaluatedEnvironmentVariables(), winrt::hresult_error);
}

void ProfileTests::ProfileWithEnvVarCircularRefsThrows()
{
const std::string profileString{ R"({
"name": "profile0",
"guid": "{6239a42c-0000-49a3-80bd-e8fdd045185c}",
"environment": {
"VAR_1": "${env:VAR_2}",
"VAR_2": "${env:VAR_3}",
"VAR_3": "${env:VAR_1}"
}
})" };
auto profile = implementation::Profile::FromJson(VerifyParseSucceeded(profileString));
VERIFY_THROWS(profile->ValidateEvaluatedEnvironmentVariables(), winrt::hresult_error);
}

void ProfileTests::ProfileWithEnvVarNoReferences()
{
const std::string profileString{ R"({
"name": "profile0",
"guid": "{6239a42c-0000-49a3-80bd-e8fdd045185c}",
"environment": {
"VAR_1": "value1",
"VAR_2": "value2",
"VAR_3": "value3"
}
})" };
const auto profile = implementation::Profile::FromJson(VerifyParseSucceeded(profileString));
std::vector<winrt::Windows::Foundation::Collections::StringMap> envVarMaps{};
envVarMaps.emplace_back(profile->EnvironmentVariables());
envVarMaps.emplace_back(profile->EvaluatedEnvironmentVariables());
for (auto& envMap : envVarMaps)
{
VERIFY_ARE_EQUAL(static_cast<uint32_t>(3), envMap.Size());
VERIFY_ARE_EQUAL(L"value1", envMap.Lookup(L"VAR_1"));
VERIFY_ARE_EQUAL(L"value2", envMap.Lookup(L"VAR_2"));
VERIFY_ARE_EQUAL(L"value3", envMap.Lookup(L"VAR_3"));
}
}

void ProfileTests::ProfileWithEnvVarProcessEnv()
{
const std::string profileString{ R"({
"name": "profile0",
"guid": "{6239a42c-0000-49a3-80bd-e8fdd045185c}",
"environment": {
"VAR_1": "${env:SystemRoot}",
"VAR_2": "${env:___DOES_NOT_EXIST_IN_ENVIRONMENT___1234567890}"
}
})" };

const auto expectedSystemRoot = wil::TryGetEnvironmentVariableW<std::wstring>(L"SystemRoot");
VERIFY_IS_FALSE(expectedSystemRoot.empty());
const auto profile = implementation::Profile::FromJson(VerifyParseSucceeded(profileString));
const auto envMap = profile->EvaluatedEnvironmentVariables();
VERIFY_ARE_EQUAL(static_cast<uint32_t>(2), envMap.Size());
VERIFY_ARE_EQUAL(expectedSystemRoot, envMap.Lookup(L"VAR_1"));
VERIFY_ARE_EQUAL(L"", envMap.Lookup(L"VAR_2"));
}

void ProfileTests::ProfileWithEnvVarComplex()
{
const std::string profileString{ R"({
"name": "profile0",
"guid": "{6239a42c-0000-49a3-80bd-e8fdd045185c}",
"environment": {
"TEST_HOME_DRIVE": "C:",
"TEST_HOME": "${env:TEST_HOME_DRIVE}${env:TEST_HOME_PATH}",
"TEST_HOME_PATH": "\\Users\\example",
"PARAM_START": "${env:TEST_HOME} abc",
"PARAM_MIDDLE": "abc ${env:TEST_HOME} abc",
"PARAM_END": "abc ${env:TEST_HOME}",
"PARAM_MULTIPLE": "${env:PARAM_START};${env:PARAM_MIDDLE};${env:PARAM_END}",
"PARAM_MULTIPLE_SAME": "${env:TEST_HOME_DRIVE};${env:TEST_HOME_DRIVE};${env:TEST_HOME_DRIVE};${env:TEST_HOME_DRIVE};${env:TEST_HOME_DRIVE};"
}
})" };

const auto profile = implementation::Profile::FromJson(VerifyParseSucceeded(profileString));
const auto envMap = profile->EvaluatedEnvironmentVariables();
VERIFY_ARE_EQUAL(static_cast<uint32_t>(8), envMap.Size());
VERIFY_ARE_EQUAL(L"C:\\Users\\example", envMap.Lookup(L"TEST_HOME"));
VERIFY_ARE_EQUAL(L"C:\\Users\\example abc", envMap.Lookup(L"PARAM_START"));
VERIFY_ARE_EQUAL(L"abc C:\\Users\\example abc", envMap.Lookup(L"PARAM_MIDDLE"));
VERIFY_ARE_EQUAL(L"abc C:\\Users\\example", envMap.Lookup(L"PARAM_END"));
VERIFY_ARE_EQUAL(L"C:\\Users\\example abc;abc C:\\Users\\example abc;abc C:\\Users\\example", envMap.Lookup(L"PARAM_MULTIPLE"));
VERIFY_ARE_EQUAL(L"C:;C:;C:;C:;C:;", envMap.Lookup(L"PARAM_MULTIPLE_SAME"));
}

void ProfileTests::ProfileWithEnvVarWithParent()
{
const std::string parentString{ R"({
"name": "profile0",
"guid": "{6239a42c-0000-49a3-80bd-e8fdd045185c}",
"environment": {
"VAR_1": "parent",
"VAR_2": "parent",
"VAR_4": "parent",
"VAR_6": "${env:VAR_3}",
"VAR_8": "${env:SystemRoot}"
}
})" };

const std::string childString{ R"({
"name": "profile0",
"guid": "{6239a42c-0000-49a3-80bd-e8fdd045185c}",
"environment": {
"VAR_1": "child",
"VAR_3": "child",
"VAR_5": "${env:VAR_4}",
"VAR_7": "${env:VAR_6}"
}
})" };

const auto parentProfile = implementation::Profile::FromJson(VerifyParseSucceeded(parentString));
const auto childProfile = implementation::Profile::FromJson(VerifyParseSucceeded(childString));
childProfile->InsertParent(parentProfile);


const auto expectedSystemRoot = wil::TryGetEnvironmentVariableW<std::wstring>(L"SystemRoot");
VERIFY_IS_FALSE(expectedSystemRoot.empty());

const auto parentEnvVars = parentProfile->EvaluatedEnvironmentVariables();
const auto childEnvVars = childProfile->EvaluatedEnvironmentVariables();

VERIFY_ARE_EQUAL(L"parent", parentEnvVars.Lookup(L"VAR_1"));
VERIFY_ARE_EQUAL(L"parent", parentEnvVars.Lookup(L"VAR_2"));
VERIFY_ARE_EQUAL(L"parent", parentEnvVars.Lookup(L"VAR_4"));
VERIFY_ARE_EQUAL(L"", parentEnvVars.Lookup(L"VAR_6"));
VERIFY_ARE_EQUAL(expectedSystemRoot, parentEnvVars.Lookup(L"VAR_8"));

VERIFY_ARE_EQUAL(L"child", childEnvVars.Lookup(L"VAR_1"));
VERIFY_ARE_EQUAL(L"parent", childEnvVars.Lookup(L"VAR_2"));
VERIFY_ARE_EQUAL(L"child", childEnvVars.Lookup(L"VAR_3"));
VERIFY_ARE_EQUAL(L"parent", childEnvVars.Lookup(L"VAR_4"));
VERIFY_ARE_EQUAL(L"parent", childEnvVars.Lookup(L"VAR_5"));
VERIFY_ARE_EQUAL(L"child", childEnvVars.Lookup(L"VAR_6"));
VERIFY_ARE_EQUAL(L"child", childEnvVars.Lookup(L"VAR_7"));
VERIFY_ARE_EQUAL(expectedSystemRoot, childEnvVars.Lookup(L"VAR_8"));
}
}
8 changes: 7 additions & 1 deletion src/cascadia/LocalTests_SettingsModel/SerializationTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,13 @@ namespace SettingsModelLocalTests

"experimental.input.forceVT": false,
"experimental.rendering.forceFullRepaint": false,
"experimental.rendering.software": false
"experimental.rendering.software": false,
"environment":
{
"KEY_1": "VALUE_1",
"KEY_2": "${env:KEY_1}",
"KEY_3": "${env:PATH}"
}
})" };

const std::string smallGlobalsString{ R"(
Expand Down
3 changes: 2 additions & 1 deletion src/cascadia/TerminalApp/AppLogic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ static const std::array<std::wstring_view, static_cast<uint32_t>(SettingsLoadWar
USES_RESOURCE(L"FailedToWriteToSettings"),
USES_RESOURCE(L"InvalidColorSchemeInCmd"),
USES_RESOURCE(L"InvalidSplitSize"),
USES_RESOURCE(L"FailedToParseStartupActions")
USES_RESOURCE(L"FailedToParseStartupActions"),
USES_RESOURCE(L"InvalidProfileEnvironmentVariables")
};
static const std::array<std::wstring_view, static_cast<uint32_t>(SettingsLoadErrors::ERRORS_SIZE)> settingsLoadErrorsLabels {
USES_RESOURCE(L"NoProfilesText"),
Expand Down
3 changes: 3 additions & 0 deletions src/cascadia/TerminalApp/Resources/en-US/Resources.resw
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,9 @@
<value>Failed to parse "startupActions".</value>
<comment>{Locked="\"startupActions\""}</comment>
</data>
<data name="InvalidProfileEnvironmentVariables" xml:space="preserve">
<value>Failed to expand profile environment variables. Please check for circular or self references.</value>
</data>
<data name="CmdCommandArgDesc" xml:space="preserve">
<value>An optional command, with arguments, to be spawned in the new tab or pane</value>
</data>
Expand Down
2 changes: 1 addition & 1 deletion src/cascadia/TerminalApp/TerminalPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -929,7 +929,7 @@ namespace winrt::TerminalApp::implementation
{
std::wstring guidWString = Utils::GuidToString(profileGuid);

StringMap envMap{};
auto envMap = settings.EnvironmentVariables();
envMap.Insert(L"WT_PROFILE_ID", guidWString);
envMap.Insert(L"WSLENV", L"WT_PROFILE_ID");

Expand Down
3 changes: 3 additions & 0 deletions src/cascadia/TerminalApp/TerminalSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

using namespace winrt::Microsoft::Terminal::TerminalControl;
using namespace winrt::Microsoft::Terminal::Settings::Model;
using namespace winrt::Windows::Foundation::Collections;
using namespace Microsoft::Console::Utils;

namespace winrt::TerminalApp::implementation
Expand Down Expand Up @@ -193,6 +194,8 @@ namespace winrt::TerminalApp::implementation
const til::color colorRef{ profile.TabColor().Value() };
_TabColor = static_cast<uint32_t>(colorRef);
}

_EnvironmentVariables = profile.EvaluatedEnvironmentVariables();
}

// Method Description:
Expand Down
2 changes: 1 addition & 1 deletion src/cascadia/TerminalApp/TerminalSettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ namespace winrt::TerminalApp::implementation
GETSET_SETTING(TerminalApp::TerminalSettings, hstring, StartingDirectory);
GETSET_SETTING(TerminalApp::TerminalSettings, hstring, StartingTitle);
GETSET_SETTING(TerminalApp::TerminalSettings, bool, SuppressApplicationTitle);
GETSET_SETTING(TerminalApp::TerminalSettings, hstring, EnvironmentVariables);
GETSET_SETTING(TerminalApp::TerminalSettings, winrt::Windows::Foundation::Collections::StringMap, EnvironmentVariables);

GETSET_SETTING(TerminalApp::TerminalSettings, Microsoft::Terminal::TerminalControl::ScrollbarState, ScrollState, Microsoft::Terminal::TerminalControl::ScrollbarState::Visible);

Expand Down
2 changes: 2 additions & 0 deletions src/cascadia/TerminalApp/TerminalSettings.idl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ namespace TerminalApp
Microsoft.Terminal.TerminalControl.IControlSettings
{
TerminalSettings();

Windows.Foundation.Collections.StringMap EnvironmentVariables;
};

}
1 change: 0 additions & 1 deletion src/cascadia/TerminalControl/IControlSettings.idl
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ namespace Microsoft.Terminal.TerminalControl

String Commandline;
String StartingDirectory;
String EnvironmentVariables;

String BackgroundImage;
Double BackgroundImageOpacity;
Expand Down
1 change: 1 addition & 0 deletions src/cascadia/TerminalSettingsEditor/Profiles.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ namespace winrt::Microsoft::Terminal::Settings::Editor::implementation
OBSERVABLE_PROJECTED_SETTING(_profile, CursorShape);
OBSERVABLE_PROJECTED_SETTING(_profile, CursorHeight);
OBSERVABLE_PROJECTED_SETTING(_profile, BellStyle);
OBSERVABLE_PROJECTED_SETTING(_profile, EnvironmentVariables);

private:
Model::Profile _profile;
Expand Down
1 change: 1 addition & 0 deletions src/cascadia/TerminalSettingsEditor/Profiles.idl
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ namespace Microsoft.Terminal.Settings.Editor
OBSERVABLE_PROJECTED_PROFILE_SETTING(Microsoft.Terminal.TerminalControl.CursorStyle, CursorShape);
OBSERVABLE_PROJECTED_PROFILE_SETTING(UInt32, CursorHeight);
OBSERVABLE_PROJECTED_PROFILE_SETTING(Microsoft.Terminal.Settings.Model.BellStyle, BellStyle);
OBSERVABLE_PROJECTED_PROFILE_SETTING(Windows.Foundation.Collections.StringMap, EnvironmentVariables);
}

runtimeclass DeleteProfileEventArgs
Expand Down
20 changes: 20 additions & 0 deletions src/cascadia/TerminalSettingsModel/CascadiaSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,8 @@ void CascadiaSettings::_ValidateSettings()
_ValidateColorSchemesInCommands();

_ValidateNoGlobalsKey();

_ValidateProfileEnvironmentVariables();
}

// Method Description:
Expand All @@ -334,6 +336,24 @@ void CascadiaSettings::_ValidateProfilesExist()
}
}

// Method Description:
// - Checks if the profiles contain invalid environment variable values
// profiles at all, we'll throw an error if there aren't any profiles.
void CascadiaSettings::_ValidateProfileEnvironmentVariables()
christapley marked this conversation as resolved.
Show resolved Hide resolved
{
for (const auto& profile : _allProfiles)
{
try
{
profile.ValidateEvaluatedEnvironmentVariables();
}
catch (...)
{
_warnings.Append(Microsoft::Terminal::Settings::Model::SettingsLoadWarnings::InvalidProfileEnvironmentVariables);
christapley marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

// Method Description:
// - Resolves the "defaultProfile", which can be a profile name, to a GUID
// and stores it back to the globals.
Expand Down
1 change: 1 addition & 0 deletions src/cascadia/TerminalSettingsModel/CascadiaSettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation

void _ValidateSettings();
void _ValidateProfilesExist();
void _ValidateProfileEnvironmentVariables();
void _ValidateDefaultProfileExists();
void _ValidateNoDuplicateProfiles();
void _ResolveDefaultProfile();
Expand Down
42 changes: 42 additions & 0 deletions src/cascadia/TerminalSettingsModel/JsonUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,48 @@ namespace Microsoft::Terminal::Settings::Model::JsonUtils
};
#endif

template<typename T>
void SetValueForKey(Json::Value& json, std::string_view key, const T& target);

template<typename T>
std::decay_t<T> GetValue(const Json::Value& json);

template<>
struct ConversionTrait<winrt::Windows::Foundation::Collections::StringMap>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we move this ConversionTrait near the end of the file, would we need the fwddecls for nit: SetValueForKey and GetValue above?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this is a bit of a chicken and egg story. I can't put it below SetValueForKey or GetValue as they both use the ConversionTrait template. So while, yes, it could go lower I don't believe it can go low enough to not require the fwd declarations and then it wouldn't be near the other ConversionTrait template specialisations.

The other alternative is that I don't put this ConversionTrait specialisation in JsonUtil.h. It could go in Profile.cpp if you think this is better?

{
winrt::Windows::Foundation::Collections::StringMap FromJson(const Json::Value& json)
{
winrt::Windows::Foundation::Collections::StringMap stringMap{};
for (Json::Value::const_iterator it = json.begin(); it != json.end(); ++it)
{
stringMap.Insert(GetValue<winrt::hstring>(it.key()), GetValue<winrt::hstring>(*it));
}
return stringMap;
}

bool CanConvert(const Json::Value&)
{
return true;
}

Json::Value ToJson(const winrt::Windows::Foundation::Collections::StringMap& val)
{
Json::Value json{ Json::ValueType::objectValue };
auto view = val.GetView();
for (auto it = view.First(); it.HasCurrent(); it.MoveNext())
{
SetValueForKey(json, til::u16u8((*it).Key()), (*it).Value());
}

return json;
}

std::string TypeDescription() const
{
return "StringMap";
}
};

template<typename T, typename TDelegatedConverter = ConversionTrait<typename std::decay<T>::type>, typename TOpt = std::optional<typename std::decay<T>::type>>
struct OptionalConverter
{
Expand Down
Loading