- Allow for Repo Index Services to set the enabled state of
authorDuncan Mac-Vicar P <dmacvicar@suse.de>
Tue, 18 Jan 2011 12:30:38 +0000 (13:30 +0100)
committerDuncan Mac-Vicar P <dmacvicar@suse.de>
Tue, 18 Jan 2011 12:31:39 +0000 (13:31 +0100)
  repositories from the server side.

- Fix priority not being set on reading service indexes

tests/parser/CMakeLists.txt
tests/parser/RepoindexFileReader_test.cc [new file with mode: 0644]
zypp/parser/RepoindexFileReader.cc
zypp/parser/RepoindexFileReader.h

index 3c36965..92a1a8a 100644 (file)
@@ -4,4 +4,5 @@ ADD_SUBDIRECTORY(ws)
 
 ADD_TESTS( ProductFileReader )
 ADD_TESTS( RepoFileReader )
+ADD_TESTS( RepoindexFileReader )
 
diff --git a/tests/parser/RepoindexFileReader_test.cc b/tests/parser/RepoindexFileReader_test.cc
new file mode 100644 (file)
index 0000000..f89ce73
--- /dev/null
@@ -0,0 +1,63 @@
+#include <sstream>
+#include <string>
+#include <zypp/Pathname.h>
+#include <zypp/parser/RepoindexFileReader.h>
+#include <zypp/base/NonCopyable.h>
+
+#include "TestSetup.h"
+
+using std::stringstream;
+using std::string;
+using namespace zypp;
+
+static string service = "<repoindex>"
+  "<repo alias=\"company-foo\" name=\"Company's Foo\""
+  "      path=\"products/foo\" distro_target=\"sle-11-i386\" priority=\"20\"/>"
+  "<repo alias=\"company-bar\" name=\"Company's Bar\""
+  "      path=\"products/bar\" distro_target=\"sle-11-i386\" enabled=\"true\"/>"
+  "<repo alias=\"company-foo-upd\" name=\"Company's Foo Updates\""
+  "      path=\"products/foo/updates\" distro_target=\"sle-11-i386\" priority=\"1\"/>"
+  "</repoindex>";
+
+struct RepoCollector : private base::NonCopyable
+{
+  bool collect( const RepoInfo &repo )
+  {
+    repos.push_back(repo);
+    return true;
+  }
+
+  RepoInfoList repos;
+};
+
+// Must be the first test!
+BOOST_AUTO_TEST_CASE(read_index_file)
+{
+  {
+    stringstream input(service);
+    RepoCollector collector;
+    parser::RepoindexFileReader parser( input, bind( &RepoCollector::collect, &collector, _1 ) );
+    BOOST_REQUIRE_EQUAL(3, collector.repos.size());
+
+    RepoInfo repo;
+    repo = collector.repos.front();
+
+    BOOST_CHECK_EQUAL("Company's Foo", repo.name());
+    BOOST_CHECK_EQUAL("company-foo", repo.alias());
+    BOOST_CHECK_EQUAL("sle-11-i386", repo.targetDistribution());
+    BOOST_CHECK_EQUAL(20, repo.priority());
+    BOOST_CHECK_EQUAL("/repo/products/foo", repo.path());
+
+    collector.repos.pop_front( );
+    repo = collector.repos.front();
+
+    BOOST_CHECK_EQUAL("company-bar", repo.alias());
+    BOOST_CHECK_EQUAL("sle-11-i386", repo.targetDistribution());
+    // "Priority should be 99 when not explictly defined"
+    BOOST_CHECK_EQUAL(99, repo.priority());
+    // "Repository is explicitly enabled"
+    BOOST_CHECK(repo.enabled());
+
+
+  }
+}
index 6311bc4..191c67b 100644 (file)
@@ -14,6 +14,8 @@
 #include "zypp/base/String.h"
 #include "zypp/base/Logger.h"
 #include "zypp/base/Gettext.h"
+#include "zypp/base/InputStream.h"
+
 #include "zypp/Pathname.h"
 
 #include "zypp/parser/xml/Reader.h"
@@ -48,7 +50,7 @@ namespace zypp
      *
      * \see RepoindexFileReader::RepoindexFileReader(Pathname,ProcessResource)
      */
-    Impl(const Pathname &repoindex_file, const ProcessResource & callback);
+    Impl(const InputStream &is, const ProcessResource & callback);
 
     /**
      * Callback provided to the XML parser.
@@ -63,12 +65,12 @@ namespace zypp
   };
   ///////////////////////////////////////////////////////////////////////
 
-  RepoindexFileReader::Impl::Impl(
-      const Pathname &repoindex_file, const ProcessResource & callback)
+  RepoindexFileReader::Impl::Impl(const InputStream &is,
+                                  const ProcessResource & callback)
     : _callback(callback)
   {
-    Reader reader( repoindex_file );
-    MIL << "Reading " << repoindex_file << endl;
+    Reader reader( is );
+    MIL << "Reading " << is.path() << endl;
     reader.foreachNode( bind( &RepoindexFileReader::Impl::consumeNode, this, _1 ) );
   }
 
@@ -102,6 +104,11 @@ namespace zypp
 
         RepoInfo info;
 
+        // enabled or disabled is controlled by the
+        // reposToEnable/Disable list, unless the
+        // enabled attribute is set
+        info.setEnabled(false);
+
         // url and/or path
         string url_s;
         s = reader_r->getAttribute("url");
@@ -143,14 +150,23 @@ namespace zypp
         if (s.get())
           info.setTargetDistribution(s.asString());
 
+        // optional priority
+        s = reader_r->getAttribute("priority");
+        if (s.get()) {
+          info.setPriority(str::strtonum<unsigned>(s.asString()));
+        }
+
+        // optional enabled
+        s = reader_r->getAttribute("enabled");
+        if (s.get()) {
+          info.setEnabled(str::strToTrue(s.asString()));
+        }
+
         DBG << info << endl;
-        
+
         // Set some defaults that are not contained in the repo information
         info.setAutorefresh( true );
-        // enabled or disabled is controlled by the
-        // reposToEnable/Disable list
-        info.setEnabled(false);        
-        
+
         // ignore the rest
         _callback(info);
         return true;
@@ -170,7 +186,12 @@ namespace zypp
   RepoindexFileReader::RepoindexFileReader(
       const Pathname & repoindex_file, const ProcessResource & callback)
     :
-      _pimpl(new Impl(repoindex_file, callback))
+      _pimpl(new Impl(InputStream(repoindex_file), callback))
+  {}
+
+  RepoindexFileReader::RepoindexFileReader(
+       const InputStream &is, const ProcessResource & callback )
+    : _pimpl(new Impl(is, callback))
   {}
 
   RepoindexFileReader::~RepoindexFileReader()
index 46f4f5c..c720f58 100644 (file)
@@ -15,6 +15,8 @@
 #include "zypp/base/PtrTypes.h"
 #include "zypp/base/NonCopyable.h"
 #include "zypp/base/Function.h"
+#include "zypp/base/InputStream.h"
+#include "zypp/Pathname.h"
 
 namespace zypp
 {
@@ -34,7 +36,7 @@ namespace zypp
    *
    *
    * \code
-   * RepoindexFileReader reader(repoindex_file, 
+   * RepoindexFileReader reader(repoindex_file,
    *                  bind( &SomeClass::callbackfunc, &SomeClassInstance, _1) );
    * \endcode
    */
@@ -52,14 +54,25 @@ namespace zypp
 
    /**
     * CTOR. Creates also \ref xml::Reader and starts reading.
-    * 
+    *
     * \param repoindexFile is the repoindex.xml file you want to read
     * \param callback is a function.
     *
     * \see RepoindexFileReader::ProcessResource
     */
-    RepoindexFileReader(
-      const Pathname & repoindexFile, const ProcessResource & callback);
+    RepoindexFileReader( const zypp::Pathname & repoindexFile,
+                         const ProcessResource & callback);
+
+    /**
+     * \short Constructor. Creates the reader and start reading.
+     *
+     * \param is a valid input stream
+     * \param callback Callback that will be called for each repository.
+     *
+     * \see RepoindexFileReader::ProcessResource
+     */
+     RepoindexFileReader( const InputStream &is,
+                          const ProcessResource & callback );
 
     /**
      * DTOR