Skip to content

Commit cf3366f

Browse files
committed
fix: preProcessedFileSubstitution replacing to ""
preProcessedFileSubstitution did not work correctly if an empty map was given. In this case `variables["ACCESS_TOKEN"]` would be read as `""` as that key was not present in the map, addtionally, it would be added. Poco::replaceInPlace would in turn replace the `%ACCESS_TOKEN%` with `""`, while it should just keep the not specified parts as is. On top op that, now variables is modified so in the next iteration of the loop the test is no longer the same. I chose to add a function rather than ifs in the body to prevent future mistakes from happening. This does however make the code more complex I also made variables a const argument Change-Id: 8d84961da571686c39effa6d5b43918151da1be4 Signed-off-by: Robbert Gurdeep Singh <[email protected]>
1 parent 2720065 commit cf3366f

File tree

1 file changed

+32
-20
lines changed

1 file changed

+32
-20
lines changed

test/FileServeWhiteBoxTests.cpp

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@ class FileServeTests : public CPPUNIT_NS::TestFixture
4242
void testPreProcessedFileSubstitution();
4343

4444
void preProcessedFileSubstitution(const std::string_view testname,
45-
std::unordered_map<std::string, std::string> variables);
45+
const std::unordered_map<std::string, std::string> variables);
46+
// Helper replace each occurence of from in str to variables[to_key] except if to_key is not in variables
47+
std::string& replaceIfExist(std::string& str, const std::string& from,
48+
const std::string& to_key,
49+
const std::unordered_map<std::string, std::string> variables);
4650
};
4751

4852
void FileServeTests::testUIDefaults()
@@ -371,7 +375,7 @@ void FileServeTests::testPreProcessedFileRoundtrip()
371375
}
372376

373377
void FileServeTests::preProcessedFileSubstitution(
374-
const std::string_view testname, std::unordered_map<std::string, std::string> variables)
378+
const std::string_view testname, const std::unordered_map<std::string, std::string> variables)
375379
{
376380
const Poco::Path path(TDIST);
377381

@@ -391,30 +395,38 @@ void FileServeTests::preProcessedFileSubstitution(
391395

392396
const std::string recon = ppf.substitute(variables);
393397

394-
Poco::replaceInPlace(orig, std::string("%ACCESS_TOKEN%"), variables["ACCESS_TOKEN"]);
395-
Poco::replaceInPlace(orig, std::string("%ACCESS_TOKEN_TTL%"),
396-
variables["ACCESS_TOKEN_TTL"]);
397-
Poco::replaceInPlace(orig, std::string("%ACCESS_HEADER%"), variables["ACCESS_HEADER"]);
398-
Poco::replaceInPlace(orig, std::string("%UI_DEFAULTS%"), variables["UI_DEFAULTS"]);
399-
Poco::replaceInPlace(orig, std::string("<!--%CSS_VARIABLES%-->"),
400-
variables["CSS_VARIABLES"]);
401-
Poco::replaceInPlace(orig, std::string("%POSTMESSAGE_ORIGIN%"),
402-
variables["POSTMESSAGE_ORIGIN"]);
403-
Poco::replaceInPlace(orig, std::string("%BRANDING_THEME%"),
404-
variables["BRANDING_THEME"]);
405-
Poco::replaceInPlace(orig, std::string("<!--%BRANDING_JS%-->"),
406-
variables["BRANDING_JS"]);
407-
Poco::replaceInPlace(orig, std::string("%FOOTER%"), variables["FOOTER"]);
408-
Poco::replaceInPlace(orig, std::string("%CHECK_FILE_INFO_OVERRIDE%"),
409-
variables["CHECK_FILE_INFO_OVERRIDE"]);
410-
Poco::replaceInPlace(orig, std::string("%BUYPRODUCT_URL%"),
411-
variables["BUYPRODUCT_URL"]);
398+
replaceIfExist(orig, std::string("%ACCESS_TOKEN%"), "ACCESS_TOKEN", variables);
399+
replaceIfExist(orig, std::string("%ACCESS_TOKEN_TTL%"), "ACCESS_TOKEN_TTL", variables);
400+
replaceIfExist(orig, std::string("%ACCESS_HEADER%"), "ACCESS_HEADER", variables);
401+
replaceIfExist(orig, std::string("%UI_DEFAULTS%"), "UI_DEFAULTS", variables);
402+
replaceIfExist(orig, std::string("<!--%CSS_VARIABLES%-->"), "CSS_VARIABLES", variables);
403+
replaceIfExist(orig, std::string("%POSTMESSAGE_ORIGIN%"), "POSTMESSAGE_ORIGIN",
404+
variables);
405+
replaceIfExist(orig, std::string("%BRANDING_THEME%"), "BRANDING_THEME", variables);
406+
replaceIfExist(orig, std::string("<!--%BRANDING_JS%-->"), "BRANDING_JS", variables);
407+
replaceIfExist(orig, std::string("%FOOTER%"), "FOOTER", variables);
408+
replaceIfExist(orig, std::string("%CHECK_FILE_INFO_OVERRIDE%"),
409+
"CHECK_FILE_INFO_OVERRIDE", variables);
410+
replaceIfExist(orig, std::string("%BUYPRODUCT_URL%"), "BUYPRODUCT_URL", variables);
412411

413412
LOK_ASSERT_EQUAL(orig, recon);
414413
}
415414
}
416415
}
417416

417+
std::string&
418+
FileServeTests::replaceIfExist(std::string& str, const std::string& from, const std::string& to_key,
419+
const std::unordered_map<std::string, std::string> variables)
420+
{
421+
auto search = variables.find(to_key);
422+
if (search == variables.end())
423+
{
424+
// key not found, do nothing
425+
return str;
426+
}
427+
return Poco::replaceInPlace(str, from, search->second);
428+
}
429+
418430
void FileServeTests::testPreProcessedFileSubstitution()
419431
{
420432
constexpr std::string_view testname = __func__;

0 commit comments

Comments
 (0)