summaryrefslogtreecommitdiff
path: root/src/buildstream/testing/_sourcetests/utils.py
blob: ca245a57d780b6306bbaeefb6cd02657db02aee3 (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#
#  Copyright (C) 2018 Codethink Limited
#  Copyright (C) 2019 Bloomberg Finance LP
#
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU Lesser General Public
#  License as published by the Free Software Foundation; either
#  version 2 of the License, or (at your option) any later version.
#
#  This library 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
#  Lesser General Public License for more details.
#
#  You should have received a copy of the GNU Lesser General Public
#  License along with this library. If not, see <http://www.gnu.org/licenses/>.
#
#   Authors:
#       Benjamin Schubert (bschubert15@bloomberg.net)
#

import os

# To make use of these test utilities it is necessary to have pytest
# available. However, we don't want to have a hard dependency on
# pytest.
try:
    import pytest
except ImportError:
    module_name = globals()["__name__"]
    msg = (
        "Could not import pytest:\n"
        "To use the {} module, you must have pytest installed.".format(module_name)
    )
    raise ImportError(msg)

from buildstream import _yaml
from .. import ALL_REPO_KINDS


# kind()
#
# Pytest fixture to get all the registered source plugins.
#
# This assumes the usage of the standard `project` and will automatically
# register the plugin in the project configuration, in addition to its junction
# configuration
#
# Yields:
#   the plugin kind name
#
@pytest.fixture(params=ALL_REPO_KINDS.keys())
def kind(request, datafiles):
    # Register plugins both on the toplevel project and on its junctions
    for project_dir in [
        str(datafiles),
        os.path.join(str(datafiles), "files", "sub-project"),
    ]:
        add_plugins_conf(project_dir, request.param)

    yield request.param


# add_plugins_conf()
#
# Add the given plugin to the configuration of the given project.
#
# Args:
#   project (str): path to the project on which to register the plugin
#   plugin_kind (str): name of the plugin kind to register
#
def add_plugins_conf(project, plugin_kind):
    _scaffolder, plugin_package = ALL_REPO_KINDS[plugin_kind]

    project_conf_file = os.path.join(project, "project.conf")
    project_conf = _yaml.roundtrip_load(project_conf_file)

    if plugin_package is not None:
        project_conf["plugins"] = [
            {
                "origin": "pip",
                "package-name": plugin_package,
                "sources": {plugin_kind: 0,},
            },
        ]

    _yaml.roundtrip_dump(project_conf, project_conf_file)


# update_project_configuration()
#
# Update the project configuration with the given updated configuration.
#
# Note: This does a simple `dict.update()` call, which will not merge recursively
#       but will replace every defined key.
#
# Args:
#   project_path (str): the path to the root of the project
#   updated_configuration (dict): configuration to merge into the existing one
#
def update_project_configuration(project_path, updated_configuration):
    project_conf_path = os.path.join(project_path, "project.conf")
    project_conf = _yaml.roundtrip_load(project_conf_path)

    project_conf.update(updated_configuration)

    _yaml.roundtrip_dump(project_conf, project_conf_path)