Merge lp:~mandel/ubuntuone-client/add-method-name into lp:ubuntuone-client

Proposed by Manuel de la Peña
Status: Merged
Approved by: Natalia Bidart
Approved revision: 1124
Merged at revision: 1124
Proposed branch: lp:~mandel/ubuntuone-client/add-method-name
Merge into: lp:ubuntuone-client
Diff against target: 177 lines (+84/-21)
2 files modified
tests/platform/windows/test_os_helper.py (+43/-5)
ubuntuone/platform/windows/os_helper.py (+41/-16)
To merge this branch: bzr merge lp:~mandel/ubuntuone-client/add-method-name
Reviewer Review Type Date Requested Status
Natalia Bidart (community) Approve
Diego Sarmentero (community) Approve
Review via email: mp+73400@code.launchpad.net

Commit message

Add extra logging information for the assert messages when used in a decorator.

Description of the change

Add extra logging information for the assert messages when used in a decorator.

To post a comment you must log in.
Revision history for this message
Natalia Bidart (nataliabidart) wrote :

* Can you please remove all the trailing spaces you added to the blank lines?

* Can you please restore all the ending dots to the messages?

review: Needs Fixing
1119. By Manuel de la Peña

Fixed spaces.

Revision history for this message
Natalia Bidart (nataliabidart) wrote :

The ending dots should not be added in _add_method_info but in the strings itself. The code style recommends adding an ending dot to every comment/message/log message, so usually a dev will write that in the same string, and we'll end up with 2 dots (is a bit unexpected to have methods adding ending dots).

review: Needs Fixing
1120. By Manuel de la Peña

Re-added the dots and changed the sed message.

Revision history for this message
Diego Sarmentero (diegosarmentero) wrote :

Instead of:

if method_name is not None:

Can not you just say?:

if not method_name:

(so you well check for empty strings too, maybe i'm wrong, but i think that is better)

TESTS??

review: Needs Information
1121. By Manuel de la Peña

Change the way in which the if statement works so that we do not accept ''

1122. By Manuel de la Peña

Added tests.

Revision history for this message
Diego Sarmentero (diegosarmentero) wrote :

+1

review: Approve
Revision history for this message
Natalia Bidart (nataliabidart) wrote :

* You're adding a lot of duplication for the tests. What about creating a new assert method and reuse that in every test?

def assert_error_raised(self, path, method_name=None):

     if method_name is None:
         self.assertRaises(AssertionError, assert_windows_path, path)
     else:
         exc = self.assertRaises(AssertionError, assert_windows_path, path,
                                 method_name)
         self.assertTrue(method_name in exc.message)

* There are lint issues:

== Python Lint Notices ==

./tests/platform/windows/test_os_helper.py:
    321: redefinition of function 'test_assert_windows_path_with_illegal_chars' from line 316

review: Needs Fixing
1123. By Manuel de la Peña

Simplified tests code and fixed a redefinition of a fucntion.

1124. By Manuel de la Peña

Fixed stupid brain fucks.

Revision history for this message
Natalia Bidart (nataliabidart) 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 'tests/platform/windows/test_os_helper.py'
2--- tests/platform/windows/test_os_helper.py 2011-08-15 20:29:25 +0000
3+++ tests/platform/windows/test_os_helper.py 2011-09-02 11:37:28 +0000
4@@ -261,30 +261,68 @@
5 class DecoratorsTestCase(TestCase):
6 """Test case for all the validators and transformers."""
7
8+ def assert_error_raised(self, path, method_name=None):
9+ if method_name is None:
10+ self.assertRaises(AssertionError, assert_windows_path, path)
11+ else:
12+ exc = self.assertRaises(AssertionError, assert_windows_path, path,
13+ method_name)
14+ self.assertTrue(method_name in exc.message)
15+
16 def test_assert_windows_path_slash(self):
17 """A path with a / is invalid."""
18 path = LONG_PATH_PREFIX + u'/a/b/'
19- self.assertRaises(AssertionError, assert_windows_path, path)
20+ self.assert_error_raised(path)
21+
22+ def test_assert_windows_method_name_path_slash(self):
23+ """A path with a / is invalid."""
24+ path = LONG_PATH_PREFIX + u'/a/b/'
25+ method_name = 'method_name'
26+ self.assert_error_raised(path, method_name)
27
28 def test_assert_windows_path_non_unicode(self):
29 """A non-unicode path is invalid."""
30 path = (LONG_PATH_PREFIX + u'C:\\Yadda').encode('utf8')
31- self.assertRaises(AssertionError, assert_windows_path, path)
32+ self.assert_error_raised(path)
33+
34+ def test_assert_windows_method_name_path_non_unicode(self):
35+ """A non-unicode path is invalid."""
36+ path = (LONG_PATH_PREFIX + u'C:\\Yadda').encode('utf8')
37+ method_name = 'method_name'
38+ self.assert_error_raised(path, method_name)
39
40 def test_assert_windows_path_non_literal(self):
41 """A non-literal path is invalid."""
42 path = u'C:\\Yadda'
43- self.assertRaises(AssertionError, assert_windows_path, path)
44+ self.assert_error_raised(path)
45+
46+ def test_assert_windows_method_name_path_non_literal(self):
47+ """A non-literal path is invalid."""
48+ path = u'C:\\Yadda'
49+ method_name = 'method_name'
50+ self.assert_error_raised(path, method_name)
51
52 def test_assert_windows_path_non_absolute(self):
53 """A non-absolute path is invalid."""
54 path = u'./yadda'
55- self.assertRaises(AssertionError, assert_windows_path, path)
56+ self.assert_error_raised(path)
57+
58+ def test_assert_windows_method_name_path_non_absolute(self):
59+ """A non-absolute path is invalid."""
60+ path = u'./yadda'
61+ method_name = 'method_name'
62+ self.assert_error_raised(path, method_name)
63
64 def test_assert_windows_path_with_illegal_chars(self):
65 """A path with illegal chars is invalid."""
66 path = u'./yadda' + u''.join(WINDOWS_ILLEGAL_CHARS_MAP)
67- self.assertRaises(AssertionError, assert_windows_path, path)
68+ self.assert_error_raised(path)
69+
70+ def test_assert_windows_method_name_path_with_illegal_chars(self):
71+ """A path with illegal chars is invalid."""
72+ path = u'./yadda' + u''.join(WINDOWS_ILLEGAL_CHARS_MAP)
73+ method_name = 'method_name'
74+ self.assert_error_raised(path, method_name)
75
76
77 class TestIllegalPathsWalk(WalkTests):
78
79=== modified file 'ubuntuone/platform/windows/os_helper.py'
80--- ubuntuone/platform/windows/os_helper.py 2011-08-15 18:54:45 +0000
81+++ ubuntuone/platform/windows/os_helper.py 2011-09-02 11:37:28 +0000
82@@ -100,8 +100,18 @@
83
84 # Functions to be used for path validation
85
86-
87-def assert_windows_path(path):
88+def _add_method_info(messages, method_name=None):
89+ """Loop through the messages and add the extra info."""
90+ updated_messages = messages.copy()
91+ if method_name:
92+ # lets update the messages to contain the method info.
93+ extra_info = 'Asserted in method "%s".' % method_name
94+ for message_name in messages:
95+ updated_messages[message_name] += extra_info
96+ return updated_messages
97+
98+
99+def assert_windows_path(path, method_name=None):
100 """Check whether 'path' is a valid windows path.
101
102 A 'valid windows path' should meet the following requirements:
103@@ -111,19 +121,28 @@
104 * is a literal path (it starts with the LONG_PATH_PREFIX prefix)
105 * do not contain any invalid character (see WINDOWS_ILLEGAL_CHARS_MAP)
106
107+ Opcionally the name of the method that called the assertion can be passed
108+ to improve the assertion message.
109 """
110- assert isinstance(path, unicode), 'Path %r should be unicode.' % path
111- assert path.startswith(LONG_PATH_PREFIX), \
112- 'Path %r should start with the LONG_PATH_PREFIX.' % path
113+ messages = {
114+ 'unicode_path': 'Path %r should be unicode.',
115+ 'long_path': 'Path %r should start with the LONG_PATH_PREFIX.',
116+ 'illegal_path': '%r should not contain any character from' +
117+ ' WINDOWS_ILLEGAL_CHARS_MAP.',
118+ }
119+ messages = _add_method_info(messages, method_name)
120+
121+ assert isinstance(path, unicode), messages['unicode_path'] % path
122+ assert path.startswith(LONG_PATH_PREFIX), messages['long_path'] % path
123 assert os.path.isabs(path.replace(LONG_PATH_PREFIX, u''))
124
125 path = path.replace(LONG_PATH_PREFIX, u'')
126 drive, path = os.path.splitdrive(path)
127 assert not any(c in WINDOWS_ILLEGAL_CHARS_MAP for c in path), \
128- '%r should not contain any character from WINDOWS_ILLEGAL_CHARS_MAP.' % path
129-
130-
131-def assert_syncdaemon_path(path):
132+ messages['illegal_path'] % path
133+
134+
135+def assert_syncdaemon_path(path, method_name=None):
136 """Check whether 'path' is a valid syncdaemon path.
137
138 A 'valid syncdaemon path' should meet the following requirements:
139@@ -133,17 +152,23 @@
140 * do not contain the LONG_PATH_PREFIX
141
142 """
143- assert isinstance(path, str), 'Path %r should be a bytes sequence.' % path
144+ messages = {
145+ 'byte_path': 'Path %r should be a bytes sequence.',
146+ 'utf8_path': 'Path %r should be encoded with utf8.',
147+ 'long_path': '%r should not start with the LONG_PATH_PREFIX.',
148+ 'unicode_chars': '%r should not contain any character from LINUX_ILLEGAL_CHARS_MAP.',
149+ }
150+ messages = _add_method_info(messages, method_name)
151+
152+ assert isinstance(path, str), messages['byte_path'] % path
153 try:
154 path = path.decode('utf8')
155 except UnicodeDecodeError:
156- msg = 'Path %r should be encoded with utf8.'
157- raise AssertionError(msg % path)
158+ raise AssertionError(messages['utf8_path'] % path)
159 # path is now a unicode, we can compare against other unicodes
160- assert not path.startswith(LONG_PATH_PREFIX), \
161- '%r should not start with the LONG_PATH_PREFIX.'
162+ assert not path.startswith(LONG_PATH_PREFIX), messages['long_path']
163 assert not any(c in LINUX_ILLEGAL_CHARS_MAP for c in path), \
164- '%r should not contain any character from LINUX_ILLEGAL_CHARS_MAP.' % path
165+ messages['unicode_chars'] % path
166
167
168 # Functions to be used for path transformation
169@@ -249,7 +274,7 @@
170 def inner(*args, **kwargs):
171 """Do the validation."""
172 for i in path_indexes:
173- validate(args[i])
174+ validate(args[i], method_name=function.__name__)
175 return function(*args, **kwargs)
176
177 return inner

Subscribers

People subscribed via source and target branches