From e81494781ecde0bb8907080e8f7b0c77c76ad76b Mon Sep 17 00:00:00 2001 From: Pierre-Yves Chibon Date: Feb 20 2017 09:05:15 +0000 Subject: Handle the situation where file_content is not set There is at least one situation where file_content was not being set. This was reported to us by email and we're fixing it here. Add unit-tests for this change --- diff --git a/pagure/ui/repo.py b/pagure/ui/repo.py index 44ef6d8..c51e195 100644 --- a/pagure/ui/repo.py +++ b/pagure/ui/repo.py @@ -535,6 +535,7 @@ def view_file(repo, identifier, filename, username=None, namespace=None): content, safe = pagure.doc_utils.convert_readme(content.data, ext) output_type = 'markup' elif not is_binary_string(content.data): + file_content = None try: file_content = encoding_utils.decode(ktc.to_bytes(content.data)) except pagure.exceptions.PagureException: @@ -542,28 +543,29 @@ def view_file(repo, identifier, filename, username=None, namespace=None): # file and let the user download it instead of displaying # it. output_type = 'binary' - try: - lexer = guess_lexer_for_filename( - filename, - file_content + if file_content: + try: + lexer = guess_lexer_for_filename( + filename, + file_content + ) + except (ClassNotFound, TypeError): + lexer = TextLexer() + + style = "tango" + + if ext in ('.diff', '.patch'): + lexer.add_filter(VisibleWhitespaceFilter( + wstokentype=False, tabs=True)) + style = "diffstyle" + content = highlight( + file_content, + lexer, + HtmlFormatter( + noclasses=True, + style=style,) ) - except (ClassNotFound, TypeError): - lexer = TextLexer() - - style = "tango" - - if ext in ('.diff', '.patch'): - lexer.add_filter(VisibleWhitespaceFilter( - wstokentype=False, tabs=True)) - style = "diffstyle" - content = highlight( - file_content, - lexer, - HtmlFormatter( - noclasses=True, - style=style,) - ) - output_type = 'file' + output_type = 'file' else: output_type = 'binary' else: diff --git a/tests/test_pagure_flask_ui_repo.py b/tests/test_pagure_flask_ui_repo.py index bca04f5..97dc566 100644 --- a/tests/test_pagure_flask_ui_repo.py +++ b/tests/test_pagure_flask_ui_repo.py @@ -20,7 +20,7 @@ import tempfile import os import pygit2 -from mock import patch +from mock import patch, MagicMock sys.path.insert(0, os.path.join(os.path.dirname( os.path.abspath(__file__)), '..')) @@ -1894,6 +1894,28 @@ class PagureFlaskRepotests(tests.Modeltests): self.assertIn( '
 barRow 0
', output.data) + @patch( + 'pagure.lib.encoding_utils.decode', + MagicMock(side_effect=pagure.exceptions.PagureException)) + def test_view_file_with_wrong_encoding(self): + """ Test the view_file endpoint. """ + + tests.create_projects(self.session) + tests.create_projects_git(self.path, bare=True) + + # Add some content to the git repo + tests.add_content_git_repo(os.path.join(self.path, 'test.git')) + tests.add_readme_git_repo(os.path.join(self.path, 'test.git')) + tests.add_binary_git_repo( + os.path.join(self.path, 'test.git'), 'test.jpg') + tests.add_binary_git_repo( + os.path.join(self.path, 'test.git'), 'test_binary') + + # View file + output = self.app.get('/test/blob/master/f/sources') + self.assertEqual(output.status_code, 200) + self.assertIn('Binary files cannot be rendered.
', output.data) + def test_view_raw_file(self): """ Test the view_raw_file endpoint. """ output = self.app.get('/foo/raw/foo/sources')