From be7348f22dccff40bf2988454fe3e02df41b0bfd Mon Sep 17 00:00:00 2001 From: jomae Date: Tue, 28 May 2024 07:07:15 +0000 Subject: [PATCH] 1.6.1dev: fix `enableBlame is not defined` javascript error when viewing an empty file with blame view (closes #13753) git-svn-id: http://trac.edgewall.org/intertrac/log:/branches/1.6-stable@17814 af82e41b-90c4-0310-8c96-b1721e28e2e2 --- trac/mimeview/api.py | 2 +- trac/versioncontrol/web_ui/tests/browser.py | 36 ++++++++++++++++++--- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/trac/mimeview/api.py b/trac/mimeview/api.py index 64b62f3b8d..2bad9839cd 100644 --- a/trac/mimeview/api.py +++ b/trac/mimeview/api.py @@ -821,7 +821,7 @@ def render(self, context, mimetype, content, filename=None, url=None, renderer=renderer.__class__.__name__, err=exception_to_unicode(e))) else: - if not result: + if result is None: continue if not (force_source or getattr(renderer, 'returns_source', diff --git a/trac/versioncontrol/web_ui/tests/browser.py b/trac/versioncontrol/web_ui/tests/browser.py index 78066d8daf..d146925de2 100644 --- a/trac/versioncontrol/web_ui/tests/browser.py +++ b/trac/versioncontrol/web_ui/tests/browser.py @@ -44,7 +44,8 @@ def get_repository(self, repos_type, repos_dir, params): youngest_rev = 1 def get_changeset(rev): - return Mock(Changeset, repos, rev, 'message', 'author', t) + message = 'Rev %s' % rev + return Mock(Changeset, repos, rev, message, 'author', t) def get_node(path, rev): if 'missing' in path: @@ -53,6 +54,12 @@ def get_node(path, rev): if 'file' in basename: kind = Node.FILE entries = () + if 'empty' in basename: + content = b'' + annotations = [] + else: + content = b'Contents for %s' % to_utf8(path) + annotations = [rev] else: kind = Node.DIRECTORY if 'dir' in basename: @@ -60,7 +67,9 @@ def get_node(path, rev): else: entries = ['dir1', 'dir2'] entries = [posixpath.join(path, entry) for entry in entries] - content = b'Contents for %s' % to_utf8(path) + content = b'' + history = [(path, r, Changeset.ADD if r == rev else Changeset.EDIT) + for r in range(rev, 0, -1)] properties = {} if 'properties' in path: properties['mock-1'] = 1 @@ -72,8 +81,10 @@ def get_node(path, rev): get_properties=lambda: properties, get_content=lambda: io.BytesIO(content), get_content_length=lambda: len(content), - get_content_type=lambda: 'application/octet-stream', - get_last_modified=lambda: t) + get_content_type=lambda: 'text/plain', + get_last_modified=lambda: t, + get_annotations=lambda: annotations, + get_history=lambda: iter(history)) return node def normalize_rev(rev): @@ -281,6 +292,9 @@ def test_node_with_blame_view(self): args={'annotate': 'blame'}) rv = self.process_request(req) fragment = str(Chrome(self.env).render_fragment(req, *rv)) + self.assertIn( + {'attrs': {'src': '/trac.cgi/chrome/common/js/blame.js'}}, + req.chrome['scripts']) for line in fragment.splitlines(): if ' enableBlame(' in line: break @@ -291,6 +305,20 @@ def test_node_with_blame_view(self): r''', "-\u0026\u003c\u003e\"'-file");''', line.strip()) + def test_empty_node_with_blame_view(self): + self.grant_perm('anonymous', 'BROWSER_VIEW', 'FILE_VIEW') + + req = MockRequest(self.env, authname='anonymous', + path_info='/browser/allow/empty-file', + args={'annotate': 'blame'}) + rv = self.process_request(req) + self.assertEqual(0, rv[1]['file']['size']) + fragment = Chrome(self.env).render_fragment(req, *rv) + self.assertIn( + {'attrs': {'src': '/trac.cgi/chrome/common/js/blame.js'}}, + req.chrome['scripts']) + self.assertIn('(The file is empty)', fragment) + def test_node_in_allowed_repos_with_file_view(self): self.grant_perm('anonymous', 'BROWSER_VIEW', 'FILE_VIEW')