httpie-cli/tests/test_cli_utils.py
Batuhan Taskaya ff6f1887b0
[Major] UI Enhancements (#1321)
* Refactor tests to use a text-based standard output. (#1318)

* Implement new style `--help` (#1316)

* Implement man page generation (#1317)

* Implement rich progress bars. (#1324)

* Man page deployment & isolation. (#1325)

* Remove all unsorted usages in the CLI docs

* Implement isolated mode for man page generation

* Add a CI job for autogenerated files

* Distribute man pages through PyPI

* Pin the date for man pages. (#1326)

* Hide suppressed arguments from --help/man pages (#1329)

* Change download spinner to line (#1328)

* Regenerate autogenerated files when pushed against to master. (#1339)

* Highlight options (#1340)

* Additional man page enhancements (#1341)

* Group options by the parent category & highlight -o/--o

* Display (and underline) the METAVAR on man pages.

* Make help message processing more robust (#1342)

* Inherit `help` from `short_help`

* Don't mirror short_help directly.

* Fixup the serialization

* Use `pager` and `man` on `--manual` when applicable (#1343)

* Run `man $program` on --manual

* Page the output of `--manual` for systems that lack man pages

* Improvements over progress bars (separate bar, status line, etc.) (#1346)

* Redesign the --help layout.

* Make our usage of rich compatible with 9.10.0

* Add `HTTPIE_NO_MAN_PAGES`

* Make tests also patch os.get_terminal_size

* Generate CLI spec from HTTPie & Man Page Hook (#1354)

* Generate CLI spec from HTTPie & add man page hook

* Use the full command space for the option headers
2022-04-14 07:43:10 -07:00

87 lines
2.3 KiB
Python

import pytest
from argparse import ArgumentParser
from unittest.mock import Mock
from httpie.cli.utils import LazyChoices
def test_lazy_choices():
mock = Mock()
getter = mock.getter
getter.return_value = ['a', 'b', 'c']
parser = ArgumentParser()
parser.register('action', 'lazy_choices', LazyChoices)
parser.add_argument(
'--option',
help="the regular option",
default='a',
metavar='SYMBOL',
choices=['a', 'b'],
)
parser.add_argument(
'--lazy-option',
help="the lazy option",
default='a',
metavar='SYMBOL',
action='lazy_choices',
getter=getter,
cache=False # for test purposes
)
# Parser initalization doesn't call it.
getter.assert_not_called()
# If we don't use --lazy-option, we don't retrieve it.
parser.parse_args([])
getter.assert_not_called()
parser.parse_args(['--option', 'b'])
getter.assert_not_called()
# If we pass a value, it will retrieve to verify.
parser.parse_args(['--lazy-option', 'c'])
getter.assert_called()
getter.reset_mock()
with pytest.raises(SystemExit):
parser.parse_args(['--lazy-option', 'z'])
getter.assert_called()
getter.reset_mock()
def test_lazy_choices_help():
mock = Mock()
getter = mock.getter
getter.return_value = ['a', 'b', 'c']
help_formatter = mock.help_formatter
help_formatter.return_value = '<my help>'
parser = ArgumentParser()
parser.register('action', 'lazy_choices', LazyChoices)
parser.add_argument(
'--lazy-option',
default='a',
metavar='SYMBOL',
action='lazy_choices',
getter=getter,
help_formatter=help_formatter,
cache=False # for test purposes
)
# Parser initalization doesn't call it.
getter.assert_not_called()
# If we don't use `--help`, we don't use it.
parser.parse_args([])
getter.assert_not_called()
help_formatter.assert_not_called()
parser.parse_args(['--lazy-option', 'b'])
help_formatter.assert_not_called()
# If we use --help, then we call it with styles
with pytest.raises(SystemExit):
parser.parse_args(['--help'])
help_formatter.assert_called_once_with(['a', 'b', 'c'], isolation_mode=False)