on xenial help sign in
Fixed style and static python errors.
be07744Leo Arias
tests/autopilot/unity8/process_helpers.py+3−2
@@ -75,7 +75,7 @@ def unlock_unity(unity_proxy_obj=None):
75 main_window_emulator.QQuickView)
75 main_window_emulator.QQuickView)
76
76
77 greeter = main_window.get_greeter()
77 greeter = main_window.get_greeter()
78− if greeter.created == False:
78+ if greeter.created is False:
79 raise RuntimeWarning("Greeter appears to be already unlocked.")
79 raise RuntimeWarning("Greeter appears to be already unlocked.")
80
80
81 # Because of potential input jerkiness under heavy load,
81 # Because of potential input jerkiness under heavy load,
@@ -98,7 +98,8 @@ def unlock_unity(unity_proxy_obj=None):
98
98
99def lock_unity(unity_proxy_obj=None):
99def lock_unity(unity_proxy_obj=None):
100 """Helper function that attempts to lock the unity greeter."""
100 """Helper function that attempts to lock the unity greeter."""
101− import evdev, time
101+ import evdev
102+ import time
102 uinput = evdev.UInput(name='unity8-autopilot-power-button',
103 uinput = evdev.UInput(name='unity8-autopilot-power-button',
103 devnode='/dev/autopilot-uinput')
104 devnode='/dev/autopilot-uinput')
104 # One press and release to turn screen off (locking unity)
105 # One press and release to turn screen off (locking unity)
tests/autopilot/unity8/shell/tests/disabled_test_hud.py+44−13
@@ -20,12 +20,13 @@
20from __future__ import absolute_import
20from __future__ import absolute_import
21
21
22from unity8.process_helpers import unlock_unity
22from unity8.process_helpers import unlock_unity
23−from unity8.shell import with_lightdm_mock, DragMixin
23+from unity8.shell import DragMixin
24from unity8.shell.tests import UnityTestCase, _get_device_emulation_scenarios
24from unity8.shell.tests import UnityTestCase, _get_device_emulation_scenarios
25
25
26from testtools.matchers import Equals
26from testtools.matchers import Equals
27from autopilot.matchers import Eventually
27from autopilot.matchers import Eventually
28
28
29+
29class TestHud(UnityTestCase, DragMixin):
30class TestHud(UnityTestCase, DragMixin):
30
31
31 """Tests the Shell HUD."""
32 """Tests the Shell HUD."""
@@ -33,10 +34,14 @@ class TestHud(UnityTestCase, DragMixin):
33 scenarios = _get_device_emulation_scenarios()
34 scenarios = _get_device_emulation_scenarios()
34
35
35 def test_show_hud_button_appears(self):
36 def test_show_hud_button_appears(self):
36− """Swiping up while an app is active must show the 'show hud' button, following some behaviours.
37+ """Swiping up while an app is active must show the 'show hud' button.
38+
39+ It should follow some behaviours.
37 The button must disappear not opening the HUD when releasing the
40 The button must disappear not opening the HUD when releasing the
38− mouse again somewhere on the screen except on the button itself following a timeout.
41+ mouse again somewhere on the screen except on the button itself
42+ following a timeout.
39− The button must disappear when touching somewhere on the screen except the button itself.
43+ The button must disappear when touching somewhere on the screen
44+ except the button itself.
40
45
41 """
46 """
42 unity_proxy = self.launch_unity()
47 unity_proxy = self.launch_unity()
@@ -55,29 +60,47 @@ class TestHud(UnityTestCase, DragMixin):
55
60
56 self.touch.press(swipe_coords.start_x, swipe_coords.start_y)
61 self.touch.press(swipe_coords.start_x, swipe_coords.start_y)
57 self.addCleanup(self._maybe_release_finger)
62 self.addCleanup(self._maybe_release_finger)
63+ self._drag(
64+ swipe_coords.start_x,
65+ swipe_coords.start_y,
66+ swipe_coords.start_x,
58− self._drag(swipe_coords.start_x, swipe_coords.start_y, swipe_coords.start_x, swipe_coords.start_y - int(edge_drag_area.distanceThreshold) - 5)
67+ swipe_coords.start_y - int(edge_drag_area.distanceThreshold) - 5)
59 self.assertThat(hud_show_button.opacity, Eventually(Equals(0.5)))
68 self.assertThat(hud_show_button.opacity, Eventually(Equals(0.5)))
69+ self.assertThat(
70+ hud_show_button.bottomMargin,
60− self.assertThat(hud_show_button.bottomMargin, Eventually(Equals(initialBottomMargin)))
71+ Eventually(Equals(initialBottomMargin)))
72+ self._drag(
73+ swipe_coords.start_x,
61− self._drag(swipe_coords.start_x, swipe_coords.start_y - int(edge_drag_area.distanceThreshold) - 5, swipe_coords.end_x, swipe_coords.start_y - int(edge_drag_area.distanceThreshold) - int(edge_drag_area.commitDistance) - 5)
74+ swipe_coords.start_y - int(edge_drag_area.distanceThreshold) - 5,
75+ swipe_coords.end_x,
76+ swipe_coords.start_y - int(edge_drag_area.distanceThreshold) -
77+ int(edge_drag_area.commitDistance) - 5)
62 self.assertThat(hud_show_button.opacity, Eventually(Equals(1.0)))
78 self.assertThat(hud_show_button.opacity, Eventually(Equals(1.0)))
63 self.assertThat(hud_show_button.bottomMargin, Eventually(Equals(0.0)))
79 self.assertThat(hud_show_button.bottomMargin, Eventually(Equals(0.0)))
64− self.touch.release();
80+ self.touch.release()
65 self.assertThat(hud.shown, Equals(False))
81 self.assertThat(hud.shown, Equals(False))
66 self.assertThat(hud_show_button.opacity, Eventually(Equals(0.0)))
82 self.assertThat(hud_show_button.opacity, Eventually(Equals(0.0)))
67
83
68 self.touch.press(swipe_coords.start_x, swipe_coords.start_y)
84 self.touch.press(swipe_coords.start_x, swipe_coords.start_y)
85+ self._drag(
86+ swipe_coords.start_x,
87+ swipe_coords.start_y,
88+ swipe_coords.start_x,
69− self._drag(swipe_coords.start_x, swipe_coords.start_y, swipe_coords.start_x, swipe_coords.end_y - int(hud_show_button.height))
89+ swipe_coords.end_y - int(hud_show_button.height))
70 self.assertThat(hud.shown, Equals(False))
90 self.assertThat(hud.shown, Equals(False))
71 self.assertThat(hud_show_button.opacity, Eventually(Equals(1.0)))
91 self.assertThat(hud_show_button.opacity, Eventually(Equals(1.0)))
72 self.touch.release()
92 self.touch.release()
73 self.assertThat(hud_show_button.opacity, Eventually(Equals(1.0)))
93 self.assertThat(hud_show_button.opacity, Eventually(Equals(1.0)))
94+ self.touch.tap(
95+ swipe_coords.end_x,
74− self.touch.tap(swipe_coords.end_x, swipe_coords.end_y - int(hud_show_button.height))
96+ swipe_coords.end_y - int(hud_show_button.height))
75 self.assertThat(hud.shown, Equals(False))
97 self.assertThat(hud.shown, Equals(False))
76 self.assertThat(hud_show_button.opacity, Eventually(Equals(0.0)))
98 self.assertThat(hud_show_button.opacity, Eventually(Equals(0.0)))
77
99
78 def test_show_hud_appears(self):
100 def test_show_hud_appears(self):
79 """Releasing the touch on the 'show hud' button must display the hud.
101 """Releasing the touch on the 'show hud' button must display the hud.
80− Test that the hud button stays on screen and tapping it opens the hud.
102+ Test that the hud button stays on screen and tapping it opens the
103+ hud.
81
104
82 """
105 """
83 unity_proxy = self.launch_unity()
106 unity_proxy = self.launch_unity()
@@ -94,7 +117,11 @@ class TestHud(UnityTestCase, DragMixin):
94
117
95 self.touch.press(swipe_coords.start_x, swipe_coords.start_y)
118 self.touch.press(swipe_coords.start_x, swipe_coords.start_y)
96 self.addCleanup(self._maybe_release_finger)
119 self.addCleanup(self._maybe_release_finger)
97− self._drag(swipe_coords.start_x, swipe_coords.start_y, swipe_coords.start_x, swipe_coords.end_y)
120+ self._drag(
121+ swipe_coords.start_x,
122+ swipe_coords.start_y,
123+ swipe_coords.start_x,
124+ swipe_coords.end_y)
98 self.assertThat(hud.shown, Eventually(Equals(False)))
125 self.assertThat(hud.shown, Eventually(Equals(False)))
99 self.assertThat(hud_show_button.opacity, Eventually(Equals(1.0)))
126 self.assertThat(hud_show_button.opacity, Eventually(Equals(1.0)))
100 self.touch.release()
127 self.touch.release()
@@ -105,7 +132,11 @@ class TestHud(UnityTestCase, DragMixin):
105 self.assertThat(hud.shown, Eventually(Equals(False)))
132 self.assertThat(hud.shown, Eventually(Equals(False)))
106
133
107 self.touch.press(swipe_coords.start_x, swipe_coords.start_y)
134 self.touch.press(swipe_coords.start_x, swipe_coords.start_y)
135+ self._drag(
136+ swipe_coords.start_x,
137+ swipe_coords.start_y,
138+ swipe_coords.start_x,
108− self._drag(swipe_coords.start_x, swipe_coords.start_y, swipe_coords.start_x, swipe_coords.end_y - int(hud_show_button.height))
139+ swipe_coords.end_y - int(hud_show_button.height))
109 self.assertThat(hud.shown, Equals(False))
140 self.assertThat(hud.shown, Equals(False))
110 self.assertThat(hud_show_button.opacity, Eventually(Equals(1.0)))
141 self.assertThat(hud_show_button.opacity, Eventually(Equals(1.0)))
111 self.touch.release()
142 self.touch.release()
tests/autopilot/unity8/shell/tests/test_emulators.py−2
@@ -32,8 +32,6 @@ try:
32except ImportError:
32except ImportError:
33 import mock
33 import mock
34
34
35−from testtools.matchers import Contains, HasLength
36−
37from unity8 import process_helpers
35from unity8 import process_helpers
38from unity8.shell import emulators, fixture_setup, tests
36from unity8.shell import emulators, fixture_setup, tests
39from unity8.shell.emulators import dash as dash_emulators
37from unity8.shell.emulators import dash as dash_emulators
tests/autopilot/unity8/shell/tests/test_lock_screen.py+4−5
@@ -1,7 +1,7 @@
1# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
1# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
2#
2#
3# Unity Autopilot Test Suite
3# Unity Autopilot Test Suite
4−# Copyright (C) 2012-2013 Canonical
4+# Copyright (C) 2012, 2013, 2014 Canonical
5#
5#
6# This program is free software: you can redistribute it and/or modify
6# This program is free software: you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
7# it under the terms of the GNU General Public License as published by
@@ -25,9 +25,7 @@ from unity8.shell import with_lightdm_mock
25from unity8.shell.tests import UnityTestCase, _get_device_emulation_scenarios
25from unity8.shell.tests import UnityTestCase, _get_device_emulation_scenarios
26
26
27from autopilot.matchers import Eventually
27from autopilot.matchers import Eventually
28−from autopilot.platform import model
29import sys
28import sys
30−from testtools import skipUnless
31from testtools.matchers import Equals
29from testtools.matchers import Equals
32import logging
30import logging
33
31
@@ -133,7 +131,7 @@ class TestLockscreen(UnityTestCase):
133 if not isinstance(code, basestring):
131 if not isinstance(code, basestring):
134 raise TypeError(
132 raise TypeError(
135 "'code' parameter must be a string, not %r."
133 "'code' parameter must be a string, not %r."
136− % type(passphrase)
134+ % type(code)
137 )
135 )
138 for num in code:
136 for num in code:
139 if not num.isdigit():
137 if not num.isdigit():
@@ -190,7 +188,8 @@ class TestLockscreen(UnityTestCase):
190 current_text = prompt.text
188 current_text = prompt.text
191 self.keyboard.type(character)
189 self.keyboard.type(character)
192 try:
190 try:
191+ self.assertThat(
193− self.assertThat(prompt.text, Eventually(Equals(current_text + character)))
192+ prompt.text, Eventually(Equals(current_text + character)))
194 except AssertionError:
193 except AssertionError:
195 if retries > 0:
194 if retries > 0:
196 self._type_character(character, prompt, retries-1)
195 self._type_character(character, prompt, retries-1)
tests/autopilot/unity8/shell/tests/test_notifications.py+15−10
@@ -211,8 +211,8 @@ class InteractiveNotificationBase(NotificationsBase):
211 self.touch.tap_object(notification.select_single(objectName="button4"))
211 self.touch.tap_object(notification.select_single(objectName="button4"))
212 self.assert_notification_action_id_was_called("action_decline_4")
212 self.assert_notification_action_id_was_called("action_decline_4")
213
213
214− def test_modal_sd_without_greeter (self):
214+ def test_modal_sd_without_greeter(self):
215− """A snap-decision on a phone should block input to shell beneath it when there's no greeter."""
215+ """Snap-decision should block input to shell without greeter."""
216 unity_proxy = self.launch_unity()
216 unity_proxy = self.launch_unity()
217 unlock_unity(unity_proxy)
217 unlock_unity(unity_proxy)
218
218
@@ -238,7 +238,8 @@ class InteractiveNotificationBase(NotificationsBase):
238 hints
238 hints
239 )
239 )
240
240
241− # verify that we cannot reveal the launcher (no longer interact with the shell)
241+ # verify that we cannot reveal the launcher (no longer interact with
242+ # the shell)
242 time.sleep(1)
243 time.sleep(1)
243 self.main_window.show_dash_swiping()
244 self.main_window.show_dash_swiping()
244 launcher = self.main_window.get_launcher()
245 launcher = self.main_window.get_launcher()
@@ -249,13 +250,14 @@ class InteractiveNotificationBase(NotificationsBase):
249 get_notification = lambda: notify_list.wait_select_single(
250 get_notification = lambda: notify_list.wait_select_single(
250 'Notification', objectName='notification1')
251 'Notification', objectName='notification1')
251 notification = get_notification()
252 notification = get_notification()
253+ self._assert_notification(
252− self._assert_notification(notification, summary, body, True, False, 1.0)
254+ notification, summary, body, True, False, 1.0)
253 self.touch.tap_object(notification.select_single(objectName="button0"))
255 self.touch.tap_object(notification.select_single(objectName="button0"))
254 self.assert_notification_action_id_was_called("action_accept")
256 self.assert_notification_action_id_was_called("action_accept")
255
257
256− def test_modal_sd_with_greeter (self):
258+ def test_modal_sd_with_greeter(self):
257− """A snap-decision on a phone should not block input to the greeter beneath it."""
259+ """A snap-decision should not block input to the greeter beneath it."""
258− unity_proxy = self.launch_unity()
260+ self.launch_unity()
259
261
260 summary = "Incoming file"
262 summary = "Incoming file"
261 body = "Frank would like to send you the file: essay.pdf"
263 body = "Frank would like to send you the file: essay.pdf"
@@ -290,7 +292,8 @@ class InteractiveNotificationBase(NotificationsBase):
290 get_notification = lambda: notify_list.wait_select_single(
292 get_notification = lambda: notify_list.wait_select_single(
291 'Notification', objectName='notification1')
293 'Notification', objectName='notification1')
292 notification = get_notification()
294 notification = get_notification()
295+ self._assert_notification(
293− self._assert_notification(notification, summary, body, True, False, 1.0)
296+ notification, summary, body, True, False, 1.0)
294 self.touch.tap_object(notification.select_single(objectName="button0"))
297 self.touch.tap_object(notification.select_single(objectName="button0"))
295 self.assert_notification_action_id_was_called("action_accept")
298 self.assert_notification_action_id_was_called("action_accept")
296
299
@@ -663,7 +666,8 @@ class EphemeralNotificationsTests(NotificationsBase):
663 icon_path = self._get_icon_path('avatars/amanda.png')
666 icon_path = self._get_icon_path('avatars/amanda.png')
664 notification.update(summary, body, icon_path)
667 notification.update(summary, body, icon_path)
665 notification.show()
668 notification.show()
669+ self._assert_notification(
666− self._assert_notification(get_notification(), summary, body, True, False, 1.0)
670+ get_notification(), summary, body, True, False, 1.0)
667
671
668 def test_update_notification_layout_change(self):
672 def test_update_notification_layout_change(self):
669 """Notification must allow updating its contents and layout while
673 """Notification must allow updating its contents and layout while
@@ -710,7 +714,8 @@ class EphemeralNotificationsTests(NotificationsBase):
710 notification.show()
714 notification.show()
711
715
712 self.assertThat(get_notification, Eventually(NotEquals(None)))
716 self.assertThat(get_notification, Eventually(NotEquals(None)))
717+ self._assert_notification(
713− self._assert_notification(get_notification(), summary, body, False, False, 1.0)
718+ get_notification(), summary, body, False, False, 1.0)
714
719
715 def test_append_hint(self):
720 def test_append_hint(self):
716 """Notification has to accumulate body-text using append-hint."""
721 """Notification has to accumulate body-text using append-hint."""
tests/autopilot/unity8/shell/tests/test_system_integration.py+10−5
@@ -23,19 +23,24 @@ import subprocess
23import unittest
23import unittest
24
24
25from autopilot import platform
25from autopilot import platform
26−from unity8.shell.emulators import UnityEmulatorBase
27from unity8.shell.tests import UnityTestCase, _get_device_emulation_scenarios
26from unity8.shell.tests import UnityTestCase, _get_device_emulation_scenarios
28
27
29−from testtools.matchers._basic import Equals
30
28
31class SystemIntegrationTests(UnityTestCase):
29class SystemIntegrationTests(UnityTestCase):
32
30
33 scenarios = _get_device_emulation_scenarios()
31 scenarios = _get_device_emulation_scenarios()
34
32
33+ @unittest.skipIf(
34+ platform.model() == "Desktop",
35− @unittest.skipIf(platform.model() == "Desktop", "Test is broken on otto, see bug 1281634.")
35+ "Test is broken on otto, see bug 1281634.")
36 def test_networkmanager_integration(self):
36 def test_networkmanager_integration(self):
37 self.launch_unity()
37 self.launch_unity()
38
38
39 # invoke policykit to check permissions
39 # invoke policykit to check permissions
40+ pid = subprocess.check_output(
40− pid = subprocess.check_output(["pidof", "-s", "unity8"], universal_newlines=True)
41+ ["pidof", "-s", "unity8"], universal_newlines=True)
42+ subprocess.check_call(
43+ "pkcheck --action-id "
41− subprocess.check_call("pkcheck --action-id org.freedesktop.NetworkManager.enable-disable-network --process " + pid, shell=True)
44+ "org.freedesktop.NetworkManager.enable-disable-network "
45+ "--process " + pid,
46+ shell=True)
tests/autopilot/unity8/shell/tests/test_upstart.py+20−11
@@ -1,7 +1,7 @@
1# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
1# -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
2#
2#
3# Unity Autopilot Test Suite
3# Unity Autopilot Test Suite
4−# Copyright (C) 2013 Canonical
4+# Copyright (C) 2013, 2014 Canonical
5#
5#
6# This program is free software: you can redistribute it and/or modify
6# This program is free software: you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
7# it under the terms of the GNU General Public License as published by
@@ -42,19 +42,24 @@ import sys
42if sys.version_info < (3,):
42if sys.version_info < (3,):
43 range = xrange
43 range = xrange
44
44
45+
45class UpstartIntegrationTests(UnityTestCase):
46class UpstartIntegrationTests(UnityTestCase):
46
47
47 scenarios = _get_device_emulation_scenarios()
48 scenarios = _get_device_emulation_scenarios()
48
49
49 def _get_status(self):
50 def _get_status(self):
51+ pid, status = os.waitpid(
50− pid, status = os.waitpid(self.process.pid, os.WUNTRACED | os.WCONTINUED | os.WNOHANG)
52+ self.process.pid, os.WUNTRACED | os.WCONTINUED | os.WNOHANG)
53+ logger.debug(
51− logger.debug("Got status: {0}; stopped: {1}; continued: {2}"\
54+ "Got status: {0}; stopped: {1}; continued: {2}".format(
52− .format(status, os.WIFSTOPPED(status), os.WIFCONTINUED(status)))
55+ status, os.WIFSTOPPED(status), os.WIFCONTINUED(status)))
53 return status
56 return status
54
57
55 def _launch_unity(self):
58 def _launch_unity(self):
56 self.patch_environment("QT_LOAD_TESTABILITY", "1")
59 self.patch_environment("QT_LOAD_TESTABILITY", "1")
60+ self.process = subprocess.Popen(
57− self.process = subprocess.Popen([get_binary_path()] + self.unity_geometry_args)
61+ [get_binary_path()] + self.unity_geometry_args)
62+
58 def ensure_stopped():
63 def ensure_stopped():
59 self.process.terminate()
64 self.process.terminate()
60 for i in range(10):
65 for i in range(10):
@@ -76,10 +81,10 @@ class UpstartIntegrationTests(UnityTestCase):
76 self.addCleanup(ensure_stopped)
81 self.addCleanup(ensure_stopped)
77
82
78 def _set_proxy(self):
83 def _set_proxy(self):
79− super(UpstartIntegrationTests, self)._set_proxy(get_proxy_object_for_existing_process(
84+ super(UpstartIntegrationTests, self)._set_proxy(
85+ get_proxy_object_for_existing_process(
80− pid=self.process.pid,
86+ pid=self.process.pid,
81− emulator_base=UnityEmulatorBase,
87+ emulator_base=UnityEmulatorBase,))
82− ))
83
88
84 def test_no_sigstop(self):
89 def test_no_sigstop(self):
85 self.patch_environment("UNITY_MIR_EMITS_SIGSTOP", "")
90 self.patch_environment("UNITY_MIR_EMITS_SIGSTOP", "")
@@ -93,10 +98,14 @@ class UpstartIntegrationTests(UnityTestCase):
93 def test_expect_sigstop(self):
98 def test_expect_sigstop(self):
94 self.patch_environment("UNITY_MIR_EMITS_SIGSTOP", "1")
99 self.patch_environment("UNITY_MIR_EMITS_SIGSTOP", "1")
95 self._launch_unity()
100 self._launch_unity()
101+ self.assertThat(
102+ lambda: os.WIFSTOPPED(self._get_status()),
96− self.assertThat(lambda: os.WIFSTOPPED(self._get_status()), Eventually(Equals(True)), "Unity8 should raise SIGSTOP when ready")
103+ Eventually(Equals(True)), "Unity8 should raise SIGSTOP when ready")
97
104
98 self.process.send_signal(signal.SIGCONT)
105 self.process.send_signal(signal.SIGCONT)
106+ self.assertThat(
107+ lambda: os.WIFCONTINUED(self._get_status()),
99− self.assertThat(lambda: os.WIFCONTINUED(self._get_status()), Eventually(Equals(True)), "Unity8 should have resumed")
108+ Eventually(Equals(True)), "Unity8 should have resumed")
100
109
101 logger.debug("Unity started, waiting for it to be ready.")
110 logger.debug("Unity started, waiting for it to be ready.")
102 self._set_proxy()
111 self._set_proxy()
tests/whitespace/check_whitespace.py+20−12
@@ -1,7 +1,7 @@
1#! /usr/bin/env python3
1#! /usr/bin/env python3
2
2
3#
3#
4−# Copyright (C) 2013 Canonical Ltd
4+# Copyright (C) 2013, 2014 Canonical Ltd
5#
5#
6# This program is free software: you can redistribute it and/or modify
6# This program is free software: you can redistribute it and/or modify
7# it under the terms of the GNU General Public License version 3 as
7# it under the terms of the GNU General Public License version 3 as
@@ -19,8 +19,8 @@
19#
19#
20
20
21#
21#
22−# Little helper program to test that source files do not contain trailing whitespace
22+# Little helper program to test that source files do not contain trailing
23−# or tab indentation.
23+# whitespace or tab indentation.
24#
24#
25# Usage: check_whitespace.py directory [ignore_prefix]
25# Usage: check_whitespace.py directory [ignore_prefix]
26#
26#
@@ -36,13 +36,13 @@ import os
36import re
36import re
37import sys
37import sys
38
38
39−# Print msg on stderr, preceded by program name and followed by newline
40
39
40+# Print msg on stderr, preceded by program name and followed by newline
41def error(msg):
41def error(msg):
42 print(os.path.basename(sys.argv[0]) + ": " + msg, file=sys.stderr)
42 print(os.path.basename(sys.argv[0]) + ": " + msg, file=sys.stderr)
43
43
44−# Function to raise errors encountered by os.walk
45
44
45+# Function to raise errors encountered by os.walk
46def raise_error(e):
46def raise_error(e):
47 raise e
47 raise e
48
48
@@ -52,6 +52,7 @@ def raise_error(e):
52whitespace_pat = re.compile(r'.*[ \t]$')
52whitespace_pat = re.compile(r'.*[ \t]$')
53tab_indent_pat = re.compile(r'^ *\t')
53tab_indent_pat = re.compile(r'^ *\t')
54
54
55+
55def scan_for_bad_whitespace(file_path):
56def scan_for_bad_whitespace(file_path):
56 global tab_indent_pat, whitespace_pat
57 global tab_indent_pat, whitespace_pat
57 errors = []
58 errors = []
@@ -69,7 +70,9 @@ def scan_for_bad_whitespace(file_path):
69 plural = 's'
70 plural = 's'
70 else:
71 else:
71 plural = ''
72 plural = ''
73+ print(
74+ "%s: bad whitespace in line%s %s" % (
72− print("%s: bad whitespace in line%s %s" % (file_path, plural, ", ".join((str(i) for i in errors))))
75+ file_path, plural, ", ".join((str(i) for i in errors))))
73 elif errors:
76 elif errors:
74 print("%s: bad whitespace in multiple lines" % file_path)
77 print("%s: bad whitespace in multiple lines" % file_path)
75 if newlines_at_end:
78 if newlines_at_end:
@@ -78,15 +81,20 @@ def scan_for_bad_whitespace(file_path):
78
81
79# Parse args
82# Parse args
80
83
84+parser = argparse.ArgumentParser(
81−parser = argparse.ArgumentParser(description = 'Test that source files do not contain trailing whitespace.')
85+ description='Test that source files do not contain trailing whitespace.')
86+parser.add_argument(
87+ 'dir', nargs=1,
82−parser.add_argument('dir', nargs = 1, help = 'The directory to (recursively) search for source files')
88+ help='The directory to (recursively) search for source files')
89+parser.add_argument(
83−parser.add_argument('ignore_prefix', nargs = '?', default=None,
90+ 'ignore_prefix', nargs='?', default=None,
84− help = 'Ignore source files with a path that starts with the given prefix.')
91+ help='Ignore source files with a path that starts with the given prefix.')
85args = parser.parse_args()
92args = parser.parse_args()
86
93
87# Files we want to check for trailing whitespace.
94# Files we want to check for trailing whitespace.
88
95
96+file_pat = (
89−file_pat = r'(.*\.(c|cpp|h|hpp|hh|in|install|js|py|qml|sh)$)|(.*CMakeLists\.txt$)'
97+ r'(.*\.(c|cpp|h|hpp|hh|in|install|js|py|qml|sh)$)|(.*CMakeLists\.txt$)')
90pat = re.compile(file_pat)
98pat = re.compile(file_pat)
91
99
92# Find all the files with matching file extension in the specified
100# Find all the files with matching file extension in the specified
@@ -97,7 +105,7 @@ ignore = args.ignore_prefix and os.path.abspath(args.ignore_prefix) or None
97
105
98found_whitespace = False
106found_whitespace = False
99try:
107try:
100− for root, dirs, files in os.walk(directory, onerror = raise_error):
108+ for root, dirs, files in os.walk(directory, onerror=raise_error):
101 for file in files:
109 for file in files:
102 path = os.path.join(root, file)
110 path = os.path.join(root, file)
103 if not (ignore and path.startswith(ignore)) and pat.match(file):
111 if not (ignore and path.startswith(ignore)) and pat.match(file):