Skip to content
This repository has been archived by the owner on Aug 4, 2023. It is now read-only.

Add AllowNoMatch attribute to hide no match warning #194

Closed
wants to merge 2 commits into from
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
1 change: 1 addition & 0 deletions libs/xml-operations/include/xml_operations.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class XmlOperation
pugi::xml_node node_;

bool skip_ = false;
bool allow_no_match_ = false;

std::string mod_name_;
fs::path game_path_;
Expand Down
12 changes: 10 additions & 2 deletions libs/xml-operations/src/xml_operations.cc
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ XmlOperation::XmlOperation(std::shared_ptr<pugi::xml_document> doc, pugi::xml_no
}

skip_ = node.attribute("Skip");
allow_no_match_ = node.attribute("AllowNoMatch");
}

void XmlOperation::ReadPath(pugi::xml_node node, std::string guid, std::string temp)
Expand Down Expand Up @@ -359,8 +360,15 @@ void XmlOperation::Apply(std::shared_ptr<pugi::xml_document> doc)
offset_data_t offset_data;
build_offset_data(offset_data, mod_path_.string().c_str());
auto [line, column] = get_location(offset_data, node_.offset_debug());
spdlog::warn("No matching node for Path {} in {} ({}:{})", GetPath(), mod_name_,
game_path_.string(), line);

const std::string msg = "No matching node for Path {} in {} ({}:{})";
if (allow_no_match_) {
spdlog::debug(msg, GetPath(), mod_name_, game_path_.string(), line);
}
else {
spdlog::warn(msg, GetPath(), mod_name_, game_path_.string(), line);
}

return;
}

Expand Down
8 changes: 8 additions & 0 deletions tests/xml/gen_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ def main():
else:
f.write("CHECK(runner.PathExists(\"" +
expected_path + "\"));")

# check log warnings
f.write("INFO(runner.DumpLog());")
if data.get("issuesExpected", "0") == "1":
f.write("CHECK(runner.HasIssues());")
else:
f.write("CHECK_FALSE(runner.HasIssues());")

f.write("}\n\n")


Expand Down
7 changes: 7 additions & 0 deletions tests/xml/noMatchWarning/no_match_allow.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "Add No Allow",
"expected": [
"/Test/Node/Meow[GUID='1']"
],
"issuesExpected": "0"
}
5 changes: 5 additions & 0 deletions tests/xml/noMatchWarning/no_match_allow_input.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<Test>
<Node>
<Meow><GUID>1</GUID></Meow>
</Node>
</Test>
5 changes: 5 additions & 0 deletions tests/xml/noMatchWarning/no_match_allow_patch.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<ModOps>
<ModOp Type="addNextSibling" Path="/Test/Node/Meow[GUID='2']" AllowNoMatch="1">
<Meow><GUID>3</GUID></Meow>
</ModOp>
</ModOps>
7 changes: 7 additions & 0 deletions tests/xml/noMatchWarning/no_match_warning.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "Add No Match",
"expected": [
"/Test/Node/Meow[GUID='1']"
],
"issuesExpected": "1"
}
5 changes: 5 additions & 0 deletions tests/xml/noMatchWarning/no_match_warning_input.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<Test>
<Node>
<Meow><GUID>1</GUID></Meow>
</Node>
</Test>
5 changes: 5 additions & 0 deletions tests/xml/noMatchWarning/no_match_warning_patch.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<ModOps>
<ModOp Type="addNextSibling" Path="/Test/Node/Meow[GUID='2']">
<Meow><GUID>3</GUID></Meow>
</ModOp>
</ModOps>
30 changes: 28 additions & 2 deletions tests/xml/runner.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include "pugixml.hpp"
#include "spdlog/sinks/ostream_sink.h"
#include "spdlog/spdlog.h"

#include "xml_operations.h"

Expand All @@ -14,6 +16,13 @@ class TestRunner
{
public:
TestRunner(std::string_view mod_path, std::string_view input, std::string_view patch) {
{
auto sink = std::make_shared<spdlog::sinks::ostream_sink_st>(test_log_);
auto test_logger = std::make_shared<spdlog::logger>("test_logger", sink);
test_logger->set_pattern("[%l] %v");
test_logger->set_level(spdlog::level::info);
spdlog::set_default_logger(test_logger);
}
{
xml_operations_ = XmlOperation::GetXmlOperationsFromFile(patch, "", input, mod_path);
}
Expand Down Expand Up @@ -42,16 +51,33 @@ class TestRunner
return exists;
}


std::string DumpXml() {
std::stringstream ss;
input_doc_->print(ss, " ");
std::string buf = ss.str();
return buf;
}

~TestRunner() = default;
bool HasIssues() {
auto log_content = test_log_.str();

if (log_content.find("[warning]") != std::string::npos ||
log_content.find("[error]") != std::string::npos) {
return true;
}

return false;
}

std::string DumpLog() {
return test_log_.str();
}

~TestRunner() {
spdlog::drop("test_logger");
}
private:
std::vector<XmlOperation> xml_operations_;
std::shared_ptr<pugi::xml_document> input_doc_ = nullptr;
std::ostringstream test_log_;
};