summaryrefslogtreecommitdiffstats
path: root/python/mach/mach/test/test_commands.py
blob: 38191b089838b465b81f4f754d5749853110b491 (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
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

import sys
from pathlib import Path

import pytest
from buildconfig import topsrcdir
from mozunit import main

import mach

ALL_COMMANDS = [
    "cmd_bar",
    "cmd_foo",
    "cmd_foobar",
    "mach-commands",
    "mach-completion",
    "mach-debug-commands",
]


@pytest.fixture
def run_completion(run_mach):
    def inner(args=[]):
        mach_dir = Path(mach.__file__).parent
        providers = [
            Path("commands.py"),
            mach_dir / "commands" / "commandinfo.py",
        ]

        def context_handler(key):
            if key == "topdir":
                return topsrcdir

        args = ["mach-completion"] + args
        return run_mach(args, providers, context_handler=context_handler)

    return inner


def format(targets):
    return "\n".join(targets) + "\n"


def test_mach_completion(run_completion):
    result, stdout, stderr = run_completion()
    assert result == 0
    assert stdout == format(ALL_COMMANDS)

    result, stdout, stderr = run_completion(["cmd_f"])
    assert result == 0
    # While it seems like this should return only commands that have
    # 'cmd_f' as a prefix, the completion script will handle this case
    # properly.
    assert stdout == format(ALL_COMMANDS)

    result, stdout, stderr = run_completion(["cmd_foo"])
    assert result == 0
    assert stdout == format(["help", "--arg"])


@pytest.mark.parametrize("shell", ("bash", "fish", "zsh"))
def test_generate_mach_completion_script(run_completion, shell):
    rv, out, err = run_completion([shell])
    print(out)
    print(err, file=sys.stderr)
    assert rv == 0
    assert err == ""

    assert "cmd_foo" in out
    assert "arg" in out
    assert "cmd_foobar" in out
    assert "cmd_bar" in out


if __name__ == "__main__":
    main()