From: Martin Vidner Date: Fri, 13 Dec 2019 12:16:33 +0000 (+0100) Subject: Split off regex matching, add a test for it X-Git-Tag: v0.8.7^2~7 X-Git-Url: http://git.ipfire.org/gitweb/index.cgi?a=commitdiff_plain;h=8ddb5f981637f0d287bc7aa4e96285f2b60356e9;p=thirdparty%2Fsnapper.git Split off regex matching, add a test for it --- diff --git a/zypp-plugin/.gitignore b/zypp-plugin/.gitignore index f4c8bb74..ff41d306 100644 --- a/zypp-plugin/.gitignore +++ b/zypp-plugin/.gitignore @@ -1,2 +1,6 @@ *.o snapper-zypp-plugin +*.test +*.log +*.trs +test-suite.log diff --git a/zypp-plugin/Makefile.am b/zypp-plugin/Makefile.am index 7ce5339c..4e6ef1d7 100644 --- a/zypp-plugin/Makefile.am +++ b/zypp-plugin/Makefile.am @@ -9,13 +9,23 @@ AM_CPPFLAGS = $(DBUS_CFLAGS) $(XML2_CFLAGS) $(JSONC_CFLAGS) AM_CXXFLAGS = -Wsuggest-override snapper_zypp_plugin_SOURCES = \ snapper_zypp_plugin.cc \ + solvable_matcher.cc solvable_matcher.h \ zypp_commit_plugin.cc zypp_commit_plugin.h \ zypp_plugin.cc zypp_plugin.h -snapper_zypp_plugin_LDFLAGS = \ +snapper_zypp_plugin_LDADD = \ ../client/libclient.la \ ../snapper/libsnapper.la \ ../dbus/libdbus.la \ $(JSONC_LIBS) \ -lboost_regex +check_PROGRAMS = regex.test +TESTS = $(check_PROGRAMS) +regex_test_SOURCES = regex_test.cc \ + solvable_matcher.cc solvable_matcher.h +regex_test_LDADD = \ + ../snapper/libsnapper.la \ + -lboost_regex \ + -lboost_unit_test_framework + endif diff --git a/zypp-plugin/regex_test.cc b/zypp-plugin/regex_test.cc new file mode 100644 index 00000000..90780d0b --- /dev/null +++ b/zypp-plugin/regex_test.cc @@ -0,0 +1,17 @@ +#define BOOST_TEST_DYN_LINK +#define BOOST_TEST_MODULE regex + +#include +#include "solvable_matcher.h" + +static +SolvableMatcher matcher(const char* regex) { + bool is_important = false; + return SolvableMatcher(regex, SolvableMatcher::Kind::REGEX, is_important); +} + +BOOST_AUTO_TEST_CASE(regex) +{ + BOOST_CHECK(matcher("mypkg").match("mypkg")); + BOOST_CHECK(!matcher("mypkg").match("yourpkg")); +} diff --git a/zypp-plugin/snapper_zypp_plugin.cc b/zypp-plugin/snapper_zypp_plugin.cc index 9f27a901..41f367d7 100644 --- a/zypp-plugin/snapper_zypp_plugin.cc +++ b/zypp-plugin/snapper_zypp_plugin.cc @@ -3,9 +3,8 @@ // getppid #include #include -// fnmatch -#include +// FIXME for userdata #include // split #include @@ -27,9 +26,9 @@ using snapper::Exception; using snapper::CodeLocation; #include "client/commands.h" #include "client/errors.h" -#include "snapper/XmlFile.h" #include "zypp_commit_plugin.h" +#include "solvable_matcher.h" ostream& operator <<(ostream& os, set ss) { bool seen_first = false; @@ -83,72 +82,6 @@ public: class SnapperZyppPlugin : public ZyppCommitPlugin { public: - struct SolvableMatcher { - enum class Kind { - GLOB, - REGEX - }; - string pattern; - Kind kind; - bool important; - - SolvableMatcher(const string& apattern, Kind akind, bool aimportant) - : pattern(apattern) - , kind(akind) - , important(aimportant) - {} - - bool match(const string& solvable) { - cerr << "DEBUG:" << "match? " << solvable << " by " << ((kind == Kind::GLOB)? "GLOB '": "REGEX '") << pattern << '\'' << endl; - bool res; - if (kind == Kind::GLOB) { - static const int flags = 0; - res = fnmatch(pattern.c_str(), solvable.c_str(), flags) == 0; - } - else { - // POSIX Extended Regular Expression Syntax - boost::regex rx_pattern(pattern, boost::regex::extended); - res = boost::regex_match(solvable, rx_pattern); - } - cerr << "DEBUG:" << "-> " << res << endl; - return res; - } - - static vector load_config(const string& cfg_filename) { - vector result; - - cerr << "DEBUG:" << "parsing " << cfg_filename << endl; - XmlFile config(cfg_filename); - // FIXME test parse errors - const xmlNode* root = config.getRootElement(); - const xmlNode* solvables_n = getChildNode(root, "solvables"); - const list solvables_l = getChildNodes(solvables_n, "solvable"); - for (auto node: solvables_l) { - string pattern; - Kind kind; - bool important = false; - - getAttributeValue(node, "important", important); - string kind_s; - getAttributeValue(node, "match", kind_s); - getValue(node, pattern); - if (kind_s == "w") { // w for wildcard - kind = Kind::GLOB; - } - else if (kind_s == "re") { // Regular Expression - kind = Kind::REGEX; - } - else { - cerr << "ERROR:" << "Unknown match attribute '" << kind_s << "', disregarding pattern '"<< pattern << "'" << endl; - continue; - } - - result.emplace_back(SolvableMatcher(pattern, kind, important)); - } - return result; - } - }; - SnapperZyppPlugin(const ProgramOptions& opts) : snapper_cfg(opts.snapper_config) , dbus_conn(opts.bus) diff --git a/zypp-plugin/solvable_matcher.cc b/zypp-plugin/solvable_matcher.cc new file mode 100644 index 00000000..9c486f43 --- /dev/null +++ b/zypp-plugin/solvable_matcher.cc @@ -0,0 +1,67 @@ +#include "solvable_matcher.h" + +#include +#include +#include +using namespace std; + +// fnmatch +#include +#include +#include "snapper/XmlFile.h" +using namespace snapper; + +std::ostream& SolvableMatcher::log = std::cerr; + +bool SolvableMatcher::match(const string& solvable) const { + log << "DEBUG:" + << "match? " << solvable + << " by " << ((kind == Kind::GLOB)? "GLOB '": "REGEX '") + << pattern << '\'' << endl; + bool res; + if (kind == Kind::GLOB) { + static const int flags = 0; + res = fnmatch(pattern.c_str(), solvable.c_str(), flags) == 0; + } + else { + // POSIX Extended Regular Expression Syntax + boost::regex rx_pattern(pattern, boost::regex::extended); + res = boost::regex_match(solvable, rx_pattern); + } + log << "DEBUG:" << "-> " << res << endl; + return res; +} + +vector SolvableMatcher::load_config(const string& cfg_filename) { + vector result; + + log << "DEBUG:" << "parsing " << cfg_filename << endl; + XmlFile config(cfg_filename); + const xmlNode* root = config.getRootElement(); + const xmlNode* solvables_n = getChildNode(root, "solvables"); + const list solvables_l = getChildNodes(solvables_n, "solvable"); + for (auto node: solvables_l) { + string pattern; + Kind kind; + bool important = false; + + getAttributeValue(node, "important", important); + string kind_s; + getAttributeValue(node, "match", kind_s); + getValue(node, pattern); + if (kind_s == "w") { // w for wildcard + kind = Kind::GLOB; + } + else if (kind_s == "re") { // Regular Expression + kind = Kind::REGEX; + } + else { + log << "ERROR:" << "Unknown match attribute '" << kind_s << "', disregarding pattern '"<< pattern << "'" << endl; + continue; + } + + result.emplace_back(SolvableMatcher(pattern, kind, important)); + } + return result; +} + diff --git a/zypp-plugin/solvable_matcher.h b/zypp-plugin/solvable_matcher.h new file mode 100644 index 00000000..4601c079 --- /dev/null +++ b/zypp-plugin/solvable_matcher.h @@ -0,0 +1,23 @@ +#include +#include +#include + +struct SolvableMatcher { + enum class Kind { + GLOB, + REGEX + }; + std::string pattern; + Kind kind; + bool important; + + static std::ostream& log; + SolvableMatcher(const std::string& apattern, Kind akind, bool aimportant) + : pattern(apattern) + , kind(akind) + , important(aimportant) + {} + + bool match(const std::string& solvable) const; + static std::vector load_config(const std::string& cfg_filename); +};