]> git.ipfire.org Git - thirdparty/snapper.git/commitdiff
Split off regex matching, add a test for it
authorMartin Vidner <mvidner@suse.cz>
Fri, 13 Dec 2019 12:16:33 +0000 (13:16 +0100)
committerMartin Vidner <mvidner@suse.cz>
Tue, 17 Dec 2019 10:22:26 +0000 (11:22 +0100)
zypp-plugin/.gitignore
zypp-plugin/Makefile.am
zypp-plugin/regex_test.cc [new file with mode: 0644]
zypp-plugin/snapper_zypp_plugin.cc
zypp-plugin/solvable_matcher.cc [new file with mode: 0644]
zypp-plugin/solvable_matcher.h [new file with mode: 0644]

index f4c8bb746205e95a18f569e53bd8e5a7afa1bf77..ff41d306eb0c76df90987ee1a8381ad6af8a8f32 100644 (file)
@@ -1,2 +1,6 @@
 *.o
 snapper-zypp-plugin
+*.test
+*.log
+*.trs
+test-suite.log
index 7ce5339cc545a73ae9a0e6138d24a7a3140fa585..4e6ef1d797cac8d3708e3b0c0e0503cee2fbb501 100644 (file)
@@ -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 (file)
index 0000000..90780d0
--- /dev/null
@@ -0,0 +1,17 @@
+#define BOOST_TEST_DYN_LINK
+#define BOOST_TEST_MODULE regex
+
+#include <boost/test/unit_test.hpp>
+#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"));
+}
index 9f27a90113a0f92756aefda7c73d13e9f406168c..41f367d738a1618e1aff802278741c4f78577ecd 100644 (file)
@@ -3,9 +3,8 @@
 // getppid
 #include <sys/types.h>
 #include <unistd.h>
-// fnmatch
-#include <fnmatch.h>
 
+// FIXME for userdata
 #include <boost/regex.hpp>
 // split
 #include <boost/algorithm/string.hpp>
@@ -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<string> 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<SolvableMatcher> load_config(const string& cfg_filename) {
-           vector<SolvableMatcher> 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<const xmlNode*> 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 (file)
index 0000000..9c486f4
--- /dev/null
@@ -0,0 +1,67 @@
+#include "solvable_matcher.h"
+
+#include <iostream>
+#include <vector>
+#include <string>
+using namespace std;
+
+// fnmatch
+#include <fnmatch.h>
+#include <boost/regex.hpp>
+#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> SolvableMatcher::load_config(const string& cfg_filename) {
+    vector<SolvableMatcher> 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<const xmlNode*> 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 (file)
index 0000000..4601c07
--- /dev/null
@@ -0,0 +1,23 @@
+#include <iostream>
+#include <vector>
+#include <string>
+
+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<SolvableMatcher> load_config(const std::string& cfg_filename);
+};