on xenial help sign in
Simple fixes to make compatible with py3 (and stay compatible with py2.7)
commit754b6d1f9fee5e02fe8be17f79282f359ef6214b authorChristopher Lee <chris.lee@canonical.com> dateMon 11 Nov 2013, 18:57 (13 years ago) parent1327512
4 files changed+41−24
tests/autopilot/unity8/__init__.py+7−4 tests/autopilot/unity8/shell/tests/__init__.py+13−14 tests/autopilot/unity8/shell/tests/test_lock_screen.py+15−6 tests/autopilot/unity8/shell/tests/test_notifications.py+6
tests/autopilot/unity8/__init__.py+7−4
@@ -82,7 +82,10 @@ def get_binary_path(binary="unity8"):
8282 )
8383 if not os.path.exists(binary_path):
8484 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()
8689 except subprocess.CalledProcessError as e:
8790 raise RuntimeError("Unable to locate %s binary: %r" % (binary, e))
8891 return binary_path
@@ -91,9 +94,9 @@ def get_binary_path(binary="unity8"):
9194def get_data_dirs():
9295 """Prepend a mock data path to XDG_DATA_DIRS."""
9396 if running_installed_tests():
94− data_path = "/usr/share/unity8/mocks/data"
97+ data_path = u"/usr/share/unity8/mocks/data"
9598 else:
96− data_path = "../../mocks/data"
99+ data_path = u"../../mocks/data"
97100 full_data_path = os.path.abspath(
98101 os.path.join(
99102 os.path.dirname(__file__),
@@ -104,7 +107,7 @@ def get_data_dirs():
104107 if os.path.exists(full_data_path):
105108 xdg_path = _get_xdg_env_path()
106109 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)
108111 else:
109112 return full_data_path
110113 else:
tests/autopilot/unity8/shell/tests/__init__.py+13−14
@@ -86,12 +86,12 @@ class UnityTestCase(AutopilotTestCase):
8686 @classmethod
8787 def setUpClass(cls):
8888 try:
89− output = subprocess.check_output([
90− "/sbin/initctl",
91− "status",
92− "unity8"
93− ], stderr=subprocess.STDOUT)
94− except subprocess.CalledProcessError, e:
89+ output = subprocess.check_output(
90+ ["/sbin/initctl", "status", "unity8"],
91+ stderr=subprocess.STDOUT,
92+ universal_newlines=True,
93+ )
94+ except subprocess.CalledProcessError as e:
9595 sys.stderr.write(
9696 "Error: `initctl status unity8` failed, most probably the "
9797 "unity8 session could not be found:\n\n"
@@ -219,12 +219,11 @@ class UnityTestCase(AutopilotTestCase):
219219 def _patch_environment(self, key, value):
220220 """Wrapper for patching env for upstart environment."""
221221 try:
222− current_value = subprocess.check_output([
223− "/sbin/initctl",
224− "get-env",
225− "--global",
226− key
227− ], stderr=subprocess.STDOUT).rstrip()
222+ current_value = subprocess.check_output(
223+ ["/sbin/initctl", "get-env", "--global", key],
224+ stderr=subprocess.STDOUT,
225+ universal_newlines=True,
226+ ).rstrip()
228227 except subprocess.CalledProcessError:
229228 current_value = None
230229
@@ -299,7 +298,7 @@ class UnityTestCase(AutopilotTestCase):
299298
300299 def _launch_unity_with_upstart(self, binary_path, args):
301300 logger.info("Starting unity")
302− self._patch_environment("QT_LOAD_TESTABILITY", 1)
301+ self._patch_environment(u"QT_LOAD_TESTABILITY", 1)
303302
304303 binary_arg = "BINARY=%s" % binary_path
305304 extra_args = "ARGS=%s" % " ".join(args)
@@ -311,7 +310,7 @@ class UnityTestCase(AutopilotTestCase):
311310 "unity8",
312311 binary_arg,
313312 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()],
315314 stderr=subprocess.STDOUT,
316315 )
317316
tests/autopilot/unity8/shell/tests/test_lock_screen.py+15−6
@@ -123,8 +123,11 @@ class TestLockscreen(UnityTestCase):
123123
124124 """
125125
126− if not isinstance(code, basestring):
127− raise TypeError("'code' parameter must be a string.")
126+ if not isinstance(code, str):
127+ raise TypeError(
128+ "'code' parameter must be a string, not %r."
129+ % type(passphrase)
130+ )
128131 for num in code:
129132 if not num.isdigit():
130133 raise ValueError(
@@ -140,8 +143,11 @@ class TestLockscreen(UnityTestCase):
140143 :raises: TypeError if passphrase is not a string.
141144
142145 """
143− if not isinstance(passphrase, basestring):
144− raise TypeError("'passphrase' parameter must be a string.")
146+ if not isinstance(passphrase, str):
147+ raise TypeError(
148+ "'passphrase' parameter must be a string, not %r."
149+ % type(passphrase)
150+ )
145151
146152 pinentryField = self.main_window.get_pinentryField()
147153 self.touch.tap_object(pinentryField)
@@ -159,8 +165,11 @@ class TestLockscreen(UnityTestCase):
159165 :raises: TypeError if passphrase is not a string.
160166
161167 """
162− if not isinstance(passphrase, basestring):
163− raise TypeError("'passphrase' parameter must be a string.")
168+ if not isinstance(passphrase, str):
169+ raise TypeError(
170+ "'passphrase' parameter must be a string, not %r."
171+ % type(passphrase)
172+ )
164173
165174 prompt = self.main_window.get_greeter().get_prompt()
166175 self.touch.tap_object(prompt)
tests/autopilot/unity8/shell/tests/test_notifications.py+6
@@ -32,9 +32,14 @@ import os
3232import logging
3333import signal
3434import subprocess
35+import sys
3536
3637logger = logging.getLogger(__name__)
3738
39+# backwards compatible alias for Python 3
40+if sys.version > '3':
41+ xrange = range
42+
3843
3944class NotificationsBase(UnityTestCase):
4045 """Base class for all notification tests that provides helper methods."""
@@ -266,6 +271,7 @@ class InteractiveNotificationBase(NotificationsBase):
266271 stdout=subprocess.PIPE,
267272 stderr=subprocess.PIPE,
268273 close_fds=True,
274+ universal_newlines=True,
269275 )
270276
271277 self.addCleanup(self._tidy_up_script_process)