on xenial help sign in
Simple fixes to make compatible with py3 (and stay compatible with py2.7)
754b6d1Christopher Lee
tests/autopilot/unity8/__init__.py+7−4
@@ -82,7 +82,10 @@ def get_binary_path(binary="unity8"):
82 )
82 )
83 if not os.path.exists(binary_path):
83 if not os.path.exists(binary_path):
84 try:
84 try:
85− binary_path = subprocess.check_output(['which', binary]).strip()
85+ binary_path = subprocess.check_output(
86+ ['which', binary],
87+ universal_newlines=True,
88+ ).strip()
86 except subprocess.CalledProcessError as e:
89 except subprocess.CalledProcessError as e:
87 raise RuntimeError("Unable to locate %s binary: %r" % (binary, e))
90 raise RuntimeError("Unable to locate %s binary: %r" % (binary, e))
88 return binary_path
91 return binary_path
@@ -91,9 +94,9 @@ def get_binary_path(binary="unity8"):
91def get_data_dirs():
94def get_data_dirs():
92 """Prepend a mock data path to XDG_DATA_DIRS."""
95 """Prepend a mock data path to XDG_DATA_DIRS."""
93 if running_installed_tests():
96 if running_installed_tests():
94− data_path = "/usr/share/unity8/mocks/data"
97+ data_path = u"/usr/share/unity8/mocks/data"
95 else:
98 else:
96− data_path = "../../mocks/data"
99+ data_path = u"../../mocks/data"
97 full_data_path = os.path.abspath(
100 full_data_path = os.path.abspath(
98 os.path.join(
101 os.path.join(
99 os.path.dirname(__file__),
102 os.path.dirname(__file__),
@@ -104,7 +107,7 @@ def get_data_dirs():
104 if os.path.exists(full_data_path):
107 if os.path.exists(full_data_path):
105 xdg_path = _get_xdg_env_path()
108 xdg_path = _get_xdg_env_path()
106 if xdg_path is not None:
109 if xdg_path is not None:
107− return "{0}:{1}".format(full_data_path, xdg_path)
110+ return u"{0}:{1}".format(full_data_path, xdg_path)
108 else:
111 else:
109 return full_data_path
112 return full_data_path
110 else:
113 else:
tests/autopilot/unity8/shell/tests/__init__.py+13−14
@@ -86,12 +86,12 @@ class UnityTestCase(AutopilotTestCase):
86 @classmethod
86 @classmethod
87 def setUpClass(cls):
87 def setUpClass(cls):
88 try:
88 try:
89− output = subprocess.check_output([
89+ output = subprocess.check_output(
90− "/sbin/initctl",
90+ ["/sbin/initctl", "status", "unity8"],
91− "status",
92− "unity8"
93− ], stderr=subprocess.STDOUT)
91+ stderr=subprocess.STDOUT,
92+ universal_newlines=True,
93+ )
94− except subprocess.CalledProcessError, e:
94+ except subprocess.CalledProcessError as e:
95 sys.stderr.write(
95 sys.stderr.write(
96 "Error: `initctl status unity8` failed, most probably the "
96 "Error: `initctl status unity8` failed, most probably the "
97 "unity8 session could not be found:\n\n"
97 "unity8 session could not be found:\n\n"
@@ -219,12 +219,11 @@ class UnityTestCase(AutopilotTestCase):
219 def _patch_environment(self, key, value):
219 def _patch_environment(self, key, value):
220 """Wrapper for patching env for upstart environment."""
220 """Wrapper for patching env for upstart environment."""
221 try:
221 try:
222− current_value = subprocess.check_output([
222+ current_value = subprocess.check_output(
223− "/sbin/initctl",
223+ ["/sbin/initctl", "get-env", "--global", key],
224− "get-env",
225− "--global",
226− key
227− ], stderr=subprocess.STDOUT).rstrip()
224+ stderr=subprocess.STDOUT,
225+ universal_newlines=True,
226+ ).rstrip()
228 except subprocess.CalledProcessError:
227 except subprocess.CalledProcessError:
229 current_value = None
228 current_value = None
230
229
@@ -299,7 +298,7 @@ class UnityTestCase(AutopilotTestCase):
299
298
300 def _launch_unity_with_upstart(self, binary_path, args):
299 def _launch_unity_with_upstart(self, binary_path, args):
301 logger.info("Starting unity")
300 logger.info("Starting unity")
302− self._patch_environment("QT_LOAD_TESTABILITY", 1)
301+ self._patch_environment(u"QT_LOAD_TESTABILITY", 1)
303
302
304 binary_arg = "BINARY=%s" % binary_path
303 binary_arg = "BINARY=%s" % binary_path
305 extra_args = "ARGS=%s" % " ".join(args)
304 extra_args = "ARGS=%s" % " ".join(args)
@@ -311,7 +310,7 @@ class UnityTestCase(AutopilotTestCase):
311 "unity8",
310 "unity8",
312 binary_arg,
311 binary_arg,
313 extra_args,
312 extra_args,
314− ] + ["%s=%s" % (k, v) for k, v in self._environment.iteritems()],
313+ ] + ["%s=%s" % (k, v) for k, v in self._environment.items()],
315 stderr=subprocess.STDOUT,
314 stderr=subprocess.STDOUT,
316 )
315 )
317
316
tests/autopilot/unity8/shell/tests/test_lock_screen.py+15−6
@@ -123,8 +123,11 @@ class TestLockscreen(UnityTestCase):
123
123
124 """
124 """
125
125
126− if not isinstance(code, basestring):
126+ if not isinstance(code, str):
127+ raise TypeError(
127− raise TypeError("'code' parameter must be a string.")
128+ "'code' parameter must be a string, not %r."
129+ % type(passphrase)
130+ )
128 for num in code:
131 for num in code:
129 if not num.isdigit():
132 if not num.isdigit():
130 raise ValueError(
133 raise ValueError(
@@ -140,8 +143,11 @@ class TestLockscreen(UnityTestCase):
140 :raises: TypeError if passphrase is not a string.
143 :raises: TypeError if passphrase is not a string.
141
144
142 """
145 """
143− if not isinstance(passphrase, basestring):
146+ if not isinstance(passphrase, str):
147+ raise TypeError(
144− raise TypeError("'passphrase' parameter must be a string.")
148+ "'passphrase' parameter must be a string, not %r."
149+ % type(passphrase)
150+ )
145
151
146 pinentryField = self.main_window.get_pinentryField()
152 pinentryField = self.main_window.get_pinentryField()
147 self.touch.tap_object(pinentryField)
153 self.touch.tap_object(pinentryField)
@@ -159,8 +165,11 @@ class TestLockscreen(UnityTestCase):
159 :raises: TypeError if passphrase is not a string.
165 :raises: TypeError if passphrase is not a string.
160
166
161 """
167 """
162− if not isinstance(passphrase, basestring):
168+ if not isinstance(passphrase, str):
169+ raise TypeError(
163− raise TypeError("'passphrase' parameter must be a string.")
170+ "'passphrase' parameter must be a string, not %r."
171+ % type(passphrase)
172+ )
164
173
165 prompt = self.main_window.get_greeter().get_prompt()
174 prompt = self.main_window.get_greeter().get_prompt()
166 self.touch.tap_object(prompt)
175 self.touch.tap_object(prompt)
tests/autopilot/unity8/shell/tests/test_notifications.py+6
@@ -32,9 +32,14 @@ import os
32import logging
32import logging
33import signal
33import signal
34import subprocess
34import subprocess
35+import sys
35
36
36logger = logging.getLogger(__name__)
37logger = logging.getLogger(__name__)
37
38
39+# backwards compatible alias for Python 3
40+if sys.version > '3':
41+ xrange = range
42+
38
43
39class NotificationsBase(UnityTestCase):
44class NotificationsBase(UnityTestCase):
40 """Base class for all notification tests that provides helper methods."""
45 """Base class for all notification tests that provides helper methods."""
@@ -266,6 +271,7 @@ class InteractiveNotificationBase(NotificationsBase):
266 stdout=subprocess.PIPE,
271 stdout=subprocess.PIPE,
267 stderr=subprocess.PIPE,
272 stderr=subprocess.PIPE,
268 close_fds=True,
273 close_fds=True,
274+ universal_newlines=True,
269 )
275 )
270
276
271 self.addCleanup(self._tidy_up_script_process)
277 self.addCleanup(self._tidy_up_script_process)