Commits

Anonymous committed 23ec75f

[svn r9906] 0.11.7.1: backported r9722 (`resource_exists` for changeset and source)

Comments (0)

Files changed (2)

trac/versioncontrol/api.py

                     version = '@%s' % resource.version
             return '%s %s%s' % (kind, resource.id, version)
 
+    def get_resource_url(self, resource, href, **kwargs):
+        if resource.realm == 'changeset':
+            return href.changeset(resource.id)
+        elif resource.realm == 'source':
+            return href.source(resource.id)
+
+    def resource_exists(self, resource):
+        repos = self.env.get_repository() # no perm.username!
+        if not repos:
+            return False
+        if resource.realm == 'changeset':
+            try:
+                repos.get_changeset(resource.id)
+                return True
+            except NoSuchChangeset:
+                return False
+        elif resource.realm == 'source':
+            try:
+                repos.get_node(resource.id, resource.version)
+                return True
+            except NoSuchNode:
+                return False
+
     # Public API methods
 
     def get_repository(self, authname):

trac/versioncontrol/tests/svn_fs.py

     has_svn = False
 
 from trac.log import logger_factory
-from trac.test import TestSetup
+from trac.test import EnvironmentStub, TestSetup
 from trac.core import TracError
+from trac.resource import Resource, resource_exists
 from trac.util.datefmt import utc
 from trac.versioncontrol import Changeset, Node, NoSuchChangeset
 from trac.versioncontrol.svn_fs import SubversionRepository
 from trac.versioncontrol import svn_fs
 
 REPOS_PATH = os.path.join(tempfile.gettempdir(), 'trac-svnrepos')
+REPOS_NAME = 'repo'
 
 HEAD = 21
 
 class SubversionRepositoryTestCase(unittest.TestCase):
 
     def setUp(self):
-        self.repos = SubversionRepository(REPOS_PATH, None,
-                                          logger_factory('test'))
+        self.env = EnvironmentStub()
+        self.env.config.set('trac', 'repository_type', 'direct-svnfs')
+        self.env.config.set('trac', 'repository_dir', REPOS_PATH)
+        self.repos = self.env.get_repository()
 
     def tearDown(self):
-        self.repos = None
+        self.repos.close()
+        self.env.shutdown()
 
+    def test_resource_exists(self):
+        node = Resource('source', u'tête')
+        self.assertEqual(True, resource_exists(self.env, node))
+        self.assertEqual(False, resource_exists(self.env, node(id='xxx')))
+        cset = Resource('changeset', HEAD)
+        self.assertEqual(True, resource_exists(self.env, cset))
+        self.assertEqual(False, resource_exists(self.env, cset(id=123456)))
+                         
     def test_repos_normalize_path(self):
         self.assertEqual('/', self.repos.normalize_path('/'))
         self.assertEqual('/', self.repos.normalize_path(''))