Expand env. vars in backgroundImage (#3204)

* Adds HasBackgroundImage() and GetExpandedBackgroundImagePath() to
Profiles.cpp/h
* Fills Terminal Settings with expanded path, rather than path value
from profiles.json
* Adds simple regression tests to detect and fail if this fix is
circumvented in the future

Fixes #2922
This commit is contained in:
Rich Turner
2019-11-15 16:58:25 -08:00
committed by Dustin L. Howett (MSFT)
parent a8e352ed87
commit efa68abd3d
4 changed files with 76 additions and 2 deletions

View File

@@ -52,6 +52,9 @@ namespace TerminalAppLocalTests
TEST_METHOD(TestLayerGlobalsOnRoot);
TEST_METHOD(TestProfileIconWithEnvVar);
TEST_METHOD(TestProfileBackgroundImageWithEnvVar);
TEST_CLASS_SETUP(ClassSetup)
{
InitializeJsonReader();
@@ -1366,4 +1369,49 @@ namespace TerminalAppLocalTests
VERIFY_ARE_EQUAL(guid3, settings._globals._defaultProfile);
}
}
void SettingsTests::TestProfileIconWithEnvVar()
{
const auto expectedPath = wil::ExpandEnvironmentStringsW<std::wstring>(L"%WINDIR%\\System32\\x_80.png");
const std::string settingsJson{ R"(
{
"profiles": [
{
"name": "profile0",
"icon": "%WINDIR%\\System32\\x_80.png"
}
]
})" };
VerifyParseSucceeded(settingsJson);
CascadiaSettings settings{};
settings._ParseJsonString(settingsJson, false);
settings.LayerJson(settings._userSettings);
VERIFY_IS_FALSE(settings._profiles.empty(), 0);
VERIFY_ARE_EQUAL(expectedPath, settings._profiles[0].GetExpandedIconPath());
}
void SettingsTests::TestProfileBackgroundImageWithEnvVar()
{
const auto expectedPath = wil::ExpandEnvironmentStringsW<std::wstring>(L"%WINDIR%\\System32\\x_80.png");
const std::string settingsJson{ R"(
{
"profiles": [
{
"name": "profile0",
"backgroundImage": "%WINDIR%\\System32\\x_80.png"
}
]
})" };
VerifyParseSucceeded(settingsJson);
CascadiaSettings settings{};
settings._ParseJsonString(settingsJson, false);
settings.LayerJson(settings._userSettings);
VERIFY_IS_FALSE(settings._profiles.empty(), 0);
GlobalAppSettings globalSettings{};
auto terminalSettings = settings._profiles[0].CreateTerminalSettings(globalSettings.GetColorSchemes());
VERIFY_ARE_EQUAL(expectedPath, terminalSettings.BackgroundImage());
}
}

View File

@@ -214,9 +214,9 @@ TerminalSettings Profile::CreateTerminalSettings(const std::unordered_map<std::w
terminalSettings.ScrollState(result);
}
if (_backgroundImage)
if (HasBackgroundImage())
{
terminalSettings.BackgroundImage(_backgroundImage.value());
terminalSettings.BackgroundImage(GetExpandedBackgroundImagePath().c_str());
}
if (_backgroundImageOpacity)
@@ -765,6 +765,11 @@ bool Profile::HasIcon() const noexcept
return _icon.has_value() && !_icon.value().empty();
}
bool Profile::HasBackgroundImage() const noexcept
{
return _backgroundImage.has_value() && !_backgroundImage.value().empty();
}
// Method Description
// - Sets this profile's tab title.
// Arguments:
@@ -800,6 +805,23 @@ winrt::hstring Profile::GetExpandedIconPath() const
return envExpandedPath;
}
// Method Description:
// - Returns this profile's background image path, if one is set, expanding
// any environment variables in the path, if there are any.
// Return Value:
// - This profile's expanded background image path / the empty string.
winrt::hstring Profile::GetExpandedBackgroundImagePath() const
{
winrt::hstring result{};
if (HasBackgroundImage())
{
result = wil::ExpandEnvironmentStringsW<std::wstring>(_backgroundImage.value().data());
}
return result;
}
// Method Description:
// - Returns the name of this profile.
// Arguments:

View File

@@ -82,6 +82,9 @@ public:
winrt::hstring GetExpandedIconPath() const;
void SetIconPath(std::wstring_view path);
bool HasBackgroundImage() const noexcept;
winrt::hstring GetExpandedBackgroundImagePath() const;
bool GetCloseOnExit() const noexcept;
bool IsHidden() const noexcept;

View File

@@ -54,6 +54,7 @@
#include <wil/stl.h>
#include <wil/com.h>
#include <wil/filesystem.h>
#include <wil/win32_helpers.h>
// GSL
// Block GSL Multi Span include because it both has C++17 deprecated iterators