jenkins-bot has submitted this change and it was merged.

Change subject: Fix newpages default namespace
......................................................................


Fix newpages default namespace

The ability to set namespaces for newitem.py, yet default to namespace 0
only was added for bug 67249, however this was inadvertantly removed in
60aceea.

Bug: 72176
Change-Id: I729e84e83c6a4d5e4e69e877cbea9780176d1f27
---
M pywikibot/pagegenerators.py
M tests/aspects.py
M tests/pagegenerators_tests.py
3 files changed, 104 insertions(+), 1 deletion(-)

Approvals:
  John Vandenberg: Looks good to me, but someone else must approve
  XZise: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/pywikibot/pagegenerators.py b/pywikibot/pagegenerators.py
index b79dfda..128f6c4 100644
--- a/pywikibot/pagegenerators.py
+++ b/pywikibot/pagegenerators.py
@@ -516,10 +516,11 @@
             # partial workaround for bug 67249
             # to use -namespace/ns with -newpages, -ns must be given before 
-newpages
             # otherwise default namespace is 0
+            namespaces = self.namespaces or 0
             total = 60
             if len(arg) >= 10:
                 total = int(arg[10:])
-            gen = NewpagesPageGenerator(namespaces=self.namespaces,
+            gen = NewpagesPageGenerator(namespaces=namespaces,
                                         total=total,
                                         site=self.site)
         elif arg.startswith('-imagesused'):
diff --git a/tests/aspects.py b/tests/aspects.py
index 40edb6d..e1b2900 100644
--- a/tests/aspects.py
+++ b/tests/aspects.py
@@ -67,6 +67,62 @@
             """
             return self.assertRegexpMatches(*args, **kwargs)
 
+    def assertPageInNamespaces(self, page, namespaces):
+        """
+        Assert that Pages is in namespaces.
+
+        @param page: Page
+        @type page: Page
+        @param namespaces: expected namespaces
+        @type namespaces: int or set of int
+        """
+        if isinstance(namespaces, int):
+            namespaces = set([namespaces])
+
+        self.assertIn(page.namespace(), namespaces,
+                      "%s not in namespace %r" % (page, namespaces))
+
+    def assertPagesInNamespaces(self, gen, namespaces):
+        """
+        Assert that generator returns Pages all in namespaces.
+
+        @param gen: generator to iterate
+        @type gen: generator
+        @param namespaces: expected namespaces
+        @type namespaces: int or set of int
+        """
+        if isinstance(namespaces, int):
+            namespaces = set([namespaces])
+
+        for page in gen:
+            self.assertPageInNamespaces(page, namespaces)
+
+    def assertPagesInNamespacesAll(self, gen, namespaces, skip=False):
+        """
+        Try to confirm that generator returns Pages for all namespaces.
+
+        @param gen: generator to iterate
+        @type gen: generator
+        @param namespaces: expected namespaces
+        @type namespaces: int or set of int
+        @param count: maximum results to process
+        @type count: int
+        @param skip: skip test if not all namespaces found
+        @param skip: bool
+        """
+        if isinstance(namespaces, int):
+            namespaces = set([namespaces])
+        else:
+            assert(isinstance(namespaces, set))
+
+        page_namespaces = [page.namespace() for page in gen]
+
+        if skip and set(page_namespaces) != namespaces:
+            raise unittest.SkipTest('Pages in namespaces %r not found.'
+                                    % list(namespaces - set(page_namespaces)))
+        else:
+            self.assertEqual(set(page_namespaces), namespaces)
+
 
 class TestLoggingMixin(TestCaseBase):
 
diff --git a/tests/pagegenerators_tests.py b/tests/pagegenerators_tests.py
index 96e5dfa..da4c390 100755
--- a/tests/pagegenerators_tests.py
+++ b/tests/pagegenerators_tests.py
@@ -196,6 +196,52 @@
         self.assertTrue(all(isinstance(item, pywikibot.ItemPage) for item in 
gen))
 
 
+class TestFactoryGenerator(DefaultSiteTestCase):
+
+    """Test pagegenerators.GeneratorFactory."""
+
+    def test_newpages_default(self):
+        gf = pagegenerators.GeneratorFactory(site=self.site)
+        gf.handleArg('-newpages')
+        gen = gf.getCombinedGenerator()
+        pages = set(gen)
+        self.assertEqual(len(pages), 60)
+
+    def test_newpages_ns_default(self):
+        gf = pagegenerators.GeneratorFactory(site=self.site)
+        gf.handleArg('-newpages:10')
+        gen = gf.getCombinedGenerator()
+        self.assertPagesInNamespaces(gen, 0)
+
+    def test_newpages_ns(self):
+        gf = pagegenerators.GeneratorFactory(site=self.site)
+        gf.handleArg('-ns:1')
+        gf.handleArg('-newpages:10')
+        gen = gf.getCombinedGenerator()
+        self.assertPagesInNamespaces(gen, 1)
+
+    def test_recentchanges_ns_default(self):
+        gf = pagegenerators.GeneratorFactory(site=self.site)
+        gf.handleArg('-recentchanges:50')
+        gen = gf.getCombinedGenerator()
+        self.assertPagesInNamespacesAll(gen, set([0, 1, 2]), skip=True)
+
+    def test_recentchanges_ns(self):
+        gf = pagegenerators.GeneratorFactory(site=self.site)
+        gf.handleArg('-ns:1')
+        gf.handleArg('-recentchanges:10')
+        gen = gf.getCombinedGenerator()
+        self.assertPagesInNamespaces(gen, 1)
+
+    def test_recentchanges_ns_multi(self):
+        gf = pagegenerators.GeneratorFactory(site=self.site)
+        gf.handleArg('-ns:1')
+        gf.handleArg('-ns:3')
+        gf.handleArg('-recentchanges:10')
+        gen = gf.getCombinedGenerator()
+        self.assertPagesInNamespaces(gen, set([1, 3]))
+
+
 if __name__ == "__main__":
     try:
         unittest.main()

-- 
To view, visit https://gerrit.wikimedia.org/r/167796
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I729e84e83c6a4d5e4e69e877cbea9780176d1f27
Gerrit-PatchSet: 4
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: John Vandenberg <[email protected]>
Gerrit-Reviewer: John Vandenberg <[email protected]>
Gerrit-Reviewer: Ladsgroup <[email protected]>
Gerrit-Reviewer: Merlijn van Deen <[email protected]>
Gerrit-Reviewer: Mpaa <[email protected]>
Gerrit-Reviewer: XZise <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to