Merge lp:~ralsina/ubuntuone-control-panel/fix-959447 into lp:ubuntuone-control-panel

Proposed by Roberto Alsina on 2012-05-14
Status: Merged
Approved by: Roberto Alsina on 2012-05-14
Approved revision: 323
Merged at revision: 320
Proposed branch: lp:~ralsina/ubuntuone-control-panel/fix-959447
Merge into: lp:ubuntuone-control-panel
Diff against target: 92 lines (+39/-3)
2 files modified
ubuntuone/controlpanel/gui/qt/folders.py (+10/-1)
ubuntuone/controlpanel/gui/qt/tests/test_folders.py (+29/-2)
To merge this branch: bzr merge lp:~ralsina/ubuntuone-control-panel/fix-959447
Reviewer Review Type Date Requested Status
Brian Curtin (community) Approve on 2012-05-14
Diego Sarmentero (community) 2012-05-14 Approve on 2012-05-14
Review via email: mp+105704@code.launchpad.net

Commit Message

 - Made CalculateSize use bytes when calling os.walk (Fixes LP:959447).

Description of the Change

Make CalculateSize use bytes for its path instead of unicode, so os.walk doesn't choke on invalid unicode paths.

To post a comment you must log in.
Diego Sarmentero (diegosarmentero) wrote :

[FAIL]
Traceback (most recent call last):
  File "X:\ubuntuone-control-panel-ralsina2\ubuntuone\controlpanel\gui\qt\tests\
test_folders.py", line 1236, in test_add_music_folder_adds_the_folder
    self.test_folder_is_added(folder_path=MUSIC_PATH)
  File "X:\ubuntuone-control-panel-ralsina2\ubuntuone\controlpanel\gui\qt\tests\
test_folders.py", line 1232, in test_folder_is_added
    self.assert_folder_added(folder_path)
  File "X:\ubuntuone-control-panel-ralsina2\ubuntuone\controlpanel\gui\qt\tests\
test_folders.py", line 1005, in assert_folder_added
    unicode(item.text(gui.LOCAL_SUBSCRIPTION_COL)))
twisted.trial.unittest.FailTest: not equal:
a = u'Purchased Music'
b = u'.ubuntuone\\Purchased from Ubuntu One'

ubuntuone.controlpanel.gui.qt.tests.test_folders.LocalFoldersPanelAddFolderTestC
ase.test_add_music_folder_adds_the_folder

review: Needs Fixing
Diego Sarmentero (diegosarmentero) wrote :

+1

review: Approve
Brian Curtin (brian.curtin) wrote :

Looks good.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'ubuntuone/controlpanel/gui/qt/folders.py'
2--- ubuntuone/controlpanel/gui/qt/folders.py 2012-04-05 21:51:49 +0000
3+++ ubuntuone/controlpanel/gui/qt/folders.py 2012-05-14 18:57:22 +0000
4@@ -20,6 +20,7 @@
5
6 import os
7 import Queue
8+import sys
9 import threading
10
11 from PyQt4 import QtGui, QtCore
12@@ -347,9 +348,17 @@
13 """A thread that calculates, in the background, the size of a folder."""
14
15 def __init__(self, path_name, queue):
16- self.path_name = path_name
17 self.queue = queue
18 self._stop = False
19+ # This makes os.walk use the "bytes" version, which doesn't
20+ # break with invalid unicode paths.
21+ # This will only work for unicode locales (ex: LANG=es_ES.UTF-8)
22+ # and will falback to as before for C and non-unicode locales.
23+ try:
24+ self.path_name = path_name.encode(sys.getfilesystemencoding())
25+ except (UnicodeEncodeError, UnicodeDecodeError):
26+ # Should never happen (haha)
27+ self.path_name = path_name
28
29 super(CalculateSize, self).__init__()
30
31
32=== modified file 'ubuntuone/controlpanel/gui/qt/tests/test_folders.py'
33--- ubuntuone/controlpanel/gui/qt/tests/test_folders.py 2012-04-11 11:48:07 +0000
34+++ ubuntuone/controlpanel/gui/qt/tests/test_folders.py 2012-05-14 18:57:22 +0000
35@@ -22,6 +22,7 @@
36 import os
37 import Queue
38 import shutil
39+import sys
40
41 from PyQt4 import QtGui
42 from twisted.internet import defer
43@@ -726,7 +727,7 @@
44
45 @defer.inlineCallbacks
46 def setUp(self):
47- self.path = 'not-existing-dir'
48+ self.path = u'not-existing-dir'
49 self.expected_size = self.build_test_dir(self.path)
50 self.queue = Queue.Queue()
51 self.kwargs['queue'] = self.queue
52@@ -742,7 +743,8 @@
53 assert not os.path.exists(dir_path)
54
55 os.makedirs(dir_path)
56- self.addCleanup(shutil.rmtree, dir_path)
57+ self.addCleanup(shutil.rmtree,
58+ dir_path.encode(sys.getfilesystemencoding()))
59
60 total_size = 0
61
62@@ -823,6 +825,31 @@
63 return total_size
64
65
66+class CalculateSizeWithInvalidPath(CalculateSizeTestCase):
67+ """Test suite for the CalculateSize thread implementation."""
68+
69+ def build_test_dir(self, dir_path):
70+ """Build a testing directory hierarchy."""
71+ total_size = super(CalculateSizeWithInvalidPath,
72+ self).build_test_dir(dir_path)
73+ # Have to crete as bytes because it's an invalid path
74+ a_file = os.path.join(dir_path.encode(sys.getfilesystemencoding()),
75+ '\xe7\xa7')
76+ with open(a_file, 'wb') as f:
77+ f.write('y' * 5000)
78+ total_size += os.path.getsize(a_file)
79+ return total_size
80+
81+ def test_run(self):
82+ """The run() method calculates the size for the given path."""
83+ self.ui.run()
84+
85+ path, size = self.queue.get(block=True, timeout=0.5)
86+
87+ self.assertEqual(path, self.path)
88+ self.assertEqual(size, self.expected_size)
89+
90+
91 class FolderItemTestCase(BaseLocalFoldersTestCase):
92 """Test suite for the FolderItem widget."""
93

Subscribers

People subscribed via source and target branches