summaryrefslogtreecommitdiff
path: root/src/buildstream/testing/_sourcetests/track_cross_junction.py
blob: 89917ac45d746ac39d97f26f1a44f27e9210f668 (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#
#  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/>.
#

# Pylint doesn't play well with fixtures and dependency injection from pytest
# pylint: disable=redefined-outer-name

import os

import pytest

from buildstream import _yaml

from .. import cli  # pylint: disable=unused-import
from .. import ALL_REPO_KINDS, create_repo
from .._utils import generate_junction
from .utils import add_plugins_conf

# Project directory
TOP_DIR = os.path.dirname(os.path.realpath(__file__))
DATA_DIR = os.path.join(TOP_DIR, "project")


def generate_element(repo, element_path, dep_name=None):
    element = {"kind": "import", "sources": [repo.source_config()]}
    if dep_name:
        element["depends"] = [dep_name]

    _yaml.roundtrip_dump(element, element_path)


def generate_import_element(tmpdir, kind, project, name):
    element_name = "import-{}.bst".format(name)
    repo_element_path = os.path.join(project, "elements", element_name)
    files = str(tmpdir.join("imported_files_{}".format(name)))
    os.makedirs(files)

    with open(os.path.join(files, "{}.txt".format(name)), "w") as f:
        f.write(name)

    repo = create_repo(kind, str(tmpdir.join("element_{}_repo".format(name))))
    repo.create(files)

    generate_element(repo, repo_element_path)

    return element_name


def generate_project(tmpdir, name, kind, config=None):
    if config is None:
        config = {}

    project_name = "project-{}".format(name)
    subproject_path = os.path.join(str(tmpdir.join(project_name)))
    os.makedirs(os.path.join(subproject_path, "elements"))

    project_conf = {"name": name, "min-version": "2.0", "element-path": "elements"}
    project_conf.update(config)
    _yaml.roundtrip_dump(project_conf, os.path.join(subproject_path, "project.conf"))
    add_plugins_conf(subproject_path, kind)

    return project_name, subproject_path


def generate_simple_stack(project, name, dependencies):
    element_name = "{}.bst".format(name)
    element_path = os.path.join(project, "elements", element_name)
    element = {"kind": "stack", "depends": dependencies}
    _yaml.roundtrip_dump(element, element_path)

    return element_name


def generate_cross_element(project, subproject_name, import_name):
    basename, _ = os.path.splitext(import_name)
    return generate_simple_stack(
        project,
        "import-{}-{}".format(subproject_name, basename),
        [{"junction": "{}.bst".format(subproject_name), "filename": import_name}],
    )


@pytest.mark.parametrize("kind", ALL_REPO_KINDS.keys())
def test_cross_junction_multiple_projects(cli, tmpdir, kind):
    tmpdir = tmpdir.join(kind)

    # Generate 3 projects: main, a, b
    _, project = generate_project(tmpdir, "main", kind, {"ref-storage": "project.refs"})
    project_a, project_a_path = generate_project(tmpdir, "a", kind)
    project_b, project_b_path = generate_project(tmpdir, "b", kind)

    # Generate an element with a trackable source for each project
    element_a = generate_import_element(tmpdir, kind, project_a_path, "a")
    element_b = generate_import_element(tmpdir, kind, project_b_path, "b")
    element_c = generate_import_element(tmpdir, kind, project, "c")

    # Create some indirections to the elements with dependencies to test --deps
    stack_a = generate_simple_stack(project_a_path, "stack-a", [element_a])
    stack_b = generate_simple_stack(project_b_path, "stack-b", [element_b])

    # Create junctions for projects a and b in main.
    junction_a = "{}.bst".format(project_a)
    junction_a_path = os.path.join(project, "elements", junction_a)
    generate_junction(tmpdir.join("repo_a"), project_a_path, junction_a_path, store_ref=False)

    junction_b = "{}.bst".format(project_b)
    junction_b_path = os.path.join(project, "elements", junction_b)
    generate_junction(tmpdir.join("repo_b"), project_b_path, junction_b_path, store_ref=False)

    # Track the junctions.
    result = cli.run(project=project, args=["source", "track", junction_a, junction_b])
    result.assert_success()

    # Import elements from a and b in to main.
    imported_a = generate_cross_element(project, project_a, stack_a)
    imported_b = generate_cross_element(project, project_b, stack_b)

    # Generate a top level stack depending on everything
    all_bst = generate_simple_stack(project, "all", [imported_a, imported_b, element_c])

    # Track without following junctions. But explicitly also track the elements in project a.
    result = cli.run(
        project=project, args=["source", "track", "--deps", "all", all_bst, "{}:{}".format(junction_a, stack_a)]
    )
    result.assert_success()

    # Elements in project b should not be tracked. But elements in project a and main should.
    expected = [element_c, "{}:{}".format(junction_a, element_a)]
    assert set(result.get_tracked_elements()) == set(expected)


@pytest.mark.parametrize("kind", ALL_REPO_KINDS.keys())
def test_track_exceptions(cli, tmpdir, kind):
    tmpdir = tmpdir.join(kind)

    _, project = generate_project(tmpdir, "main", kind, {"ref-storage": "project.refs"})
    project_a, project_a_path = generate_project(tmpdir, "a", kind)

    element_a = generate_import_element(tmpdir, kind, project_a_path, "a")
    element_b = generate_import_element(tmpdir, kind, project_a_path, "b")

    all_bst = generate_simple_stack(project_a_path, "all", [element_a, element_b])

    junction_a = "{}.bst".format(project_a)
    junction_a_path = os.path.join(project, "elements", junction_a)
    generate_junction(tmpdir.join("repo_a"), project_a_path, junction_a_path, store_ref=False)

    result = cli.run(project=project, args=["source", "track", junction_a])
    result.assert_success()

    imported_b = generate_cross_element(project, project_a, element_b)
    indirection = generate_simple_stack(project, "indirection", [imported_b])

    result = cli.run(
        project=project,
        args=[
            "source",
            "track",
            "--deps",
            "all",
            "--except",
            indirection,
            "{}:{}".format(junction_a, all_bst),
            imported_b,
        ],
    )
    result.assert_success()

    expected = ["{}:{}".format(junction_a, element_a), "{}:{}".format(junction_a, element_b)]
    assert set(result.get_tracked_elements()) == set(expected)