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 all 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
184 changes: 184 additions & 0 deletions src/cascadia/LocalTests_SettingsModel/ProfileTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,13 @@ 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_METHOD(ProfileWithEnvVarAppendingToExistingProcessEnvVer);

TEST_CLASS_SETUP(ClassSetup)
{
Expand Down Expand Up @@ -307,4 +314,181 @@ 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"));
}

void ProfileTests::ProfileWithEnvVarAppendingToExistingProcessEnvVer()
{
const std::string profileString{ R"({
"name": "profile0",
"guid": "{6239a42c-0000-49a3-80bd-e8fdd045185c}",
"environment": {
"PATH": "${env:PATH};C:\\MyAwesomeFolder",
"SystemRoot": "prepend${env:SystemRoot}"
}
})" };

const auto profile = implementation::Profile::FromJson(VerifyParseSucceeded(profileString));
const auto envMap = profile->EvaluatedEnvironmentVariables();

auto expectedPath = wil::TryGetEnvironmentVariableW<std::wstring>(L"PATH");
VERIFY_IS_FALSE(expectedPath.empty());
expectedPath += L";C:\\MyAwesomeFolder";

auto expectedSystemRoot = wil::TryGetEnvironmentVariableW<std::wstring>(L"SystemRoot");
VERIFY_IS_FALSE(expectedSystemRoot.empty());
expectedSystemRoot.insert(0, L"prepend");

VERIFY_ARE_EQUAL(static_cast<uint32_t>(2), envMap.Size());
VERIFY_ARE_EQUAL(expectedPath, envMap.Lookup(L"PATH"));
VERIFY_ARE_EQUAL(expectedSystemRoot, envMap.Lookup(L"SystemRoot"));
}
}
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 @@ -260,6 +260,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 @@ -939,7 +939,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
Loading