Merge lp:~canonical-platform-qa/autopilot/depends_for_gsettings into lp:autopilot

Proposed by Christopher Lee
Status: Merged
Approved by: Jean-Baptiste Lallement
Approved revision: 571
Merged at revision: 566
Proposed branch: lp:~canonical-platform-qa/autopilot/depends_for_gsettings
Merge into: lp:autopilot
Diff against target: 240 lines (+92/-80)
3 files modified
autopilot/_fixtures.py (+60/-49)
autopilot/tests/unit/test_fixtures.py (+31/-30)
debian/control (+1/-1)
To merge this branch: bzr merge lp:~canonical-platform-qa/autopilot/depends_for_gsettings
Reviewer Review Type Date Requested Status
Iain Lane (community) Approve
PS Jenkins bot continuous-integration Approve
Autopilot Hackers Pending
Review via email: mp+265621@code.launchpad.net

Commit message

Resolves dependencies that not everyone cares about. Using Gio API instead of gsettings binary.

Description of the change

Resolves dependencies that not everyone cares about. Using Gio API instead of gsettings binary.

To post a comment you must log in.
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
570. By Christopher Lee

Correct test

571. By Christopher Lee

Actually change setting now (needs to be bool, not string). test now passes.

Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
Revision history for this message
Iain Lane (laney) wrote :

OK, thanks!

Two minor comments

  - Recommends probably isn't that useful here since it isn't installed in many circumstances. I'd probably remove it and just ask consumers to install the package if they want this. They probably already have it if they are testing the osk anyway.
  - Do you need to upgrade gir1.2-glib-2.0 to Depends now? This isn't actually *that* important since python3-gi depends on it, but if you rely on something then it's best to have depends explicitly.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'autopilot/_fixtures.py'
2--- autopilot/_fixtures.py 2015-07-16 03:26:31 +0000
3+++ autopilot/_fixtures.py 2015-07-23 02:56:28 +0000
4@@ -19,8 +19,7 @@
5
6 from fixtures import Fixture
7 import logging
8-import subprocess
9-
10+from gi.repository import Gio
11
12 logger = logging.getLogger(__name__)
13
14@@ -58,52 +57,64 @@
15 osk_schema = 'com.canonical.keyboard.maliit'
16 osk_show_key = 'stay-hidden'
17
18- def __init__(self):
19- super().__init__()
20- self._original_value = get_gsettings_value(
21- self.osk_schema,
22- self.osk_show_key
23- )
24-
25 def setUp(self):
26 super().setUp()
27- set_gsettings_value(self.osk_schema, self.osk_show_key, 'false')
28- self.addCleanup(
29- set_gsettings_value,
30- self.osk_schema,
31- self.osk_show_key,
32- self._original_value
33- )
34-
35-
36-def get_gsettings_value(schema, key):
37- """Return the output of gsettings get as a string or None if the call
38- fails.
39-
40- """
41- command = ['/usr/bin/gsettings', 'get', schema, key]
42- try:
43- output = subprocess.check_output(
44- command,
45- stderr=subprocess.PIPE,
46- universal_newlines=True
47- )
48- return output.rstrip('\n')
49- except subprocess.CalledProcessError as e:
50- logger.warning(
51- 'Failed to get gsettings value for {schema}/{key}: {error}'.format(
52- schema=schema, key=key, error=e.output
53- )
54- )
55-
56-
57-def set_gsettings_value(schema, key, value):
58- command = ['/usr/bin/gsettings', 'set', schema, key, value]
59- try:
60- subprocess.check_output(command, stderr=subprocess.PIPE)
61- except subprocess.CalledProcessError as e:
62- logger.warning(
63- 'Failed to set gsettings value {sch}/{key} to {v}: {error}'.format(
64- sch=schema, key=key, v=value, error=e.output
65- )
66- )
67+
68+ try:
69+ _original_value = get_bool_gsettings_value(
70+ self.osk_schema,
71+ self.osk_show_key
72+ )
73+ set_bool_gsettings_value(
74+ self.osk_schema,
75+ self.osk_show_key,
76+ False
77+ )
78+ self.addCleanup(
79+ set_bool_gsettings_value,
80+ self.osk_schema,
81+ self.osk_show_key,
82+ _original_value
83+ )
84+ except ValueError as e:
85+ logger.warning('Failed to set OSK gsetting: {}'.format(e))
86+
87+
88+def get_bool_gsettings_value(schema, key):
89+ """Return the boolean value for schema/key combo.
90+
91+ :raises ValueError: If either ``schema`` or ``key`` are not valid.
92+
93+ """
94+ setting = _gsetting_get_setting(schema, key)
95+
96+ return setting.get_boolean(key)
97+
98+
99+def set_bool_gsettings_value(schema, key, value):
100+ """Set the boolean value ``value`` for schema/key combo.
101+
102+ :raises ValueError: If either ``schema`` or ``key`` are not valid.
103+
104+ """
105+
106+ setting = _gsetting_get_setting(schema, key)
107+
108+ setting.set_boolean(key, value)
109+
110+
111+def _gsetting_get_setting(schema, key):
112+ if schema not in Gio.Settings.list_schemas():
113+ raise ValueError('schema {} is not installed.'.format(schema))
114+
115+ setting = Gio.Settings.new(schema)
116+
117+ if key not in setting.keys():
118+ raise ValueError(
119+ 'key \'{key}\' is not available for schema \'{schema}\''.format(
120+ key=key,
121+ schema=schema
122+ )
123+ )
124+
125+ return setting
126
127=== modified file 'autopilot/tests/unit/test_fixtures.py'
128--- autopilot/tests/unit/test_fixtures.py 2015-07-16 01:52:23 +0000
129+++ autopilot/tests/unit/test_fixtures.py 2015-07-23 02:56:28 +0000
130@@ -19,11 +19,8 @@
131
132
133 from testtools import TestCase
134-from testtools.matchers import (
135- Not,
136- Raises,
137-)
138-from unittest.mock import patch
139+from testtools.matchers import raises
140+from unittest.mock import patch, Mock
141 import autopilot._fixtures as ap_fixtures
142
143
144@@ -40,47 +37,51 @@
145
146 class GSettingsAccessTests(TestCase):
147
148- def test_incorrect_schema_doesnt_raise_exception(self):
149- self.assertThat(
150- lambda: ap_fixtures.get_gsettings_value('foo', 'bar'),
151- Not(Raises())
152+ def test_incorrect_schema_raises_exception(self):
153+ self.assertThat(
154+ lambda: ap_fixtures._gsetting_get_setting('com.foo.', 'baz'),
155+ raises(ValueError)
156+ )
157+
158+ def test_incorrect_key_raises_exception(self):
159+ self.assertThat(
160+ lambda: ap_fixtures._gsetting_get_setting(
161+ 'org.gnome.system.locale',
162+ 'baz'
163+ ),
164+ raises(ValueError)
165 )
166
167 def test_get_value_returns_expected_value(self):
168- with patch.object(ap_fixtures.subprocess, 'check_output') as check_out:
169- check_out.return_value = 'buzz'
170- self.assertEqual(
171- ap_fixtures.get_gsettings_value('foo', 'bar'),
172- 'buzz'
173- )
174-
175- def test_get_value_strips_newline(self):
176- with patch.object(ap_fixtures.subprocess, 'check_output') as check_out:
177- check_out.return_value = 'buzz\n'
178- self.assertEqual(
179- ap_fixtures.get_gsettings_value('foo', 'bar'),
180- 'buzz'
181+ with patch.object(ap_fixtures, '_gsetting_get_setting') as get_setting:
182+ setting = Mock()
183+ setting.get_boolean.return_value = True
184+ get_setting.return_value = setting
185+ self.assertEqual(
186+ ap_fixtures.get_bool_gsettings_value('foo', 'bar'),
187+ True
188 )
189
190
191 class OSKAlwaysEnabledTests(TestCase):
192
193- def test_sets_stayhidden_to_false(self):
194- with patch.object(ap_fixtures, 'set_gsettings_value') as set_gsetting:
195+ @patch.object(ap_fixtures, '_gsetting_get_setting')
196+ def test_sets_stayhidden_to_False(self, gs):
197+ with patch.object(ap_fixtures, 'set_bool_gsettings_value') as set_gs:
198 with ap_fixtures.OSKAlwaysEnabled():
199- set_gsetting.assert_called_once_with(
200+ set_gs.assert_called_once_with(
201 'com.canonical.keyboard.maliit',
202 'stay-hidden',
203- 'false'
204+ False
205 )
206
207 def test_resets_value_to_original(self):
208- with patch.object(ap_fixtures, 'set_gsettings_value') as set_gset:
209- with patch.object(ap_fixtures, 'get_gsettings_value') as get_gset:
210- get_gset.return_value = 'foo'
211+ with patch.object(ap_fixtures, 'set_bool_gsettings_value') as set_gs:
212+ with patch.object(ap_fixtures, 'get_bool_gsettings_value') as get_gs: # NOQA
213+ get_gs.return_value = 'foo'
214 with ap_fixtures.OSKAlwaysEnabled():
215 pass
216- set_gset.assert_called_with(
217+ set_gs.assert_called_with(
218 'com.canonical.keyboard.maliit',
219 'stay-hidden',
220 'foo'
221
222=== modified file 'debian/control'
223--- debian/control 2015-07-16 03:24:34 +0000
224+++ debian/control 2015-07-23 02:56:28 +0000
225@@ -11,7 +11,6 @@
226 gir1.2-ibus-1.0,
227 gir1.2-ubuntu-app-launch-2 | gir1.2-upstart-app-launch-2,
228 graphviz,
229- libglib2.0-bin,
230 libjs-jquery,
231 libjs-underscore,
232 liblttng-ust-dev,
233@@ -69,6 +68,7 @@
234 python3-evdev,
235 python3-xlib (>=0.14+20091101-1ubuntu3),
236 recordmydesktop,
237+ ubuntu-keyboard-data,
238 Breaks: libautopilot-gtk (<< 1.4),
239 libautopilot-qt (<< 1.4),
240 Description: Utility to write and run integration tests easily (Python 3)

Subscribers

People subscribed via source and target branches