def test_main(commands, argv, cmd_cls_string): """Tests that arguments are parsed correctly.""" setattr(commands, cmd_cls_string, mock.Mock()) cmd_class = getattr(commands, cmd_cls_string) app.main(argv) cmd_class.assert_called_once_with(argv[1:], color=False, global_=False, verbose=False) app._CmdAction.flag = None
def test_main( commands: mock.MagicMock, argv: List[str], cmd_cls_string: str ) -> None: """Tests that arguments are parsed correctly.""" setattr(commands, cmd_cls_string, mock.Mock()) cmd_class = getattr(commands, cmd_cls_string) app.main(argv) cmd_class.assert_called_once_with( argv[1:], color=False, global_=False, verbose=False ) app._CmdAction.flag = None # pylint: disable=protected-access
def test_init(shell: str, snapshot: Snapshot, capsys: CaptureFixture) -> None: """Tests the --init option.""" exit_code = app.main(["--init", shell]) assert exit_code == 0 captured = capsys.readouterr() assert captured.out == snapshot
def test_main_exceptions(_get_argparser): """Tests that main handles exceptions appropriately.""" class TestError(Exception): pass def raise_error(opt, verbose=False): if opt == 1: raise errors.FunkyError(returncode=5) elif opt == 2: raise TestError('Test Exception') _get_argparser.side_effect = functools.partial(raise_error, 1) assert app.main() == 5 _get_argparser.side_effect = functools.partial(raise_error, 2) with pytest.raises(TestError): app.main()
def test_main_validate_args(logger: mock.MagicMock, argv: List[str]) -> None: """Tests that arguments are validated properly.""" assert app.main(argv) == 2 logger.error.called_once() funky.app._CmdAction.flag = None # pylint: disable=protected-access funky.app._CmdAction.option_string = ( None # pylint: disable=protected-access )
def test_setup_shell(shell: str, contents: Optional[str], tmp_home: Path, snapshot: Snapshot) -> None: """Tests the --setup-shell option.""" config_file = tmp_home / SHELL_TO_CONFIG[shell] if contents: config_file.write_text(contents) exit_code = app.main(["--setup-shell", shell]) assert exit_code == 0 assert config_file.read_text() == snapshot
def test_main_exceptions(_get_argparser: mock.MagicMock) -> None: """Tests that main handles exceptions appropriately.""" class TestError(Exception): pass def raise_error(opt: int, verbose: bool = True) -> None: del verbose if opt == 1: raise errors.FunkyError(returncode=5) if opt == 2: raise TestError("Test Exception") _get_argparser.side_effect = functools.partial(raise_error, 1) assert app.main() == 5 _get_argparser.side_effect = functools.partial(raise_error, 2) with pytest.raises(TestError): app.main()
import os import sys this_dir = os.path.dirname(os.path.realpath(__file__)) sys.path.insert(0, this_dir) from funky import app # noqa if __name__ == "__main__": sys.exit(app.main())
def test_main_validate_args(logger, argv): """Tests that arguments are validated properly.""" assert app.main(argv) == 2 logger.error.called_once() funky.app._CmdAction.flag = None funky.app._CmdAction.option_string = None