summaryrefslogtreecommitdiff
path: root/tests/plugins/loading.py
blob: 1c8a321d5262395f03ae862fbbaacf32edc70a2d (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
108
109
110
# Pylint doesn't play well with fixtures and dependency injection from pytest
# pylint: disable=redefined-outer-name

#
# This test case tests the failure modes of loading a plugin
# after it has already been discovered via it's origin.
#

import os
import pytest

from buildstream.exceptions import ErrorDomain
from buildstream.testing import cli  # pylint: disable=unused-import
from buildstream import _yaml


DATA_DIR = os.path.join(os.path.dirname(os.path.realpath(__file__)), "loading")


def update_project(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)


# Sets up the element.bst file so that it requires a source
# or element plugin.
#
def setup_element(project_path, plugin_type, plugin_name):
    element_dir = os.path.join(project_path, "elements")
    element_path = os.path.join(element_dir, "element.bst")
    os.makedirs(element_dir, exist_ok=True)

    if plugin_type == "elements":
        element = {"kind": plugin_name}
    else:
        element = {"kind": "manual", "sources": [{"kind": plugin_name}]}

    _yaml.roundtrip_dump(element, element_path)


####################################################
#                     Tests                        #
####################################################
@pytest.mark.datafiles(DATA_DIR)
@pytest.mark.parametrize("plugin_type", [("elements"), ("sources")])
def test_nosetup(cli, datafiles, plugin_type):
    project = str(datafiles)

    update_project(project, {"plugins": [{"origin": "local", "path": "plugins/nosetup", plugin_type: ["nosetup"]}]})
    setup_element(project, plugin_type, "nosetup")

    result = cli.run(project=project, args=["show", "element.bst"])
    result.assert_main_error(ErrorDomain.PLUGIN, "missing-setup-function")


@pytest.mark.datafiles(DATA_DIR)
@pytest.mark.parametrize("plugin_type", [("elements"), ("sources")])
def test_setup_not_function(cli, datafiles, plugin_type):
    project = str(datafiles)

    update_project(
        project,
        {"plugins": [{"origin": "local", "path": "plugins/setupnotfunction", plugin_type: ["setupnotfunction"]}]},
    )
    setup_element(project, plugin_type, "setupnotfunction")

    result = cli.run(project=project, args=["show", "element.bst"])
    result.assert_main_error(ErrorDomain.PLUGIN, "setup-is-not-function")


@pytest.mark.datafiles(DATA_DIR)
@pytest.mark.parametrize("plugin_type", [("elements"), ("sources")])
def test_setup_returns_not_type(cli, datafiles, plugin_type):
    project = str(datafiles)

    update_project(
        project,
        {
            "plugins": [
                {"origin": "local", "path": "plugins/setupreturnsnottype", plugin_type: ["setupreturnsnottype"]}
            ]
        },
    )
    setup_element(project, plugin_type, "setupreturnsnottype")

    result = cli.run(project=project, args=["show", "element.bst"])
    result.assert_main_error(ErrorDomain.PLUGIN, "setup-returns-not-type")


@pytest.mark.datafiles(DATA_DIR)
@pytest.mark.parametrize("plugin_type", [("elements"), ("sources")])
def test_setup_returns_bad_type(cli, datafiles, plugin_type):
    project = str(datafiles)

    update_project(
        project,
        {
            "plugins": [
                {"origin": "local", "path": "plugins/setupreturnsbadtype", plugin_type: ["setupreturnsbadtype"]}
            ]
        },
    )
    setup_element(project, plugin_type, "setupreturnsbadtype")

    result = cli.run(project=project, args=["show", "element.bst"])
    result.assert_main_error(ErrorDomain.PLUGIN, "setup-returns-bad-type")