summaryrefslogtreecommitdiff
path: root/tests/frontend/init.py
blob: 01686b7c6626bc7851549c5c270ba79412f8b09a (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
# Pylint doesn't play well with fixtures and dependency injection from pytest
# pylint: disable=redefined-outer-name

import os
import pytest
from buildstream.testing import cli  # pylint: disable=unused-import

from buildstream import _yaml
from buildstream._frontend.app import App
from buildstream._exceptions import ErrorDomain, LoadErrorReason
from buildstream._versions import BST_FORMAT_VERSION


def test_defaults(cli, tmpdir):
    project = str(tmpdir)
    project_path = os.path.join(project, "project.conf")

    result = cli.run(args=["init", "--project-name", "foo", project])
    result.assert_success()

    project_conf = _yaml.load(project_path)
    assert project_conf.get_str("name") == "foo"
    assert project_conf.get_str("format-version") == str(BST_FORMAT_VERSION)
    assert project_conf.get_str("element-path") == "elements"


def test_all_options(cli, tmpdir):
    project = str(tmpdir)
    project_path = os.path.join(project, "project.conf")

    result = cli.run(
        args=[
            "init",
            "--project-name",
            "foo",
            "--format-version",
            "2",
            "--element-path",
            "ponies",
            project,
        ]
    )
    result.assert_success()

    project_conf = _yaml.load(project_path)
    assert project_conf.get_str("name") == "foo"
    assert project_conf.get_str("format-version") == str(2)
    assert project_conf.get_str("element-path") == "ponies"

    elements_dir = os.path.join(project, "ponies")
    assert os.path.isdir(elements_dir)


def test_no_project_name(cli, tmpdir):
    result = cli.run(args=["init", str(tmpdir)])
    result.assert_main_error(ErrorDomain.APP, "unspecified-project-name")


def test_project_exists(cli, tmpdir):
    project = str(tmpdir)
    project_path = os.path.join(project, "project.conf")
    with open(project_path, "w") as f:
        f.write("name: pony\n")

    result = cli.run(args=["init", "--project-name", "foo", project])
    result.assert_main_error(ErrorDomain.APP, "project-exists")


def test_force_overwrite_project(cli, tmpdir):
    project = str(tmpdir)
    project_path = os.path.join(project, "project.conf")
    with open(project_path, "w") as f:
        f.write("name: pony\n")

    result = cli.run(args=["init", "--project-name", "foo", "--force", project])
    result.assert_success()

    project_conf = _yaml.load(project_path)
    assert project_conf.get_str("name") == "foo"
    assert project_conf.get_str("format-version") == str(BST_FORMAT_VERSION)


def test_relative_path_directory_as_argument(cli, tmpdir):
    project = os.path.join(str(tmpdir), "child-directory")
    os.makedirs(project, exist_ok=True)
    project_path = os.path.join(project, "project.conf")
    rel_path = os.path.relpath(project)

    result = cli.run(args=["init", "--project-name", "foo", rel_path])
    result.assert_success()

    project_conf = _yaml.load(project_path)
    assert project_conf.get_str("name") == "foo"
    assert project_conf.get_int("format-version") == BST_FORMAT_VERSION
    assert project_conf.get_str("element-path") == "elements"


def test_set_directory_and_directory_as_argument(cli, tmpdir):
    result = cli.run(
        args=["-C", "/foo/bar", "init", "--project-name", "foo", "/boo/far"]
    )
    result.assert_main_error(ErrorDomain.APP, "init-with-set-directory")


@pytest.mark.parametrize("project_name", [("Micheal Jackson"), ("one+one")])
def test_bad_project_name(cli, tmpdir, project_name):
    result = cli.run(args=["init", "--project-name", str(tmpdir)])
    result.assert_main_error(ErrorDomain.LOAD, LoadErrorReason.INVALID_SYMBOL_NAME)


@pytest.mark.parametrize("format_version", [(str(-1)), (str(BST_FORMAT_VERSION + 1))])
def test_bad_format_version(cli, tmpdir, format_version):
    result = cli.run(
        args=[
            "init",
            "--project-name",
            "foo",
            "--format-version",
            format_version,
            str(tmpdir),
        ]
    )
    result.assert_main_error(ErrorDomain.APP, "invalid-format-version")


@pytest.mark.parametrize(
    "element_path", [("/absolute/path"), ("../outside/of/project")]
)
def test_bad_element_path(cli, tmpdir, element_path):
    result = cli.run(
        args=[
            "init",
            "--project-name",
            "foo",
            "--element-path",
            element_path,
            str(tmpdir),
        ]
    )
    result.assert_main_error(ErrorDomain.APP, "invalid-element-path")


@pytest.mark.parametrize("element_path", [("foo"), ("foo/bar")])
def test_element_path_interactive(cli, tmp_path, monkeypatch, element_path):
    project = tmp_path
    project_conf_path = project.joinpath("project.conf")

    class DummyInteractiveApp(App):
        def __init__(self, *args, **kwargs):
            super().__init__(*args, **kwargs)
            self.interactive = True

        @classmethod
        def create(cls, *args, **kwargs):
            return DummyInteractiveApp(*args, **kwargs)

        def _init_project_interactive(
            self, *args, **kwargs
        ):  # pylint: disable=arguments-differ
            return ("project_name", "0", element_path)

    monkeypatch.setattr(App, "create", DummyInteractiveApp.create)

    result = cli.run(args=["init", str(project)])
    result.assert_success()

    full_element_path = project.joinpath(element_path)
    assert full_element_path.exists()

    project_conf = _yaml.load(str(project_conf_path))
    assert project_conf.get_str("name") == "project_name"
    assert project_conf.get_str("format-version") == "0"
    assert project_conf.get_str("element-path") == element_path