summaryrefslogtreecommitdiff
path: root/utils/trackertestutils/dconf.py
blob: e3b8e9984a7ee77dbdad99d44703c8481255e96b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#
# Copyright (C) 2010, Nokia <jean-luc.lamadon@nokia.com>
# Copyright (C) 2019, Sam Thursfield <sam@afuera.me.uk>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.
#

import logging
import os
import subprocess

log = logging.getLogger(__name__)


class DConfClient(object):
    """
    Allow changing Tracker configuration in DConf.

    Tests should be run with a separate DConf profile so that these changes do
    not affect the user's configuration. The 'trackertest' profile exists for
    this reason, and the constructor will fail if this isn't the profile in
    use, to avoid any risk of modifying or removing your real configuration.

    We use the `gsettings` binary rather than using the Gio.Settings API.
    This is to avoid the need to set DCONF_PROFILE in our own process
    environment.
    """

    def __init__(self, sandbox):
        self.env = os.environ
        self.env.update(sandbox.extra_env)
        self.env['DBUS_SESSION_BUS_ADDRESS'] = sandbox.get_session_bus_address()

    def _check_using_correct_dconf_profile(self):
        profile = self.env.get("DCONF_PROFILE")
        if not profile:
            raise Exception(
                "DCONF_PROFILE is not set in the environment. This class must "
                "be created inside a TrackerDBussandbox to avoid risk of "
                "interfering with real settings.")
        if not os.path.exists(profile):
            raise Exception(
                "Unable to find DConf profile '%s'. Check that Tracker and "
                "the test suite have been correctly installed (you must pass "
                "--enable-functional-tests to configure)." % profile)

        assert os.path.basename(profile) == "trackertest"

    def write(self, schema, key, value):
        """
        Write a settings value.
        """
        subprocess.run(['gsettings', 'set', schema, key, value.print_(False)],
                       env=self.env,
                       stdout=subprocess.PIPE,
                       stderr=subprocess.PIPE)