From 7524e36b0546694eaaf748d1c45472878c2ea383 Mon Sep 17 00:00:00 2001 From: Henrik Skupin Date: Tue, 10 Apr 2018 09:31:10 +0200 Subject: [PATCH] Bug 1452864 - [mozfile] tree() broken with Unicode characters in path. r=gbrown The patch correctly marks the delimiters for the tree output as Unicode, and also updates mozprofile to correctly serialize the Profile object when str() is used. MozReview-Commit-ID: AjUHa6zGHQe --HG-- extra : rebase_source : d4fa6c5db91184dee6a2abe788aa23d0c6255be6 --- testing/mozbase/mozfile/mozfile/mozfile.py | 6 ++-- testing/mozbase/mozfile/tests/manifest.ini | 1 + testing/mozbase/mozfile/tests/test_tree.py | 33 +++++++++++++++++++ .../mozbase/mozprofile/mozprofile/profile.py | 6 +++- .../mozprofile/tests/test_profile_view.py | 14 ++++---- 5 files changed, 50 insertions(+), 10 deletions(-) create mode 100644 testing/mozbase/mozfile/tests/test_tree.py diff --git a/testing/mozbase/mozfile/mozfile/mozfile.py b/testing/mozbase/mozfile/mozfile/mozfile.py index 1b1d8780258c..4313521b8f42 100644 --- a/testing/mozbase/mozfile/mozfile/mozfile.py +++ b/testing/mozbase/mozfile/mozfile/mozfile.py @@ -261,9 +261,9 @@ ascii_delimeters = { # unicode delimiters unicode_delimeters = { - 'vertical_line': '│', - 'item_marker': '├', - 'last_child': '└' + 'vertical_line': u'│', + 'item_marker': u'├', + 'last_child': u'└' } diff --git a/testing/mozbase/mozfile/tests/manifest.ini b/testing/mozbase/mozfile/tests/manifest.ini index 088421a16956..43739b7f60e1 100644 --- a/testing/mozbase/mozfile/tests/manifest.ini +++ b/testing/mozbase/mozfile/tests/manifest.ini @@ -5,4 +5,5 @@ subsuite = mozbase, os == "linux" [test_move_remove.py] [test_tempdir.py] [test_tempfile.py] +[test_tree.py] [test_url.py] diff --git a/testing/mozbase/mozfile/tests/test_tree.py b/testing/mozbase/mozfile/tests/test_tree.py new file mode 100644 index 000000000000..d8ebee274940 --- /dev/null +++ b/testing/mozbase/mozfile/tests/test_tree.py @@ -0,0 +1,33 @@ +#!/usr/bin/env python +# coding=UTF-8 + +from __future__ import absolute_import + +import os +import shutil +import tempfile +import unittest + +import mozunit + +from mozfile import tree + + +class TestTree(unittest.TestCase): + """Test the tree function.""" + + def test_unicode_paths(self): + """Test creating tree structure from a Unicode path.""" + try: + tmpdir = tempfile.mkdtemp(suffix=u'tmp🍪') + os.mkdir(os.path.join(tmpdir, u'dir🍪')) + with open(os.path.join(tmpdir, u'file🍪'), 'w') as f: + f.write('foo') + + self.assertEqual(u'{}\n├file🍪\n└dir🍪'.format(tmpdir), tree(tmpdir)) + finally: + shutil.rmtree(tmpdir) + + +if __name__ == '__main__': + mozunit.main() diff --git a/testing/mozbase/mozprofile/mozprofile/profile.py b/testing/mozbase/mozprofile/mozprofile/profile.py index 78b08e284f09..e96239c9529e 100644 --- a/testing/mozbase/mozprofile/mozprofile/profile.py +++ b/testing/mozbase/mozprofile/mozprofile/profile.py @@ -358,7 +358,11 @@ class Profile(object): for key, value in parts])) return retval - __str__ = summary + def __str__(self): + return unicode(self).encode('utf-8') + + def __unicode__(self): + return self.summary() class FirefoxProfile(Profile): diff --git a/testing/mozbase/mozprofile/tests/test_profile_view.py b/testing/mozbase/mozprofile/tests/test_profile_view.py index cf53b6ec4b3f..cbb429f468d5 100644 --- a/testing/mozbase/mozprofile/tests/test_profile_view.py +++ b/testing/mozbase/mozprofile/tests/test_profile_view.py @@ -44,13 +44,15 @@ class TestProfilePrint(unittest.TestCase): finally: mozfile.rmtree(tempdir) - def test_strcast(self): - """ - test casting to a string - """ - + def test_str_cast(self): + """Test casting to a string.""" profile = mozprofile.Profile() - self.assertEqual(str(profile), profile.summary()) + self.assertEqual(str(profile), profile.summary().encode("UTF-8")) + + def test_unicode_cast(self): + """Test casting to a unicode string.""" + profile = mozprofile.Profile() + self.assertEqual(unicode(profile), profile.summary()) def test_profile_diff(self): profile1 = mozprofile.Profile()