Skip to content

Commit 644ac56

Browse files
authored
If a profile did not have a GUID, generate one (#2117)
Fixes #2108 Adds tests too!
1 parent 83a4c22 commit 644ac56

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

src/cascadia/TerminalApp/Profile.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,10 @@ Profile Profile::FromJson(const Json::Value& json)
350350
{
351351
result._guid = Utils::GuidFromString(GetWstringFromJson(guid));
352352
}
353+
else
354+
{
355+
result._guid = Utils::CreateGuid();
356+
}
353357

354358
// Core Settings
355359
if (auto foreground{ json[JsonKey(ForegroundKey)] })

src/cascadia/ut_app/JsonTests.cpp

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "precomp.h"
55

66
#include "../TerminalApp/ColorScheme.h"
7+
#include "../TerminalApp/Profile.h"
78

89
using namespace Microsoft::Console;
910
using namespace TerminalApp;
@@ -20,6 +21,8 @@ namespace TerminalAppUnitTests
2021

2122
TEST_METHOD(ParseInvalidJson);
2223
TEST_METHOD(ParseSimpleColorScheme);
24+
TEST_METHOD(ProfileGeneratesGuid);
25+
TEST_METHOD(GeneratedGuidRoundtrips);
2326

2427
TEST_CLASS_SETUP(ClassSetup)
2528
{
@@ -97,4 +100,82 @@ namespace TerminalAppUnitTests
97100
VERIFY_ARE_EQUAL(expected, actual);
98101
}
99102
}
103+
104+
void JsonTests::ProfileGeneratesGuid()
105+
{
106+
// Parse some profiles without guids. We should generate new guids for
107+
// them. The null guid _is_ a valid guid, so we won't re-generate that
108+
// guid. null is _not_ a valid guid, so we'll regenerate that.
109+
110+
const std::string profileWithoutGuid{ R"({
111+
"name" : "profile0"
112+
})" };
113+
const std::string secondProfileWithoutGuid{ R"({
114+
"name" : "profile1"
115+
})" };
116+
const std::string profileWithNullForGuid{ R"({
117+
"name" : "profile2",
118+
"guid" : null
119+
})" };
120+
const std::string profileWithNullGuid{ R"({
121+
"name" : "profile3",
122+
"guid" : "{00000000-0000-0000-0000-000000000000}"
123+
})" };
124+
const std::string profileWithGuid{ R"({
125+
"name" : "profile4",
126+
"guid" : "{6239a42c-1de4-49a3-80bd-e8fdd045185c}"
127+
})" };
128+
129+
const auto profile0Json = VerifyParseSucceeded(profileWithoutGuid);
130+
const auto profile1Json = VerifyParseSucceeded(secondProfileWithoutGuid);
131+
const auto profile2Json = VerifyParseSucceeded(profileWithNullForGuid);
132+
const auto profile3Json = VerifyParseSucceeded(profileWithNullGuid);
133+
const auto profile4Json = VerifyParseSucceeded(profileWithGuid);
134+
135+
const auto profile0 = Profile::FromJson(profile0Json);
136+
const auto profile1 = Profile::FromJson(profile1Json);
137+
const auto profile2 = Profile::FromJson(profile2Json);
138+
const auto profile3 = Profile::FromJson(profile3Json);
139+
const auto profile4 = Profile::FromJson(profile4Json);
140+
const GUID cmdGuid = Utils::GuidFromString(L"{6239a42c-1de4-49a3-80bd-e8fdd045185c}");
141+
const GUID nullGuid{ 0 };
142+
143+
VERIFY_ARE_EQUAL(profile4.GetGuid(), cmdGuid);
144+
145+
VERIFY_ARE_NOT_EQUAL(profile0.GetGuid(), nullGuid);
146+
VERIFY_ARE_NOT_EQUAL(profile1.GetGuid(), nullGuid);
147+
VERIFY_ARE_NOT_EQUAL(profile2.GetGuid(), nullGuid);
148+
VERIFY_ARE_EQUAL(profile3.GetGuid(), nullGuid);
149+
150+
VERIFY_ARE_NOT_EQUAL(profile0.GetGuid(), cmdGuid);
151+
VERIFY_ARE_NOT_EQUAL(profile1.GetGuid(), cmdGuid);
152+
VERIFY_ARE_NOT_EQUAL(profile2.GetGuid(), cmdGuid);
153+
154+
VERIFY_ARE_NOT_EQUAL(profile0.GetGuid(), profile1.GetGuid());
155+
VERIFY_ARE_NOT_EQUAL(profile2.GetGuid(), profile1.GetGuid());
156+
}
157+
158+
void JsonTests::GeneratedGuidRoundtrips()
159+
{
160+
// Parse a profile without a guid.
161+
// We should automatically generate a GUID for that profile.
162+
// When that profile is serialized and deserialized again, the GUID we
163+
// generated for it should persist.
164+
const std::string profileWithoutGuid{ R"({
165+
"name" : "profile0"
166+
})" };
167+
const auto profile0Json = VerifyParseSucceeded(profileWithoutGuid);
168+
169+
const auto profile0 = Profile::FromJson(profile0Json);
170+
const GUID nullGuid{ 0 };
171+
172+
VERIFY_ARE_NOT_EQUAL(profile0.GetGuid(), nullGuid);
173+
174+
const auto serializedProfile = profile0.ToJson();
175+
176+
const auto profile1 = Profile::FromJson(serializedProfile);
177+
178+
VERIFY_ARE_EQUAL(profile1.GetGuid(), profile0.GetGuid());
179+
}
180+
100181
}

0 commit comments

Comments
 (0)