summaryrefslogtreecommitdiff
path: root/tests/frontend/completions.py
blob: af26a98e8e69971eee3543d148a726dce5864836 (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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
# 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

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

MAIN_COMMANDS = ["artifact ", "build ", "help ", "init ", "shell ", "show ", "source ", "workspace "]

MAIN_OPTIONS = [
    "--builders ",
    "-c ",
    "-C ",
    "--cache-buildtrees ",
    "--colors ",
    "--config ",
    "--debug ",
    "--default-mirror ",
    "--directory ",
    "--error-lines ",
    "--fetchers ",
    "--log-file ",
    "--max-jobs ",
    "--message-lines ",
    "--network-retries ",
    "--no-colors ",
    "--no-debug ",
    "--no-interactive ",
    "--no-strict ",
    "--no-verbose ",
    "-o ",
    "--option ",
    "--on-error ",
    "--pull-buildtrees ",
    "--pushers ",
    "--strict ",
    "--verbose ",
    "--version ",
]

SOURCE_COMMANDS = [
    "checkout ",
    "fetch ",
    "track ",
]

ARTIFACT_COMMANDS = [
    "checkout ",
    "delete ",
    "push ",
    "pull ",
    "log ",
    "list-contents ",
    "show ",
]

WORKSPACE_COMMANDS = ["close ", "list ", "open ", "reset "]

PROJECT_ELEMENTS = [
    "compose-all.bst",
    "compose-exclude-dev.bst",
    "compose-include-bin.bst",
    "import-bin.bst",
    "import-dev.bst",
    "target.bst",
]

INVALID_ELEMENTS = [
    "target.foo",
    "target.bst.bar",
]

MIXED_ELEMENTS = PROJECT_ELEMENTS + INVALID_ELEMENTS


def assert_completion(cli, cmd, word_idx, expected, cwd=None):
    result = cli.run(
        project=".", cwd=cwd, env={"_BST_COMPLETION": "complete", "COMP_WORDS": cmd, "COMP_CWORD": str(word_idx)}
    )
    words = []
    if result.output:
        words = result.output.splitlines()

    # The order is meaningless, bash will
    # take the results and order it by its
    # own little heuristics
    words = sorted(words)
    expected = sorted(expected)
    assert words == expected


def assert_completion_failed(cli, cmd, word_idx, expected, cwd=None):
    result = cli.run(cwd=cwd, env={"_BST_COMPLETION": "complete", "COMP_WORDS": cmd, "COMP_CWORD": str(word_idx)})
    words = []
    if result.output:
        words = result.output.splitlines()

    # The order is meaningless, bash will
    # take the results and order it by its
    # own little heuristics
    words = sorted(words)
    expected = sorted(expected)
    assert words != expected


@pytest.mark.parametrize(
    "cmd,word_idx,expected",
    [
        ("bst", 0, []),
        ("bst ", 1, MAIN_COMMANDS),
        ("bst artifact ", 2, ARTIFACT_COMMANDS),
        ("bst source ", 2, SOURCE_COMMANDS),
        ("bst w ", 1, ["workspace "]),
        ("bst workspace ", 2, WORKSPACE_COMMANDS),
    ],
)
def test_commands(cli, cmd, word_idx, expected):
    assert_completion(cli, cmd, word_idx, expected)


@pytest.mark.parametrize(
    "cmd,word_idx,expected",
    [
        ("bst -", 1, MAIN_OPTIONS),
        ("bst --l", 1, ["--log-file "]),
        # Test that options of subcommands also complete
        ("bst --no-colors build -", 3, ["--deps ", "-d ", "--remote ", "-r "]),
        # Test the behavior of completing after an option that has a
        # parameter that cannot be completed, vs an option that has
        # no parameter
        ("bst --fetchers ", 2, []),
        ("bst --no-colors ", 2, MAIN_COMMANDS),
    ],
)
def test_options(cli, cmd, word_idx, expected):
    assert_completion(cli, cmd, word_idx, expected)


@pytest.mark.parametrize(
    "cmd,word_idx,expected",
    [
        ("bst --on-error ", 2, ["continue ", "quit ", "terminate "]),
        ("bst --cache-buildtrees ", 2, ["always ", "auto ", "never "]),
        ("bst show --deps ", 3, ["all ", "build ", "none ", "plan ", "run "]),
        ("bst show --deps=", 2, ["all ", "build ", "none ", "plan ", "run "]),
        ("bst show --deps b", 3, ["build "]),
        ("bst show --deps=b", 2, ["build "]),
        ("bst show --deps r", 3, ["run "]),
        ("bst source track --deps ", 4, ["all ", "none "]),
    ],
)
def test_option_choice(cli, cmd, word_idx, expected):
    assert_completion(cli, cmd, word_idx, expected)


@pytest.mark.datafiles(os.path.join(DATA_DIR, "project"))
@pytest.mark.parametrize(
    "cmd,word_idx,expected,subdir",
    [
        # Note that elements/ and files/ are partial completions and
        # as such do not come with trailing whitespace
        ("bst --config ", 2, ["cache/", "elements/", "files/", "project.conf "], None),
        ("bst --log-file ", 2, ["cache/", "elements/", "files/", "project.conf "], None),
        ("bst --config f", 2, ["files/"], None),
        ("bst --log-file f", 2, ["files/"], None),
        ("bst --config files", 2, ["files/bin-files/", "files/dev-files/"], None),
        ("bst --log-file files", 2, ["files/bin-files/", "files/dev-files/"], None),
        ("bst --config files/", 2, ["files/bin-files/", "files/dev-files/"], None),
        ("bst --log-file elements/", 2, [os.path.join("elements", e) + " " for e in PROJECT_ELEMENTS], None),
        ("bst --config ../", 2, ["../cache/", "../elements/", "../files/", "../project.conf "], "files"),
        ("bst --config ../elements/", 2, [os.path.join("..", "elements", e) + " " for e in PROJECT_ELEMENTS], "files"),
        ("bst --config ../nofile", 2, [], "files"),
        ("bst --config /pony/rainbow/nobodyhas/this/file", 2, [], "files"),
    ],
)
def test_option_file(datafiles, cli, cmd, word_idx, expected, subdir):
    cwd = str(datafiles)
    if subdir:
        cwd = os.path.join(cwd, subdir)
    assert_completion(cli, cmd, word_idx, expected, cwd=cwd)


@pytest.mark.datafiles(os.path.join(DATA_DIR, "project"))
@pytest.mark.parametrize(
    "cmd,word_idx,expected,subdir",
    [
        # Note that regular files like project.conf are not returned when
        # completing for a directory
        ("bst --directory ", 2, ["cache/", "elements/", "files/"], None),
        ("bst --directory elements/", 2, [], None),
        ("bst --directory ", 2, ["dev-files/", "bin-files/"], "files"),
        ("bst --directory ../", 2, ["../cache/", "../elements/", "../files/"], "files"),
    ],
)
def test_option_directory(datafiles, cli, cmd, word_idx, expected, subdir):
    cwd = str(datafiles)
    if subdir:
        cwd = os.path.join(cwd, subdir)
    assert_completion(cli, cmd, word_idx, expected, cwd=cwd)


@pytest.mark.datafiles(DATA_DIR)
@pytest.mark.parametrize(
    "project,cmd,word_idx,expected,subdir",
    [
        # When running in the project directory
        ("project", "bst show ", 2, [e + " " for e in PROJECT_ELEMENTS], None),
        (
            "project",
            "bst build com",
            2,
            ["compose-all.bst ", "compose-include-bin.bst ", "compose-exclude-dev.bst "],
            None,
        ),
        # When running from the files subdir
        ("project", "bst show ", 2, [e + " " for e in PROJECT_ELEMENTS], "files"),
        (
            "project",
            "bst build com",
            2,
            ["compose-all.bst ", "compose-include-bin.bst ", "compose-exclude-dev.bst "],
            "files",
        ),
        # When passing the project directory
        ("project", "bst --directory ../ show ", 4, [e + " " for e in PROJECT_ELEMENTS], "files"),
        (
            "project",
            "bst --directory ../ build com",
            4,
            ["compose-all.bst ", "compose-include-bin.bst ", "compose-exclude-dev.bst "],
            "files",
        ),
        # Also try multi arguments together
        ("project", "bst --directory ../ artifact checkout t ", 5, ["target.bst "], "files"),
        ("project", "bst --directory ../ artifact checkout --directory ", 6, ["bin-files/", "dev-files/"], "files"),
        # When running in the project directory
        ("no-element-path", "bst show ", 2, [e + " " for e in PROJECT_ELEMENTS] + ["files/"], None),
        (
            "no-element-path",
            "bst build com",
            2,
            ["compose-all.bst ", "compose-include-bin.bst ", "compose-exclude-dev.bst "],
            None,
        ),
        # When running from the files subdir
        ("no-element-path", "bst show ", 2, [e + " " for e in PROJECT_ELEMENTS] + ["files/"], "files"),
        (
            "no-element-path",
            "bst build com",
            2,
            ["compose-all.bst ", "compose-include-bin.bst ", "compose-exclude-dev.bst "],
            "files",
        ),
        # When passing the project directory
        ("no-element-path", "bst --directory ../ show ", 4, [e + " " for e in PROJECT_ELEMENTS] + ["files/"], "files"),
        ("no-element-path", "bst --directory ../ show f", 4, ["files/"], "files"),
        ("no-element-path", "bst --directory ../ show files/", 4, ["files/bin-files/", "files/dev-files/"], "files"),
        (
            "no-element-path",
            "bst --directory ../ build com",
            4,
            ["compose-all.bst ", "compose-include-bin.bst ", "compose-exclude-dev.bst "],
            "files",
        ),
        # Also try multi arguments together
        ("no-element-path", "bst --directory ../ artifact checkout t ", 5, ["target.bst "], "files"),
        (
            "no-element-path",
            "bst --directory ../ artifact checkout --directory ",
            6,
            ["bin-files/", "dev-files/"],
            "files",
        ),
        # When element-path have sub-folders
        ("sub-folders", "bst show base", 2, ["base/wanted.bst "], None),
        ("sub-folders", "bst show base/", 2, ["base/wanted.bst "], None),
    ],
)
def test_argument_element(datafiles, cli, project, cmd, word_idx, expected, subdir):
    cwd = os.path.join(str(datafiles), project)
    if subdir:
        cwd = os.path.join(cwd, subdir)
    assert_completion(cli, cmd, word_idx, expected, cwd=cwd)


@pytest.mark.datafiles(DATA_DIR)
@pytest.mark.parametrize(
    "project,cmd,word_idx,expected,subdir",
    [
        # When element has invalid suffix
        ("project", "bst --directory ../ show ", 4, [e + " " for e in MIXED_ELEMENTS], "files")
    ],
)
def test_argument_element_invalid(datafiles, cli, project, cmd, word_idx, expected, subdir):
    cwd = os.path.join(str(datafiles), project)
    if subdir:
        cwd = os.path.join(cwd, subdir)
    assert_completion_failed(cli, cmd, word_idx, expected, cwd=cwd)


@pytest.mark.parametrize(
    "cmd,word_idx,expected",
    [
        ("bst he", 1, ["help "]),
        ("bst help ", 2, MAIN_COMMANDS),
        ("bst help artifact ", 3, ARTIFACT_COMMANDS),
        ("bst help in", 2, ["init "]),
        ("bst help source ", 3, SOURCE_COMMANDS),
        ("bst help artifact ", 3, ARTIFACT_COMMANDS),
        ("bst help w", 2, ["workspace "]),
        ("bst help workspace ", 3, WORKSPACE_COMMANDS),
    ],
)
def test_help_commands(cli, cmd, word_idx, expected):
    assert_completion(cli, cmd, word_idx, expected)


@pytest.mark.datafiles(os.path.join(DATA_DIR, "project"))
def test_argument_artifact(cli, datafiles):
    project = str(datafiles)

    # Build an import element with no dependencies (as there will only be ONE cache key)
    result = cli.run(project=project, args=["build", "import-bin.bst"])  # Has no dependencies
    result.assert_success()

    # Get the key and the artifact ref ($project/$element_name/$key)
    key = cli.get_element_key(project, "import-bin.bst")
    artifact = os.path.join("test", "import-bin", key)

    # Test autocompletion of the artifact
    cmds = ["bst artifact log ", "bst artifact log t", "bst artifact log test/"]

    for i, cmd in enumerate(cmds):
        word_idx = 3
        result = cli.run(
            project=project,
            cwd=project,
            env={"_BST_COMPLETION": "complete", "COMP_WORDS": cmd, "COMP_CWORD": str(word_idx)},
        )

        if result.output:
            words = result.output.splitlines()  # This leaves an extra space on each e.g. ['foo.bst ']
            words = [word.strip() for word in words]

            if i == 0:
                expected = PROJECT_ELEMENTS + [artifact]  # We should now be able to see the artifact
            elif i == 1:
                expected = ["target.bst", artifact]
            elif i == 2:
                expected = [artifact]

            assert expected == words