test news item filtering
authorAlec Warner <antarus@gentoo.org>
Fri, 7 Sep 2007 10:08:18 +0000 (10:08 -0000)
committerAlec Warner <antarus@gentoo.org>
Fri, 7 Sep 2007 10:08:18 +0000 (10:08 -0000)
svn path=/main/trunk/; revision=7751

pym/portage/tests/news/__test__ [new file with mode: 0644]
pym/portage/tests/news/test_NewsItem.py

diff --git a/pym/portage/tests/news/__test__ b/pym/portage/tests/news/__test__
new file mode 100644 (file)
index 0000000..e69de29
index 1fa357fb878ce1d571cbbf421ca81811dd109abd..fef66b022b068ed352cd00d928f9d308e31bf66e 100644 (file)
@@ -3,13 +3,17 @@
 # Distributed under the terms of the GNU General Public License v2
 # $Id: test_varExpand.py 5596 2007-01-12 08:08:53Z antarus $
 
-from portage.tests import TestCase, TestLoader
+import os
+from portage.tests import TestCase
 from portage.news import NewsItem
 from portage.const import PROFILE_PATH
+from portage.dbapi.virtual import testdbapi
+from tempfile import mkstemp
+# TODO(antarus) Make newsitem use a loader so we can load using a string instead of a tempfile
 
 class NewsItemTestCase(TestCase):
-       
-       self.fakeItem = """
+       """These tests suck: they use your running config instead of making their own"""
+       fakeItem = """
 Title: YourSQL Upgrades from 4.0 to 4.1
 Author: Ciaran McCreesh <ciaranm@gentoo.org>
 Content-Type: text/plain
@@ -39,45 +43,55 @@ against YourSQL:
 
 The revdep-rebuild tool is provided by app-portage/gentoolkit.
 """
+       def setUp(self):
+               self.profile = "/usr/portage/profiles/default-linux/x86/2007.0/"
+               self.keywords = "x86"
+               # Use fake/test dbapi to avoid slow tests
+               self.vardb = testdbapi()
+               # self.vardb.inject_cpv('sys-apps/portage-2.0', { 'SLOT' : 0 })
+               # Consumers only use ARCH, so avoid portage.settings by using a dict
+               self.settings = { 'ARCH' : 'x86' }
+
+       def testDisplayIfProfile(self):
+               tmpItem = self.fakeItem[:].replace("#Display-If-Profile:", "Display-If-Profile: %s" %
+                       self.profile)
 
-       from portage import settings
-       import time
-
-       def testDisplayIfProfile():
-               from portage.const import PROFILE_PATH
-               tmpItem = self.fakeItem.replace("#Display-If-Profile:", "Display-If-Profile: %s" %
-                       os.readlink( PROFILE_PATH ) )
-
-               item = _processItem(tmpItem)
-               self.assertTrue( item.isRelevant( os.readlink( PROFILE_PATH ) ),
-                       msg="Expected %s to be relevant, but it was not!" % tmpItem )
-
-       def testDisplayIfInstalled():
-               tmpItem = self.fakeItem.replace("#Display-If-Installed:", "Display-If-Profile: %s" %
-                       "sys-apps/portage" )
-
-               item = _processItem(tmpItem)
-               self.assertTrue( item.isRelevant( portage.settings ),
-                       msg="Expected %s to be relevant, but it was not!" % tmpItem )
-
+               try:
+                       item = self._processItem(tmpItem)
+                       self.assertTrue(item.isRelevant(self.vardb, self.settings, self.profile),
+                               msg="Expected %s to be relevant, but it was not!" % tmpItem)
+               finally:
+                       os.unlink(item.path)
 
-       def testDisplayIfKeyword():
-               from portage import settings
-               tmpItem = self.fakeItem.replace("#Display-If-Keyword:", "Display-If-Keyword: %s" %
-                       settings["ACCEPT_KEYWORDS"].split()[0] )
+       def testDisplayIfInstalled(self):
+               tmpItem = self.fakeItem[:].replace("#Display-If-Installed:", "Display-If-Installed: %s" %
+                       "sys-apps/portage")
 
-               item = _processItem(tmpItem)
-               self.assertTrue( item.isRelevant( os.readlink( PROFILE_PATH ) ),
-                       msg="Expected %s to be relevant, but it was not!" % tmpItem )
-               
+               try:
+                       item = self._processItem(tmpItem)
+                       self.assertTrue(item.isRelevant(self.vardb, self.settings, self.profile),
+                               msg="Expected %s to be relevant, but it was not!" % tmpItem)
+               finally:
+                       os.unlink(item.path)
 
-       def _processItem( self, item ):
+       def testDisplayIfKeyword(self):
+               tmpItem = self.fakeItem[:].replace("#Display-If-Keyword:", "Display-If-Keyword: %s" %
+                       self.keywords)
 
-               path = os.path.join(settings["PORTAGE_TMPDIR"], str(time.time()))
-               f = open(path)
+               try:
+                       item = self._processItem(tmpItem)
+                       self.assertTrue(item.isRelevant(self.vardb, self.settings, self.profile),
+                               msg="Expected %s to be relevant, but it was not!" % tmpItem)
+               finally:
+                       os.unlink(item.path)
+
+       def _processItem(self, item):
+               filename = None
+               fd, filename = mkstemp()
+               f = os.fdopen(fd, 'wb')
                f.write(item)
                f.close
                try:
-                       return NewsItem( path, 0 )
+                       return NewsItem(filename, 0)
                except TypeError:
-                       self.fail("Error while processing news item %s" % path )
+                       self.fail("Error while processing news item %s" % filename)