summaryrefslogtreecommitdiff
path: root/tests/frontend/init.py
blob: f6e7dd68a044d165989dcf21cf73070aa8821022 (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
import os
import pytest
from tests.testutils import cli

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(project=project, args=['init', '--project-name', 'foo'])
    result.assert_success()

    project_conf = _yaml.load(project_path)
    assert project_conf['name'] == 'foo'
    assert project_conf['format-version'] == str(BST_FORMAT_VERSION)
    assert project_conf['element-path'] == 'elements'


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

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

    project_conf = _yaml.load(project_path)
    assert project_conf['name'] == 'foo'
    assert project_conf['format-version'] == str(2)
    assert project_conf['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(project=str(tmpdir), args=['init'])
    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(project=project, args=['init', '--project-name', 'foo'])
    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(project=project, args=['init', '--project-name', 'foo', '--force'])
    result.assert_success()

    project_conf = _yaml.load(project_path)
    assert project_conf['name'] == 'foo'
    assert project_conf['format-version'] == str(BST_FORMAT_VERSION)


@pytest.mark.parametrize("project_name", [('Micheal Jackson'), ('one+one')])
def test_bad_project_name(cli, tmpdir, project_name):
    result = cli.run(project=str(tmpdir), args=['init', '--project-name', project_name])
    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(project=str(tmpdir), args=[
        'init', '--project-name', 'foo', '--format-version', format_version
    ])
    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(project=str(tmpdir), args=[
        'init', '--project-name', 'foo', '--element-path', element_path
    ])
    result.assert_main_error(ErrorDomain.APP, 'invalid-element-path')


@pytest.mark.parametrize("element_path", [('/absolute/path'), ('../outside/of/project')])
def test_bad_element_path(cli, tmpdir, element_path):
    result = cli.run(project=str(tmpdir), args=[
        'init', '--project-name', 'foo', '--element-path', element_path
    ])
    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):
            return ('project_name', '0', element_path)

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

    result = cli.run(project=str(project), args=['init'])
    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['name'] == 'project_name'
    assert project_conf['format-version'] == '0'
    assert project_conf['element-path'] == element_path