1import pytest
2
3from pip._internal.cli import cmdoptions
4from pip._internal.cli.base_command import Command
5from pip._internal.models.format_control import FormatControl
6
7
8class SimpleCommand(Command):
9
10    def __init__(self):
11        super(SimpleCommand, self).__init__('fake', 'fake summary')
12
13    def add_options(self):
14        self.cmd_opts.add_option(cmdoptions.no_binary())
15        self.cmd_opts.add_option(cmdoptions.only_binary())
16
17    def run(self, options, args):
18        self.options = options
19
20
21def test_no_binary_overrides():
22    cmd = SimpleCommand()
23    cmd.main(['fake', '--only-binary=:all:', '--no-binary=fred'])
24    format_control = FormatControl({'fred'}, {':all:'})
25    assert cmd.options.format_control == format_control
26
27
28def test_only_binary_overrides():
29    cmd = SimpleCommand()
30    cmd.main(['fake', '--no-binary=:all:', '--only-binary=fred'])
31    format_control = FormatControl({':all:'}, {'fred'})
32    assert cmd.options.format_control == format_control
33
34
35def test_none_resets():
36    cmd = SimpleCommand()
37    cmd.main(['fake', '--no-binary=:all:', '--no-binary=:none:'])
38    format_control = FormatControl(set(), set())
39    assert cmd.options.format_control == format_control
40
41
42def test_none_preserves_other_side():
43    cmd = SimpleCommand()
44    cmd.main(
45        ['fake', '--no-binary=:all:', '--only-binary=fred',
46         '--no-binary=:none:'])
47    format_control = FormatControl(set(), {'fred'})
48    assert cmd.options.format_control == format_control
49
50
51def test_comma_separated_values():
52    cmd = SimpleCommand()
53    cmd.main(['fake', '--no-binary=1,2,3'])
54    format_control = FormatControl({'1', '2', '3'}, set())
55    assert cmd.options.format_control == format_control
56
57
58@pytest.mark.parametrize(
59    "no_binary,only_binary,argument,expected",
60    [
61        ({"fred"}, set(), "fred", frozenset(["source"])),
62        ({"fred"}, {":all:"}, "fred", frozenset(["source"])),
63        (set(), {"fred"}, "fred", frozenset(["binary"])),
64        ({":all:"}, {"fred"}, "fred", frozenset(["binary"]))
65    ]
66)
67def test_fmt_ctl_matches(no_binary, only_binary, argument, expected):
68    fmt = FormatControl(no_binary, only_binary)
69    assert fmt.get_allowed_formats(argument) == expected
70